webpack/test/MultiWatching.unittest.js

83 lines
1.9 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 SyncHook = require("tapable").SyncHook;
2017-01-18 21:42:59 +08:00
const MultiWatching = require("../lib/MultiWatching");
2018-01-24 20:17:21 +08:00
const createWatching = () => {
return {
2018-05-07 21:26:04 +08:00
invalidate: jest.fn(),
2019-06-01 01:05:10 +08:00
suspend: jest.fn(),
resume: jest.fn(),
2018-05-07 21:26:04 +08:00
close: jest.fn()
};
};
const createCompiler = () => {
const compiler = {
2017-11-28 22:01:41 +08:00
hooks: {
watchClose: new SyncHook([])
}
};
return compiler;
};
2017-01-18 21:42:59 +08:00
describe("MultiWatching", () => {
2018-05-07 21:26:04 +08:00
let watchings;
let compiler;
let 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-05-07 21:26:04 +08:00
expect(watchings[0].invalidate.mock.calls.length).toBe(1);
expect(watchings[1].invalidate.mock.calls.length).toBe(1);
});
});
2019-06-01 01:05:10 +08:00
describe("suspend", () => {
it("suspends each watching", () => {
myMultiWatching.suspend();
expect(watchings[0].suspend.mock.calls.length).toBe(1);
expect(watchings[1].suspend.mock.calls.length).toBe(1);
});
it("resume each watching", () => {
myMultiWatching.resume();
expect(watchings[0].resume.mock.calls.length).toBe(1);
expect(watchings[1].resume.mock.calls.length).toBe(1);
});
});
2017-01-18 21:42:59 +08:00
describe("close", () => {
let callback;
2018-05-07 21:26:04 +08:00
const callClosedFinishedCallback = watching => {
watching.close.mock.calls[0][0]();
};
2017-01-18 21:42:59 +08:00
beforeEach(() => {
2018-05-07 21:26:04 +08:00
callback = jest.fn();
myMultiWatching.close(callback);
});
2017-01-18 21:42:59 +08:00
it("closes each watching", () => {
2018-05-07 21:26:04 +08:00
expect(watchings[0].close.mock.calls.length).toBe(1);
expect(watchings[1].close.mock.calls.length).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-05-07 21:26:04 +08:00
expect(callback.mock.calls.length).toBe(1);
});
});
});