Skip to content

Commit 2627e68

Browse files
author
pemrouz
committed
fix: failing tests and test promisified pull
1 parent 76ba325 commit 2627e68

1 file changed

Lines changed: 89 additions & 50 deletions

File tree

test.js

Lines changed: 89 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ var core = require('rijs.core').default
77
, mockery = require('mockery')
88
, headers = { 'content-type': 'application/data' }
99
, request = { headers: { 'x-forwarded-for': 10 }}
10-
, socket = { emit: function(type, data){ return socket.emitted = emitted = [type, data]}, request: request}
11-
, other = { emit: function(type, data){ return other.emitted = [type, data]}, request: request }
12-
, sockets, opts, emitted, connection, receive, connect, reconnect, connected, disconnected
10+
, socket = { emit: function(type, data){ return socket.emitted.push(data)}, request: request}
11+
, other = { /*emit: function(type, data){ return other.emitted.push(data)},*/ request: request }
12+
, sockets, connection, receive, connect, reconnect, connected, disconnected
13+
1314
describe('Backpressure', function(){
1415

1516
describe('Server', function(){
@@ -24,7 +25,8 @@ describe('Backpressure', function(){
2425
})
2526

2627
beforeEach(function(){
27-
opts = emitted = socket.emitted = other.emitted = null
28+
socket.emitted = []
29+
other.emitted = []
2830
sockets = [socket, other]
2931
})
3032

@@ -34,7 +36,7 @@ describe('Backpressure', function(){
3436
expect(ripple.to).to.be.not.ok
3537
expect(ripple.from).to.be.not.ok
3638

37-
var ripple = back(sync(data(core()), { server: { foo: 'bar' }}))
39+
var ripple = back(sync(data(core())))
3840
expect(ripple.io).to.be.ok
3941
expect(ripple.to).to.be.a('function')
4042
expect(ripple.from).to.be.a('function')
@@ -43,11 +45,11 @@ describe('Backpressure', function(){
4345
it('should transform correctly', function(){
4446
var ripple
4547

46-
ripple = back(next(sync(data(core()), { server: { foo: 'bar' }})))
48+
ripple = back(next(sync(data(core()))))
4749
expect(ripple.to({ socket: { deps: { foo: 1 }}, name: 'bar' })).to.be.eql(false)
4850
expect(ripple.to({ socket: { deps: { foo: 1 }}, name: 'foo' })).to.be.eql(5)
4951

50-
ripple = back(nonext(sync(data(core()), { server: { foo: 'bar' }})))
52+
ripple = back(nonext(sync(data(core()))))
5153
expect(ripple.to({ socket: { deps: { foo: 1 }}, name: 'bar' })).to.be.eql(false)
5254
expect(ripple.to({ socket: { deps: { foo: 1 }}, name: 'foo' }))
5355
.to.be.eql({ socket: { deps: { foo: 1 }}, name: 'foo' })
@@ -63,92 +65,115 @@ describe('Backpressure', function(){
6365
}
6466
})
6567

66-
it('should not send new resource unless required', function(){
67-
var ripple = back(sync(data(core()), { server: { foo: 'bar' }}))
68+
it('should not send new resource unless required', function(done){
69+
var ripple = back(sync(data(core())))
6870
expect(socket.deps).to.be.eql({})
6971
expect(other.deps).to.be.eql({})
7072
ripple('foo', { foo: 'bar' })
71-
expect(other.emitted).to.be.not.ok
72-
expect(socket.emitted).to.be.not.ok
73+
74+
time(d => {
75+
expect(other.emitted).to.be.eql([])
76+
expect(socket.emitted).to.be.eql([])
77+
done()
78+
})
7379
})
7480

75-
it('should not accept on non-existent resources (server)', function(){
76-
var ripple = back(sync(data(core()), { server: { foo: 'bar' }}))
81+
it('should not accept on non-existent resources (server)', function(done){
82+
var ripple = back(sync(data(core())))
7783

7884
receive.call(socket, { name: 'foo', type: 'pull' })
79-
expect(socket.deps).to.be.eql({})
80-
expect(other.deps).to.be.eql({})
81-
expect(ripple.resources.foo).to.be.not.ok
85+
time(d => {
86+
expect(socket.deps).to.be.eql({})
87+
expect(other.deps).to.be.eql({})
88+
expect(ripple.resources.foo).to.be.not.ok
89+
done()
90+
})
8291
})
8392

84-
it('should ripple(!) changes - efficiently', function(){
85-
var ripple = back(sync(data(core()), { server: { foo: 'bar' }}))
93+
it('should ripple(!) changes - efficiently', function(done){
94+
var ripple = back(sync(data(core())))
8695

8796
ripple('foo', { foo: 'bar' })
97+
8898
receive.call(socket, { name: 'foo', type: 'pull' })
89-
expect(socket.emitted[1]).to.be.eql({ name: 'foo', time: 0, type: 'update', value: { foo: 'bar' }, headers})
90-
expect(other.emitted).to.be.not.ok
9199
expect(socket.deps).to.be.eql({ foo: 1 })
92100
expect(other.deps).to.be.eql({})
93101

94102
// should not oversend
95-
socket.emitted = other.emitted = null
96103
ripple('foo', { foo: 'baz' })
97-
expect(socket.emitted[1]).to.be.eql({ name: 'foo', time: 1, type: 'update', value: { foo: 'baz' }, headers})
98-
expect(other.emitted).to.be.not.ok
99104

100105
// subsequent changes should ripple
101-
socket.emitted = other.emitted = null
102106
receive.call({ deps: {} }, { name: 'foo', type: 'update', value: { foo: 'boo' }})
103107
expect(ripple.resources.foo.body).to.be.eql({ foo: 'boo' })
104-
expect(socket.emitted[1]).to.be.eql({ name: 'foo', time: 2, type: 'update', value: { foo: 'boo' }, headers})
105-
expect(other.emitted).to.be.not.ok
108+
109+
time(d => {
110+
expect(socket.emitted).to.be.eql([
111+
{ name: 'foo', time: 0, type: 'update', value: { foo: 'bar' }, headers }
112+
, { name: 'foo', time: 1, type: 'update', value: { foo: 'baz' }, headers }
113+
, { name: 'foo', time: 2, type: 'update', value: { foo: 'boo' }, headers }
114+
])
115+
expect(other.emitted).to.be.eql([])
116+
done()
117+
})
106118
})
107119

108-
it('should respect existing transforms (outgoing - block)', function(){
109-
var ripple = sync(data(core()), { server: { foo: 'bar' }})
120+
it('should respect existing transforms (outgoing - block)', function(done){
121+
var ripple = sync(data(core()))
110122
ripple.to = falsy
111123
ripple = back(ripple)
112124

113125
socket.deps = { foo: 1 }
114126
ripple('foo', { foo: 'bar' })
115-
expect(socket.emitted).to.be.not.ok
116-
expect(other.emitted).to.be.not.ok
127+
128+
time(d => {
129+
expect(socket.emitted).to.be.eql([])
130+
expect(other.emitted).to.be.eql([])
131+
done()
132+
})
117133
})
118134

119-
it('should respect existing transforms (outgoing)', function(){
120-
var ripple = sync(data(core()), { server: { foo: 'bar' }})
135+
it('should respect existing transforms (outgoing)', function(done){
136+
var ripple = sync(data(core()))
121137
ripple.to = hash
122138
ripple = back(ripple)
123139

124140
socket.deps = { foo: 1 }
125141
ripple('foo', { foo: 'bar' })
126-
expect(socket.emitted[1]).to.be.eql({ name: 'foo', time: 0, type: 'update', value: { hash: 785935555 }, headers })
127-
expect(other.emitted).to.be.not.ok
142+
time(d => {
143+
expect(socket.emitted).to.be.eql([{ name: 'foo', time: 0, type: 'update', value: { hash: 785935555 }, headers }])
144+
expect(other.emitted).to.be.eql([])
145+
done()
146+
})
128147
})
129148

130-
it('should respect existing transforms (incoming - block)', function(){
131-
var ripple = sync(data(core()), { server: { foo: 'bar' }})
149+
it('should respect existing transforms (incoming - block)', function(done){
150+
var ripple = sync(data(core()))
132151
ripple.from = falsy
133152
ripple = back(ripple)
134153

135154
ripple('foo', { foo: 'bar' })
136155
receive.call({ deps: {}}, { name: 'foo', type: 'update', value: { foo: 'baz' }})
137156
expect(ripple.resources.foo.body).to.be.eql({ foo: 'bar' })
138-
expect(socket.emitted).to.be.not.ok
139-
expect(other.emitted).to.be.not.ok
157+
time(d => {
158+
expect(socket.emitted).to.be.eql([])
159+
expect(other.emitted).to.be.eql([])
160+
done()
161+
})
140162
})
141163

142-
it('should respect existing transforms (incoming - pass through)', function(){
143-
var ripple = sync(data(core()), { server: { foo: 'bar' }})
164+
it('should respect existing transforms (incoming - pass through)', function(done){
165+
var ripple = sync(data(core()))
144166
ripple.from = hash
145167
ripple = back(ripple)
146168

147169
ripple('foo', { foo: 'bar' })
148170
receive.call({ deps: {}}, { name: 'foo', type: 'update', value: { foo: 'baz' }})
149171
expect(ripple.resources.foo.body).to.be.eql({ hash: 785943243 })
150-
expect(socket.emitted).to.be.not.ok
151-
expect(other.emitted).to.be.not.ok
172+
time(d => {
173+
expect(socket.emitted).to.be.eql([])
174+
expect(other.emitted).to.be.eql([])
175+
done()
176+
})
152177
})
153178
})
154179

@@ -160,6 +185,7 @@ describe('Backpressure', function(){
160185
})
161186

162187
before(function(){
188+
this.timeout(5000)
163189
mockery.enable({ warnOnUnregistered: false })
164190
mockery.registerMock('socket.io-client', sioClient)
165191
keys(require.cache).map(d => delete require.cache[d])
@@ -253,14 +279,13 @@ describe('Backpressure', function(){
253279

254280
expect(ripple.requested).to.eql({})
255281
connect()
256-
expect(ripple.requested).to.eql({
257-
'foo': 1
258-
, 'bar': 1
259-
})
260-
expect(emitted).to.eql([
282+
expect(ripple.requested.foo instanceof Promise).to.be.ok
283+
expect(ripple.requested.bar instanceof Promise).to.be.ok
284+
285+
time(d => expect(emitted).to.eql([
261286
['change', { name: 'foo', type: 'pull' }]
262287
, ['change', { name: 'bar', type: 'pull' }]
263-
])
288+
]))
264289

265290
function load(ripple) {
266291
ripple('foo', [])
@@ -287,17 +312,31 @@ describe('Backpressure', function(){
287312

288313
time(50, function(){
289314
expect(emitted).to.have.lengthOf(1)
290-
expect(ripple.requested).to.eql({ 'x-foo': 1 })
315+
expect(ripple.requested['x-foo'] instanceof Promise).to.be.ok
291316
done()
292317
})
293318
})
294319

320+
it('should subscribe node to updates', function(){
321+
document.body.innerHTML = '<x-foo></x-foo>'
322+
var ripple = back(sync(draw(data(core()))))
323+
, foo = document.body.firstChild
324+
325+
ripple.pull('bar', foo)
326+
expect(attr('data')(foo)).to.be.eql('bar')
327+
328+
ripple.pull('bar', foo)
329+
expect(attr('data')(foo)).to.be.eql('bar')
330+
331+
ripple.pull('baz', foo)
332+
expect(attr('data')(foo)).to.be.eql('bar baz')
333+
})
334+
295335
})
296336

297337
})
298338

299339
function sioServer(o){
300-
opts = o
301340
return {
302341
use: function(fn){
303342
sockets.map(function(s){ fn(s, noop) })

0 commit comments

Comments
 (0)