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(), { headings: [] })}> 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-barcode-scanner', () => { 53 const { container, queryByText, getAllByRole, queryAllByText, queryByDisplayValue } = 54 customRender( 55 <APISection 56 packageName="expo-barcode-scanner" 57 apiName="BarCodeScanner" 58 forceVersion="unversioned" 59 /> 60 ); 61 62 expect(getAllByRole('heading', { level: 2 })).toHaveLength(6); 63 expect(getAllByRole('heading', { level: 3 })).toHaveLength(16); 64 65 expect(queryByText('Components')); 66 expect(queryByText('Hooks')); 67 68 expect(queryByDisplayValue('BarCodeEvent')); 69 expect(queryByDisplayValue('BarCodeScannerProps')); 70 expect(queryByDisplayValue('Subscription')); 71 expect(queryByDisplayValue('usePermissions')); 72 73 expect(queryAllByText('Constants')).toHaveLength(0); 74 expect(queryAllByText('Props')).toHaveLength(0); 75 76 expect(container).toMatchSnapshot(); 77 }); 78 79 test('expo-pedometer', () => { 80 const { container, queryByText, getAllByRole, queryAllByText, queryByDisplayValue } = 81 customRender(<APISection packageName="expo-pedometer" forceVersion="v42.0.0" />); 82 83 expect(getAllByRole('heading', { level: 2 })).toHaveLength(4); 84 expect(getAllByRole('heading', { level: 3 })).toHaveLength(11); 85 expect(getAllByRole('table')).toHaveLength(3); 86 87 expect(queryByText('Methods')); 88 expect(queryByText('Enums')); 89 expect(queryByText('Interfaces')); 90 expect(queryByText('Types')); 91 92 expect(queryByDisplayValue('PermissionResponse')); 93 expect(queryByDisplayValue('PermissionStatus')); 94 95 expect(queryAllByText('Constants')).toHaveLength(0); 96 expect(queryAllByText('Event Subscriptions')).toHaveLength(0); 97 expect(queryAllByText('Hooks')).toHaveLength(0); 98 99 expect(container).toMatchSnapshot(); 100 }); 101}); 102