1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-17 16:53:34 +00:00

[EC-598] feat: add test for request/response

This commit is contained in:
Andreas Coroiu
2023-01-27 13:38:17 +01:00
parent 0f875b93a1
commit 18dbaf2a4e

View File

@@ -22,21 +22,37 @@ describe("Messenger", () => {
messengerB.addHandler(handlerB.handler); messengerB.addHandler(handlerB.handler);
}); });
it("should deliver message to B when sending request to A", () => { it("should deliver message to B when sending request from A", () => {
const message = createMessage(); const request = createRequest();
messengerA.request(message); messengerA.request(request);
const received = handlerB.recieve(); const received = handlerB.recieve();
expect(received.length).toBe(1); expect(received.length).toBe(1);
expect(received[0].message).toMatchObject(message); expect(received[0].message).toMatchObject(request);
});
it("should return response from B when sending request from A", async () => {
const request = createRequest();
const response = createResponse();
const requestPromise = messengerA.request(request);
const received = handlerB.recieve();
received[0].respond(response);
const returned = await requestPromise;
expect(returned).toMatchObject(response);
}); });
}); });
type TestMessage = Message & { testId: string }; type TestMessage = Message & { testId: string };
function createMessage(): TestMessage { function createRequest(): TestMessage {
return { testId: Utils.newGuid(), type: "TestMessage" } as any; return { testId: Utils.newGuid(), type: "TestRequest" } as any;
}
function createResponse(): TestMessage {
return { testId: Utils.newGuid(), type: "TestResponse" } as any;
} }
class TestChannelPair { class TestChannelPair {
@@ -49,12 +65,14 @@ class TestChannelPair {
this.channelA = { this.channelA = {
messages$: subjectA, messages$: subjectA,
postMessage: (message) => subjectB.next(message), postMessage: (message) => {
subjectB.next(message);
},
}; };
this.channelB = { this.channelB = {
messages$: subjectB, messages$: subjectB,
postMessage: (message) => subjectB.next(message), postMessage: (message) => subjectA.next(message),
}; };
} }
} }