2017-01-18 21:42:59 +08:00
|
|
|
"use strict";
|
2017-01-07 14:56:47 +08:00
|
|
|
|
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 = () => {
|
2017-01-07 14:56:47 +08:00
|
|
|
return {
|
|
|
|
invalidate: sinon.spy(),
|
|
|
|
close: sinon.spy()
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-03-06 11:32:34 +08:00
|
|
|
const createCompiler = () => {
|
|
|
|
const compiler = {
|
2017-11-28 22:01:41 +08:00
|
|
|
hooks: {
|
|
|
|
watchClose: new SyncHook([])
|
|
|
|
}
|
2017-03-06 11:32:34 +08:00
|
|
|
};
|
2017-11-28 22:01:41 +08:00
|
|
|
Tapable.addCompatLayer(compiler);
|
2017-03-06 11:32:34 +08:00
|
|
|
return compiler;
|
|
|
|
};
|
|
|
|
|
2017-01-18 21:42:59 +08:00
|
|
|
describe("MultiWatching", () => {
|
2017-03-06 11:32:34 +08:00
|
|
|
let watchings, compiler, myMultiWatching;
|
2017-01-07 14:56:47 +08:00
|
|
|
|
2017-01-18 21:42:59 +08:00
|
|
|
beforeEach(() => {
|
2017-01-07 14:56:47 +08:00
|
|
|
watchings = [createWatching(), createWatching()];
|
2017-03-06 11:32:34 +08:00
|
|
|
compiler = createCompiler();
|
|
|
|
myMultiWatching = new MultiWatching(watchings, compiler);
|
2017-01-07 14:56:47 +08:00
|
|
|
});
|
|
|
|
|
2017-01-18 21:42:59 +08:00
|
|
|
describe("invalidate", () => {
|
2018-01-24 20:17:21 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
myMultiWatching.invalidate();
|
|
|
|
});
|
2017-01-07 14:56:47 +08:00
|
|
|
|
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-07 14:56:47 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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-07 14:56:47 +08:00
|
|
|
|
2017-01-18 21:42:59 +08:00
|
|
|
beforeEach(() => {
|
2017-01-07 14:56:47 +08:00
|
|
|
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-07 14:56:47 +08:00
|
|
|
});
|
|
|
|
|
2017-01-18 21:42:59 +08:00
|
|
|
it("calls callback after each watching has closed", () => {
|
2017-01-07 14:56:47 +08:00
|
|
|
callClosedFinishedCallback(watchings[0]);
|
|
|
|
callClosedFinishedCallback(watchings[1]);
|
2018-01-24 20:17:21 +08:00
|
|
|
expect(callback.callCount).toBe(1);
|
2017-01-07 14:56:47 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|