Skip to content

Commit acb49b1

Browse files
authored
Add tests for findDirectoryNode (#6808)
* Add module name mapper for 'src/directory' * Add tests for useRouteFinder * Update workflow to generate the json files * Update import paths with new module name mapper * Rename typo, tests -> test * Add tests for findDirectoryNode
1 parent 39c284b commit acb49b1

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { findDirectoryNode } from '../findDirectoryNode';
2+
3+
jest.mock(
4+
'@/directory/directory.json',
5+
() => {
6+
const mockDirectory = {
7+
route: '/',
8+
children: [
9+
{
10+
route: '/route1/route2',
11+
children: [
12+
{
13+
route: '/route1/route2/child1',
14+
children: [
15+
{
16+
route: '/route1/route2/child1/child1'
17+
},
18+
{
19+
route: '/route1/route2/child1/child2'
20+
},
21+
{
22+
route: '/route1/route2/child1/child3'
23+
}
24+
]
25+
},
26+
{
27+
route: '/route1/route2/child2'
28+
},
29+
{
30+
route: '/route1/route2/child3'
31+
}
32+
]
33+
}
34+
]
35+
};
36+
37+
return mockDirectory;
38+
},
39+
{ virtual: true }
40+
);
41+
42+
describe('findDirectoryNode', () => {
43+
it('should return directory page node if it exists', () => {
44+
const result = findDirectoryNode('/route1/route2/child1');
45+
46+
expect(result).toEqual({
47+
route: '/route1/route2/child1',
48+
children: [
49+
{
50+
route: '/route1/route2/child1/child1'
51+
},
52+
{
53+
route: '/route1/route2/child1/child2'
54+
},
55+
{
56+
route: '/route1/route2/child1/child3'
57+
}
58+
]
59+
});
60+
});
61+
62+
it('should return undefined if page not is not found', () => {
63+
const result = findDirectoryNode('/route1/route2/child4');
64+
65+
expect(result).toEqual(undefined);
66+
});
67+
});

src/utils/findDirectoryNode.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import directory from '@/directory/directory.json';
2+
import { PageNode } from '@/directory/directory';
23

3-
export const findDirectoryNode = (route, dir = directory) => {
4+
const directoryCast = directory as PageNode;
5+
6+
export const findDirectoryNode = (route: string, dir = directoryCast) => {
47
if (dir.route === route) {
58
return dir;
69
} else if (dir.children && dir.children.length) {

0 commit comments

Comments
 (0)