-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
128 lines (109 loc) · 3.56 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
var expect = require('chai').expect
, core = require('rijs.core').default
, data = require('rijs.data').default
, react = require('./').default
describe('Reactive Data', function() {
it('should react to changes in data', function(done){
var ripple = react(data(core()))
ripple('foo', [])
ripple('foo').once('change', function(foo){
expect(!!ripple.resources.foo.body.observer).to.be.eql(!!Object.observe)
expect(!!ripple.observer).to.be.eql(!Object.observe)
expect(foo).to.eql(['bar'])
done()
})
ripple('foo').push('bar')
})
it('should add change details - push', function(done){
if (!Object.observe) return done()
var ripple = react(data(core()))
ripple('foo', [])
ripple('foo').push('bar')
ripple('foo').once('change', function(foo, change){
expect(change).to.eql({ key: '0', value: 'bar', type: 'push' })
done()
})
})
it('should add change details - update', function(done){
if (!Object.observe) return done()
var ripple = react(data(core()))
ripple('foo', ['foo'])
ripple('foo')[0] = 'bar'
ripple('foo').once('change', function(foo, change){
expect(change).to.eql({ key: '0', value: 'bar', type: 'update' })
done()
})
})
it('should add change details - remove', function(done){
if (!Object.observe) return done()
var ripple = react(data(core()))
ripple('foo', ['foo'])
ripple('foo').pop()
ripple('foo').once('change', function(foo, change){
expect(change).to.eql({ key: '0', value: 'foo', type: 'remove' })
done()
})
})
it('should add change details - load', function(done){
var ripple = react(data(core()))
ripple.once('change', function(res, change){
expect(change).to.eql({ type: 'load' })
ripple('foo').once('change', function(foo, change){
expect(change.type).to.be.not.ok
ripple('foo').once('change', function(foo, change){
expect(change).to.be.not.ok
done()
})
ripple('foo').emit('change')
})
ripple('foo', ['foo'])
})
ripple('foo', ['foo'])
})
it('should not duplicate observers', function(done){
var ripple = react(data(core()))
, count = 0
ripple('foo', ['foo'])
ripple('foo').on('change', function(){ count++ })
ripple('foo').emit('change')
ripple('foo').emit('change')
ripple('foo').push('bar')
setTimeout(function(){
expect(count).to.eql(3)
}, 150)
setTimeout(done, 200)
})
it('should have at least two level observation', function(done){
var ripple = react(data(core()))
ripple('foo', [{foo:'bar'}])
ripple('foo').once('change', function(data){
expect(data).to.eql([{foo:'foo'}])
done()
})
ripple('foo')[0].foo = 'foo'
})
it('should allow opting out of reaction per resource', function(done){
var ripple = react(data(core()))
, count = 0
ripple('foo', { foo:'bar' }, { reactive: false })
ripple('foo').once('change', function(){ count++ })
ripple('foo').foo = 'foo'
setTimeout(function(){
expect(ripple.resources.foo.headers.reactive).to.be.false
expect(ripple.resources.foo.body.observer).to.not.exist
expect(count).to.be.eql(0)
done()
}, 150)
})
it('should not duplicate manual emits', function(done){
var ripple = react(data(core()))
, count = 0
ripple('foo').on('change', function(){ count++ })
ripple('foo', ['foo'])
ripple('foo', ['foo'])
setTimeout(function(){
expect(count).to.eql(2)
}, 150)
setTimeout(done, 200)
})
})