-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
matchPath.js
87 lines (71 loc) · 3.02 KB
/
matchPath.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Universal Router (https://www.kriasoft.com/universal-router/)
*
* Copyright © 2015-2016 Konstantin Tarkus, Kriasoft LLC. All rights reserved.
*
* This source code is licensed under the Apache 2.0 license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
import { expect } from 'chai';
import { matchPath, matchBasePath } from '../src/matchPath';
describe('matchPath(routePath, urlPath)', () => {
it('should return null if path not fond (1)', () => {
const result = matchPath('/', '/a');
expect(result).to.be.null;
});
it('should return null if path not fond (2)', () => {
const result = matchPath('/a', '/');
expect(result).to.be.null;
});
it('should return keys and params (1)', () => {
const result = matchPath('/a', '/a');
expect(result).to.be.deep.equal({ path: '/a', keys: [], params: {} });
});
it('should return keys and params (2)', () => {
const result = matchPath('/:a/:b', '/1/2');
expect(result).to.be.ok;
expect(result).to.have.property('path', '/1/2');
expect(result).to.have.property('keys').and.be.an('array').lengthOf(2);
expect(result).to.have.property('params').and.be.deep.equal({ a: '1', b: '2' });
});
it('should return keys and params (3)', () => {
const result = matchPath('/', '');
expect(result).to.be.ok;
expect(result).to.have.property('path', '/');
expect(result).to.have.property('keys').and.be.an('array').lengthOf(0);
expect(result).to.have.property('params').and.be.an('object');
expect(Object.keys(result.params)).to.have.lengthOf(0);
});
it('should return keys and params (4)', () => {
const result = matchPath('/:a/:b?', '/1');
expect(result).to.be.ok;
expect(result).to.have.property('path', '/1');
expect(result).to.have.property('keys').and.be.an('array').lengthOf(2);
expect(result).to.have.property('params').and.be.deep.equal({ a: '1', b: undefined });
});
it('should return keys and params (5)', () => {
const result = matchPath('/:a/:b?', '/1/2');
expect(result).to.be.ok;
expect(result).to.have.property('path', '/1/2');
expect(result).to.have.property('keys').and.be.an('array').lengthOf(2);
expect(result).to.have.property('params').and.be.deep.equal({ a: '1', b: '2' });
});
it('should match to an array of paths', () => {
const result = matchPath(['/e', '/f'], '/f');
expect(result).to.be.deep.equal({ path: '/f', keys: [], params: {} });
});
});
describe('matchBasePath(routePath, urlPath)', () => {
it('should return keys and params (4)', () => {
const result = matchBasePath('/c', '/c/d');
expect(result).to.be.deep.equal({ path: '/c', keys: [], params: {} });
});
it('should return keys and params (5)', () => {
const result = matchBasePath('/', '/a/b');
expect(result).to.be.deep.equal({ path: '/', keys: [], params: {} });
});
it('should return keys and params (6)', () => {
const result = matchBasePath('/', '');
expect(result).to.be.deep.equal({ path: '/', keys: [], params: {} });
});
});