1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-18 17:23:37 +00:00

[PM-5189] Fixing jest tests within AutofillOverlayButton

This commit is contained in:
Cesar Gonzalez
2024-04-08 14:56:59 -05:00
parent 5731347925
commit a93ec6610e
2 changed files with 41 additions and 24 deletions

View File

@@ -1,7 +1,9 @@
import { mock } from "jest-mock-extended";
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status"; import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
import { createInitAutofillOverlayButtonMessageMock } from "../../../spec/autofill-mocks"; import { createInitAutofillOverlayButtonMessageMock } from "../../../spec/autofill-mocks";
import { postWindowMessage } from "../../../spec/testing-utils"; import { flushPromises, postWindowMessage } from "../../../spec/testing-utils";
import AutofillOverlayButton from "./autofill-overlay-button"; import AutofillOverlayButton from "./autofill-overlay-button";
@@ -9,13 +11,20 @@ describe("AutofillOverlayButton", () => {
globalThis.customElements.define("autofill-overlay-button", AutofillOverlayButton); globalThis.customElements.define("autofill-overlay-button", AutofillOverlayButton);
let autofillOverlayButton: AutofillOverlayButton; let autofillOverlayButton: AutofillOverlayButton;
let messageConnectorIframe: HTMLIFrameElement;
const portKey: string = "overlayButtonPortKey";
beforeEach(() => { beforeEach(() => {
document.body.innerHTML = `<autofill-overlay-button></autofill-overlay-button>`; document.body.innerHTML = `<autofill-overlay-button></autofill-overlay-button>`;
autofillOverlayButton = document.querySelector("autofill-overlay-button"); autofillOverlayButton = document.querySelector("autofill-overlay-button");
autofillOverlayButton["messageOrigin"] = "https://localhost/"; autofillOverlayButton["messageOrigin"] = "https://localhost/";
messageConnectorIframe = mock<HTMLIFrameElement>({
contentWindow: { postMessage: jest.fn() },
});
autofillOverlayButton["messageConnectorIframe"] = messageConnectorIframe;
jest.spyOn(autofillOverlayButton as any, "initMessageConnector").mockResolvedValue(undefined);
jest.spyOn(globalThis.document, "createElement"); jest.spyOn(globalThis.document, "createElement");
jest.spyOn(globalThis.parent, "postMessage"); jest.spyOn(messageConnectorIframe.contentWindow, "postMessage");
}); });
afterEach(() => { afterEach(() => {
@@ -23,10 +32,14 @@ describe("AutofillOverlayButton", () => {
}); });
describe("initAutofillOverlayButton", () => { describe("initAutofillOverlayButton", () => {
it("creates the button element with the locked icon when the user's auth status is not Unlocked", () => { it("creates the button element with the locked icon when the user's auth status is not Unlocked", async () => {
postWindowMessage( postWindowMessage(
createInitAutofillOverlayButtonMessageMock({ authStatus: AuthenticationStatus.Locked }), createInitAutofillOverlayButtonMessageMock({
authStatus: AuthenticationStatus.Locked,
portKey,
}),
); );
await flushPromises();
expect(autofillOverlayButton["buttonElement"]).toMatchSnapshot(); expect(autofillOverlayButton["buttonElement"]).toMatchSnapshot();
expect(autofillOverlayButton["buttonElement"].querySelector("svg")).toBe( expect(autofillOverlayButton["buttonElement"].querySelector("svg")).toBe(
@@ -34,8 +47,9 @@ describe("AutofillOverlayButton", () => {
); );
}); });
it("creates the button element with the normal icon when the user's auth status is Unlocked ", () => { it("creates the button element with the normal icon when the user's auth status is Unlocked ", async () => {
postWindowMessage(createInitAutofillOverlayButtonMessageMock()); postWindowMessage(createInitAutofillOverlayButtonMessageMock({ portKey }));
await flushPromises();
expect(autofillOverlayButton["buttonElement"]).toMatchSnapshot(); expect(autofillOverlayButton["buttonElement"]).toMatchSnapshot();
expect(autofillOverlayButton["buttonElement"].querySelector("svg")).toBe( expect(autofillOverlayButton["buttonElement"].querySelector("svg")).toBe(
@@ -43,55 +57,60 @@ describe("AutofillOverlayButton", () => {
); );
}); });
it("posts a message to the background indicating that the icon was clicked", () => { it("posts a message to the background indicating that the icon was clicked", async () => {
postWindowMessage(createInitAutofillOverlayButtonMessageMock()); postWindowMessage(createInitAutofillOverlayButtonMessageMock({ portKey }));
await flushPromises();
autofillOverlayButton["buttonElement"].click(); autofillOverlayButton["buttonElement"].click();
expect(globalThis.parent.postMessage).toHaveBeenCalledWith( expect(messageConnectorIframe.contentWindow.postMessage).toHaveBeenCalledWith(
{ command: "overlayButtonClicked" }, { command: "overlayButtonClicked", portKey },
"https://localhost/", "*",
); );
}); });
}); });
describe("global event listeners", () => { describe("global event listeners", () => {
beforeEach(() => { beforeEach(() => {
postWindowMessage(createInitAutofillOverlayButtonMessageMock()); postWindowMessage(createInitAutofillOverlayButtonMessageMock({ portKey }));
}); });
it("does not post a message to close the autofill overlay if the element is focused during the focus check", () => { it("does not post a message to close the autofill overlay if the element is focused during the focus check", async () => {
jest.spyOn(globalThis.document, "hasFocus").mockReturnValue(true); jest.spyOn(globalThis.document, "hasFocus").mockReturnValue(true);
postWindowMessage({ command: "checkAutofillOverlayButtonFocused" }); postWindowMessage({ command: "checkAutofillOverlayButtonFocused" });
await flushPromises();
expect(globalThis.parent.postMessage).not.toHaveBeenCalledWith({ expect(messageConnectorIframe.contentWindow.postMessage).not.toHaveBeenCalledWith({
command: "closeAutofillOverlay", command: "closeAutofillOverlay",
}); });
}); });
it("posts a message to close the autofill overlay if the element is not focused during the focus check", () => { it("posts a message to close the autofill overlay if the element is not focused during the focus check", async () => {
jest.spyOn(globalThis.document, "hasFocus").mockReturnValue(false); jest.spyOn(globalThis.document, "hasFocus").mockReturnValue(false);
postWindowMessage({ command: "checkAutofillOverlayButtonFocused" }); postWindowMessage({ command: "checkAutofillOverlayButtonFocused" });
await flushPromises();
expect(globalThis.parent.postMessage).toHaveBeenCalledWith( expect(messageConnectorIframe.contentWindow.postMessage).toHaveBeenCalledWith(
{ command: "closeAutofillOverlay" }, { command: "closeAutofillOverlay", portKey },
"https://localhost/", "*",
); );
}); });
it("updates the user's auth status", () => { it("updates the user's auth status", async () => {
autofillOverlayButton["authStatus"] = AuthenticationStatus.Locked; autofillOverlayButton["authStatus"] = AuthenticationStatus.Locked;
postWindowMessage({ postWindowMessage({
command: "updateAutofillOverlayButtonAuthStatus", command: "updateAutofillOverlayButtonAuthStatus",
authStatus: AuthenticationStatus.Unlocked, authStatus: AuthenticationStatus.Unlocked,
}); });
await flushPromises();
expect(autofillOverlayButton["authStatus"]).toBe(AuthenticationStatus.Unlocked); expect(autofillOverlayButton["authStatus"]).toBe(AuthenticationStatus.Unlocked);
}); });
it("updates the page color scheme meta tag", () => { it("updates the page color scheme meta tag", async () => {
const colorSchemeMetaTag = globalThis.document.createElement("meta"); const colorSchemeMetaTag = globalThis.document.createElement("meta");
colorSchemeMetaTag.setAttribute("name", "color-scheme"); colorSchemeMetaTag.setAttribute("name", "color-scheme");
colorSchemeMetaTag.setAttribute("content", "light"); colorSchemeMetaTag.setAttribute("content", "light");
@@ -101,6 +120,7 @@ describe("AutofillOverlayButton", () => {
command: "updateOverlayPageColorScheme", command: "updateOverlayPageColorScheme",
colorScheme: "dark", colorScheme: "dark",
}); });
await flushPromises();
expect(colorSchemeMetaTag.getAttribute("content")).toBe("dark"); expect(colorSchemeMetaTag.getAttribute("content")).toBe("dark");
}); });

View File

@@ -24,14 +24,11 @@ describe("AutofillOverlayList", () => {
autofillOverlayList = document.querySelector("autofill-overlay-list"); autofillOverlayList = document.querySelector("autofill-overlay-list");
autofillOverlayList["messageOrigin"] = "https://localhost/"; autofillOverlayList["messageOrigin"] = "https://localhost/";
messageConnectorIframe = mock<HTMLIFrameElement>({ messageConnectorIframe = mock<HTMLIFrameElement>({
contentWindow: { contentWindow: { postMessage: jest.fn() },
postMessage: jest.fn(),
},
}); });
autofillOverlayList["messageConnectorIframe"] = messageConnectorIframe; autofillOverlayList["messageConnectorIframe"] = messageConnectorIframe;
jest.spyOn(autofillOverlayList as any, "initMessageConnector").mockResolvedValue(undefined); jest.spyOn(autofillOverlayList as any, "initMessageConnector").mockResolvedValue(undefined);
jest.spyOn(globalThis.document, "createElement"); jest.spyOn(globalThis.document, "createElement");
jest.spyOn(globalThis.parent, "postMessage");
}); });
afterEach(() => { afterEach(() => {