-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
165 lines (124 loc) · 4.24 KB
/
test.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import route, {
configure,
defaults,
match,
router,
toPath,
toRegexp,
toURL
} from './index.next.js'
import erre from 'erre'
import {expect} from 'chai'
describe('rawth', function() {
it('the default options will be exported', () => {
expect(defaults).to.be.ok
})
it('the default options can be configured', () => {
expect(defaults.base).to.be.equal('https://localhost')
configure({
base: 'http://localhost'
})
expect(defaults.base).to.be.equal('http://localhost')
configure({
base: 'https://localhost'
})
expect(defaults.base).to.be.equal('https://localhost')
})
it('the toPath method returns valid string paths', () => {
expect(toPath('/:foo/:bar', {foo: 'foo', bar: 'bar'}))
.to.be.equal('/foo/bar')
})
it('the toURL method will return a proper URL object', () => {
const keys = []
const path = toRegexp(':foo/:bar', keys)
const url = toURL('foo/bar', path, {keys})
expect(url.pathname).to.be.equal('/foo/bar')
expect(url.hostname).to.be.equal('localhost')
expect(url.protocol).to.be.equal('https:')
expect(url.params.foo).to.be.equal('foo')
expect(url.params.bar).to.be.equal('bar')
})
it('the toURL method will provide undefined for empty optional parameters', () => {
const keys = []
const path = toRegexp(':foo?/:bar?', keys)
const url = toURL('foo', path, {keys})
expect(url.params.foo).to.be.equal('foo')
expect(url.params.bar).to.be.equal(undefined)
})
it('the match method will return true only for the routes matching the test regex', () => {
const path = toRegexp('/:foo/:bar')
expect(match('/foo/bar', path)).to.be.ok
expect(match('/foo/bar/', path)).to.be.ok
expect(match('/foo', path)).to.be.not.ok
})
it('the streaming router gets exported', () => {
expect(router).to.be.ok
})
it('the hash routes can be matched as well', (done) => {
const fooBarStream = route('#:foo/:bar')
fooBarStream.on.value(({params, hash}) => {
expect(params.foo).to.be.equal('foo')
expect(params.bar).to.be.equal('bar')
expect(hash).to.be.equal('#foo/bar')
fooBarStream.end()
done()
return erre.off()
})
router.push('#foo/bar')
})
it('a subroute stream gets properly created', done => {
const fooBarStream = route(':foo/:bar')
fooBarStream.on.value(({params}) => {
expect(params.foo).to.be.equal('foo')
expect(params.bar).to.be.equal('bar')
fooBarStream.end()
done()
return erre.off()
})
router.push('foo/bar')
})
it('subroute streams don\'t get called if the route doesn\'t match', () => {
const fooBarStream = route(':foo/:bar')
const fail = () => expect.fail('you should never get here')
fooBarStream.on.value(fail)
router.push('foo')
fooBarStream.off.value(fail)
})
it('encoded URIs will be decoded with decodeURI', done => {
const fooBarStream = route(':foo/:bar\\?(.*)')
fooBarStream.on.value((path) => {
expect(path.params.foo).to.be.equal('foo')
expect(path.params.bar).to.be.equal('ba r')
expect(path.search).to.be.equal('?x=test&y=%C3%A9')
fooBarStream.end()
done()
return erre.off()
})
router.push('foo/ba%20r?x=test&y=%C3%A9')
})
it('encoded path elements (but NOT query) will be decoded with decodeURIComponent', done => {
const fooBarStream = route(':foo/:bar\\?(.*)')
fooBarStream.on.value((path) => {
expect(path.params.foo).to.be.equal('foo;v=1')
expect(path.params.bar).to.be.equal('ba&r')
// NOTE: decodeURIComponent() would also decode component separators such as /, ?, &
// reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI
expect(path.search).to.be.equal('?y=%26&z=end') // %26 is &
fooBarStream.end()
done()
return erre.off()
})
router.push('foo;v=1/ba%26r?y=%26&z=end')
})
it('bypass router arguments different from strings', (done) => ((count = 0) => {
const increment = () => ++count
router.on.value(increment)
router.push('foo/baz')
router.push(Symbol())
setTimeout(() => {
expect(count).to.be.equal(1)
router.off.value(increment)
done()
}, 100)
})())
})