Skip to content

Commit 51c35f5

Browse files
committed
Merge pull request #257 from jpbackman/add-io-unit-tests
Unit tests for modules/io.js
2 parents eb78136 + cce55bd commit 51c35f5

File tree

2 files changed

+271
-1
lines changed

2 files changed

+271
-1
lines changed

lib/modules/io.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,6 @@ module.exports = function(ioServer, options) {
7979
emitProgressStart: emitProgressStart,
8080
emitCompileError: emitCompileError,
8181
emitCompileSuccess: emitCompileSuccess
82-
}
82+
};
83+
8384
};

test/unit/modules/io.test.js

+269
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
'use strict';
2+
3+
var proxyquire = require('proxyquire'),
4+
path = require('path'),
5+
chai = require('chai'),
6+
sinon = require('sinon'),
7+
sinonChai = require('sinon-chai'),
8+
expect = chai.expect,
9+
ioPath = path.resolve(process.cwd(), 'lib/modules/io');
10+
11+
chai.use(sinonChai);
12+
13+
describe('module io', function() {
14+
15+
var fs, parser, server, socket, opt, ioModule, io;
16+
beforeEach(setUp);
17+
18+
describe('initialized with options.styleVariables', function() {
19+
20+
var styleFile;
21+
beforeEach(function() {
22+
sinon.spy(console, 'error');
23+
styleFile = 'foo/bar.scss';
24+
opt.styleVariables = styleFile;
25+
fs.existsSync = sinon.stub().withArgs(styleFile).returns(false);
26+
io = ioModule(server, opt);
27+
});
28+
29+
afterEach(function() {
30+
console.error.restore();
31+
});
32+
33+
it('logs error to console if file does not exist', function() {
34+
expect(console.error).to.have.been.calledWith('Could not find SASS variables file', styleFile);
35+
});
36+
37+
it('causes module to return undefined if file does not exist', function() {
38+
expect(io).to.be.undefined;
39+
});
40+
41+
});
42+
43+
describe('emitProgressStart()', function() {
44+
45+
it('emits socket event "styleguide progress start"', function() {
46+
io.emitProgressStart();
47+
expect(socket.emit).to.have.been.calledWith('styleguide progress start');
48+
});
49+
50+
});
51+
52+
describe('emitCompileSuccess()', function() {
53+
54+
it('emits socket event "styleguide compile success"', function() {
55+
io.emitCompileSuccess();
56+
expect(socket.emit).to.have.been.calledWith('styleguide compile success');
57+
});
58+
59+
it('emits socket event "styleguide progress end"', function() {
60+
io.emitCompileSuccess();
61+
expect(socket.emit).to.have.been.calledWith('styleguide progress end');
62+
});
63+
64+
});
65+
66+
describe('emitCompileError()', function() {
67+
68+
it('emits socket event "styleguide compile error" with error data', function() {
69+
var error = { message: 'fail' };
70+
io.emitCompileError(error);
71+
expect(socket.emit).to.have.been.calledWith('styleguide compile error', error);
72+
});
73+
74+
it('emits socket event "styleguide progress end"', function() {
75+
io.emitCompileError();
76+
expect(socket.emit).to.have.been.calledWith('styleguide progress end');
77+
});
78+
79+
});
80+
81+
describe('socket connection listener', function() {
82+
83+
var listener;
84+
85+
beforeEach(function() {
86+
listener = server.on.getCall(0).args[1];
87+
});
88+
89+
it('is registered on socket "connection" event', function() {
90+
expect(server.on).to.have.been.calledWith('connection');
91+
expect(listener).to.be.a('function');
92+
});
93+
94+
it('registers listener on "request variables from server" event', function() {
95+
listener.call(undefined, socket);
96+
expect(socket.on).to.have.been.calledWith('request variables from server');
97+
});
98+
99+
it('registers listener on "variables to server" event', function() {
100+
listener.call(undefined, socket);
101+
expect(socket.on).to.have.been.calledWith('variables to server');
102+
});
103+
104+
it('emits "styleguide compile success" on connection event if previous compile succeeded', function() {
105+
io.emitCompileSuccess();
106+
socket.emit.reset();
107+
listener.call(undefined, socket);
108+
expect(socket.emit).to.have.been.calledWith('styleguide compile success').and.calledWith('styleguide progress end');
109+
});
110+
111+
it('emits "styleguide compile error" on connection event if previous compile failed', function() {
112+
io.emitCompileError();
113+
socket.emit.reset();
114+
listener.call(undefined, socket);
115+
expect(socket.emit).to.have.been.calledWith('styleguide compile error').and.calledWith('styleguide progress end');
116+
});
117+
118+
});
119+
120+
describe('loading variables', function() {
121+
122+
var listener,
123+
readCallback;
124+
125+
beforeEach(function() {
126+
sinon.spy(console, 'error');
127+
opt.styleVariables = 'test/vars.scss';
128+
listener = getSocketListener('request variables from server');
129+
fs.readFile = sinon.spy();
130+
131+
listener.call();
132+
readCallback = fs.readFile.lastCall.args[2];
133+
});
134+
135+
afterEach(function() {
136+
console.error.restore();
137+
});
138+
139+
it('reads file defined in options.styleVariables', function() {
140+
expect(fs.readFile).to.have.been.calledWith(opt.styleVariables, { encoding: 'utf8' });
141+
});
142+
143+
it('passes file contents to variable parser.parseVariables', function() {
144+
var fileData = '$foo: 10px;';
145+
readCallback.call(undefined, undefined, fileData);
146+
expect(parser.parseVariables).to.have.been.calledWith(fileData, 'scss');
147+
});
148+
149+
it('emits variable data with socket event "variables from server"', function() {
150+
var data = [{ name: 'foo', value: '10px' }];
151+
parser.parseVariables = sinon.stub().returns(data);
152+
readCallback.call();
153+
expect(socket.emit).to.have.been.calledWith('variables from server', data);
154+
});
155+
156+
it('only logs error to console if reading variables file fails', function() {
157+
socket.emit.reset();
158+
readCallback.call(undefined, 'read fail');
159+
expect(console.error).to.have.been.calledWith('read fail');
160+
expect(parser.parseVariables).not.to.have.been.called;
161+
expect(socket.emit).not.to.have.been.called;
162+
});
163+
164+
});
165+
166+
describe('saving variables', function() {
167+
168+
var listener,
169+
readCallback,
170+
writeCallback,
171+
fileData = '$foo: 10px;',
172+
newVariables = [{ name: 'foo', value: '16px' }],
173+
newData = '$foo: 16px;';
174+
175+
beforeEach(function() {
176+
sinon.spy(console, 'error');
177+
opt.styleVariables = 'test/vars.scss';
178+
listener = getSocketListener('variables to server');
179+
fs.readFile = sinon.spy();
180+
fs.writeFile = sinon.spy();
181+
182+
listener.call(undefined, newVariables);
183+
readCallback = fs.readFile.lastCall.args[2];
184+
});
185+
186+
afterEach(function() {
187+
console.error.restore();
188+
});
189+
190+
it('reads file defined in options.styleVariables', function() {
191+
expect(fs.readFile).to.have.been.calledWith(opt.styleVariables, { encoding: 'utf8' });
192+
});
193+
194+
it('passes original file contents to variable parser.setVariables', function() {
195+
196+
readCallback.call(undefined, undefined, fileData);
197+
expect(parser.setVariables).to.have.been.calledWith(fileData, 'scss');
198+
});
199+
200+
it('passes new variables to variable parser.setVariables', function() {
201+
readCallback.call(undefined, undefined, fileData);
202+
expect(parser.setVariables).to.have.been.calledWith(fileData, 'scss', newVariables);
203+
});
204+
205+
it('writes data returned from parser.setVariables() to options.styleVariables file', function() {
206+
parser.setVariables = sinon.stub().returns(newData);
207+
readCallback();
208+
expect(fs.writeFile).to.have.been.calledWith(opt.styleVariables, newData);
209+
});
210+
211+
it('emits new variable data with socket event "variables saved to server"', function() {
212+
readCallback();
213+
writeCallback = fs.writeFile.lastCall.args[2];
214+
215+
writeCallback.call(undefined, undefined, newData);
216+
expect(socket.emit).to.have.been.calledWith('variables saved to server', newData);
217+
});
218+
219+
it('only logs error to console if writing new variables data fails', function() {
220+
socket.emit.reset();
221+
readCallback.call();
222+
writeCallback = fs.writeFile.lastCall.args[2];
223+
224+
writeCallback.call(undefined, 'write fail');
225+
expect(console.error).to.have.been.calledWith('write fail');
226+
expect(socket.emit).not.to.have.been.called;
227+
});
228+
229+
});
230+
231+
function setUp() {
232+
233+
socket = {
234+
conn: { id: '123' },
235+
emit: sinon.spy(),
236+
on: sinon.spy()
237+
};
238+
239+
server = {
240+
sockets: socket,
241+
on: sinon.spy()
242+
};
243+
244+
opt = {};
245+
fs = {};
246+
parser = {
247+
parseVariables: sinon.spy(),
248+
setVariables: sinon.spy()
249+
};
250+
251+
ioModule = proxyquire(ioPath, {
252+
fs: fs,
253+
'./variable-parser': parser
254+
});
255+
256+
io = ioModule(server, opt);
257+
}
258+
259+
function getSocketListener(event) {
260+
var connectionListener = server.on.lastCall.args[1];
261+
connectionListener.call(undefined, socket);
262+
for (var i = 0; i < socket.on.callCount; i += 1) {
263+
if (socket.on.getCall(i).args[0] === event) {
264+
return socket.on.getCall(i).args[1];
265+
}
266+
}
267+
}
268+
269+
});

0 commit comments

Comments
 (0)