webpack/test/MultiWatching.unittest.js

67 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-01-18 21:42:59 +08:00
"use strict";
2017-11-28 22:01:41 +08:00
const Tapable = require("tapable").Tapable;
const SyncHook = require("tapable").SyncHook;
2017-01-18 21:42:59 +08:00
const sinon = require("sinon");
const MultiWatching = require("../lib/MultiWatching");
2018-01-24 20:17:21 +08:00
const createWatching = () => {
return {
invalidate: sinon.spy(),
close: sinon.spy()
};
};
const createCompiler = () => {
const compiler = {
2017-11-28 22:01:41 +08:00
hooks: {
watchClose: new SyncHook([])
}
};
2017-11-28 22:01:41 +08:00
Tapable.addCompatLayer(compiler);
return compiler;
};
2017-01-18 21:42:59 +08:00
describe("MultiWatching", () => {
let watchings, compiler, myMultiWatching;
2017-01-18 21:42:59 +08:00
beforeEach(() => {
watchings = [createWatching(), createWatching()];
compiler = createCompiler();
myMultiWatching = new MultiWatching(watchings, compiler);
});
2017-01-18 21:42:59 +08:00
describe("invalidate", () => {
2018-01-24 20:17:21 +08:00
beforeEach(() => {
myMultiWatching.invalidate();
});
2017-01-18 21:42:59 +08:00
it("invalidates each watching", () => {
2018-01-24 20:17:21 +08:00
expect(watchings[0].invalidate.callCount).toBe(1);
expect(watchings[1].invalidate.callCount).toBe(1);
});
});
2017-01-18 21:42:59 +08:00
describe("close", () => {
let callback;
2018-02-25 18:46:17 +08:00
const callClosedFinishedCallback = watching =>
watching.close.getCall(0).args[0]();
2017-01-18 21:42:59 +08:00
beforeEach(() => {
callback = sinon.spy();
myMultiWatching.close(callback);
});
2017-01-18 21:42:59 +08:00
it("closes each watching", () => {
2018-01-24 20:17:21 +08:00
expect(watchings[0].close.callCount).toBe(1);
expect(watchings[1].close.callCount).toBe(1);
});
2017-01-18 21:42:59 +08:00
it("calls callback after each watching has closed", () => {
callClosedFinishedCallback(watchings[0]);
callClosedFinishedCallback(watchings[1]);
2018-01-24 20:17:21 +08:00
expect(callback.callCount).toBe(1);
});
});
});