mockzilla-webextension
Version:
A mocking toolkit for web-extensions leveraging the power of TypeScript to enhance your jest experience.
45 lines (44 loc) • 1.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.mockEvent = exports.MockzillaEvent = void 0;
const mockzilla_1 = require("mockzilla");
class MockzillaEvent {
constructor(prefix) {
this.listeners = [];
this.disabled = false;
this.addListener = jest.fn((callback) => {
this.disabledCheck("addListener");
this.listeners.push(callback);
});
this.removeListener = jest.fn((callback) => {
this.disabledCheck("removeListener");
this.listeners = this.listeners.filter((listener) => listener !== callback);
});
this.hasListener = jest.fn((callback) => {
this.disabledCheck("hasListener");
return this.listeners.includes(callback);
});
this.hasListeners = jest.fn(() => {
this.disabledCheck("hasListeners");
return this.listeners.length > 0;
});
this.prefix = prefix;
}
disable() {
this.disabled = true;
}
emit(...args) {
this.listeners.forEach((listener) => listener(...args));
}
disabledCheck(what) {
if (this.disabled)
throw new mockzilla_1.MockzillaError(`Mock "${this.prefix}.${what}" has been used after tests have finished! You might have a memory leak there.`);
}
}
exports.MockzillaEvent = MockzillaEvent;
function mockEvent(builder) {
const mock = new MockzillaEvent(builder.mockPath);
builder.mock(mock);
return mock;
}
exports.mockEvent = mockEvent;