client-test.js
5.54 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* eslint-env mocha, browser */
var sinon = require('sinon');
describe("client", function() {
var s, client, clientOverlay, processUpdate;
beforeEach(function() {
s = sinon.sandbox.create();
});
afterEach(function() {
s.restore();
});
context("with default options", function() {
beforeEach(function setup() {
global.__resourceQuery = ''; // eslint-disable-line no-underscore-dangle
global.window = {
EventSource: sinon.stub().returns({
close: sinon.spy()
})
};
});
beforeEach(loadClient);
it("should connect to __webpack_hmr", function() {
sinon.assert.calledOnce(window.EventSource);
sinon.assert.calledWithNew(window.EventSource);
sinon.assert.calledWith(window.EventSource, '/__webpack_hmr');
});
it("should trigger webpack on successful builds", function() {
var eventSource = window.EventSource.lastCall.returnValue;
eventSource.onmessage(makeMessage({
action: 'built',
time: 100,
hash: 'deadbeeffeddad',
errors: [],
warnings: [],
modules: []
}));
sinon.assert.calledOnce(processUpdate);
});
it("should trigger webpack on successful syncs", function() {
var eventSource = window.EventSource.lastCall.returnValue;
eventSource.onmessage(makeMessage({
action: 'sync',
time: 100,
hash: 'deadbeeffeddad',
errors: [],
warnings: [],
modules: []
}));
sinon.assert.calledOnce(processUpdate);
});
it("should call subscribeAll handler on default messages", function() {
var spy = sinon.spy();
client.subscribeAll(spy);
var message = {
action: 'built',
time: 100,
hash: 'deadbeeffeddad',
errors: [],
warnings: [],
modules: []
};
var eventSource = window.EventSource.lastCall.returnValue;
eventSource.onmessage(makeMessage(message));
sinon.assert.calledOnce(spy);
sinon.assert.calledWith(spy, message);
});
it("should call subscribeAll handler on custom messages", function() {
var spy = sinon.spy();
client.subscribeAll(spy);
var eventSource = window.EventSource.lastCall.returnValue;
eventSource.onmessage(makeMessage({
action: 'thingy'
}));
sinon.assert.calledOnce(spy);
sinon.assert.calledWith(spy, { action: 'thingy' });
});
it("should call only custom handler on custom messages", function() {
var spy = sinon.spy();
client.subscribe(spy);
var eventSource = window.EventSource.lastCall.returnValue;
eventSource.onmessage(makeMessage({
custom: 'thingy'
}));
eventSource.onmessage(makeMessage({
action: 'built'
}));
sinon.assert.calledOnce(spy);
sinon.assert.calledWith(spy, { custom: 'thingy' });
sinon.assert.notCalled(processUpdate);
});
it("should test more of the client's functionality");
});
context("with name options", function() {
beforeEach(function setup() {
global.__resourceQuery = '?name=test'; // eslint-disable-line no-underscore-dangle
global.window = {
EventSource: sinon.stub().returns({
close: sinon.spy()
})
};
});
beforeEach(loadClient);
it("should not trigger webpack if event obj name is different", function() {
var eventSource = window.EventSource.lastCall.returnValue;
eventSource.onmessage(makeMessage({
name: 'foo',
action: 'built',
time: 100,
hash: 'deadbeeffeddad',
errors: [],
warnings: [],
modules: []
}));
sinon.assert.notCalled(processUpdate);
});
it("should not trigger webpack on successful syncs if obj name is different", function() {
var eventSource = window.EventSource.lastCall.returnValue;
eventSource.onmessage(makeMessage({
name: 'bar',
action: 'sync',
time: 100,
hash: 'deadbeeffeddad',
errors: [],
warnings: [],
modules: []
}));
sinon.assert.notCalled(processUpdate);
});
});
context("with no browser environment", function() {
beforeEach(function setup() {
global.__resourceQuery = ''; // eslint-disable-line no-underscore-dangle
delete global.window;
});
beforeEach(loadClient);
it("should not connect", function() {
// doesn't error
});
});
context("with no EventSource", function() {
beforeEach(function setup() {
global.__resourceQuery = ''; // eslint-disable-line no-underscore-dangle
global.window = {};
s.stub(console, 'warn');
});
beforeEach(loadClient);
it("should emit warning and not connect", function() {
sinon.assert.calledOnce(console.warn);
sinon.assert.calledWithMatch(console.warn, /EventSource/);
});
});
function makeMessage(obj) {
return { data: typeof obj === 'string' ? obj : JSON.stringify(obj) };
}
function loadClient() {
var path = require.resolve('../client');
delete require.cache[path];
client = require(path);
}
beforeEach(function() {
clientOverlay = {
exports: { showProblems: sinon.stub(), clear: sinon.stub() }
};
require.cache[require.resolve('../client-overlay')] = clientOverlay;
processUpdate = sinon.stub();
require.cache[require.resolve('../process-update')] = {
exports: processUpdate
};
});
afterEach(function() {
delete require.cache[require.resolve('../client-overlay')];
delete require.cache[require.resolve('../process-update')];
});
});