From 171006cc354eda60fe6acead9eb8d05b56ea4c8d Mon Sep 17 00:00:00 2001 From: Cesar Gonzalez Date: Mon, 8 Apr 2024 15:32:27 -0500 Subject: [PATCH] [PM-5189] Adjusting AutofillInit jest tests --- .../autofill/content/autofill-init.spec.ts | 20 +++++++++---------- .../src/autofill/content/autofill-init.ts | 15 +++++--------- ... autofill-overlay-inline-menu-elements.ts} | 2 +- .../autofill-overlay-inline-menu-elements.ts | 4 ++-- .../dom-element-visibility.service.ts | 4 ++-- 5 files changed, 20 insertions(+), 25 deletions(-) rename apps/browser/src/autofill/overlay/abstractions/{inline-menu-elements.ts => autofill-overlay-inline-menu-elements.ts} (92%) diff --git a/apps/browser/src/autofill/content/autofill-init.spec.ts b/apps/browser/src/autofill/content/autofill-init.spec.ts index 6b8420835fa..f914b0d9c37 100644 --- a/apps/browser/src/autofill/content/autofill-init.spec.ts +++ b/apps/browser/src/autofill/content/autofill-init.spec.ts @@ -1,7 +1,8 @@ -import { mock } from "jest-mock-extended"; +import { mock, MockProxy } from "jest-mock-extended"; import AutofillPageDetails from "../models/autofill-page-details"; import AutofillScript from "../models/autofill-script"; +import { AutofillOverlayInlineMenuElements } from "../overlay/content/autofill-overlay-inline-menu-elements"; import AutofillOverlayContentService from "../services/autofill-overlay-content.service"; import { flushPromises, sendMockExtensionMessage } from "../spec/testing-utils"; @@ -9,8 +10,9 @@ import { AutofillExtensionMessage } from "./abstractions/autofill-init"; import AutofillInit from "./autofill-init"; describe("AutofillInit", () => { + let inlineMenuElements: MockProxy; + let autofillOverlayContentService: MockProxy; let autofillInit: AutofillInit; - const autofillOverlayContentService = mock(); const originalDocumentReadyState = document.readyState; let sendExtensionMessageSpy: jest.SpyInstance; @@ -20,7 +22,9 @@ describe("AutofillInit", () => { addListener: jest.fn(), }, }); - autofillInit = new AutofillInit(autofillOverlayContentService); + inlineMenuElements = mock(); + autofillOverlayContentService = mock(); + autofillInit = new AutofillInit(autofillOverlayContentService, inlineMenuElements); sendExtensionMessageSpy = jest .spyOn(autofillInit as any, "sendExtensionMessage") .mockImplementation(); @@ -164,8 +168,7 @@ describe("AutofillInit", () => { sendMockExtensionMessage(message, sender, sendResponse); await flushPromises(); - expect(chrome.runtime.sendMessage).toHaveBeenCalledWith({ - command: "collectPageDetailsResponse", + expect(sendExtensionMessageSpy).toHaveBeenCalledWith("collectPageDetailsResponse", { tab: message.tab, details: pageDetails, sender: message.sender, @@ -208,14 +211,11 @@ describe("AutofillInit", () => { }); it("skips calling the InsertAutofillContentService and does not fill the form if the url to fill is not equal to the current tab url", async () => { - const fillScript = mock(); - const message = { + sendMockExtensionMessage({ command: "fillForm", fillScript, pageDetailsUrl: "https://a-different-url.com", - }; - - sendMockExtensionMessage(message); + }); await flushPromises(); expect(autofillInit["insertAutofillContentService"].fillForm).not.toHaveBeenCalledWith( diff --git a/apps/browser/src/autofill/content/autofill-init.ts b/apps/browser/src/autofill/content/autofill-init.ts index 49a2c1bd8ac..32ab9ceb0ea 100644 --- a/apps/browser/src/autofill/content/autofill-init.ts +++ b/apps/browser/src/autofill/content/autofill-init.ts @@ -1,7 +1,7 @@ import { EVENTS } from "@bitwarden/common/autofill/constants"; import AutofillPageDetails from "../models/autofill-page-details"; -import { InlineMenuElements } from "../overlay/abstractions/inline-menu-elements"; +import { AutofillOverlayInlineMenuElements } from "../overlay/abstractions/autofill-overlay-inline-menu-elements"; import { AutofillOverlayContentService } from "../services/abstractions/autofill-overlay-content.service"; import CollectAutofillContentService from "../services/collect-autofill-content.service"; import DomElementVisibilityService from "../services/dom-element-visibility.service"; @@ -17,7 +17,7 @@ import { class AutofillInit implements AutofillInitInterface { private readonly sendExtensionMessage = sendExtensionMessage; private readonly autofillOverlayContentService: AutofillOverlayContentService | undefined; - private readonly inlineMenuElements: InlineMenuElements | undefined; + private readonly inlineMenuElements: AutofillOverlayInlineMenuElements | undefined; private readonly domElementVisibilityService: DomElementVisibilityService; private readonly collectAutofillContentService: CollectAutofillContentService; private readonly insertAutofillContentService: InsertAutofillContentService; @@ -37,7 +37,7 @@ class AutofillInit implements AutofillInitInterface { */ constructor( autofillOverlayContentService?: AutofillOverlayContentService, - inlineMenuElements?: InlineMenuElements, + inlineMenuElements?: AutofillOverlayInlineMenuElements, ) { this.autofillOverlayContentService = autofillOverlayContentService; if (this.autofillOverlayContentService) { @@ -118,8 +118,7 @@ class AutofillInit implements AutofillInitInterface { return pageDetails; } - void chrome.runtime.sendMessage({ - command: "collectPageDetailsResponse", + void this.sendExtensionMessage("collectPageDetailsResponse", { tab: message.tab, details: pageDetails, sender: message.sender, @@ -157,11 +156,7 @@ class AutofillInit implements AutofillInitInterface { * is opened. */ private blurAndRemoveOverlay() { - if (!this.autofillOverlayContentService) { - return; - } - - this.autofillOverlayContentService.blurMostRecentOverlayField(true); + this.autofillOverlayContentService?.blurMostRecentOverlayField(true); } /** diff --git a/apps/browser/src/autofill/overlay/abstractions/inline-menu-elements.ts b/apps/browser/src/autofill/overlay/abstractions/autofill-overlay-inline-menu-elements.ts similarity index 92% rename from apps/browser/src/autofill/overlay/abstractions/inline-menu-elements.ts rename to apps/browser/src/autofill/overlay/abstractions/autofill-overlay-inline-menu-elements.ts index 6b7163a3c74..5d5d457d071 100644 --- a/apps/browser/src/autofill/overlay/abstractions/inline-menu-elements.ts +++ b/apps/browser/src/autofill/overlay/abstractions/autofill-overlay-inline-menu-elements.ts @@ -9,7 +9,7 @@ export type InlineMenuExtensionMessageHandlers = { checkIsInlineMenuListVisible: () => boolean; }; -export interface InlineMenuElements { +export interface AutofillOverlayInlineMenuElements { extensionMessageHandlers: InlineMenuExtensionMessageHandlers; isElementInlineMenu(element: HTMLElement): boolean; destroy(): void; diff --git a/apps/browser/src/autofill/overlay/content/autofill-overlay-inline-menu-elements.ts b/apps/browser/src/autofill/overlay/content/autofill-overlay-inline-menu-elements.ts index 7144d95369f..41350adc697 100644 --- a/apps/browser/src/autofill/overlay/content/autofill-overlay-inline-menu-elements.ts +++ b/apps/browser/src/autofill/overlay/content/autofill-overlay-inline-menu-elements.ts @@ -7,8 +7,8 @@ import { import { AutofillOverlayElement } from "../../utils/autofill-overlay.enum"; import { InlineMenuExtensionMessageHandlers, - InlineMenuElements as InlineMenuElementsInterface, -} from "../abstractions/inline-menu-elements"; + AutofillOverlayInlineMenuElements as InlineMenuElementsInterface, +} from "../abstractions/autofill-overlay-inline-menu-elements"; import AutofillOverlayButtonIframe from "../iframe-content/autofill-overlay-button-iframe"; import AutofillOverlayListIframe from "../iframe-content/autofill-overlay-list-iframe"; diff --git a/apps/browser/src/autofill/services/dom-element-visibility.service.ts b/apps/browser/src/autofill/services/dom-element-visibility.service.ts index 7c6038edf98..b1990b39d9e 100644 --- a/apps/browser/src/autofill/services/dom-element-visibility.service.ts +++ b/apps/browser/src/autofill/services/dom-element-visibility.service.ts @@ -1,4 +1,4 @@ -import { InlineMenuElements } from "../overlay/abstractions/inline-menu-elements"; +import { AutofillOverlayInlineMenuElements } from "../overlay/abstractions/autofill-overlay-inline-menu-elements"; import { FillableFormFieldElement, FormFieldElement } from "../types"; import { DomElementVisibilityService as domElementVisibilityServiceInterface } from "./abstractions/dom-element-visibility.service"; @@ -6,7 +6,7 @@ import { DomElementVisibilityService as domElementVisibilityServiceInterface } f class DomElementVisibilityService implements domElementVisibilityServiceInterface { private cachedComputedStyle: CSSStyleDeclaration | null = null; - constructor(private inlineMenuElements?: InlineMenuElements) {} + constructor(private inlineMenuElements?: AutofillOverlayInlineMenuElements) {} /** * Checks if a form field is viewable. This is done by checking if the element is within the