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:00:37 -05:00
parent c8c64d2923
commit 0c786eafa6
2 changed files with 91 additions and 17 deletions

View File

@@ -1,10 +1,23 @@
import AutofillInit from "../../../content/autofill-init";
import { AutofillOverlayElement } from "../../../enums/autofill-overlay.enum";
import { flushPromises, sendMockExtensionMessage } from "../../../spec/testing-utils";
import { AutofillInlineMenuContentService } from "./autofill-inline-menu-content.service"; import { AutofillInlineMenuContentService } from "./autofill-inline-menu-content.service";
describe("AutofillInlineMenuContentService", () => { describe("AutofillInlineMenuContentService", () => {
let autofillInlineMenuContentService: AutofillInlineMenuContentService; let autofillInlineMenuContentService: AutofillInlineMenuContentService;
let autofillInit: AutofillInit;
let sendExtensionMessageSpy: jest.SpyInstance;
beforeEach(() => { beforeEach(() => {
document.body.innerHTML = "";
autofillInlineMenuContentService = new AutofillInlineMenuContentService(); autofillInlineMenuContentService = new AutofillInlineMenuContentService();
autofillInit = new AutofillInit(null, autofillInlineMenuContentService);
autofillInit.init();
sendExtensionMessageSpy = jest.spyOn(
autofillInlineMenuContentService as any,
"sendExtensionMessage",
);
}); });
afterEach(() => { afterEach(() => {
@@ -20,5 +33,69 @@ describe("AutofillInlineMenuContentService", () => {
}); });
}); });
describe("extension message handlers", () => {}); describe("extension message handlers", () => {
describe("closeAutofillInlineMenu", () => {
it("closes the inline menu button", async () => {
sendMockExtensionMessage({
command: "appendAutofillInlineMenuToDom",
overlayElement: AutofillOverlayElement.Button,
});
sendMockExtensionMessage({
command: "closeAutofillInlineMenu",
overlayElement: AutofillOverlayElement.Button,
});
await flushPromises();
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("autofillOverlayElementClosed", {
overlayElement: AutofillOverlayElement.Button,
});
});
it("closes the inline menu list", () => {
sendMockExtensionMessage({
command: "appendAutofillInlineMenuToDom",
overlayElement: AutofillOverlayElement.List,
});
sendMockExtensionMessage({
command: "closeAutofillInlineMenu",
overlayElement: AutofillOverlayElement.List,
});
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("autofillOverlayElementClosed", {
overlayElement: AutofillOverlayElement.List,
});
});
it("closes both inline menu elements and removes the body element mutation observer", async () => {
const removeBodyElementObserverSpy = jest.spyOn(
autofillInlineMenuContentService as any,
"removeBodyElementObserver",
);
sendMockExtensionMessage({
command: "appendAutofillInlineMenuToDom",
overlayElement: AutofillOverlayElement.Button,
});
sendMockExtensionMessage({
command: "appendAutofillInlineMenuToDom",
overlayElement: AutofillOverlayElement.List,
});
sendMockExtensionMessage({
command: "closeAutofillInlineMenu",
});
await flushPromises();
expect(removeBodyElementObserverSpy).toHaveBeenCalled();
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("autofillOverlayElementClosed", {
overlayElement: AutofillOverlayElement.Button,
});
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("autofillOverlayElementClosed", {
overlayElement: AutofillOverlayElement.List,
});
});
});
});
}); });

View File

@@ -113,31 +113,27 @@ export class AutofillInlineMenuContentService implements AutofillInlineMenuConte
* also remove the inline menu reposition event listeners. * also remove the inline menu reposition event listeners.
*/ */
private closeInlineMenuButton() { private closeInlineMenuButton() {
if (!this.buttonElement) { if (this.buttonElement) {
return;
}
this.buttonElement.remove(); this.buttonElement.remove();
this.isButtonVisible = false; this.isButtonVisible = false;
void this.sendExtensionMessage("autofillOverlayElementClosed", { void this.sendExtensionMessage("autofillOverlayElementClosed", {
overlayElement: AutofillOverlayElement.Button, overlayElement: AutofillOverlayElement.Button,
}); });
} }
}
/** /**
* Removes the inline menu list from the DOM if it is currently present. * Removes the inline menu list from the DOM if it is currently present.
*/ */
private closeInlineMenuList() { private closeInlineMenuList() {
if (!this.listElement) { if (this.listElement) {
return;
}
this.listElement.remove(); this.listElement.remove();
this.isListVisible = false; this.isListVisible = false;
void this.sendExtensionMessage("autofillOverlayElementClosed", { void this.sendExtensionMessage("autofillOverlayElementClosed", {
overlayElement: AutofillOverlayElement.List, overlayElement: AutofillOverlayElement.List,
}); });
} }
}
/** /**
* Updates the position of both the inline menu button and inline menu list. * Updates the position of both the inline menu button and inline menu list.
@@ -388,6 +384,7 @@ export class AutofillInlineMenuContentService implements AutofillInlineMenuConte
const secondToLastChildIsInlineMenuButton = secondToLastChild === this.buttonElement; const secondToLastChildIsInlineMenuButton = secondToLastChild === this.buttonElement;
if ( if (
!lastChild ||
(lastChildIsInlineMenuList && secondToLastChildIsInlineMenuButton) || (lastChildIsInlineMenuList && secondToLastChildIsInlineMenuButton) ||
(lastChildIsInlineMenuButton && !this.isListVisible) (lastChildIsInlineMenuButton && !this.isListVisible)
) { ) {