From ea56e76f7df5f453a832760919bb7fb716fe419f Mon Sep 17 00:00:00 2001 From: timngyn Date: Wed, 17 Jan 2024 15:02:52 -0800 Subject: [PATCH 1/7] Add module name mapper for 'src/directory' --- jest.config.js | 3 ++- src/components/Layout/Layout.tsx | 2 +- src/components/Menu/Menu.tsx | 2 +- src/components/Menu/MenuItem.tsx | 2 +- src/components/NextPrevious/index.tsx | 2 +- src/components/Overview/Overview.tsx | 2 +- src/components/PageLastUpdated/PageLastUpdated.tsx | 2 +- src/utils/useRouteFinder.ts | 2 +- tsconfig.json | 3 ++- 9 files changed, 11 insertions(+), 9 deletions(-) diff --git a/jest.config.js b/jest.config.js index 19b690ea1df..3f1a9496739 100644 --- a/jest.config.js +++ b/jest.config.js @@ -17,7 +17,8 @@ module.exports = { '@/components/(.*)': '/src/components/$1', '@/constants/(.*)': '/src/constants/$1', '@/utils/(.*)': '/src/utils/$1', - '@/data/(.*)': '/src/data/$1' + '@/data/(.*)': '/src/data/$1', + '@/directory/(.*)': '/src/directory/$1' }, transformIgnorePatterns: [] }; diff --git a/src/components/Layout/Layout.tsx b/src/components/Layout/Layout.tsx index 4ff15b2b217..cd7a7f9ad39 100644 --- a/src/components/Layout/Layout.tsx +++ b/src/components/Layout/Layout.tsx @@ -37,7 +37,7 @@ import { LayoutProvider } from '@/components/Layout'; import { TableOfContents } from '@/components/TableOfContents'; import type { HeadingInterface } from '@/components/TableOfContents/TableOfContents'; import { PlatformNavigator } from '@/components/PlatformNavigator'; -import flatDirectory from 'src/directory/flatDirectory.json'; +import flatDirectory from '@/directory/flatDirectory.json'; import { Breadcrumbs } from '@/components/Breadcrumbs'; import { debounce } from '@/utils/debounce'; import { DocSearch } from '@docsearch/react'; diff --git a/src/components/Menu/Menu.tsx b/src/components/Menu/Menu.tsx index 01dd5a84239..832b811a336 100644 --- a/src/components/Menu/Menu.tsx +++ b/src/components/Menu/Menu.tsx @@ -1,7 +1,7 @@ import { ReactElement } from 'react'; import { MenuItem } from './MenuItem'; import { Platform } from '@/data/platforms'; -import { PageNode } from 'src/directory/directory'; +import { PageNode } from '@/directory/directory'; import { findDirectoryNode } from '@/utils/findDirectoryNode'; import { BUILD_A_BACKEND, PREV_BUILD_A_BACKEND } from '@/data/routes'; diff --git a/src/components/Menu/MenuItem.tsx b/src/components/Menu/MenuItem.tsx index f6bd86972c3..4856308b938 100644 --- a/src/components/Menu/MenuItem.tsx +++ b/src/components/Menu/MenuItem.tsx @@ -5,7 +5,7 @@ import { IconExternalLink, IconChevron } from '@/components/Icons'; import Link from 'next/link'; import { JS_PLATFORMS, Platform, JSPlatform } from '@/data/platforms'; import { LayoutContext } from '@/components/Layout'; -import { PageNode } from 'src/directory/directory'; +import { PageNode } from '@/directory/directory'; enum Levels { Category = 1, diff --git a/src/components/NextPrevious/index.tsx b/src/components/NextPrevious/index.tsx index 783146d905b..6fb65189a40 100644 --- a/src/components/NextPrevious/index.tsx +++ b/src/components/NextPrevious/index.tsx @@ -1,6 +1,6 @@ import Link from 'next/link'; import { View, Flex } from '@aws-amplify/ui-react'; -import directory from 'src/directory/directory.json'; +import directory from '@/directory/directory.json'; import { useRouter } from 'next/router'; import { useCurrentPlatform } from '@/utils/useCurrentPlatform'; import { IconChevron } from '@/components/Icons'; diff --git a/src/components/Overview/Overview.tsx b/src/components/Overview/Overview.tsx index 23ce8acd7b3..296f1ffcad5 100644 --- a/src/components/Overview/Overview.tsx +++ b/src/components/Overview/Overview.tsx @@ -1,4 +1,4 @@ -import { PageNode } from 'src/directory/directory'; +import { PageNode } from '@/directory/directory'; import { Card, Flex, View, Text } from '@aws-amplify/ui-react'; import Link from 'next/link'; import { useRouter } from 'next/router'; diff --git a/src/components/PageLastUpdated/PageLastUpdated.tsx b/src/components/PageLastUpdated/PageLastUpdated.tsx index 5605e8987bd..fe2076a51d8 100644 --- a/src/components/PageLastUpdated/PageLastUpdated.tsx +++ b/src/components/PageLastUpdated/PageLastUpdated.tsx @@ -1,5 +1,5 @@ import { Text, View } from '@aws-amplify/ui-react'; -import { PageNode } from 'src/directory/directory'; +import { PageNode } from '@/directory/directory'; type PageLastUpdatedProps = { directoryData: PageNode; diff --git a/src/utils/useRouteFinder.ts b/src/utils/useRouteFinder.ts index 9aa22fa3acc..6d17472e7c8 100644 --- a/src/utils/useRouteFinder.ts +++ b/src/utils/useRouteFinder.ts @@ -1,4 +1,4 @@ -import flatDirectory from 'src/directory/flatDirectory.json'; +import flatDirectory from '@/directory/flatDirectory.json'; import { useRouter } from 'next/router'; import { Platform } from '@/data/platforms'; diff --git a/tsconfig.json b/tsconfig.json index ec8206dd4ef..2e464bf1ffc 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,7 +20,8 @@ "@/plugins/*": ["src/plugins/*"], "@/protected/*": ["src/protected/*"], "@/themes/*": ["src/themes/*"], - "@/utils/*": ["src/utils/*"] + "@/utils/*": ["src/utils/*"], + "@/directory/*": ["src/directory/*"] } }, "extends": "./tsconfig.base.json", From 83adfc56671f31e11a35086dbca53df8e87bc94a Mon Sep 17 00:00:00 2001 From: timngyn Date: Wed, 17 Jan 2024 15:03:11 -0800 Subject: [PATCH 2/7] Add tests for useRouteFinder --- src/utils/__tests__/useRouteFinder.tests.ts | 77 +++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/utils/__tests__/useRouteFinder.tests.ts diff --git a/src/utils/__tests__/useRouteFinder.tests.ts b/src/utils/__tests__/useRouteFinder.tests.ts new file mode 100644 index 00000000000..7441188374f --- /dev/null +++ b/src/utils/__tests__/useRouteFinder.tests.ts @@ -0,0 +1,77 @@ +import { renderHook } from '@testing-library/react'; +import { useRouteFinder } from '../useRouteFinder'; + +const routerMock = { + __esModule: true, + useRouter: () => { + return { + pathname: '' + }; + } +}; + +jest.mock('next/router', () => routerMock); + +const flatDirectoryMock = {}; + +jest.mock('@/directory/flatDirectory.json', () => flatDirectoryMock); + +describe('useRouteFinder', () => { + it('should replace "/[platform]" with "/[platform]/prev" when isPrev is false', () => { + routerMock.useRouter = () => { + return { + pathname: '/[platform]/build-a-backend/auth/set-up-auth' + }; + }; + + flatDirectoryMock['/[platform]/prev/build-a-backend/auth/set-up-auth'] = { + path: 'src/pages/[platform]/prev/build-a-backend/auth/set-up-auth/index.mdx', + platforms: ['react'], + route: '/[platform]/prev/build-a-backend/auth/set-up-auth' + }; + + const { result } = renderHook(() => useRouteFinder('react', false)); + + expect(result.current).toEqual( + '/[platform]/prev/build-a-backend/auth/set-up-auth' + ); + }); + + it('should replace "/[platform]/prev" with "/[platform]" when isPrev is true', () => { + routerMock.useRouter = () => { + return { + pathname: '/[platform]/prev/build-a-backend/auth/set-up-auth' + }; + }; + + flatDirectoryMock['/[platform]/build-a-backend/auth/set-up-auth'] = { + path: 'src/pages/[platform]/build-a-backend/auth/set-up-auth/index.mdx', + platforms: ['react'], + route: '/[platform]/build-a-backend/auth/set-up-auth' + }; + + const { result } = renderHook(() => useRouteFinder('react', true)); + + expect(result.current).toEqual( + '/[platform]/build-a-backend/auth/set-up-auth' + ); + }); + + it('should not return the newRoute if the provided platform is not included in pageNode.platforms', () => { + routerMock.useRouter = () => { + return { + pathname: '/[platform]/prev/build-a-backend/auth/set-up-auth' + }; + }; + + flatDirectoryMock['/[platform]/build-a-backend/auth/set-up-auth'] = { + path: 'src/pages/[platform]/build-a-backend/auth/set-up-auth/index.mdx', + platforms: ['react'], + route: '/[platform]/build-a-backend/auth/set-up-auth' + }; + + const { result } = renderHook(() => useRouteFinder('angular', true)); + + expect(result.current).toEqual(undefined); + }); +}); From 996d821bb6fee958db7c2760a725b9f9f3f1a30d Mon Sep 17 00:00:00 2001 From: timngyn Date: Wed, 17 Jan 2024 15:53:45 -0800 Subject: [PATCH 3/7] Update workflow to generate the json files --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f3b2ca66647..b4dfe198276 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -17,7 +17,7 @@ jobs: - name: Install Dependencies run: yarn - name: Run tests - run: yarn test + run: yarn prebuild && yarn test - name: Run Build run: yarn build env: From 85627d98838b3a67be18b6f4e580b61a19f8dfc9 Mon Sep 17 00:00:00 2001 From: timngyn Date: Wed, 17 Jan 2024 15:55:12 -0800 Subject: [PATCH 4/7] Update import paths with new module name mapper --- src/utils/findDirectoryNode.ts | 2 +- src/utils/getChildPageNodes.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/findDirectoryNode.ts b/src/utils/findDirectoryNode.ts index 41e47e8e21f..473fdc318a2 100644 --- a/src/utils/findDirectoryNode.ts +++ b/src/utils/findDirectoryNode.ts @@ -1,4 +1,4 @@ -import directory from '../directory/directory.json'; +import directory from '@/directory/directory.json'; export const findDirectoryNode = (route, dir = directory) => { if (dir.route === route) { diff --git a/src/utils/getChildPageNodes.ts b/src/utils/getChildPageNodes.ts index 611b69cd333..65976a36e55 100644 --- a/src/utils/getChildPageNodes.ts +++ b/src/utils/getChildPageNodes.ts @@ -1,4 +1,4 @@ -import directory from '../directory/directory.json'; +import directory from '@/directory/directory.json'; import { PageNode } from 'src/directory/directory'; /** From ac3f1d15c258fad13d57f09453670e9e0297fca1 Mon Sep 17 00:00:00 2001 From: timngyn Date: Wed, 17 Jan 2024 15:59:04 -0800 Subject: [PATCH 5/7] Rename typo, tests -> test --- .../__tests__/{useRouteFinder.tests.ts => useRouteFinder.test.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/utils/__tests__/{useRouteFinder.tests.ts => useRouteFinder.test.ts} (100%) diff --git a/src/utils/__tests__/useRouteFinder.tests.ts b/src/utils/__tests__/useRouteFinder.test.ts similarity index 100% rename from src/utils/__tests__/useRouteFinder.tests.ts rename to src/utils/__tests__/useRouteFinder.test.ts From 191dc79efe4c8b46352de9f8c25ba5a90e7fcc0d Mon Sep 17 00:00:00 2001 From: timngyn Date: Wed, 17 Jan 2024 16:39:31 -0800 Subject: [PATCH 6/7] Add tests for getChildPageNodes --- src/utils/__tests__/getChildPageNodes.test.ts | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/utils/__tests__/getChildPageNodes.test.ts diff --git a/src/utils/__tests__/getChildPageNodes.test.ts b/src/utils/__tests__/getChildPageNodes.test.ts new file mode 100644 index 00000000000..269a9868502 --- /dev/null +++ b/src/utils/__tests__/getChildPageNodes.test.ts @@ -0,0 +1,87 @@ +import { getChildPageNodes } from '../getChildPageNodes'; + +jest.mock( + '@/directory/directory.json', + () => { + const mockDirectory = { + route: '/', + children: [ + { + route: '/route1/route2', + children: [ + { + route: '/route1/route2/child1', + children: [ + { + route: '/route1/route2/child1/child1' + }, + { + route: '/route1/route2/child1/child2' + }, + { + route: '/route1/route2/child1/child3' + } + ] + }, + { + route: '/route1/route2/child2' + }, + { + route: '/route1/route2/child3' + } + ] + } + ] + }; + + return mockDirectory; + }, + { virtual: true } +); + +describe('getChildPageNodes', () => { + it('should return the children of a non-root node when the route matches a non-root node', () => { + const result = getChildPageNodes('/route1/route2'); + + expect(result).toEqual([ + { + route: '/route1/route2/child1', + children: [ + { + route: '/route1/route2/child1/child1' + }, + { + route: '/route1/route2/child1/child2' + }, + { + route: '/route1/route2/child1/child3' + } + ] + }, + { + route: '/route1/route2/child2' + }, + { + route: '/route1/route2/child3' + } + ]); + }); + + it('should return empty array when the route does not match any node in the directory', () => { + const result = getChildPageNodes('/route1/route20'); + + expect(result).toEqual([]); + }); + + it('should handle the case when the route is an empty string', () => { + const result = getChildPageNodes(''); + + expect(result).toEqual([]); + }); + + it('should return empty array when children array is empty or undefined', () => { + const result = getChildPageNodes('/route1/route2/child2'); + + expect(result).toEqual([]); + }); +}); From 1595a1120cb26db6784fd1ab2d5c0f8eda0eb38e Mon Sep 17 00:00:00 2001 From: timngyn Date: Thu, 25 Jan 2024 10:59:36 -0800 Subject: [PATCH 7/7] Remove outdated test --- src/utils/__tests__/useRouteFinder.test.ts | 77 ---------------------- 1 file changed, 77 deletions(-) delete mode 100644 src/utils/__tests__/useRouteFinder.test.ts diff --git a/src/utils/__tests__/useRouteFinder.test.ts b/src/utils/__tests__/useRouteFinder.test.ts deleted file mode 100644 index 7441188374f..00000000000 --- a/src/utils/__tests__/useRouteFinder.test.ts +++ /dev/null @@ -1,77 +0,0 @@ -import { renderHook } from '@testing-library/react'; -import { useRouteFinder } from '../useRouteFinder'; - -const routerMock = { - __esModule: true, - useRouter: () => { - return { - pathname: '' - }; - } -}; - -jest.mock('next/router', () => routerMock); - -const flatDirectoryMock = {}; - -jest.mock('@/directory/flatDirectory.json', () => flatDirectoryMock); - -describe('useRouteFinder', () => { - it('should replace "/[platform]" with "/[platform]/prev" when isPrev is false', () => { - routerMock.useRouter = () => { - return { - pathname: '/[platform]/build-a-backend/auth/set-up-auth' - }; - }; - - flatDirectoryMock['/[platform]/prev/build-a-backend/auth/set-up-auth'] = { - path: 'src/pages/[platform]/prev/build-a-backend/auth/set-up-auth/index.mdx', - platforms: ['react'], - route: '/[platform]/prev/build-a-backend/auth/set-up-auth' - }; - - const { result } = renderHook(() => useRouteFinder('react', false)); - - expect(result.current).toEqual( - '/[platform]/prev/build-a-backend/auth/set-up-auth' - ); - }); - - it('should replace "/[platform]/prev" with "/[platform]" when isPrev is true', () => { - routerMock.useRouter = () => { - return { - pathname: '/[platform]/prev/build-a-backend/auth/set-up-auth' - }; - }; - - flatDirectoryMock['/[platform]/build-a-backend/auth/set-up-auth'] = { - path: 'src/pages/[platform]/build-a-backend/auth/set-up-auth/index.mdx', - platforms: ['react'], - route: '/[platform]/build-a-backend/auth/set-up-auth' - }; - - const { result } = renderHook(() => useRouteFinder('react', true)); - - expect(result.current).toEqual( - '/[platform]/build-a-backend/auth/set-up-auth' - ); - }); - - it('should not return the newRoute if the provided platform is not included in pageNode.platforms', () => { - routerMock.useRouter = () => { - return { - pathname: '/[platform]/prev/build-a-backend/auth/set-up-auth' - }; - }; - - flatDirectoryMock['/[platform]/build-a-backend/auth/set-up-auth'] = { - path: 'src/pages/[platform]/build-a-backend/auth/set-up-auth/index.mdx', - platforms: ['react'], - route: '/[platform]/build-a-backend/auth/set-up-auth' - }; - - const { result } = renderHook(() => useRouteFinder('angular', true)); - - expect(result.current).toEqual(undefined); - }); -});