1import { render, screen, RenderOptions } from '@testing-library/react'; 2import GithubSlugger from 'github-slugger'; 3import React, { PropsWithChildren, ReactElement } from 'react'; 4 5import { HeadingsContext } from '../page-higher-order/withHeadingManager'; 6import APISection from './APISection'; 7 8import { HeadingManager } from '~/common/headingManager'; 9 10const Wrapper = ({ children }: PropsWithChildren<object>) => ( 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 } = render(<APISection packageName="expo-none" />); 22 23 expect(screen.getAllByText('No API data file found, sorry!')).toHaveLength(1); 24 25 expect(container).toMatchSnapshot(); 26 }); 27 28 test('expo-apple-authentication', () => { 29 const { container } = customRender( 30 <APISection packageName="expo-apple-authentication" forceVersion="unversioned" /> 31 ); 32 33 expect(screen.getAllByRole('heading', { level: 2 })).toHaveLength(6); 34 expect(screen.getAllByRole('heading', { level: 3 })).toHaveLength(25); 35 expect(screen.getAllByRole('table')).toHaveLength(11); 36 37 expect(screen.queryByText('Event Subscriptions')); 38 expect(screen.queryByText('Components')); 39 40 expect(screen.queryByDisplayValue('AppleAuthenticationButton')); 41 expect(screen.queryByDisplayValue('AppleAuthenticationButtonProps')); 42 expect(screen.queryByDisplayValue('Subscription')); 43 44 expect(screen.queryAllByText('Constants')).toHaveLength(0); 45 expect(screen.queryAllByText('Hooks')).toHaveLength(0); 46 expect(screen.queryAllByText('Interfaces')).toHaveLength(0); 47 48 expect(container).toMatchSnapshot(); 49 }); 50 51 test('expo-barcode-scanner', () => { 52 const { container } = customRender( 53 <APISection 54 packageName="expo-barcode-scanner" 55 apiName="BarCodeScanner" 56 forceVersion="unversioned" 57 /> 58 ); 59 60 expect(screen.getAllByRole('heading', { level: 2 })).toHaveLength(7); 61 expect(screen.getAllByRole('heading', { level: 3 })).toHaveLength(19); 62 63 expect(screen.queryByText('Components')); 64 expect(screen.queryByText('Hooks')); 65 66 expect(screen.queryByDisplayValue('BarCodeEvent')); 67 expect(screen.queryByDisplayValue('BarCodeScannerProps')); 68 expect(screen.queryByDisplayValue('Subscription')); 69 expect(screen.queryByDisplayValue('usePermissions')); 70 expect(screen.queryByDisplayValue('Inherited Props')); 71 72 expect(screen.queryAllByText('Constants')).toHaveLength(0); 73 74 expect(container).toMatchSnapshot(); 75 }); 76 77 test('expo-pedometer', () => { 78 const { container } = customRender( 79 <APISection packageName="expo-pedometer" forceVersion="v45.0.0" /> 80 ); 81 82 expect(screen.getAllByRole('heading', { level: 2 })).toHaveLength(4); 83 expect(screen.getAllByRole('heading', { level: 3 })).toHaveLength(11); 84 expect(screen.getAllByRole('table')).toHaveLength(6); 85 86 expect(screen.queryByText('Methods')); 87 expect(screen.queryByText('Enums')); 88 expect(screen.queryByText('Interfaces')); 89 expect(screen.queryByText('Types')); 90 91 expect(screen.queryByDisplayValue('PermissionResponse')); 92 expect(screen.queryByDisplayValue('PermissionStatus')); 93 94 expect(screen.queryAllByText('Constants')).toHaveLength(0); 95 expect(screen.queryAllByText('Event Subscriptions')).toHaveLength(0); 96 expect(screen.queryAllByText('Hooks')).toHaveLength(0); 97 98 expect(container).toMatchSnapshot(); 99 }); 100 101 test('expo-asset', () => { 102 customRender(<APISection packageName="expo-asset" forceVersion="unversioned" />); 103 104 expect(screen.getAllByRole('heading', { level: 2 })).toHaveLength(3); 105 expect(screen.getAllByRole('heading', { level: 3 })).toHaveLength(18); 106 expect(screen.getAllByRole('table')).toHaveLength(7); 107 108 expect(screen.queryByText('Classes')); 109 expect(screen.queryByText('Asset Properties')); 110 expect(screen.queryByText('Asset Methods')); 111 112 expect(screen.queryByDisplayValue('localUri')); 113 expect(screen.queryByDisplayValue('fromURI()')); 114 115 expect(screen.queryAllByText('Props')).toHaveLength(0); 116 expect(screen.queryAllByText('Enums')).toHaveLength(0); 117 }); 118}); 119