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

[PM-5189] Implementing jest tests for AutofillInlineMenuContentService

This commit is contained in:
Cesar Gonzalez
2024-06-11 11:44:24 -05:00
parent 0c786eafa6
commit c2e62940e0
2 changed files with 84 additions and 13 deletions

View File

@@ -1,6 +1,6 @@
import AutofillInit from "../../../content/autofill-init"; import AutofillInit from "../../../content/autofill-init";
import { AutofillOverlayElement } from "../../../enums/autofill-overlay.enum"; import { AutofillOverlayElement } from "../../../enums/autofill-overlay.enum";
import { flushPromises, sendMockExtensionMessage } from "../../../spec/testing-utils"; import { sendMockExtensionMessage } from "../../../spec/testing-utils";
import { AutofillInlineMenuContentService } from "./autofill-inline-menu-content.service"; import { AutofillInlineMenuContentService } from "./autofill-inline-menu-content.service";
@@ -8,12 +8,17 @@ describe("AutofillInlineMenuContentService", () => {
let autofillInlineMenuContentService: AutofillInlineMenuContentService; let autofillInlineMenuContentService: AutofillInlineMenuContentService;
let autofillInit: AutofillInit; let autofillInit: AutofillInit;
let sendExtensionMessageSpy: jest.SpyInstance; let sendExtensionMessageSpy: jest.SpyInstance;
let observeBodyMutationsSpy: jest.SpyInstance;
beforeEach(() => { beforeEach(() => {
document.body.innerHTML = ""; globalThis.document.body.innerHTML = "";
autofillInlineMenuContentService = new AutofillInlineMenuContentService(); autofillInlineMenuContentService = new AutofillInlineMenuContentService();
autofillInit = new AutofillInit(null, autofillInlineMenuContentService); autofillInit = new AutofillInit(null, autofillInlineMenuContentService);
autofillInit.init(); autofillInit.init();
observeBodyMutationsSpy = jest.spyOn(
autofillInlineMenuContentService["bodyElementMutationObserver"] as any,
"observe",
);
sendExtensionMessageSpy = jest.spyOn( sendExtensionMessageSpy = jest.spyOn(
autofillInlineMenuContentService as any, autofillInlineMenuContentService as any,
"sendExtensionMessage", "sendExtensionMessage",
@@ -35,6 +40,10 @@ describe("AutofillInlineMenuContentService", () => {
describe("extension message handlers", () => { describe("extension message handlers", () => {
describe("closeAutofillInlineMenu", () => { describe("closeAutofillInlineMenu", () => {
beforeEach(() => {
observeBodyMutationsSpy.mockImplementation();
});
it("closes the inline menu button", async () => { it("closes the inline menu button", async () => {
sendMockExtensionMessage({ sendMockExtensionMessage({
command: "appendAutofillInlineMenuToDom", command: "appendAutofillInlineMenuToDom",
@@ -45,14 +54,13 @@ describe("AutofillInlineMenuContentService", () => {
command: "closeAutofillInlineMenu", command: "closeAutofillInlineMenu",
overlayElement: AutofillOverlayElement.Button, overlayElement: AutofillOverlayElement.Button,
}); });
await flushPromises();
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("autofillOverlayElementClosed", { expect(sendExtensionMessageSpy).toHaveBeenCalledWith("autofillOverlayElementClosed", {
overlayElement: AutofillOverlayElement.Button, overlayElement: AutofillOverlayElement.Button,
}); });
}); });
it("closes the inline menu list", () => { it("closes the inline menu list", async () => {
sendMockExtensionMessage({ sendMockExtensionMessage({
command: "appendAutofillInlineMenuToDom", command: "appendAutofillInlineMenuToDom",
overlayElement: AutofillOverlayElement.List, overlayElement: AutofillOverlayElement.List,
@@ -85,7 +93,6 @@ describe("AutofillInlineMenuContentService", () => {
sendMockExtensionMessage({ sendMockExtensionMessage({
command: "closeAutofillInlineMenu", command: "closeAutofillInlineMenu",
}); });
await flushPromises();
expect(removeBodyElementObserverSpy).toHaveBeenCalled(); expect(removeBodyElementObserverSpy).toHaveBeenCalled();
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("autofillOverlayElementClosed", { expect(sendExtensionMessageSpy).toHaveBeenCalledWith("autofillOverlayElementClosed", {
@@ -97,5 +104,77 @@ describe("AutofillInlineMenuContentService", () => {
}); });
}); });
}); });
describe("appendAutofillInlineMenuToDom", () => {
beforeEach(() => {
observeBodyMutationsSpy.mockImplementation();
});
describe("creating the inline menu button", () => {
it("creates a `div` button element if the user browser is Firefox", () => {
autofillInlineMenuContentService["isFirefoxBrowser"] = true;
sendMockExtensionMessage({
command: "appendAutofillInlineMenuToDom",
overlayElement: AutofillOverlayElement.Button,
});
expect(autofillInlineMenuContentService["buttonElement"]).toBeInstanceOf(HTMLDivElement);
});
});
describe("creating the inline menu list", () => {
it("creates a `div` list element if the user browser is Firefox", () => {
autofillInlineMenuContentService["isFirefoxBrowser"] = true;
sendMockExtensionMessage({
command: "appendAutofillInlineMenuToDom",
overlayElement: AutofillOverlayElement.List,
});
expect(autofillInlineMenuContentService["listElement"]).toBeInstanceOf(HTMLDivElement);
});
});
});
describe("toggleAutofillInlineMenuHidden", () => {
it("sets the inline elements as hidden if the elements do not exist", () => {
sendMockExtensionMessage({
command: "toggleAutofillInlineMenuHidden",
isInlineMenuHidden: false,
});
expect(autofillInlineMenuContentService["isButtonVisible"]).toBe(false);
expect(autofillInlineMenuContentService["isListVisible"]).toBe(false);
});
it("sets the inline elements as visible", () => {
autofillInlineMenuContentService["buttonElement"] = document.createElement("div");
autofillInlineMenuContentService["listElement"] = document.createElement("div");
sendMockExtensionMessage({
command: "toggleAutofillInlineMenuHidden",
isInlineMenuHidden: false,
});
expect(autofillInlineMenuContentService["isButtonVisible"]).toBe(true);
expect(autofillInlineMenuContentService["isListVisible"]).toBe(true);
});
it("sets the inline elements as hidden", () => {
autofillInlineMenuContentService["buttonElement"] = document.createElement("div");
autofillInlineMenuContentService["listElement"] = document.createElement("div");
autofillInlineMenuContentService["isButtonVisible"] = true;
autofillInlineMenuContentService["isListVisible"] = true;
sendMockExtensionMessage({
command: "toggleAutofillInlineMenuHidden",
isInlineMenuHidden: true,
});
expect(autofillInlineMenuContentService["isButtonVisible"]).toBe(false);
expect(autofillInlineMenuContentService["isListVisible"]).toBe(false);
});
});
}); });
}); });

View File

@@ -193,10 +193,6 @@ export class AutofillInlineMenuContentService implements AutofillInlineMenuConte
* to create the element if it already exists in the DOM. * to create the element if it already exists in the DOM.
*/ */
private createButton() { private createButton() {
if (this.buttonElement) {
return;
}
if (this.isFirefoxBrowser) { if (this.isFirefoxBrowser) {
this.buttonElement = globalThis.document.createElement("div"); this.buttonElement = globalThis.document.createElement("div");
new AutofillInlineMenuButtonIframe(this.buttonElement); new AutofillInlineMenuButtonIframe(this.buttonElement);
@@ -222,10 +218,6 @@ export class AutofillInlineMenuContentService implements AutofillInlineMenuConte
* to create the element if it already exists in the DOM. * to create the element if it already exists in the DOM.
*/ */
private createList() { private createList() {
if (this.listElement) {
return;
}
if (this.isFirefoxBrowser) { if (this.isFirefoxBrowser) {
this.listElement = globalThis.document.createElement("div"); this.listElement = globalThis.document.createElement("div");
new AutofillInlineMenuListIframe(this.listElement); new AutofillInlineMenuListIframe(this.listElement);