1import { render, screen } from '@testing-library/react'; 2import { RouterContext } from 'next/dist/shared/lib/router-context'; 3import { NextRouter } from 'next/router'; 4import { ReactElement } from 'react'; 5 6import { Footer } from './Footer'; 7import { githubUrl } from './utils'; 8 9const withTestRouter = (tree: ReactElement, router: Partial<NextRouter> = {}) => ( 10 <RouterContext.Provider value={router as NextRouter}>{tree}</RouterContext.Provider> 11); 12 13describe('Footer', () => { 14 test('displays default links', () => { 15 const router = { pathname: '/example/' }; 16 render(withTestRouter(<Footer title="test-title" />, router)); 17 18 screen.getByText('Ask a question on the forums'); 19 screen.getByText('Edit this page'); 20 }); 21 22 test('displays forums link with tag', () => { 23 const router = { pathname: '/sdk/' }; 24 render(withTestRouter(<Footer title="test-title" />, router)); 25 26 screen.getByText('Ask a question on the forums about test-title'); 27 }); 28 29 test('displays issues link', () => { 30 const router = { pathname: '/sdk/' }; 31 render(withTestRouter(<Footer title="test-title" />, router)); 32 33 screen.getByText('View open bug reports for test-title'); 34 }); 35 36 test('displays correct issues link for 3rd-party package', () => { 37 const router = { pathname: '/sdk/' }; 38 render( 39 withTestRouter( 40 <Footer 41 title="GestureHandler" 42 sourceCodeUrl="https://github.com/software-mansion/react-native-gesture-handler" 43 packageName="react-native-gesture-handler" 44 />, 45 router 46 ) 47 ); 48 49 const link = screen.getByRole('link', { 50 name: 'View open bug reports for GestureHandler', 51 }); 52 expect(link.getAttribute('href')).toBe( 53 'https://github.com/software-mansion/react-native-gesture-handler/issues' 54 ); 55 }); 56}); 57 58describe('githubUrl', () => { 59 const EDIT_URL_PREFIX = 'https://github.com/expo/expo/edit/main/docs/pages'; 60 61 test('non-versioned page', () => { 62 expect(githubUrl('/guides')).toBe(EDIT_URL_PREFIX + '/guides.mdx'); 63 }); 64 65 test('nested non-versioned page', () => { 66 expect(githubUrl('/build/introduction')).toBe(EDIT_URL_PREFIX + '/build/introduction.mdx'); 67 }); 68 69 test('versioned index page', () => { 70 expect(githubUrl('/versions/v42.0.0')).toBe(EDIT_URL_PREFIX + '/versions/v42.0.0/index.mdx'); 71 }); 72 73 test('nested versioned page', () => { 74 expect(githubUrl('/versions/v42.0.0/sdk/av')).toBe( 75 EDIT_URL_PREFIX + '/versions/v42.0.0/sdk/av.mdx' 76 ); 77 }); 78 79 test('latest index page', () => { 80 expect(githubUrl('/versions/latest')).toBe(EDIT_URL_PREFIX + '/versions/unversioned/index.mdx'); 81 }); 82 83 test('nested latest page', () => { 84 expect(githubUrl('/versions/latest/sdk/av')).toBe( 85 EDIT_URL_PREFIX + '/versions/unversioned/sdk/av.mdx' 86 ); 87 }); 88 89 test('unversioned index page', () => { 90 expect(githubUrl('/versions/unversioned')).toBe( 91 EDIT_URL_PREFIX + '/versions/unversioned/index.mdx' 92 ); 93 }); 94 95 test('nested unversioned page', () => { 96 expect(githubUrl('/versions/unversioned/sdk/av')).toBe( 97 EDIT_URL_PREFIX + '/versions/unversioned/sdk/av.mdx' 98 ); 99 }); 100}); 101