@simplito/privmx-webendpoint
Version:
PrivMX Web Endpoint library
77 lines (76 loc) • 3.67 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../__mocks__/utils");
const constants_1 = require("../__mocks__/constants");
const mockContainerSubscriber_1 = require("../__mocks__/mockContainerSubscriber");
const subscriptions_1 = require("../subscriptions");
const Types_1 = require("../../Types");
describe("Store event manager", () => {
let { q, manager } = (0, utils_1.createTestSetup)();
let mockEventsManager = manager.getStoreEventManager(new mockContainerSubscriber_1.MockContainerSubscriber(q));
beforeEach(() => {
let { q: _q, manager } = (0, utils_1.createTestSetup)();
q = _q;
mockEventsManager = manager.getStoreEventManager(new mockContainerSubscriber_1.MockContainerSubscriber(q));
});
it("should add callback for event", async () => {
const sub = (0, subscriptions_1.createStoreSubscription)({
type: Types_1.StoreEventType.STORE_UPDATE,
id: "",
selector: Types_1.StoreEventSelectorType.CONTEXT_ID,
callbacks: [() => { }],
});
await mockEventsManager.subscribeFor([sub]);
expect(mockEventsManager.listeners.size).toBe(1);
});
it("should function to remove callback from event", async () => {
const sub = (0, subscriptions_1.createStoreSubscription)({
type: Types_1.StoreEventType.STORE_UPDATE,
id: "",
selector: Types_1.StoreEventSelectorType.CONTEXT_ID,
callbacks: [() => { }],
});
const [subId] = await mockEventsManager.subscribeFor([sub]);
expect(mockEventsManager.listeners.size).toBe(1);
await mockEventsManager.unsubscribeFrom([subId]);
expect(mockEventsManager.listeners.size).toBe(0);
});
it("should register multiple callbacks for channel", async () => {
const storeEventCb = jest.fn();
const sub = (0, subscriptions_1.createStoreSubscription)({
type: Types_1.StoreEventType.STORE_UPDATE,
id: "",
selector: Types_1.StoreEventSelectorType.CONTEXT_ID,
callbacks: [storeEventCb, storeEventCb],
});
const [subId] = await mockEventsManager.subscribeFor([sub]);
q.dispatchEvent((0, constants_1.MOCK_STORE_CREATED_EVENT)(subId));
//adding task on end of js event loop
await (0, utils_1.waitForNextTick)();
expect(storeEventCb).toHaveBeenCalledTimes(2);
});
it("should handle subscription for two channels", async () => {
const storeEventCb = jest.fn();
const messageEventCb = jest.fn();
const STORE_ID = "98dsyvs87dybv9a87dyvb98";
const subA = (0, subscriptions_1.createStoreSubscription)({
type: Types_1.StoreEventType.STORE_CREATE,
id: "",
selector: Types_1.StoreEventSelectorType.CONTEXT_ID,
callbacks: [storeEventCb],
});
const subB = (0, subscriptions_1.createStoreSubscription)({
type: Types_1.StoreEventType.FILE_DELETE,
id: STORE_ID,
selector: Types_1.StoreEventSelectorType.STORE_ID,
callbacks: [messageEventCb],
});
const [subIdA, subIdB] = await mockEventsManager.subscribeFor([subA, subB]);
q.dispatchEvent((0, constants_1.MOCK_STORE_CREATED_EVENT)(subIdA));
q.dispatchEvent((0, constants_1.MOCK_STORE_FILE_DELETED_EVENT)(STORE_ID, subIdB));
//adding task on end of js event loop
await (0, utils_1.waitForNextTick)();
expect(storeEventCb).toHaveBeenCalledTimes(1);
expect(messageEventCb).toHaveBeenCalledTimes(1);
});
});