1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 16:23:44 +00:00

[PM-5189] Implementing jest tests for AutofillInlineMenuContentService

This commit is contained in:
Cesar Gonzalez
2024-06-11 10:19:26 -05:00
parent e0ee50f43c
commit c8c64d2923
4 changed files with 54 additions and 13 deletions

View File

@@ -269,7 +269,7 @@ describe("AutofillInit", () => {
it("removes the overlay when filling the form", async () => { it("removes the overlay when filling the form", async () => {
const blurAndRemoveOverlaySpy = jest.spyOn( const blurAndRemoveOverlaySpy = jest.spyOn(
autofillInit as any, autofillInit as any,
"blurFocusedFieldAndRemoveInlineMenu", "blurFocusedFieldAndCloseInlineMenu",
); );
sendMockExtensionMessage({ sendMockExtensionMessage({
command: "fillForm", command: "fillForm",

View File

@@ -123,7 +123,7 @@ class AutofillInit implements AutofillInitInterface {
return; return;
} }
this.blurFocusedFieldAndRemoveInlineMenu(); this.blurFocusedFieldAndCloseInlineMenu();
await this.sendExtensionMessage("updateIsFieldCurrentlyFilling", { await this.sendExtensionMessage("updateIsFieldCurrentlyFilling", {
isFieldCurrentlyFilling: true, isFieldCurrentlyFilling: true,
}); });
@@ -143,7 +143,7 @@ class AutofillInit implements AutofillInitInterface {
* in cases where the background unlock or vault item reprompt popout * in cases where the background unlock or vault item reprompt popout
* is opened. * is opened.
*/ */
private blurFocusedFieldAndRemoveInlineMenu() { private blurFocusedFieldAndCloseInlineMenu() {
this.autofillOverlayContentService?.blurMostRecentlyFocusedField(true); this.autofillOverlayContentService?.blurMostRecentlyFocusedField(true);
} }

View File

@@ -0,0 +1,24 @@
import { AutofillInlineMenuContentService } from "./autofill-inline-menu-content.service";
describe("AutofillInlineMenuContentService", () => {
let autofillInlineMenuContentService: AutofillInlineMenuContentService;
beforeEach(() => {
autofillInlineMenuContentService = new AutofillInlineMenuContentService();
});
afterEach(() => {
jest.clearAllMocks();
});
describe("isElementInlineMenu", () => {
it("returns true if the passed element is the inline menu", () => {
const element = document.createElement("div");
autofillInlineMenuContentService["listElement"] = element;
expect(autofillInlineMenuContentService.isElementInlineMenu(element)).toBe(true);
});
});
describe("extension message handlers", () => {});
});

View File

@@ -35,7 +35,7 @@ export class AutofillInlineMenuContentService implements AutofillInlineMenuConte
zIndex: "2147483647", zIndex: "2147483647",
}; };
private readonly extensionMessageHandlers: InlineMenuExtensionMessageHandlers = { private readonly extensionMessageHandlers: InlineMenuExtensionMessageHandlers = {
closeAutofillInlineMenu: ({ message }) => this.removeInlineMenu(message), closeAutofillInlineMenu: ({ message }) => this.closeInlineMenu(message),
appendAutofillInlineMenuToDom: ({ message }) => this.appendInlineMenuElements(message), appendAutofillInlineMenuToDom: ({ message }) => this.appendInlineMenuElements(message),
toggleAutofillInlineMenuHidden: ({ message }) => this.toggleInlineMenuHidden(message), toggleAutofillInlineMenuHidden: ({ message }) => this.toggleInlineMenuHidden(message),
checkIsAutofillInlineMenuButtonVisible: () => this.isInlineMenuButtonVisible(), checkIsAutofillInlineMenuButtonVisible: () => this.isInlineMenuButtonVisible(),
@@ -46,18 +46,32 @@ export class AutofillInlineMenuContentService implements AutofillInlineMenuConte
this.setupMutationObserver(); this.setupMutationObserver();
} }
/**
* Returns the message handlers for the autofill inline menu content service.
*/
get messageHandlers() { get messageHandlers() {
return this.extensionMessageHandlers; return this.extensionMessageHandlers;
} }
/**
* Identifies if the passed element corresponds to the inline menu button or list.
*
* @param element - The element being checked
*/
isElementInlineMenu(element: HTMLElement) { isElementInlineMenu(element: HTMLElement) {
return element === this.buttonElement || element === this.listElement; return element === this.buttonElement || element === this.listElement;
} }
/**
* Identifies if the inline menu button is currently visible.
*/
private isInlineMenuButtonVisible() { private isInlineMenuButtonVisible() {
return this.isButtonVisible; return this.isButtonVisible;
} }
/**
* Identifies if the inline menu list is currently visible.
*/
private isInlineMenuListVisible() { private isInlineMenuListVisible() {
return this.isListVisible; return this.isListVisible;
} }
@@ -78,27 +92,27 @@ export class AutofillInlineMenuContentService implements AutofillInlineMenuConte
* unobserve the body element to ensure the mutation observer no * unobserve the body element to ensure the mutation observer no
* longer triggers. * longer triggers.
*/ */
private removeInlineMenu = (message?: AutofillExtensionMessage) => { private closeInlineMenu = (message?: AutofillExtensionMessage) => {
if (message?.overlayElement === AutofillOverlayElement.Button) { if (message?.overlayElement === AutofillOverlayElement.Button) {
this.removeInlineMenuButton(); this.closeInlineMenuButton();
return; return;
} }
if (message?.overlayElement === AutofillOverlayElement.List) { if (message?.overlayElement === AutofillOverlayElement.List) {
this.removeInlineMenuList(); this.closeInlineMenuList();
return; return;
} }
this.removeBodyElementObserver(); this.removeBodyElementObserver();
this.removeInlineMenuButton(); this.closeInlineMenuButton();
this.removeInlineMenuList(); this.closeInlineMenuList();
}; };
/** /**
* Removes the inline menu button from the DOM if it is currently present. Will * Removes the inline menu button from the DOM if it is currently present. Will
* also remove the inline menu reposition event listeners. * also remove the inline menu reposition event listeners.
*/ */
private removeInlineMenuButton() { private closeInlineMenuButton() {
if (!this.buttonElement) { if (!this.buttonElement) {
return; return;
} }
@@ -113,7 +127,7 @@ export class AutofillInlineMenuContentService implements AutofillInlineMenuConte
/** /**
* 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 removeInlineMenuList() { private closeInlineMenuList() {
if (!this.listElement) { if (!this.listElement) {
return; return;
} }
@@ -412,7 +426,7 @@ export class AutofillInlineMenuContentService implements AutofillInlineMenuConte
clearTimeout(this.mutationObserverIterationsResetTimeout); clearTimeout(this.mutationObserverIterationsResetTimeout);
this.mutationObserverIterations = 0; this.mutationObserverIterations = 0;
void this.sendExtensionMessage("blurMostRecentlyFocusedField"); void this.sendExtensionMessage("blurMostRecentlyFocusedField");
this.removeInlineMenu(); this.closeInlineMenu();
return true; return true;
} }
@@ -420,8 +434,11 @@ export class AutofillInlineMenuContentService implements AutofillInlineMenuConte
return false; return false;
} }
/**
* Disconnects the mutation observers and removes the inline menu elements from the DOM.
*/
destroy() { destroy() {
this.documentElementMutationObserver?.disconnect(); this.documentElementMutationObserver?.disconnect();
this.removeInlineMenu(); this.closeInlineMenu();
} }
} }