|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { splitLocaleRoutes } from '../src/split-locale-routes' |
| 3 | + |
| 4 | +describe('splitLocaleRoutes (#244)', () => { |
| 5 | + test('splits one registry into pages + globalLocaleRoutes keyed by name', () => { |
| 6 | + const result = splitLocaleRoutes([ |
| 7 | + { |
| 8 | + name: 'products_tag', |
| 9 | + path: '/products/:tag', |
| 10 | + file: '~/pages/_dynamic.vue', |
| 11 | + paths: { fr: '/produits/:tag', de: '/produkte/:tag' }, |
| 12 | + }, |
| 13 | + { |
| 14 | + name: 'products_category', |
| 15 | + path: '/products/category/:category', |
| 16 | + file: '~/pages/_dynamic.vue', |
| 17 | + paths: { fr: '/produits/categorie/:category' }, |
| 18 | + }, |
| 19 | + ]) |
| 20 | + |
| 21 | + expect(result.pages).toEqual([ |
| 22 | + { name: 'products_tag', path: '/products/:tag', file: '~/pages/_dynamic.vue' }, |
| 23 | + { name: 'products_category', path: '/products/category/:category', file: '~/pages/_dynamic.vue' }, |
| 24 | + ]) |
| 25 | + expect(result.globalLocaleRoutes).toEqual({ |
| 26 | + products_tag: { fr: '/produits/:tag', de: '/produkte/:tag' }, |
| 27 | + products_category: { fr: '/produits/categorie/:category' }, |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + test('shared wrapper file does not collapse locale paths', () => { |
| 32 | + const shared = '~/pages/_dynamic.vue' |
| 33 | + const { pages, globalLocaleRoutes } = splitLocaleRoutes([ |
| 34 | + { name: 'a', path: '/a', file: shared, paths: { fr: '/aa' } }, |
| 35 | + { name: 'b', path: '/b', file: shared, paths: { fr: '/bb' } }, |
| 36 | + ]) |
| 37 | + |
| 38 | + expect(pages).toHaveLength(2) |
| 39 | + expect(pages[0]!.file).toBe(pages[1]!.file) |
| 40 | + expect(globalLocaleRoutes.a).toEqual({ fr: '/aa' }) |
| 41 | + expect(globalLocaleRoutes.b).toEqual({ fr: '/bb' }) |
| 42 | + }) |
| 43 | + |
| 44 | + test('supports false to disable localization; omits paths when unset', () => { |
| 45 | + const { pages, globalLocaleRoutes } = splitLocaleRoutes([ |
| 46 | + { name: 'plain', path: '/plain', file: '~/pages/plain.vue' }, |
| 47 | + { name: 'fixed', path: '/fixed', file: '~/pages/fixed.vue', paths: false }, |
| 48 | + ]) |
| 49 | + |
| 50 | + expect(pages.map((p) => p.name)).toEqual(['plain', 'fixed']) |
| 51 | + expect(globalLocaleRoutes).toEqual({ fixed: false }) |
| 52 | + expect(globalLocaleRoutes).not.toHaveProperty('plain') |
| 53 | + }) |
| 54 | + |
| 55 | + test('throws on duplicate or incomplete entries', () => { |
| 56 | + expect(() => |
| 57 | + splitLocaleRoutes([ |
| 58 | + { name: 'x', path: '/x', file: '~/x.vue' }, |
| 59 | + { name: 'x', path: '/y', file: '~/y.vue' }, |
| 60 | + ]), |
| 61 | + ).toThrow(/duplicate route name "x"/) |
| 62 | + |
| 63 | + expect(() => splitLocaleRoutes([{ name: '', path: '/x', file: '~/x.vue' }])).toThrow(/non-empty `name`/) |
| 64 | + expect(() => splitLocaleRoutes([{ name: 'x', path: '', file: '~/x.vue' }])).toThrow(/needs a `path`/) |
| 65 | + expect(() => splitLocaleRoutes([{ name: 'x', path: '/x', file: '' }])).toThrow(/needs a `file`/) |
| 66 | + }) |
| 67 | +}) |
0 commit comments