Skip to content

Commit

Permalink
Test new behavior in wpwatch unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Schmid committed Feb 7, 2018
1 parent b2882c8 commit 3524849
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
12 changes: 9 additions & 3 deletions tests/webpack.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,23 @@ const StatsMock = () => ({
toString: sinon.stub().returns('testStats'),
});

const CompilerMock = (sandbox, statsMock) => ({
const WatchMock = sandbox => ({
close: sandbox.stub().callsFake(cb => cb())
});

const CompilerMock = (sandbox, statsMock, watchMock) => ({
run: sandbox.stub().yields(null, statsMock),
watch: sandbox.stub().yields(null, statsMock)
watch: sandbox.stub().returns(watchMock).yields(null, statsMock)
});

const webpackMock = sandbox => {
const statsMock = StatsMock(sandbox);
const compilerMock = CompilerMock(sandbox, statsMock);
const watchMock = WatchMock(sandbox);
const compilerMock = CompilerMock(sandbox, statsMock, watchMock);
const mock = sinon.stub().returns(compilerMock);
mock.compilerMock = compilerMock;
mock.statsMock = statsMock;
mock.watchMock = watchMock;
return mock;
};

Expand Down
15 changes: 9 additions & 6 deletions tests/wpwatch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,26 @@ describe('wpwatch', function() {
it('should call callback on subsequent runs', () => {
const wpwatch = module.wpwatch.bind(module);
let watchCallbackSpy;
webpackMock.compilerMock.watch.callsFake((options, cb) => {
webpackMock.compilerMock.watch.onFirstCall().callsFake((options, cb) => {
// We'll spy the callback registered for watch
watchCallbackSpy = sandbox.spy(cb);

// Schedule second call after 2 seconds
setTimeout(() => {
watchCallbackSpy(null, { call: 2 });
process.nextTick(() => watchCallbackSpy(null, { call: 2, hash: '2' }));
}, 2000);
process.nextTick(() => watchCallbackSpy(null, { call: 1 }));
process.nextTick(() => watchCallbackSpy(null, { call: 1, hash: '1' }));
return webpackMock.watchMock;
});
spawnStub.resolves();

return expect(wpwatch()).to.be.fulfilled
.then(() => BbPromise.delay(3000))
.then(() => BbPromise.join(
expect(spawnStub).to.not.have.been.called,
expect(webpackMock.compilerMock.watch).to.have.been.calledOnce,
expect(spawnStub).to.have.been.calledOnce,
expect(spawnStub).to.have.been.calledWithExactly('webpack:compile:watch'),
expect(webpackMock.compilerMock.watch).to.have.been.calledTwice,
expect(webpackMock.watchMock.close).to.have.been.calledOnce,
expect(watchCallbackSpy).to.have.been.calledTwice
));
});
Expand All @@ -181,7 +184,7 @@ describe('wpwatch', function() {
// Ignore the exception. The spy will record it.
}
}, 2000);
process.nextTick(() => watchCallbackSpy(null, { call: 1 }));
process.nextTick(() => watchCallbackSpy(null, { call: 3, hash: '3' }));
});
spawnStub.resolves();

Expand Down

0 comments on commit 3524849

Please sign in to comment.