Skip to content

Commit 3524849

Browse files
author
Frank Schmid
committed
Test new behavior in wpwatch unit tests
1 parent b2882c8 commit 3524849

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

tests/webpack.mock.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,23 @@ const StatsMock = () => ({
1212
toString: sinon.stub().returns('testStats'),
1313
});
1414

15-
const CompilerMock = (sandbox, statsMock) => ({
15+
const WatchMock = sandbox => ({
16+
close: sandbox.stub().callsFake(cb => cb())
17+
});
18+
19+
const CompilerMock = (sandbox, statsMock, watchMock) => ({
1620
run: sandbox.stub().yields(null, statsMock),
17-
watch: sandbox.stub().yields(null, statsMock)
21+
watch: sandbox.stub().returns(watchMock).yields(null, statsMock)
1822
});
1923

2024
const webpackMock = sandbox => {
2125
const statsMock = StatsMock(sandbox);
22-
const compilerMock = CompilerMock(sandbox, statsMock);
26+
const watchMock = WatchMock(sandbox);
27+
const compilerMock = CompilerMock(sandbox, statsMock, watchMock);
2328
const mock = sinon.stub().returns(compilerMock);
2429
mock.compilerMock = compilerMock;
2530
mock.statsMock = statsMock;
31+
mock.watchMock = watchMock;
2632
return mock;
2733
};
2834

tests/wpwatch.test.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,23 +145,26 @@ describe('wpwatch', function() {
145145
it('should call callback on subsequent runs', () => {
146146
const wpwatch = module.wpwatch.bind(module);
147147
let watchCallbackSpy;
148-
webpackMock.compilerMock.watch.callsFake((options, cb) => {
148+
webpackMock.compilerMock.watch.onFirstCall().callsFake((options, cb) => {
149149
// We'll spy the callback registered for watch
150150
watchCallbackSpy = sandbox.spy(cb);
151151

152152
// Schedule second call after 2 seconds
153153
setTimeout(() => {
154-
watchCallbackSpy(null, { call: 2 });
154+
process.nextTick(() => watchCallbackSpy(null, { call: 2, hash: '2' }));
155155
}, 2000);
156-
process.nextTick(() => watchCallbackSpy(null, { call: 1 }));
156+
process.nextTick(() => watchCallbackSpy(null, { call: 1, hash: '1' }));
157+
return webpackMock.watchMock;
157158
});
158159
spawnStub.resolves();
159160

160161
return expect(wpwatch()).to.be.fulfilled
161162
.then(() => BbPromise.delay(3000))
162163
.then(() => BbPromise.join(
163-
expect(spawnStub).to.not.have.been.called,
164-
expect(webpackMock.compilerMock.watch).to.have.been.calledOnce,
164+
expect(spawnStub).to.have.been.calledOnce,
165+
expect(spawnStub).to.have.been.calledWithExactly('webpack:compile:watch'),
166+
expect(webpackMock.compilerMock.watch).to.have.been.calledTwice,
167+
expect(webpackMock.watchMock.close).to.have.been.calledOnce,
165168
expect(watchCallbackSpy).to.have.been.calledTwice
166169
));
167170
});
@@ -181,7 +184,7 @@ describe('wpwatch', function() {
181184
// Ignore the exception. The spy will record it.
182185
}
183186
}, 2000);
184-
process.nextTick(() => watchCallbackSpy(null, { call: 1 }));
187+
process.nextTick(() => watchCallbackSpy(null, { call: 3, hash: '3' }));
185188
});
186189
spawnStub.resolves();
187190

0 commit comments

Comments
 (0)