-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathutil.test.js
More file actions
110 lines (95 loc) · 3.7 KB
/
Copy pathutil.test.js
File metadata and controls
110 lines (95 loc) · 3.7 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { exec, pathRankSort, prepareVNodeForRanking, segmentize, rank } from '../src/util';
const strip = str => segmentize(str).join('/');
describe('util', () => {
describe('strip', () => {
it('should strip preceeding slashes', () => {
expect(strip('')).toBe('');
expect(strip('/')).toBe('');
expect(strip('/a')).toBe('a');
expect(strip('//a')).toBe('a');
expect(strip('//a/')).toBe('a');
});
it('should strip trailing slashes', () => {
expect(strip('')).toBe('');
expect(strip('/')).toBe('');
expect(strip('a/')).toBe('a');
expect(strip('/a//')).toBe('a');
});
});
describe('rank', () => {
it('should return rank of path segments', () => {
expect(rank('')).toBe('5');
expect(rank('/')).toBe('5');
expect(rank('//')).toBe('5');
expect(rank('a/b/c')).toBe('555');
expect(rank('/a/b/c/')).toBe('555');
expect(rank('/:a/b?/:c?/:d*/:e+')).toEqual('45312');
});
});
describe('segmentize', () => {
it('should split path on slashes', () => {
expect(segmentize('')).toEqual(['']);
expect(segmentize('/')).toEqual(['']);
expect(segmentize('//')).toEqual(['']);
expect(segmentize('a/b/c')).toEqual(['a','b','c']);
expect(segmentize('/a/b/c/')).toEqual(['a','b','c']);
});
});
describe('pathRankSort', () => {
it('should sort by highest rank first', () => {
let paths = arr => arr.map( path => ({ props:{path}} ) );
let clean = vnode => { delete vnode.rank; delete vnode.index; return vnode; };
expect(
paths(['/:a*', '/a', '/:a+', '/:a?', '/a/:b*']).filter(prepareVNodeForRanking).sort(pathRankSort).map(clean)
).toEqual(
paths(['/a/:b*', '/a', '/:a?', '/:a+', '/:a*'])
);
});
it('should return default routes last', () => {
let paths = arr => arr.map( path => ({props:{path}}) );
let clean = vnode => { delete vnode.rank; delete vnode.index; return vnode; };
let defaultPath = {props:{default:true}};
let p = paths(['/a/b/', '/a/b', '/', 'b']);
p.splice(2,0,defaultPath);
expect(
p.filter(prepareVNodeForRanking).sort(pathRankSort).map(clean)
).toEqual(
paths(['/a/b/', '/a/b', '/', 'b']).concat(defaultPath)
);
});
});
describe('exec', () => {
it('should match explicit equality', () => {
expect(exec('/','/', {})).toEqual({});
expect(exec('/a', '/a', {})).toEqual({});
expect(exec('/a', '/b', {})).toEqual(false);
expect(exec('/a/b', '/a/b', {})).toEqual({});
expect(exec('/a/b', '/a/a', {})).toEqual(false);
expect(exec('/a/b', '/b/b', {})).toEqual(false);
});
it('should match param segments', () => {
expect(exec('/', '/:foo', {})).toEqual(false);
expect(exec('/bar', '/:foo', {})).toEqual({ foo:'bar' });
});
it('should match optional param segments', () => {
expect(exec('/', '/:foo?', {})).toEqual({ foo:undefined });
expect(exec('/bar', '/:foo?', {})).toEqual({ foo:'bar' });
expect(exec('/', '/:foo?/:bar?', {})).toEqual({ foo:undefined, bar:undefined });
expect(exec('/bar', '/:foo?/:bar?', {})).toEqual({ foo:'bar', bar:undefined });
expect(exec('/bar', '/:foo?/bar', {})).toEqual(false);
expect(exec('/foo/bar', '/:foo?/bar', {})).toEqual({ foo:'foo' });
});
it('should match splat param segments', () => {
expect(exec('/', '/:foo*', {})).toEqual({ foo:'' });
expect(exec('/a', '/:foo*', {})).toEqual({ foo:'a' });
expect(exec('/a/b', '/:foo*', {})).toEqual({ foo:'a/b' });
expect(exec('/a/b/c', '/:foo*', {})).toEqual({ foo:'a/b/c' });
});
it('should match required splat param segments', () => {
expect(exec('/', '/:foo+', {})).toEqual(false);
expect(exec('/a', '/:foo+', {})).toEqual({ foo:'a' });
expect(exec('/a/b', '/:foo+', {})).toEqual({ foo:'a/b' });
expect(exec('/a/b/c', '/:foo+', {})).toEqual({ foo:'a/b/c' });
});
});
});