-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjouter.test.js
154 lines (138 loc) · 4.74 KB
/
jouter.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
import { routeRe, route, createRouter } from './jouter'
describe('createRouter', () => {
let currentPath = '/foo'
let listeners = []
beforeEach(() => {
currentPath = '/foo'
listeners = []
})
const fakePathHandler = {
get: jest.fn(() => currentPath),
set: jest.fn((path, title) => {
currentPath = path
listeners.forEach(f => f())
}),
swap: jest.fn((path, title) => {
currentPath = path
listeners.forEach(f => f())
}),
listen: jest.fn(f => listeners.push(f))
}
test('will return a router object', () => {
const router = createRouter()
expect(router.add).toBeInstanceOf(Function)
expect(router.start).toBeInstanceOf(Function)
expect(router.handleEvent).toBeInstanceOf(Function)
expect(router.go).toBeInstanceOf(Function)
})
test('switching paths', () => {
const router = createRouter(fakePathHandler)
router.go('/test')
expect(currentPath).toBe('/test')
expect(fakePathHandler.set).toHaveBeenCalledWith('/test', undefined)
})
test('switching paths using replace', () => {
const router = createRouter(fakePathHandler)
router.replace('/test')
expect(currentPath).toBe('/test')
expect(fakePathHandler.swap).toHaveBeenCalledWith('/test', undefined)
})
test('adding and handling routes', () => {
const router = createRouter(fakePathHandler)
const fn = jest.fn()
router.add(fn, '/:x')
router.go('/bar')
expect(fn).toHaveBeenCalledWith('bar')
})
test('start routing', () => {
const router = createRouter(fakePathHandler)
const fn = jest.fn()
router.add(fn, '/:x')
router.start()
expect(listeners.length).toBe(1)
listeners[0]()
expect(fn).toHaveBeenCalledWith('foo')
})
test('handing events', () => {
const router = createRouter(fakePathHandler)
const fn = jest.fn()
const elem = document.createElement('a')
elem.href = '/baz'
router.add(fn, '/:x')
elem.addEventListener('click', router.handleEvent)
elem.dispatchEvent(new Event('click'));
expect(fn).toHaveBeenCalledWith('baz')
expect(currentPath).toBe('/baz')
})
test('handling events on nested objects', () => {
const router = createRouter(fakePathHandler)
const fn = jest.fn()
const elem = document.createElement('a')
elem.href = '/baz'
elem.innerHTML = '<span>click here</span>';
const nested = elem.querySelector('span');
router.add(fn, '/:x')
elem.addEventListener('click', router.handleEvent)
nested.dispatchEvent(new Event('click', { bubbles: true, cancelable: true }));
expect(fn).toHaveBeenCalledWith('baz')
expect(currentPath).toBe('/baz')
});
test('router object is a function', () => {
const router = createRouter(fakePathHandler)
expect(router).toBeInstanceOf(Function)
})
test('router objects can be used as route handlers', () => {
const router = createRouter(fakePathHandler)
const subrouter = createRouter(fakePathHandler)
const fn = jest.fn()
router.add(subrouter, '/sub/...')
subrouter.add(fn, '/foo/:x')
router.go('/sub/foo/2')
expect(fn).toHaveBeenCalledWith('2')
})
test('router object can be created with a decorator function', () => {
const injectedDependency = { foo: 'bar' }
const fn = jest.fn()
const router = createRouter({
...fakePathHandler,
decorate: fn => (...args) => fn(injectedDependency, ...args)
})
router.add(fn, '/:x')
router.go('/12')
expect(fn).toHaveBeenCalledWith(injectedDependency, '12')
})
test('unmatched route will invoke the error handler', () => {
const errorHandler = jest.fn()
const router = createRouter({
...fakePathHandler,
onNoMatch: errorHandler,
})
router.add(jest.fn(), '/:x')
router.go('/')
expect(errorHandler).toHaveBeenCalledWith("/")
});
test('error handler is not invoked if subroute matches', () => {
const errorHandler = jest.fn();
const router = createRouter({
...fakePathHandler,
onNoMatch: errorHandler
})
const subrouter = createRouter(fakePathHandler)
router.add(subrouter, '/sub/...')
subrouter.add(jest.fn(), '/foo/:x')
router.go('/sub/foo/2')
expect(errorHandler).not.toHaveBeenCalled()
});
test('if subroute is unmatched, error handler is invoked', () => {
const errorHandler = jest.fn();
const router = createRouter({
...fakePathHandler,
onNoMatch: errorHandler
})
const subrouter = createRouter(fakePathHandler)
router.add(subrouter, '/sub/...')
subrouter.add(jest.fn(), '/foo/:x')
router.go('/bogus')
expect(errorHandler).toHaveBeenCalledWith('/bogus')
})
})