-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
213 lines (196 loc) · 5.17 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
var inWindow = require('in-window')
var test = require('tape')
var thataway = require('./')
var display
if (inWindow) {
display = document.getElementById('output')
}
function print (out) {
display && (display.innerHTML += `<h4><span class='test-pass'>✔︎</span> ${out}</h4>`)
}
module.exports = (function () {
test('thataway', function (t) {
t.ok(thataway, 'thataway exists')
t.end()
})
test('should subscribe', function (t) {
t.ok(thataway().subscribe(function () {}), 'thataway did add listener', print('thataway did add listener'))
t.end()
})
test('should unsubscribe', function (t) {
var router = thataway()
router.subscribe(hear)
function hear () {
t.fail('listener should be unsubscribed')
}
t.ok(
router.unsubscribe(hear),
'unsubscribed a listener',
print('unsubscribed a listener')
)
router.navigate('/')
t.end()
})
test('should call listener on url change', function (t) {
var router = thataway()
router.register('/a', {})
router.subscribe(
function (e) {
t.ok(true, 'called listener', print('called listener'))
t.end()
}
)
router.navigate('/a')
})
test('should pass path to update method', function (t) {
var router = thataway()
router.register('/b', {})
router.subscribe(
function (data) {
t.equal(data.path, '/b', 'passed path to update', print('passed path to update'))
t.end()
}
)
router.navigate('/b')
})
test('should have navigate method', function (t) {
t.ok(thataway().navigate, 'navigate method exists', print('navigate method exists'))
t.end()
})
test('should navigate to a path', function (t) {
var router = thataway()
router.register('/c', {})
router.navigate('/c')
t.equal(
window.location.pathname,
'/c',
print('navigated to correct location')
)
t.end()
})
test('should have register method', function (t) {
t.ok(thataway().register, 'has register method', print('has register'))
t.end()
})
test('should register paths', function (t) {
var router = thataway()
router.register('/a', {'title': 'sup'})
router.register('/b', {'title': 'nope'})
t.equal(
router.register('/c', {'title': 'yolo'}),
3,
'registered all paths',
print('registered all paths')
)
t.end()
})
test('should get paramaterized route data', function (t) {
var router = thataway()
router.register('/thing/:comment/:id', {stuff: 'YOLO'})
t.deepEqual(
router('/thing/123/456'),
{
stuff: 'YOLO',
path: '/thing/123/456',
params: {
path: '/thing/123/456',
comment: '123',
id: '456'
},
query: {},
hash: {}
},
'got correct route data',
print('got correct route data')
)
t.end()
})
test('should return first route match, then check for pattern match', function (t) {
var router = thataway()
router.register('/thing/:id', {stuff: 'NOLO'})
router.register('/thing/', {stuff: 'YOLO'})
t.deepEqual(
router('/thing/'),
{
stuff: 'YOLO',
path: '/thing',
query: {},
hash: {}
},
'route matched path before parameterized path',
print('route matched path before parameterized path')
)
t.end()
})
test('should match on root path "/"', function (t) {
t.plan(1)
var router = thataway()
router.register('/', {stuff: 'ROOT DOWN'})
t.deepEqual(
router('/'),
{path: '/', query: {}, stuff: 'ROOT DOWN', hash: {}},
'matched root path',
print('matched root path')
)
})
test('should work with history.back', function (t) {
t.plan(4)
var results = ['HOME', 'A', 'B', 'A']
var counter = 0
var router = thataway()
router.register('/', {title: 'HOME'})
router.register('/a', {title: 'A'})
router.register('/b', {title: 'B'})
router.subscribe(function (data) {
var expected = results[counter]
t.equal(
data.title,
expected,
`matched path ${data.title}`,
print(`matched path ${data.title}`)
)
counter++
})
router.navigate('/')
router.navigate('/a')
router.navigate('/b')
window.history.back()
})
test('should get query string and hash data', function (t) {
var router = thataway()
router.register('/thing/:id', {stuff: 'YOLO'})
router.subscribe(function (data) {
t.deepEqual(
data,
{
stuff: 'YOLO',
path: '/thing/123',
params: {
path: '/thing/123',
id: '123'
},
query: {
maybe: 'sure'
},
hash: {
create: 'thing'
}
},
'got correct query and hash data',
print('got correct query and hash data')
)
t.end()
})
// WARN: you must specify query string *BEFORE* hash
router.navigate('/thing/123?maybe=sure#create=thing')
})
test('should reset url to root', function (t) {
thataway().navigate('/')
t.equal(
window.location.pathname,
'/',
'reset url', print('reset url')
)
t.end()
})
}())