mirror of
https://github.com/bitwarden/browser.git
synced 2024-06-20 09:35:22 +02:00
* [PM-5303] Cannot login with SSO * [PM-5303] Adding documentation to newly created ContentMessageHandler class * [PM-5303] Updating manifest v3 implementation to use the newly scoped name * [PM-5303] Adding jest tests to implementation
92 lines
2.8 KiB
TypeScript
92 lines
2.8 KiB
TypeScript
import { postWindowMessage, sendExtensionRuntimeMessage } from "../jest/testing-utils";
|
|
|
|
import ContentMessageHandler from "./content-message-handler";
|
|
|
|
describe("ContentMessageHandler", () => {
|
|
let contentMessageHandler: ContentMessageHandler;
|
|
const sendMessageSpy = jest.spyOn(chrome.runtime, "sendMessage");
|
|
|
|
beforeEach(() => {
|
|
contentMessageHandler = new ContentMessageHandler();
|
|
});
|
|
|
|
afterEach(() => {
|
|
jest.clearAllMocks();
|
|
contentMessageHandler.destroy();
|
|
});
|
|
|
|
describe("init", () => {
|
|
it("should add event listeners", () => {
|
|
const addEventListenerSpy = jest.spyOn(window, "addEventListener");
|
|
const addListenerSpy = jest.spyOn(chrome.runtime.onMessage, "addListener");
|
|
|
|
contentMessageHandler.init();
|
|
|
|
expect(addEventListenerSpy).toHaveBeenCalledTimes(1);
|
|
expect(addListenerSpy).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe("handleWindowMessage", () => {
|
|
beforeEach(() => {
|
|
contentMessageHandler.init();
|
|
});
|
|
|
|
it("ignores messages from other sources", () => {
|
|
postWindowMessage({ command: "authResult" }, "https://localhost/", null);
|
|
|
|
expect(sendMessageSpy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("ignores messages without a command", () => {
|
|
postWindowMessage({});
|
|
|
|
expect(sendMessageSpy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("sends an authResult message", () => {
|
|
postWindowMessage({ command: "authResult", lastpass: true, code: "code", state: "state" });
|
|
|
|
expect(sendMessageSpy).toHaveBeenCalledTimes(1);
|
|
expect(sendMessageSpy).toHaveBeenCalledWith({
|
|
command: "authResult",
|
|
code: "code",
|
|
state: "state",
|
|
lastpass: true,
|
|
referrer: "localhost",
|
|
});
|
|
});
|
|
|
|
it("sends a webAuthnResult message", () => {
|
|
postWindowMessage({ command: "webAuthnResult", data: "data", remember: true });
|
|
|
|
expect(sendMessageSpy).toHaveBeenCalledTimes(1);
|
|
expect(sendMessageSpy).toHaveBeenCalledWith({
|
|
command: "webAuthnResult",
|
|
data: "data",
|
|
remember: true,
|
|
referrer: "localhost",
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("handleExtensionMessage", () => {
|
|
beforeEach(() => {
|
|
contentMessageHandler.init();
|
|
});
|
|
|
|
it("ignores the message to the extension background if it is not present in the forwardCommands list", () => {
|
|
sendExtensionRuntimeMessage({ command: "someOtherCommand" });
|
|
|
|
expect(sendMessageSpy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("forwards the message to the extension background if it is present in the forwardCommands list", () => {
|
|
sendExtensionRuntimeMessage({ command: "bgUnlockPopoutOpened" });
|
|
|
|
expect(sendMessageSpy).toHaveBeenCalledTimes(1);
|
|
expect(sendMessageSpy).toHaveBeenCalledWith({ command: "bgUnlockPopoutOpened" });
|
|
});
|
|
});
|
|
});
|