|
1 | 1 | import { describe, expect, it } from 'vitest' |
2 | 2 | import { |
3 | 3 | warnMissingParamParsers, |
| 4 | + collectMissingParamParsers, |
4 | 5 | generateParamParsersTypesDeclarations, |
5 | 6 | generateParamsTypes, |
6 | 7 | generateParamParserOptions, |
@@ -71,6 +72,87 @@ describe('warnMissingParamParsers', () => { |
71 | 72 | }) |
72 | 73 | }) |
73 | 74 |
|
| 75 | +describe('collectMissingParamParsers', () => { |
| 76 | + it('returns empty array for routes without param parsers', () => { |
| 77 | + const tree = new PrefixTree(DEFAULT_OPTIONS) |
| 78 | + tree.insert('users', 'users.vue') |
| 79 | + tree.insert('posts/[id]', 'posts/[id].vue') |
| 80 | + |
| 81 | + const paramParsers: ParamParsersMap = new Map() |
| 82 | + |
| 83 | + const result = collectMissingParamParsers(tree, paramParsers) |
| 84 | + expect(result).toEqual([]) |
| 85 | + }) |
| 86 | + |
| 87 | + it('returns empty array for native parsers', () => { |
| 88 | + const tree = new PrefixTree(DEFAULT_OPTIONS) |
| 89 | + tree.insert('users/[id=int]', 'users/[id=int].vue') |
| 90 | + tree.insert('posts/[active=bool]', 'posts/[active=bool].vue') |
| 91 | + |
| 92 | + const paramParsers: ParamParsersMap = new Map() |
| 93 | + |
| 94 | + const result = collectMissingParamParsers(tree, paramParsers) |
| 95 | + expect(result).toEqual([]) |
| 96 | + }) |
| 97 | + |
| 98 | + it('collects missing custom parsers with route and file info', () => { |
| 99 | + const tree = new PrefixTree(DEFAULT_OPTIONS) |
| 100 | + tree.insert('users/[id=uuid]', 'users/[id=uuid].vue') |
| 101 | + |
| 102 | + const paramParsers: ParamParsersMap = new Map() |
| 103 | + |
| 104 | + const result = collectMissingParamParsers(tree, paramParsers) |
| 105 | + expect(result).toEqual([ |
| 106 | + { |
| 107 | + parser: 'uuid', |
| 108 | + routePath: '/users/:id', |
| 109 | + filePaths: ['users/[id=uuid].vue'], |
| 110 | + }, |
| 111 | + ]) |
| 112 | + }) |
| 113 | + |
| 114 | + it('returns empty array when custom parsers exist in map', () => { |
| 115 | + const tree = new PrefixTree(DEFAULT_OPTIONS) |
| 116 | + tree.insert('users/[id=uuid]', 'users/[id=uuid].vue') |
| 117 | + |
| 118 | + const paramParsers: ParamParsersMap = new Map([ |
| 119 | + [ |
| 120 | + 'uuid', |
| 121 | + { |
| 122 | + name: 'uuid', |
| 123 | + typeName: 'Param_uuid', |
| 124 | + relativePath: 'parsers/uuid', |
| 125 | + absolutePath: '/path/to/parsers/uuid', |
| 126 | + }, |
| 127 | + ], |
| 128 | + ]) |
| 129 | + |
| 130 | + const result = collectMissingParamParsers(tree, paramParsers) |
| 131 | + expect(result).toEqual([]) |
| 132 | + }) |
| 133 | + |
| 134 | + it('collects multiple missing parsers from different routes', () => { |
| 135 | + const tree = new PrefixTree(DEFAULT_OPTIONS) |
| 136 | + tree.insert('users/[id=uuid]', 'users/[id=uuid].vue') |
| 137 | + tree.insert('posts/[slug=slug]', 'posts/[slug=slug].vue') |
| 138 | + |
| 139 | + const paramParsers: ParamParsersMap = new Map() |
| 140 | + |
| 141 | + const result = collectMissingParamParsers(tree, paramParsers) |
| 142 | + expect(result).toHaveLength(2) |
| 143 | + expect(result).toContainEqual({ |
| 144 | + parser: 'uuid', |
| 145 | + routePath: '/users/:id', |
| 146 | + filePaths: ['users/[id=uuid].vue'], |
| 147 | + }) |
| 148 | + expect(result).toContainEqual({ |
| 149 | + parser: 'slug', |
| 150 | + routePath: '/posts/:slug', |
| 151 | + filePaths: ['posts/[slug=slug].vue'], |
| 152 | + }) |
| 153 | + }) |
| 154 | +}) |
| 155 | + |
74 | 156 | describe('generateParamParsersTypesDeclarations', () => { |
75 | 157 | it('returns empty string for empty param parsers map', () => { |
76 | 158 | const paramParsers: ParamParsersMap = new Map() |
|
0 commit comments