Skip to content

Commit 98f08cb

Browse files
authored
refactor: routing compatibles (#79)
1 parent bb35abb commit 98f08cb

File tree

5 files changed

+125
-389
lines changed

5 files changed

+125
-389
lines changed

packages/vue-i18n-routing/src/__test__/compatibles.test.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ describe('localePath', () => {
7676
routes: [
7777
{ path: '/', name: 'index', component: { template: '<div>index</div>' } },
7878
{ path: '/about', name: 'about', component: { template: '<div>About</div>' } },
79+
{
80+
path: '/path/:param',
81+
name: 'as-a-test',
82+
component: { template: '<div>Testing</div>' }
83+
},
7984
{ path: '/:pathMatch(.*)*', name: 'not-found', component: { template: '<div>Not Found</div>' } }
8085
],
8186
history: createMemoryHistory()
@@ -106,6 +111,12 @@ describe('localePath', () => {
106111
assert.equal(vm.localePath('/?foo=1'), '/ja?foo=1')
107112
assert.equal(vm.localePath('/about?foo=1'), '/ja/about?foo=1')
108113
assert.equal(vm.localePath('/about?foo=1&test=2'), '/ja/about?foo=1&test=2')
114+
assert.equal(vm.localePath('/path/as a test?foo=bar sentence'), '/ja/path/as a test?foo=bar+sentence')
115+
assert.equal(
116+
vm.localePath('/path/as%20a%20test?foo=bar%20sentence'),
117+
'/ja/path/as%20a%20test?foo=bar+sentence'
118+
)
119+
assert.equal(vm.localePath({ path: '/about', hash: '#foo=bar' }), '/ja/about#foo=bar')
109120

110121
// no define path
111122
assert.equal(vm.localePath('/vue-i18n'), '/ja/vue-i18n')
@@ -397,6 +408,11 @@ describe('switchLocalePath', () => {
397408
name: 'count',
398409
component: { template: '<div>Category</div>' }
399410
},
411+
{
412+
path: '/as a test',
413+
name: 'as-a-test',
414+
component: { template: '<div>Testing</div>' }
415+
},
400416
{
401417
path: '/:pathMatch(.*)*',
402418
name: 'not-found',
@@ -443,20 +459,28 @@ describe('switchLocalePath', () => {
443459
assert.equal(vm.switchLocalePath('fr'), '/fr/about?foo=b%C3%A4r&four=%E5%9B%9B')
444460
assert.equal(vm.switchLocalePath('en'), '/en/about?foo=b%C3%A4r&four=%E5%9B%9B')
445461

462+
await router.push('/ja/about#foo=bar')
463+
assert.equal(vm.switchLocalePath('ja'), '/ja/about#foo=bar')
464+
assert.equal(vm.switchLocalePath('fr'), '/fr/about#foo=bar')
465+
assert.equal(vm.switchLocalePath('en'), '/en/about#foo=bar')
466+
467+
await router.push('/ja/about?foo=é')
468+
assert.equal(vm.switchLocalePath('ja'), '/ja/about?foo=%C3%A9')
469+
446470
await router.push('/ja/category/1')
447471
assert.equal(vm.switchLocalePath('ja'), '/ja/category/japanese')
448472
assert.equal(vm.switchLocalePath('en'), '/en/category/english')
449473
assert.equal(vm.switchLocalePath('fr'), '/fr/category/franch')
450474

451475
await router.push('/ja/count/三')
452-
assert.equal(vm.switchLocalePath('ja'), '/ja/count/%E4%B8%89')
453-
assert.equal(vm.switchLocalePath('en'), '/en/count/%E4%B8%89')
454-
assert.equal(vm.switchLocalePath('fr'), '/fr/count/%E4%B8%89')
476+
assert.equal(vm.switchLocalePath('ja'), '/ja/count/')
477+
assert.equal(vm.switchLocalePath('en'), '/en/count/')
478+
assert.equal(vm.switchLocalePath('fr'), '/fr/count/')
455479

456480
await router.push('/ja/count/三?foo=bär&four=四&foo=bar')
457-
assert.equal(vm.switchLocalePath('ja'), '/ja/count/%E4%B8%89?foo=b%C3%A4r&foo=bar&four=%E5%9B%9B')
458-
assert.equal(vm.switchLocalePath('fr'), '/fr/count/%E4%B8%89?foo=b%C3%A4r&foo=bar&four=%E5%9B%9B')
459-
assert.equal(vm.switchLocalePath('en'), '/en/count/%E4%B8%89?foo=b%C3%A4r&foo=bar&four=%E5%9B%9B')
481+
assert.equal(vm.switchLocalePath('ja'), '/ja/count/?foo=b%C3%A4r&foo=bar&four=%E5%9B%9B')
482+
assert.equal(vm.switchLocalePath('fr'), '/fr/count/?foo=b%C3%A4r&foo=bar&four=%E5%9B%9B')
483+
assert.equal(vm.switchLocalePath('en'), '/en/count/?foo=b%C3%A4r&foo=bar&four=%E5%9B%9B')
460484

461485
await router.push('/ja/foo')
462486
assert.equal(vm.switchLocalePath('ja'), '/ja/not-found-japanese')
Lines changed: 0 additions & 286 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, it, assert, test } from 'vitest'
22

3-
import { resolvedRouteToObject } from '../compatibles/utils'
43
import { adjustRoutePathForTrailingSlash, getLocaleRouteName, findBrowserLocale } from '../utils'
54

65
import type { BrowserLocale } from '../utils'
@@ -221,288 +220,3 @@ describe('findBrowserLocale', () => {
221220
assert.ok(locale === 'ja')
222221
})
223222
})
224-
225-
describe('resolvedRouteToObject', () => {
226-
it('should map resolved route without special characters', () => {
227-
const expected = {
228-
fullPath: '/ja/about',
229-
hash: '',
230-
query: {},
231-
name: 'about___ja',
232-
path: '/ja/about',
233-
params: {},
234-
matched: [
235-
{
236-
path: '/ja/about',
237-
redirect: undefined,
238-
name: 'about___ja',
239-
meta: {},
240-
aliasOf: undefined,
241-
beforeEnter: undefined,
242-
props: {
243-
default: false
244-
},
245-
children: [],
246-
instances: {},
247-
leaveGuards: {},
248-
updateGuards: {},
249-
enterCallbacks: {},
250-
components: {
251-
default: {
252-
template: '<div>About</div>'
253-
}
254-
}
255-
}
256-
],
257-
meta: {},
258-
redirectedFrom: undefined,
259-
href: '/ja/about'
260-
}
261-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
262-
assert.deepEqual(resolvedRouteToObject(expected as any), expected as any)
263-
})
264-
265-
it('should map resolved route without special characters and query', () => {
266-
const expected = {
267-
fullPath: '/ja/about?foo=1&test=2',
268-
hash: '',
269-
query: {
270-
foo: '1',
271-
test: '2'
272-
},
273-
name: 'about___ja',
274-
path: '/ja/about',
275-
params: {},
276-
matched: [
277-
{
278-
path: '/ja/about',
279-
redirect: undefined,
280-
name: 'about___ja',
281-
meta: {},
282-
aliasOf: undefined,
283-
beforeEnter: undefined,
284-
props: {
285-
default: false
286-
},
287-
children: [],
288-
instances: {},
289-
leaveGuards: {},
290-
updateGuards: {},
291-
enterCallbacks: {},
292-
components: {
293-
default: {
294-
template: '<div>About</div>'
295-
}
296-
}
297-
}
298-
],
299-
meta: {},
300-
redirectedFrom: undefined,
301-
href: '/ja/about?foo=1&test=2'
302-
}
303-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
304-
assert.deepEqual(resolvedRouteToObject(expected as any), expected as any)
305-
})
306-
307-
it('should map resolved route without special characters and query with special characters', () => {
308-
const expected = {
309-
fullPath: '/ja/about?foo=b%C3%A4r&foo=bar&four=%E5%9B%9B',
310-
hash: '',
311-
query: {
312-
foo: ['bär', 'bar'],
313-
four: '四'
314-
},
315-
name: 'about___ja',
316-
path: '/ja/about',
317-
params: {},
318-
matched: [
319-
{
320-
path: '/ja/about',
321-
redirect: undefined,
322-
name: 'about___ja',
323-
meta: {},
324-
aliasOf: undefined,
325-
beforeEnter: undefined,
326-
props: {
327-
default: false
328-
},
329-
children: [],
330-
instances: {},
331-
leaveGuards: {},
332-
updateGuards: {},
333-
enterCallbacks: {},
334-
components: {
335-
default: {
336-
template: '<div>About</div>'
337-
}
338-
}
339-
}
340-
],
341-
meta: {},
342-
redirectedFrom: undefined,
343-
href: '/ja/about?foo=b%C3%A4r&foo=bar&four=%E5%9B%9B'
344-
}
345-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
346-
assert.deepEqual(resolvedRouteToObject(expected as any), expected as any)
347-
})
348-
349-
it('should map resolved route with special characters and query with special characters', () => {
350-
const provided = {
351-
fullPath: '/ja/count/三?foo=b%C3%A4r&foo=bar&four=%E5%9B%9B',
352-
hash: '',
353-
query: {
354-
foo: ['bär', 'bar'],
355-
four: '四'
356-
},
357-
name: 'count___ja',
358-
path: '/ja/count/三',
359-
params: {
360-
id: '三'
361-
},
362-
matched: [
363-
{
364-
path: '/ja/count/:id',
365-
redirect: undefined,
366-
name: 'count___ja',
367-
meta: {},
368-
aliasOf: undefined,
369-
beforeEnter: undefined,
370-
props: {
371-
default: false
372-
},
373-
children: [],
374-
instances: {},
375-
leaveGuards: {},
376-
updateGuards: {},
377-
enterCallbacks: {},
378-
components: {
379-
default: {
380-
template: '<div>Category</div>'
381-
}
382-
}
383-
}
384-
],
385-
meta: {},
386-
redirectedFrom: undefined,
387-
href: '/ja/count/%E5%9B%9B?foo=b%C3%A4r&foo=bar&four=%E5%9B%9B'
388-
}
389-
const expected = {
390-
fullPath: '/ja/count/%E4%B8%89?foo=b%C3%A4r&foo=bar&four=%E5%9B%9B',
391-
hash: '',
392-
query: {
393-
foo: ['bär', 'bar'],
394-
four: '四'
395-
},
396-
name: 'count___ja',
397-
path: '/ja/count/%E4%B8%89',
398-
params: {
399-
id: '三'
400-
},
401-
matched: [
402-
{
403-
path: '/ja/count/:id',
404-
redirect: undefined,
405-
name: 'count___ja',
406-
meta: {},
407-
aliasOf: undefined,
408-
beforeEnter: undefined,
409-
props: {
410-
default: false
411-
},
412-
children: [],
413-
instances: {},
414-
leaveGuards: {},
415-
updateGuards: {},
416-
enterCallbacks: {},
417-
components: {
418-
default: {
419-
template: '<div>Category</div>'
420-
}
421-
}
422-
}
423-
],
424-
meta: {},
425-
redirectedFrom: undefined,
426-
href: '/ja/count/%E4%B8%89?foo=b%C3%A4r&foo=bar&four=%E5%9B%9B'
427-
}
428-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
429-
assert.deepEqual(resolvedRouteToObject(provided as any), expected as any)
430-
})
431-
432-
it('should map resolved route with special characters', () => {
433-
const provided = {
434-
fullPath: '/ja/count/三',
435-
hash: '',
436-
query: {},
437-
name: 'count___ja',
438-
path: '/ja/count/三',
439-
params: {
440-
id: '三'
441-
},
442-
matched: [
443-
{
444-
path: '/ja/count/:id',
445-
redirect: undefined,
446-
name: 'count___ja',
447-
meta: {},
448-
aliasOf: undefined,
449-
beforeEnter: undefined,
450-
props: {
451-
default: false
452-
},
453-
children: [],
454-
instances: {},
455-
leaveGuards: {},
456-
updateGuards: {},
457-
enterCallbacks: {},
458-
components: {
459-
default: {
460-
template: '<div>Category</div>'
461-
}
462-
}
463-
}
464-
],
465-
meta: {},
466-
redirectedFrom: undefined,
467-
href: '/ja/count/三'
468-
}
469-
const expected = {
470-
fullPath: '/ja/count/%E4%B8%89',
471-
hash: '',
472-
query: {},
473-
name: 'count___ja',
474-
path: '/ja/count/%E4%B8%89',
475-
params: {
476-
id: '三'
477-
},
478-
matched: [
479-
{
480-
path: '/ja/count/:id',
481-
redirect: undefined,
482-
name: 'count___ja',
483-
meta: {},
484-
aliasOf: undefined,
485-
beforeEnter: undefined,
486-
props: {
487-
default: false
488-
},
489-
children: [],
490-
instances: {},
491-
leaveGuards: {},
492-
updateGuards: {},
493-
enterCallbacks: {},
494-
components: {
495-
default: {
496-
template: '<div>Category</div>'
497-
}
498-
}
499-
}
500-
],
501-
meta: {},
502-
redirectedFrom: undefined,
503-
href: '/ja/count/%E4%B8%89'
504-
}
505-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
506-
assert.deepEqual(resolvedRouteToObject(provided as any), expected as any)
507-
})
508-
})

0 commit comments

Comments
 (0)