1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-02-10 00:21:27 +01:00

[PM-5189] Implementing jest tests for the AutofillOverlayContentService

This commit is contained in:
Cesar Gonzalez 2024-06-07 12:55:08 -05:00
parent 938cb74e6e
commit 025327e141
No known key found for this signature in database
GPG Key ID: 3381A5457F8CCECF
2 changed files with 48 additions and 1 deletions

View File

@ -3,10 +3,11 @@ import { mock } from "jest-mock-extended";
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
import { EVENTS, AutofillOverlayVisibility } from "@bitwarden/common/autofill/constants";
import AutofillInit from "../content/autofill-init";
import { AutofillOverlayElement, RedirectFocusDirection } from "../enums/autofill-overlay.enum";
import AutofillField from "../models/autofill-field";
import { createAutofillFieldMock } from "../spec/autofill-mocks";
import { flushPromises } from "../spec/testing-utils";
import { flushPromises, sendMockExtensionMessage } from "../spec/testing-utils";
import { ElementWithOpId, FormFieldElement } from "../types";
import { AutoFillConstants } from "./autofill-constants";
@ -15,11 +16,14 @@ import { AutofillOverlayContentService } from "./autofill-overlay-content.servic
const defaultWindowReadyState = document.readyState;
const defaultDocumentVisibilityState = document.visibilityState;
describe("AutofillOverlayContentService", () => {
let autofillInit: AutofillInit;
let autofillOverlayContentService: AutofillOverlayContentService;
let sendExtensionMessageSpy: jest.SpyInstance;
beforeEach(() => {
autofillOverlayContentService = new AutofillOverlayContentService();
autofillInit = new AutofillInit(autofillOverlayContentService);
autofillInit.init();
sendExtensionMessageSpy = jest
.spyOn(autofillOverlayContentService as any, "sendExtensionMessage")
.mockResolvedValue(undefined);
@ -1312,4 +1316,46 @@ describe("AutofillOverlayContentService", () => {
);
});
});
describe("extension onMessage handlers", () => {
describe("openAutofillInlineMenu message handler", () => {
it("sends a message to the background to trigger an update in the inline menu's position", async () => {
autofillOverlayContentService["mostRecentlyFocusedField"] =
mock<ElementWithOpId<FormFieldElement>>();
sendMockExtensionMessage({
command: "openAutofillInlineMenu",
});
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("updateAutofillInlineMenuPosition", {
overlayElement: AutofillOverlayElement.Button,
});
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("updateAutofillInlineMenuPosition", {
overlayElement: AutofillOverlayElement.List,
});
});
});
describe("addNewVaultItemFromOverlay message handler", () => {
it("sends an extension message with the cipher login details to add to the user's vault", async () => {
jest
.spyOn(autofillOverlayContentService as any, "isInlineMenuListVisible")
.mockResolvedValue(true);
sendMockExtensionMessage({
command: "addNewVaultItemFromOverlay",
});
await flushPromises();
expect(sendExtensionMessageSpy).toHaveBeenCalledWith("autofillOverlayAddNewVaultItem", {
login: {
username: "",
password: "",
uri: "http://localhost/",
hostname: "localhost",
},
});
});
});
});
});

View File

@ -116,6 +116,7 @@ export class AutofillOverlayContentService implements AutofillOverlayContentServ
*/
openAutofillInlineMenu(options: OpenAutofillInlineMenuOptions = {}) {
const { isFocusingFieldElement, isOpeningFullAutofillInlineMenu, authStatus } = options;
// TODO: It's likely that this method functions more cleanly from the scope of the background. Will address this in a future PR when time allows.
if (!this.mostRecentlyFocusedField) {
return;
}