1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-02-06 23:51:28 +01: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
No known key found for this signature in database
GPG Key ID: 3381A5457F8CCECF
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 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<AutofillOverlayInlineMenuElements>;
let autofillOverlayContentService: MockProxy<AutofillOverlayContentService>;
let autofillInit: AutofillInit;
const autofillOverlayContentService = mock<AutofillOverlayContentService>();
const originalDocumentReadyState = document.readyState;
let sendExtensionMessageSpy: jest.SpyInstance;
@ -20,7 +22,9 @@ describe("AutofillInit", () => {
addListener: jest.fn(),
},
});
autofillInit = new AutofillInit(autofillOverlayContentService);
inlineMenuElements = mock<AutofillOverlayInlineMenuElements>();
autofillOverlayContentService = mock<AutofillOverlayContentService>();
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<AutofillScript>();
const message = {
sendMockExtensionMessage({
command: "fillForm",
fillScript,
pageDetailsUrl: "https://a-different-url.com",
};
sendMockExtensionMessage(message);
});
await flushPromises();
expect(autofillInit["insertAutofillContentService"].fillForm).not.toHaveBeenCalledWith(

View File

@ -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);
}
/**

View File

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

View File

@ -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";

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 { 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