-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
154 lines (122 loc) · 4.09 KB
/
test.js
File metadata and controls
154 lines (122 loc) · 4.09 KB
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
require('functionbind');
var expect = require('chai').expect;
var async = require('async');
var Backbone = require('backbone');
var _ = require('underscore');
var Socket = require('./index');
describe('socket-client', function() {
var Cards = Backbone.Collection.extend({ url: 'api/cards' });
var Trees = Backbone.Collection.extend({ url: 'api/trees' });
var ivan, alex, dima;
Backbone.Model.prototype.idAttribute = '_id';
Backbone.sync = function() {}; // stub ajax calls
function createUser(name, cb) {
var user = { name: name };
user.cards = new Cards([
{ _id: 1, name: 'create plugin for backbone', treeId: 1 },
{ _id: 2, name: 'with support of socket.io', treeId: 1 },
{ _id: 3, name: 'to sync data with server simpler', treeId: 2 }
]);
user.trees = new Trees([
{ _id: 1, name: 'Tree 1' },
{ _id: 2, name: 'Tree 2' }
]);
user.socket = new Socket({ 'force new connection': true, url: 'http://localhost:7358' })
.add(user.cards, 'cards', validateCard)
.add(user.trees, 'trees');
user.socket.join([1]);
user.socket.once('viewers', function() { cb(null, user) });
}
// dummy validator
function validateCard(json) {
return json.treeId === 1 || json.treeId === 2;
}
function timeout(done) {
_.delay(done, 20);
}
beforeEach(function(done) {
async.parallel([
function(cb) { createUser('ivan', cb); },
function(cb) { createUser('alex', cb); },
function(cb) { createUser('dima', cb); }
], function(err, result) {
ivan = result[0];
alex = result[1];
dima = result[2];
done(err);
});
});
afterEach(function() {
ivan.socket.socket.disconnect();
alex.socket.socket.disconnect();
dima.socket.socket.disconnect();
});
it('ignores events with socketId: true', function(done) {
ivan.cards.remove(ivan.cards.get(1), { socketId: ivan.socket.socketId() });
alex.socket.on('remove-cards', done);
dima.socket.on('remove-cards', done);
timeout(done);
});
it('emits add event', function(done) {
ivan.cards.add({ _id: 4, name: 'test add', treeId: 1 });
var next = _.after(2, function() {
expect(alex.cards).length(4);
expect(dima.cards).length(4);
done();
});
alex.socket.on('cards:add', function(data) {
expect(_.keys(data)).length(4);
expect(data._id).equal(4);
expect(data.socketId).equal(ivan.socket.socketId());
next();
});
dima.socket.on('cards:add', next);
});
it('emits change event', function(done) {
var next = _.after(4, function() {
expect(dima.trees.get(1).get('name')).equal('t1');
expect(ivan.trees.get(1).get('name')).equal('t1');
expect(dima.trees.get(2).get('name')).equal('t2');
expect(alex.trees.get(2).get('name')).equal('t2');
done();
});
alex.trees.get(1).save({ name: 't1' });
ivan.trees.get(2).save({ name: 't2' });
ivan.socket.on('trees:change', next);
alex.socket.on('trees:change', next);
dima.socket.on('trees:change', next);
});
it('emit remove event', function(done) {
var next = _.after(6, function() {
expect(dima.cards).length(0);
expect(ivan.cards).length(0);
done();
});
ivan.cards.remove(ivan.cards.get(1));
ivan.cards.remove(ivan.cards.get(2));
ivan.cards.remove(ivan.cards.get(3));
alex.socket.on('cards:remove', next);
dima.socket.on('cards:remove', next);
});
it('uses validator to prevent alient content', function(done) {
alex.cards.get(1).save({ treeId: 3 });
ivan.socket.on('cards:change', done);
dima.socket.on('cards:change', done);
timeout(done);
});
it('supports multiply trees', function(done) {
dima.socket.join([2, 1]);
dima.socket.on('viewers', function(viewers) {
viewers.length > 1
? expect(viewers).length(3)
: expect(viewers).length(1);
next();
});
var next = _.after(2, function() {
dima.cards.get(3).save({ name: 'modified tree' });
alex.socket.on('trees:change', done);
ivan.socket.on('trees:change', done);
timeout(done);
});
});
});