|
| 1 | +import { expect } from 'chai' |
| 2 | +import { localizeRoutes } from '../src/resolve' |
| 3 | +import { VUE_I18N_ROUTING_DEFAULTS } from '../src/constants' |
| 4 | + |
| 5 | +import type { VueI18nRoute } from '../src/types' |
| 6 | + |
| 7 | +describe('localizeRoutes', function () { |
| 8 | + describe('basic', function () { |
| 9 | + it('should be localized routing', function () { |
| 10 | + const routes: VueI18nRoute[] = [ |
| 11 | + { |
| 12 | + path: '/', |
| 13 | + name: 'home' |
| 14 | + }, |
| 15 | + { |
| 16 | + path: '/about', |
| 17 | + name: 'about' |
| 18 | + } |
| 19 | + ] |
| 20 | + const localeCodes = ['en', 'ja'] |
| 21 | + const localizedRoutes = localizeRoutes(routes, { localeCodes }) |
| 22 | + |
| 23 | + expect(localizedRoutes.length).to.equal(4) |
| 24 | + localeCodes.forEach(locale => { |
| 25 | + routes.forEach(route => { |
| 26 | + expect(localizedRoutes).to.deep.include({ |
| 27 | + path: `/${locale}${route.path === '/' ? '' : route.path}`, |
| 28 | + name: `${route.name}${VUE_I18N_ROUTING_DEFAULTS.routesNameSeparator}${locale}` |
| 29 | + }) |
| 30 | + }) |
| 31 | + }) |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + describe('has children', function () { |
| 36 | + it('should be localized routing', function () { |
| 37 | + const routes: VueI18nRoute[] = [ |
| 38 | + { |
| 39 | + path: '/user/:id', |
| 40 | + name: 'user', |
| 41 | + children: [ |
| 42 | + { |
| 43 | + path: 'profile', |
| 44 | + name: 'user-profile' |
| 45 | + }, |
| 46 | + { |
| 47 | + path: 'posts', |
| 48 | + name: 'user-posts' |
| 49 | + } |
| 50 | + ] |
| 51 | + } |
| 52 | + ] |
| 53 | + const children: VueI18nRoute[] = routes[0].children |
| 54 | + |
| 55 | + const localeCodes = ['en', 'ja'] |
| 56 | + const localizedRoutes = localizeRoutes(routes, { localeCodes }) |
| 57 | + |
| 58 | + expect(localizedRoutes.length).to.equal(2) |
| 59 | + localeCodes.forEach(locale => { |
| 60 | + routes.forEach(route => { |
| 61 | + expect(localizedRoutes).to.deep.include({ |
| 62 | + path: `/${locale}${route.path === '/' ? '' : route.path}`, |
| 63 | + name: `${route.name}${VUE_I18N_ROUTING_DEFAULTS.routesNameSeparator}${locale}`, |
| 64 | + children: children.map(child => ({ |
| 65 | + path: child.path, |
| 66 | + name: `${child.name}${VUE_I18N_ROUTING_DEFAULTS.routesNameSeparator}${locale}` |
| 67 | + })) |
| 68 | + }) |
| 69 | + }) |
| 70 | + }) |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + describe('trailing slash', function () { |
| 75 | + it('should be localized routing', function () { |
| 76 | + const routes: VueI18nRoute[] = [ |
| 77 | + { |
| 78 | + path: '/', |
| 79 | + name: 'home' |
| 80 | + }, |
| 81 | + { |
| 82 | + path: '/about', |
| 83 | + name: 'about' |
| 84 | + } |
| 85 | + ] |
| 86 | + const localeCodes = ['en', 'ja'] |
| 87 | + const localizedRoutes = localizeRoutes(routes, { localeCodes, trailingSlash: true }) |
| 88 | + |
| 89 | + expect(localizedRoutes.length).to.equal(4) |
| 90 | + localeCodes.forEach(locale => { |
| 91 | + routes.forEach(route => { |
| 92 | + expect(localizedRoutes).to.deep.include({ |
| 93 | + path: `/${locale}${route.path === '/' ? '' : route.path}/`, |
| 94 | + name: `${route.name}${VUE_I18N_ROUTING_DEFAULTS.routesNameSeparator}${locale}` |
| 95 | + }) |
| 96 | + }) |
| 97 | + }) |
| 98 | + }) |
| 99 | + }) |
| 100 | + |
| 101 | + describe('route name separator', function () { |
| 102 | + it('should be localized routing', function () { |
| 103 | + const routes: VueI18nRoute[] = [ |
| 104 | + { |
| 105 | + path: '/', |
| 106 | + name: 'home' |
| 107 | + }, |
| 108 | + { |
| 109 | + path: '/about', |
| 110 | + name: 'about' |
| 111 | + } |
| 112 | + ] |
| 113 | + const localeCodes = ['en', 'ja'] |
| 114 | + const localizedRoutes = localizeRoutes(routes, { localeCodes, routesNameSeparator: '__' }) |
| 115 | + |
| 116 | + expect(localizedRoutes.length).to.equal(4) |
| 117 | + localeCodes.forEach(locale => { |
| 118 | + routes.forEach(route => { |
| 119 | + expect(localizedRoutes).to.deep.include({ |
| 120 | + path: `/${locale}${route.path === '/' ? '' : route.path}`, |
| 121 | + name: `${route.name}${'__'}${locale}` |
| 122 | + }) |
| 123 | + }) |
| 124 | + }) |
| 125 | + }) |
| 126 | + }) |
| 127 | +}) |
0 commit comments