1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-01-24 21:41:33 +01: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
No known key found for this signature in database
GPG Key ID: 3381A5457F8CCECF
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 () => {
const blurAndRemoveOverlaySpy = jest.spyOn(
autofillInit as any,
"blurFocusedFieldAndRemoveInlineMenu",
"blurFocusedFieldAndCloseInlineMenu",
);
sendMockExtensionMessage({
command: "fillForm",

View File

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