@simplito/privmx-webendpoint
Version:
PrivMX Web Endpoint library
80 lines (79 loc) • 3.79 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../__mocks__/utils");
const constants_1 = require("../__mocks__/constants");
const subscriptions_1 = require("../subscriptions");
const Types_1 = require("../../Types");
const mockContainerSubscriber_1 = require("../__mocks__/mockContainerSubscriber");
describe("Inbox event manager", () => {
let { q, manager } = (0, utils_1.createTestSetup)();
let mockEventsManager = manager.getInboxEventManager(new mockContainerSubscriber_1.MockContainerSubscriber(q));
beforeEach(() => {
let { q: _q, manager } = (0, utils_1.createTestSetup)();
q = _q;
mockEventsManager = manager.getInboxEventManager(new mockContainerSubscriber_1.MockContainerSubscriber(q));
});
it("should add callback for event", async () => {
const sub = (0, subscriptions_1.createInboxSubscription)({
type: Types_1.InboxEventType.INBOX_UPDATE,
id: "",
selector: Types_1.InboxEventSelectorType.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.createInboxSubscription)({
type: Types_1.InboxEventType.INBOX_UPDATE,
id: "",
selector: Types_1.InboxEventSelectorType.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.createInboxSubscription)({
type: Types_1.InboxEventType.INBOX_UPDATE,
id: "",
selector: Types_1.InboxEventSelectorType.CONTEXT_ID,
callbacks: [storeEventCb, storeEventCb],
});
const [subId] = await mockEventsManager.subscribeFor([sub]);
q.dispatchEvent((0, constants_1.MOCK_INBOX_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 inboxEventCb = jest.fn();
const entryEventCb = jest.fn();
const inboxId = "98dsyvb8as7ybd0asydvb0as";
const subA = (0, subscriptions_1.createInboxSubscription)({
type: Types_1.InboxEventType.INBOX_CREATE,
id: "",
selector: Types_1.InboxEventSelectorType.CONTEXT_ID,
callbacks: [inboxEventCb],
});
const subB = (0, subscriptions_1.createInboxSubscription)({
type: Types_1.InboxEventType.ENTRY_DELETE,
id: inboxId,
selector: Types_1.InboxEventSelectorType.INBOX_ID,
callbacks: [entryEventCb],
});
const [subIdA, subIdB] = await mockEventsManager.subscribeFor([subA, subB]);
const event = (0, constants_1.MOCK_INBOX_ENTRY_DELETED_EVENT)(inboxId, subIdB);
const eventCreated = (0, constants_1.MOCK_INBOX_CREATED_EVENT)(subIdA);
q.dispatchEvent(eventCreated);
await (0, utils_1.waitForNextTick)();
q.dispatchEvent(event);
//adding task on end of js event loop
await (0, utils_1.waitForNextTick)();
expect(inboxEventCb).toHaveBeenCalledTimes(1);
expect(entryEventCb).toHaveBeenCalledTimes(1);
});
});