1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-01-20 21:01:29 +01: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
No known key found for this signature in database
GPG Key ID: E70B5FFC81DFEC1A

View File

@ -22,21 +22,37 @@ describe("Messenger", () => {
messengerB.addHandler(handlerB.handler);
});
it("should deliver message to B when sending request to A", () => {
const message = createMessage();
messengerA.request(message);
it("should deliver message to B when sending request from A", () => {
const request = createRequest();
messengerA.request(request);
const received = handlerB.recieve();
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 };
function createMessage(): TestMessage {
return { testId: Utils.newGuid(), type: "TestMessage" } as any;
function createRequest(): TestMessage {
return { testId: Utils.newGuid(), type: "TestRequest" } as any;
}
function createResponse(): TestMessage {
return { testId: Utils.newGuid(), type: "TestResponse" } as any;
}
class TestChannelPair {
@ -49,12 +65,14 @@ class TestChannelPair {
this.channelA = {
messages$: subjectA,
postMessage: (message) => subjectB.next(message),
postMessage: (message) => {
subjectB.next(message);
},
};
this.channelB = {
messages$: subjectB,
postMessage: (message) => subjectB.next(message),
postMessage: (message) => subjectA.next(message),
};
}
}