UNPKG

8.33 kBJavaScriptView Raw
1var Runtime = require('../lib/runtime');
2var assert = require('assert');
3var Registry = require('./fixture/mem_registry');
4var EventEmitter = require('events').EventEmitter;
5
6describe('Runtime', function(){
7 describe('Reactive', function(done){
8 var runtime = null;
9 beforeEach(function() {
10 var reg = new Registry();
11 runtime = new Runtime({registry: reg});
12 });
13
14 it('implements the filter method.', function() {
15 assert.ok(runtime.filter);
16 });
17
18 it('implements the map method.', function() {
19 assert.ok(runtime.map);
20 });
21
22 it('implements the zip method.', function() {
23 assert.ok(runtime.zip);
24 });
25
26 it('implements the subscribe method.', function() {
27 assert.ok(runtime.subscribe);
28 });
29
30 it('implements the observe method.', function() {
31 assert.ok(runtime.observe);
32 });
33
34 it('calls the calls the filter method on deviceready', function() {
35 var d = { type: 'test' };
36
37 runtime
38 .filter(function(e) {
39 assert.equal(e.type, 'test');
40 done();
41 });
42
43 runtime.emit('deviceready', d);
44 });
45
46 it('calls subscribe when the observable chain is complete.', function(done) {
47 var d = { type: 'test' };
48 runtime
49 .subscribe(function(d) {
50 assert.equal(d.type, 'test');
51 done();
52 });
53
54 runtime.emit('deviceready', d);
55 });
56
57 it('calls map on the observable chain when an event occurs', function(done) {
58 var d = { type: 'test' };
59 runtime
60 .map(function(d) {
61 assert.equal(d.type, 'test');
62 return d;
63 })
64 .subscribe(function(x) {
65 assert.equal(x.type, 'test');
66 done();
67 });
68
69 runtime.emit('deviceready', d);
70
71 });
72
73 it('only calls zip when all conditions are fulfilled.', function(done) {
74 var d1 = { type: 'test' };
75 var d2 = { type: 'test2' };
76
77 var filter2 = runtime.filter(function(d) {
78 return d.type == 'test2';
79 });
80
81 runtime
82 .filter(function(d) {
83 return d.type == 'test';
84 }).zip(filter2, function(){
85 return arguments;
86 })
87 .subscribe(function(x) {
88 assert.equal(x[0].type, 'test');
89 assert.equal(x[1].type, 'test2');
90 done();
91 });
92
93 runtime.emit('deviceready', d1);
94 runtime.emit('deviceready', d2);
95 });
96
97 describe('Runtime#observe', function() {
98 it('returns a Subscription when a subscribe function is provided', function() {
99 var q = runtime.where({ type: 'test' });
100
101 var sub = runtime.observe(q, function(device) {
102 // do nothing
103 });
104
105 assert(typeof(sub.dispose) === 'function');
106 });
107
108 it('returns an Observable when a subscribe function is not provided', function() {
109 var q = runtime.where({ type: 'test' });
110
111 var obs = runtime.observe(q);
112
113 assert(typeof(obs.subscribe) === 'function');
114 });
115
116 it('will take a single query as the first argument', function(done) {
117 var q = runtime.where({ type: 'test' });
118 var d = { type: 'test' };
119
120 runtime.observe(q, function(device) {
121 assert.equal(device.type, 'test');
122 done();
123 });
124
125 runtime.emit('deviceready', d);
126 });
127
128 it('will call the observe callback when the query is fullfilled.', function(done) {
129 var q = runtime.where({type: 'test'});
130 var d = { type: 'test' };
131 runtime.observe([q], function(device) {
132 assert.equal(device.type, 'test');
133 done();
134 });
135
136 runtime.emit('deviceready', d);
137 });
138
139 it('will call the observe callback when all queries are fullfilled.', function(done) {
140 var q1 = runtime.where({ type: 'test1' });
141 var q2 = runtime.where({ type: 'test2' });
142
143 var d1 = { type: 'test1' };
144 var d2 = { type: 'test2' };
145
146 runtime.observe([q1, q2], function(one, two) {
147 assert.equal(one.type, 'test1');
148 assert.equal(two.type, 'test2');
149 done();
150 });
151
152 runtime.emit('deviceready', d1);
153 runtime.emit('deviceready', d2);
154 });
155
156 it('will call the observe callback when all queries are fullfilled, and when events happen in any order', function(done) {
157 var q1 = runtime.where({ type: 'test1' });
158 var q2 = runtime.where({ type: 'test2' });
159
160 var d1 = { type: 'test1' };
161 var d2 = { type: 'test2' };
162
163 runtime.observe([q1, q2], function(one, two) {
164 assert.equal(one.type, 'test1');
165 assert.equal(two.type, 'test2');
166 done();
167 });
168
169 runtime.emit('deviceready', d2);
170 runtime.emit('deviceready', d1);
171 });
172
173 it('will fire if a device exists in the registry and doesn\'t call deviceready', function(done) {
174 runtime._jsDevices['1'] = { id: '1', type: 'test1' };
175
176 var q1 = runtime.where({ type: 'test1' });
177 var q2 = runtime.where({ type: 'test2' });
178
179 var d2 = { type: 'test2' };
180
181 runtime.observe([q1, q2], function(one, two) {
182 assert.equal(one.type, 'test1');
183 assert.equal(two.type, 'test2');
184 done();
185 });
186
187 runtime.emit('deviceready', d2);
188 });
189 });
190
191 describe('Extended reactive syntax', function() {
192 it('will respond the same way using the extended reactive syntax.', function(done) {
193 runtime
194 .filter(function(d) {
195 return d.type === 'test1';
196 })
197 .zip(runtime.filter(function(d){
198 return d.type === 'test2';
199 }), function() {
200 return Array.prototype.slice.call(arguments);
201 })
202 .subscribe(function(x) {
203 var devOne = x[0];
204 var devTwo = x[1];
205 assert.equal(devOne.type, 'test1');
206 assert.equal(devTwo.type, 'test2');
207 done();
208 });
209
210 var d1 = { type: 'test1' };
211 var d2 = { type: 'test2' };
212
213 runtime.emit('deviceready', d1);
214 runtime.emit('deviceready', d2);
215 });
216
217 it('take will only fire with one pair.', function(done) {
218 var emitter = new EventEmitter();
219 var fired = 0;
220 runtime
221 .filter(function(d) {
222 return d.type === 'test1';
223 })
224 .zip(runtime.filter(function(d){
225 return d.type === 'test2';
226 }), function() {
227 return Array.prototype.slice.call(arguments);
228 })
229 .take(1)
230 .subscribe(function(x) {
231 var devOne = x[0];
232 var devTwo = x[1];
233 assert.equal(devOne.type, 'test1');
234 assert.equal(devTwo.type, 'test2');
235 fired++;
236 emitter.on('complete', function() {
237 assert.equal(fired, 1);
238 done();
239 });
240 });
241
242 var d1 = { type: 'test1' };
243 var d2 = { type: 'test2' };
244
245 runtime.emit('deviceready', d1);
246 runtime.emit('deviceready', d2);
247 runtime.emit('deviceready', d1);
248 runtime.emit('deviceready', d2);
249 emitter.emit('complete');
250 });
251
252 it('will only fire take one time', function(done) {
253 var d = { type: 'test' };
254 var fired = 0;
255 var emitter = new EventEmitter();
256 runtime
257 .take(1)
258 .subscribe(function(x) {
259 assert.equal(x.type, 'test');
260 fired++;
261 emitter.on('complete', function(){
262 assert.equal(fired, 1);
263 done();
264 });
265 });
266
267 runtime.emit('deviceready', d);
268 emitter.emit('complete');
269 });
270
271 it('will only fire take twice.', function(done) {
272 var d = { type: 'test' };
273 var fired = 0;
274 var emitter = new EventEmitter();
275 runtime
276 .take(2)
277 .subscribe(function(x) {
278 assert.equal(x.type, 'test');
279 fired++;
280 if(fired > 1) {
281 emitter.on('complete', function(){
282 assert.equal(fired, 2);
283 done();
284 });
285 }
286 });
287
288 runtime.emit('deviceready', d);
289 runtime.emit('deviceready', d);
290 runtime.emit('deviceready', d);
291 emitter.emit('complete');
292 });
293
294
295
296 });
297 });
298});
299