1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-18 17:23:37 +00:00

[PM-5189] Adjusting AutofillInit jest tests

This commit is contained in:
Cesar Gonzalez
2024-04-08 15:32:27 -05:00
parent f1e90edb9e
commit 171006cc35
5 changed files with 20 additions and 25 deletions

View File

@@ -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 AutofillPageDetails from "../models/autofill-page-details";
import AutofillScript from "../models/autofill-script"; 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 AutofillOverlayContentService from "../services/autofill-overlay-content.service";
import { flushPromises, sendMockExtensionMessage } from "../spec/testing-utils"; import { flushPromises, sendMockExtensionMessage } from "../spec/testing-utils";
@@ -9,8 +10,9 @@ import { AutofillExtensionMessage } from "./abstractions/autofill-init";
import AutofillInit from "./autofill-init"; import AutofillInit from "./autofill-init";
describe("AutofillInit", () => { describe("AutofillInit", () => {
let inlineMenuElements: MockProxy<AutofillOverlayInlineMenuElements>;
let autofillOverlayContentService: MockProxy<AutofillOverlayContentService>;
let autofillInit: AutofillInit; let autofillInit: AutofillInit;
const autofillOverlayContentService = mock<AutofillOverlayContentService>();
const originalDocumentReadyState = document.readyState; const originalDocumentReadyState = document.readyState;
let sendExtensionMessageSpy: jest.SpyInstance; let sendExtensionMessageSpy: jest.SpyInstance;
@@ -20,7 +22,9 @@ describe("AutofillInit", () => {
addListener: jest.fn(), addListener: jest.fn(),
}, },
}); });
autofillInit = new AutofillInit(autofillOverlayContentService); inlineMenuElements = mock<AutofillOverlayInlineMenuElements>();
autofillOverlayContentService = mock<AutofillOverlayContentService>();
autofillInit = new AutofillInit(autofillOverlayContentService, inlineMenuElements);
sendExtensionMessageSpy = jest sendExtensionMessageSpy = jest
.spyOn(autofillInit as any, "sendExtensionMessage") .spyOn(autofillInit as any, "sendExtensionMessage")
.mockImplementation(); .mockImplementation();
@@ -164,8 +168,7 @@ describe("AutofillInit", () => {
sendMockExtensionMessage(message, sender, sendResponse); sendMockExtensionMessage(message, sender, sendResponse);
await flushPromises(); await flushPromises();
expect(chrome.runtime.sendMessage).toHaveBeenCalledWith({ expect(sendExtensionMessageSpy).toHaveBeenCalledWith("collectPageDetailsResponse", {
command: "collectPageDetailsResponse",
tab: message.tab, tab: message.tab,
details: pageDetails, details: pageDetails,
sender: message.sender, 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 () => { 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<AutofillScript>(); sendMockExtensionMessage({
const message = {
command: "fillForm", command: "fillForm",
fillScript, fillScript,
pageDetailsUrl: "https://a-different-url.com", pageDetailsUrl: "https://a-different-url.com",
}; });
sendMockExtensionMessage(message);
await flushPromises(); await flushPromises();
expect(autofillInit["insertAutofillContentService"].fillForm).not.toHaveBeenCalledWith( expect(autofillInit["insertAutofillContentService"].fillForm).not.toHaveBeenCalledWith(

View File

@@ -1,7 +1,7 @@
import { EVENTS } from "@bitwarden/common/autofill/constants"; import { EVENTS } from "@bitwarden/common/autofill/constants";
import AutofillPageDetails from "../models/autofill-page-details"; 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 { AutofillOverlayContentService } from "../services/abstractions/autofill-overlay-content.service";
import CollectAutofillContentService from "../services/collect-autofill-content.service"; import CollectAutofillContentService from "../services/collect-autofill-content.service";
import DomElementVisibilityService from "../services/dom-element-visibility.service"; import DomElementVisibilityService from "../services/dom-element-visibility.service";
@@ -17,7 +17,7 @@ import {
class AutofillInit implements AutofillInitInterface { class AutofillInit implements AutofillInitInterface {
private readonly sendExtensionMessage = sendExtensionMessage; private readonly sendExtensionMessage = sendExtensionMessage;
private readonly autofillOverlayContentService: AutofillOverlayContentService | undefined; private readonly autofillOverlayContentService: AutofillOverlayContentService | undefined;
private readonly inlineMenuElements: InlineMenuElements | undefined; private readonly inlineMenuElements: AutofillOverlayInlineMenuElements | undefined;
private readonly domElementVisibilityService: DomElementVisibilityService; private readonly domElementVisibilityService: DomElementVisibilityService;
private readonly collectAutofillContentService: CollectAutofillContentService; private readonly collectAutofillContentService: CollectAutofillContentService;
private readonly insertAutofillContentService: InsertAutofillContentService; private readonly insertAutofillContentService: InsertAutofillContentService;
@@ -37,7 +37,7 @@ class AutofillInit implements AutofillInitInterface {
*/ */
constructor( constructor(
autofillOverlayContentService?: AutofillOverlayContentService, autofillOverlayContentService?: AutofillOverlayContentService,
inlineMenuElements?: InlineMenuElements, inlineMenuElements?: AutofillOverlayInlineMenuElements,
) { ) {
this.autofillOverlayContentService = autofillOverlayContentService; this.autofillOverlayContentService = autofillOverlayContentService;
if (this.autofillOverlayContentService) { if (this.autofillOverlayContentService) {
@@ -118,8 +118,7 @@ class AutofillInit implements AutofillInitInterface {
return pageDetails; return pageDetails;
} }
void chrome.runtime.sendMessage({ void this.sendExtensionMessage("collectPageDetailsResponse", {
command: "collectPageDetailsResponse",
tab: message.tab, tab: message.tab,
details: pageDetails, details: pageDetails,
sender: message.sender, sender: message.sender,
@@ -157,11 +156,7 @@ class AutofillInit implements AutofillInitInterface {
* is opened. * is opened.
*/ */
private blurAndRemoveOverlay() { private blurAndRemoveOverlay() {
if (!this.autofillOverlayContentService) { this.autofillOverlayContentService?.blurMostRecentOverlayField(true);
return;
}
this.autofillOverlayContentService.blurMostRecentOverlayField(true);
} }
/** /**

View File

@@ -9,7 +9,7 @@ export type InlineMenuExtensionMessageHandlers = {
checkIsInlineMenuListVisible: () => boolean; checkIsInlineMenuListVisible: () => boolean;
}; };
export interface InlineMenuElements { export interface AutofillOverlayInlineMenuElements {
extensionMessageHandlers: InlineMenuExtensionMessageHandlers; extensionMessageHandlers: InlineMenuExtensionMessageHandlers;
isElementInlineMenu(element: HTMLElement): boolean; isElementInlineMenu(element: HTMLElement): boolean;
destroy(): void; destroy(): void;

View File

@@ -7,8 +7,8 @@ import {
import { AutofillOverlayElement } from "../../utils/autofill-overlay.enum"; import { AutofillOverlayElement } from "../../utils/autofill-overlay.enum";
import { import {
InlineMenuExtensionMessageHandlers, InlineMenuExtensionMessageHandlers,
InlineMenuElements as InlineMenuElementsInterface, AutofillOverlayInlineMenuElements as InlineMenuElementsInterface,
} from "../abstractions/inline-menu-elements"; } from "../abstractions/autofill-overlay-inline-menu-elements";
import AutofillOverlayButtonIframe from "../iframe-content/autofill-overlay-button-iframe"; import AutofillOverlayButtonIframe from "../iframe-content/autofill-overlay-button-iframe";
import AutofillOverlayListIframe from "../iframe-content/autofill-overlay-list-iframe"; import AutofillOverlayListIframe from "../iframe-content/autofill-overlay-list-iframe";

View File

@@ -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 { FillableFormFieldElement, FormFieldElement } from "../types";
import { DomElementVisibilityService as domElementVisibilityServiceInterface } from "./abstractions/dom-element-visibility.service"; import { DomElementVisibilityService as domElementVisibilityServiceInterface } from "./abstractions/dom-element-visibility.service";
@@ -6,7 +6,7 @@ import { DomElementVisibilityService as domElementVisibilityServiceInterface } f
class DomElementVisibilityService implements domElementVisibilityServiceInterface { class DomElementVisibilityService implements domElementVisibilityServiceInterface {
private cachedComputedStyle: CSSStyleDeclaration | null = null; 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 * Checks if a form field is viewable. This is done by checking if the element is within the