|
1 | 1 | import { exec } from '../router.js';
|
2 | 2 |
|
3 |
| -function doExec(path, route, opts) { |
4 |
| - return exec(path, route, { path, query: {}, params: {}, ...(opts || {}) }); |
| 3 | +function execPath(path, pattern, opts) { |
| 4 | + return exec(path, pattern, { path, query: {}, params: {}, ...(opts || {}) }); |
5 | 5 | }
|
6 | 6 |
|
7 | 7 | describe('match', () => {
|
8 | 8 | it('Base route', () => {
|
9 |
| - const accurateResult = doExec('/', '/'); |
| 9 | + const accurateResult = execPath('/', '/'); |
10 | 10 | expect(accurateResult).toEqual({ path: '/', params: {}, query: {} });
|
11 | 11 |
|
12 |
| - const inaccurateResult = doExec('/user/1', '/'); |
| 12 | + const inaccurateResult = execPath('/user/1', '/'); |
13 | 13 | expect(inaccurateResult).toEqual(undefined);
|
14 | 14 | });
|
15 | 15 |
|
16 | 16 | it('Param route', () => {
|
17 |
| - const accurateResult = doExec('/user/2', '/user/:id'); |
18 |
| - expect(accurateResult).toEqual({ path: '/user/2', params: { id: '2' }, query: {}, id: '2' }); |
| 17 | + const accurateResult = execPath('/user/2', '/user/:id'); |
| 18 | + expect(accurateResult).toEqual({ path: '/user/2', params: { id: '2' }, id: '2', query: {} }); |
19 | 19 |
|
20 |
| - const inaccurateResult = doExec('/', '/user/:id'); |
| 20 | + const inaccurateResult = execPath('/', '/user/:id'); |
21 | 21 | expect(inaccurateResult).toEqual(undefined);
|
22 | 22 | });
|
23 | 23 |
|
24 | 24 | it('Optional param route', () => {
|
25 |
| - const accurateResult = doExec('/user', '/user/:id?'); |
26 |
| - expect(accurateResult).toEqual({ path: '/user', params: { id: undefined }, query: {} }); |
| 25 | + const accurateResult = execPath('/user', '/user/:id?'); |
| 26 | + expect(accurateResult).toEqual({ path: '/user', params: { id: undefined }, id: undefined, query: {} }); |
27 | 27 |
|
28 |
| - const inaccurateResult = doExec('/', '/user/:id?'); |
| 28 | + const inaccurateResult = execPath('/', '/user/:id?'); |
29 | 29 | expect(inaccurateResult).toEqual(undefined);
|
30 | 30 | });
|
31 | 31 |
|
| 32 | + it('Optional rest param route "/:x*"', () => { |
| 33 | + const accurateResult = execPath('/user', '/user/:id?'); |
| 34 | + expect(accurateResult).toEqual({ path: '/user', params: { id: undefined }, id: undefined, query: {} }); |
| 35 | + |
| 36 | + const inaccurateResult = execPath('/', '/user/:id?'); |
| 37 | + expect(inaccurateResult).toEqual(undefined); |
| 38 | + }); |
| 39 | + |
| 40 | + it('Rest param route "/:x+"', () => { |
| 41 | + const matchedResult = execPath('/user/foo', '/user/:id+'); |
| 42 | + expect(matchedResult).toEqual({ path: '/user/foo', params: { id: 'foo' } }); |
| 43 | + |
| 44 | + const matchedResultWithSlash = execPath('/user/foo/bar', '/user/:id+'); |
| 45 | + expect(matchedResultWithSlash).toEqual({ path: '/user/foo/bar', params: { id: 'foo/bar' }, id: 'foo/bar' }); |
| 46 | + |
| 47 | + const emptyResult = execPath('/user', '/user/:id+'); |
| 48 | + expect(emptyResult).toEqual(undefined); |
| 49 | + |
| 50 | + const mismatchedResult = execPath('/', '/user/:id+'); |
| 51 | + expect(mismatchedResult).toEqual(undefined); |
| 52 | + }); |
| 53 | + |
32 | 54 | it('Handles leading/trailing slashes', () => {
|
33 |
| - const result = doExec('/about-late/_SEGMENT1_/_SEGMENT2_/', '/about-late/:seg1/:seg2/'); |
| 55 | + const result = execPath('/about-late/_SEGMENT1_/_SEGMENT2_/', '/about-late/:seg1/:seg2/'); |
34 | 56 | expect(result).toEqual({
|
| 57 | + path: '/about-late/_SEGMENT1_/_SEGMENT2_/', |
35 | 58 | params: {
|
36 | 59 | seg1: '_SEGMENT1_',
|
37 | 60 | seg2: '_SEGMENT2_'
|
38 | 61 | },
|
39 |
| - path: '/about-late/_SEGMENT1_/_SEGMENT2_/', |
40 |
| - query: {}, |
41 | 62 | seg1: '_SEGMENT1_',
|
42 |
| - seg2: '_SEGMENT2_' |
| 63 | + seg2: '_SEGMENT2_', |
| 64 | + query: {}, |
43 | 65 | });
|
44 | 66 | });
|
45 | 67 |
|
46 | 68 | it('should not overwrite existing properties', () => {
|
47 |
| - const result = doExec('/foo/bar', '/:path/:query'); |
| 69 | + const result = execPath('/foo/bar', '/:path/:query', { path: '/custom-path' }); |
48 | 70 | expect(result).toEqual({
|
49 | 71 | params: { path: 'foo', query: 'bar' },
|
50 |
| - path: '/foo/bar', |
| 72 | + path: '/custom-path', |
51 | 73 | query: {}
|
52 | 74 | });
|
53 | 75 | });
|
|
0 commit comments