|
| 1 | +import { getChildPageNodes } from '../getChildPageNodes'; |
| 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('getChildPageNodes', () => { |
| 43 | + it('should return the children of a non-root node when the route matches a non-root node', () => { |
| 44 | + const result = getChildPageNodes('/route1/route2'); |
| 45 | + |
| 46 | + expect(result).toEqual([ |
| 47 | + { |
| 48 | + route: '/route1/route2/child1', |
| 49 | + children: [ |
| 50 | + { |
| 51 | + route: '/route1/route2/child1/child1' |
| 52 | + }, |
| 53 | + { |
| 54 | + route: '/route1/route2/child1/child2' |
| 55 | + }, |
| 56 | + { |
| 57 | + route: '/route1/route2/child1/child3' |
| 58 | + } |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + route: '/route1/route2/child2' |
| 63 | + }, |
| 64 | + { |
| 65 | + route: '/route1/route2/child3' |
| 66 | + } |
| 67 | + ]); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should return empty array when the route does not match any node in the directory', () => { |
| 71 | + const result = getChildPageNodes('/route1/route20'); |
| 72 | + |
| 73 | + expect(result).toEqual([]); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should handle the case when the route is an empty string', () => { |
| 77 | + const result = getChildPageNodes(''); |
| 78 | + |
| 79 | + expect(result).toEqual([]); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should return empty array when children array is empty or undefined', () => { |
| 83 | + const result = getChildPageNodes('/route1/route2/child2'); |
| 84 | + |
| 85 | + expect(result).toEqual([]); |
| 86 | + }); |
| 87 | +}); |
0 commit comments