-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
182 lines (139 loc) · 5.19 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
var core = require('rijs.core')
, update = require('utilise/update')
, clone = require('utilise/clone')
, expect = require('chai').expect
, time = require('utilise/time')
, keys = require('utilise/keys')
, data = require('./')
, key = require('utilise/key')
, set = require('utilise/set')
, to = require('utilise/to')
describe('Data Type', function() {
it('should create data resource', function(){
var ripple = data(core())
ripple('foo', { foo: 'bar' })
expect(ripple('foo')).to.eql({ foo: 'bar' })
})
it('should not automatically create data resource if no body', function(){
var ripple = data(core())
expect(ripple('foo')).to.be.not.ok
expect(ripple.resources.foo).to.not.be.ok
})
it('should create data resource if body function', function(){
var ripple = data(core())
, fn = d => 'foo'
ripple('foo', fn, { 'content-type': 'application/data' })
expect(ripple('foo')).to.eql(fn)
})
it('should not create data resource', function(){
var ripple = data(core())
ripple('baz', String)
expect(ripple.resources['baz']).to.not.be.ok
})
it('should emit local change events', function(){
var ripple = data(core())
, fn = function(){ result = to.arr(arguments) }
, result
ripple('foo', {}).on('change', fn)
ripple('foo').emit('change')
expect(result).to.eql([undefined])
ripple('foo').emit('change', { change: 'yep' })
expect(result).to.eql([{ change: 'yep' }])
})
it('should emit global change events', function(){
var ripple = data(core())
, fn = function(){ result = to.arr(arguments) }
, result
ripple('foo', {})
ripple.on('change', fn)
ripple('foo').emit('change')
expect(result).to.eql(['foo', undefined])
ripple('foo').emit('change', { change: 'yep' })
expect(result).to.eql(['foo', { change: 'yep' }])
})
it('should proxy global change events to local', function(){
var ripple = data(core())
, fn = function(){ result = to.arr(arguments) }
, result
ripple('foo', {}).on('change', fn)
ripple.emit('change', 'foo')
expect(result).to.be.not.ok
ripple.emit('change', ['foo'])
expect(result).to.be.not.ok
ripple.emit('change', ['foo', false])
expect(result).to.be.not.ok
ripple.emit('change', ['foo', { key: 'yep' }])
expect(result).to.eql([{ key: 'yep' }])
})
it('should not duplicate listeners', function(){
var ripple = data(core())
ripple('foo', [1])
ripple('foo', [2])
expect(ripple.resources.foo.body.on.change.length).to.equal(1)
expect(ripple.resources.foo.body.on.change.$bubble).to.be.a('function')
})
it('should not destroy existing headers by default', function(){
var ripple = data(core())
ripple({ name: 'name', body: ['foo'], headers: { foo: 'bar' }})
expect(ripple.resources.name.headers.foo).to.be.eql('bar')
ripple({ name: 'name', body: ['bar'] })
expect(ripple.resources.name.headers.foo).to.be.eql('bar')
ripple({ name: 'name', body: ['lorem'], headers: {} })
expect(ripple.resources.name.headers.foo).to.be.eql('bar')
ripple({ name: 'name', body: ['baz'], headers: { foo: 'baz' } })
expect(ripple.resources.name.headers.foo).to.be.eql('baz')
})
it('should not lose all listeners', function(){
var ripple = data(core())
ripple('foo', ['foo'])
.on('change', String)
.on('change.foo', Date)
.on('foo', Function)
.on('foo.bar', Boolean)
ripple('foo', ['bar'])
expect(ripple('foo').on.change[0]).to.eql(String)
expect(ripple('foo').on.change.length).to.eql(3)
expect(ripple('foo').on.change.$foo).to.eql(Date)
expect(ripple('foo').on.foo[0]).to.eql(Function)
expect(ripple('foo').on.foo.length).to.eql(2)
expect(ripple('foo').on.foo.$bar).to.eql(Boolean)
})
it('should not lose log and update it on overwrite', function(){
var ripple = data(core())
, changes = []
ripple.on('change', function(d, change){ changes.push(clone(change)) })
ripple('foo', ['foo'])
update(0, 'bar')(ripple('foo'))
ripple('foo', ['baz'])
update(0, 'boo')(ripple('foo'))
expect(changes).to.eql([
{ time: 0, type: 'update', value: ['foo'] }
, { time: 1, type: 'update', value: 'bar', key: '0' }
, { time: 2, type: 'update', value: ['baz'] }
, { time: 3, type: 'update', value: 'boo', key: '0' }
])
expect(ripple('foo').log).to.eql([null, null, null, null])
})
it('should not lose existing headers', function(){
var ripple = data(core())
ripple('foo', { bar: 1 }, { foo: { bar: 5 } })
expect(ripple.resources.foo.headers.foo).to.eql({ bar: 5 })
ripple('foo', { bar: 2 })
expect(ripple.resources.foo.headers.foo).to.eql({ bar: 5 })
})
it('should make active change accessible', function(done){
var ripple = data(core())
, result
ripple('foo', { bar: 5 })
ripple.on('change', function(data){
result = ripple.change
})
expect(ripple.change).to.be.not.ok
update('bar', 15)(ripple('foo'))
expect(ripple.change).to.be.not.ok
time(10, function(){
expect(result).to.eql(['foo', { type: 'update', key: 'bar', value: 15, time: 1 }])
done()
})
})
})