1import { render, RenderOptions } from '@testing-library/react';
2import GithubSlugger from 'github-slugger';
3import React, { FC, ReactElement } from 'react';
4
5import { HeadingsContext } from '../page-higher-order/withHeadingManager';
6import APISection from './APISection';
7
8import { HeadingManager } from '~/common/headingManager';
9
10const Wrapper: FC = ({ children }) => (
11  <HeadingsContext.Provider value={new HeadingManager(new GithubSlugger(), {})}>
12    {children}
13  </HeadingsContext.Provider>
14);
15
16const customRender = (element: ReactElement, options?: Omit<RenderOptions, 'wrapper'>) =>
17  render(element, { wrapper: Wrapper, ...options });
18
19describe('APISection', () => {
20  test('no data', () => {
21    const { container, getAllByText } = render(<APISection packageName="expo-none" />);
22
23    expect(getAllByText('No API data file found, sorry!')).toHaveLength(1);
24
25    expect(container).toMatchSnapshot();
26  });
27
28  test('expo-apple-authentication', () => {
29    const { container, queryByText, getAllByRole, queryAllByText, queryByDisplayValue } =
30      customRender(
31        <APISection packageName="expo-apple-authentication" forceVersion="unversioned" />
32      );
33
34    expect(getAllByRole('heading', { level: 2 })).toHaveLength(5);
35    expect(getAllByRole('heading', { level: 3 })).toHaveLength(20);
36    expect(getAllByRole('table')).toHaveLength(6);
37
38    expect(queryByText('Event Subscriptions'));
39    expect(queryByText('Components'));
40
41    expect(queryByDisplayValue('AppleAuthenticationButton'));
42    expect(queryByDisplayValue('AppleAuthenticationButtonProps'));
43    expect(queryByDisplayValue('Subscription'));
44
45    expect(queryAllByText('Constants')).toHaveLength(0);
46    expect(queryAllByText('Hooks')).toHaveLength(0);
47    expect(queryAllByText('Interfaces')).toHaveLength(0);
48
49    expect(container).toMatchSnapshot();
50  });
51
52  test('expo-pedometer', () => {
53    const { container, queryByText, getAllByRole, queryAllByText, queryByDisplayValue } =
54      customRender(<APISection packageName="expo-pedometer" forceVersion="v42.0.0" />);
55
56    expect(getAllByRole('heading', { level: 2 })).toHaveLength(4);
57    expect(getAllByRole('heading', { level: 3 })).toHaveLength(11);
58    expect(getAllByRole('table')).toHaveLength(3);
59
60    expect(queryByText('Methods'));
61    expect(queryByText('Enums'));
62    expect(queryByText('Interfaces'));
63    expect(queryByText('Types'));
64
65    expect(queryByDisplayValue('PermissionResponse'));
66    expect(queryByDisplayValue('PermissionStatus'));
67
68    expect(queryAllByText('Constants')).toHaveLength(0);
69    expect(queryAllByText('Event Subscriptions')).toHaveLength(0);
70    expect(queryAllByText('Hooks')).toHaveLength(0);
71
72    expect(container).toMatchSnapshot();
73  });
74});
75