-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.test.js
249 lines (210 loc) · 6.46 KB
/
index.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
const { is, header } = require('utilise/pure')
, { test } = require('tap')
, core = require('./')
test('should create empty resource from name', ({ plan, same }) => {
plan(1)
const ripple = core()
same(ripple('foo', 'bar'), 'bar')
})
test('should fail to create resource it does not understand', ({ plan, same }) => {
plan(1)
const ripple = core()
same(ripple('foo'), undefined)
})
test('should set content-type by default', ({ plan, same }) => {
plan(1)
const ripple = core()
ripple('foo', 'bar')
same(header('content-type')(ripple.resources['foo']), 'text/plain')
})
test('should create resource using alias', ({ plan, same }) => {
plan(1)
const ripple = core()
same(ripple.register('foo', 'bar'), 'bar')
})
test('should create and get resource from name', ({ plan, same }) => {
plan(1)
const ripple = core()
ripple('foo', 'bar')
same(ripple('foo'), 'bar')
})
test('should create and get resource from obj', ({ plan, same }) => {
plan(2)
const ripple = core()
same(ripple({ name: 'foo', body: 'bar', headers: {} }), 'bar')
same(ripple.resources.foo, {
name: 'foo'
, body: 'bar'
, headers: { 'content-type': 'text/plain' }
})
})
test('should import multiple resources from object', ({ plan, same }) => {
plan(2)
const ripple = core()
same(ripple({
foo: { name: 'foo', body: 'foo', headers: {} }
, bar: { name: 'bar', body: 'bar', headers: {} }
}), [ 'foo', 'bar' ])
same(ripple.resources, {
foo: { name: 'foo', body: 'foo', headers: { 'content-type': 'text/plain' } }
, bar: { name: 'bar', body: 'bar', headers: { 'content-type': 'text/plain' } }
})
})
test('should create import resources from another node', ({ plan, same }) => {
plan(1)
const ripple = core()
ripple({ name: 'foo', body: 'bar', headers: {} })
const ripple2 = core()
ripple2(ripple)
same(ripple2.resources.foo, {
name: 'foo'
, body: 'bar'
, headers: { 'content-type': 'text/plain' }
})
})
test('should use explicitly set headers', ({ plan, same }) => {
plan(2)
const ripple = core()
same(ripple({
name: 'sth'
, body: { a : 1 }
, headers: { 'content-type': 'text/plain' }
}), { a : 1 })
same(ripple.resources.sth, {
name: 'sth'
, body: { a : 1 }
, headers: { 'content-type': 'text/plain' }
})
})
test('should create two different ripple nodes', ({ plan, ok }) => {
plan(1)
const ripple1 = core()
, ripple2 = core()
ok(ripple1 !== ripple2)
})
test('should register multiple resources', ({ plan, ok }) => {
plan(2)
const ripple = core()
ripple([{name:'foo', body:'1'}, {name:'bar', body:'1'}])
ok('foo' in ripple.resources)
ok('bar' in ripple.resources)
})
test('should destroy existing headers by default', ({ plan, same }) => {
plan(2)
const ripple = core()
ripple({ name: 'name', body: 'foo', headers: { 'foo': 'bar' }})
same(ripple.resources.name.headers.foo, 'bar')
ripple({ name:'name', body: 'baz' })
same(ripple.resources.name.headers.foo, undefined)
})
test('should fail if cannot interpret resource', ({ plan, notOk }) => {
plan(4)
const ripple = core()
notOk(ripple('foo', []))
notOk(ripple('bar'))
notOk('foo' in ripple.resources)
notOk('bar' in ripple.resources)
})
test('should fail if uses api incorrectly', ({ plan, same }) => {
plan(3)
const ripple = core()
same(ripple(), ripple)
same(ripple(String), false)
same(ripple.resources, {})
})
test('should skip empty objects', ({ plan, same }) => {
plan(2)
const ripple = core()
same(ripple({}), [])
same(ripple.resources, {})
})
test('should not register type it does not understand', ({ plan, same }) => {
plan(2)
const ripple = core()
same(ripple({ name: 'foo', body: 'foo', headers: { 'content-type': 'application/jsx' }}), false)
same(ripple.resources, {})
})
test('should emit global change events', ({ plan, same }) => {
plan(2)
let ripple = core()
, params
, fn = (name, change) => { params = [name, change] }
ripple.on('change', fn)
ripple('foo', 'bar')
same(params, ['foo', { type: 'update', value: 'bar', time: undefined }])
ripple.types['data'] = { header: 'data', check: String }
ripple('boo', { log: ['baz'] })
same(params, ['boo', { type: 'update', value: { log: ['baz'] }, time: 0 }])
})
test('should indicate if new resource', ({ plan, same }) => {
plan(4)
const ripple = core()
ripple.once('change', (d, change) => {
same(d, 'foo')
same(change, { type: 'update', value: 'foo', time: undefined })
ripple.once('change', (d, change) => {
same(d, 'foo')
same(change, { type: 'update', value: 'bar', time: undefined })
})
ripple('foo', 'bar')
})
ripple('foo', 'foo')
})
test('should register new types by order', ({ plan, same }) => {
plan(3)
const ripple = core()
ripple('text', 'text')
same(ripple.resources.text.headers['content-type'], 'text/plain')
ripple.types['priority/low'] = {
header: 'priority/low'
, check: String
, priority: -10
}
ripple('low', 'low')
same(ripple.resources.low.headers['content-type'], 'text/plain')
ripple.types['priority/high'] = {
header: 'priority/high'
, check: String
, priority: 10
}
ripple('high', 'high')
same(ripple.resources.high.headers['content-type'], 'priority/high')
})
test('should register promise - resource', ({ plan, same}) => {
plan(3)
const ripple = core()
ripple(Promise.resolve({ name:'foo', body: 'bar' }))
.then(d => same('foo' in ripple.resources, true))
.then(d => same(ripple.resources.foo.body, 'bar'))
same('foo' in ripple.resources, false)
})
test('should register promise - body 1', ({ plan, same }) => {
plan(3)
const ripple = core()
ripple('foo', Promise.resolve('bar'))
.then(d => same('foo' in ripple.resources, true))
.then(d => same(ripple.resources.foo.body, 'bar'))
same('foo' in ripple.resources, false)
})
test('should register promise - body 2', ({ plan, same }) => {
plan(3)
const ripple = core()
ripple({ name: 'foo', body: Promise.resolve('bar') })
.then(d => same('foo' in ripple.resources, true))
.then(d => same(ripple.resources.foo.body, 'bar'))
same('foo' in ripple.resources, false)
})
test('should link resource', ({ plan, same }) => {
plan(5)
const ripple = core({ aliases: { bar: 'foo' }})
same(ripple.aliases, {
src: { 'foo': 'bar' }
, dst: { 'bar': 'foo' }
})
ripple('bar', 'bar')
same(ripple('foo'), 'bar')
same(ripple('bar'), 'bar')
ripple('foo', 'foo')
same(ripple('foo'), 'foo')
same(ripple('bar'), 'foo')
})