Skip to content

Commit

Permalink
[PM-8833] Implementing jest tests for OverlayBackground
Browse files Browse the repository at this point in the history
  • Loading branch information
cagonzalezcs committed Oct 1, 2024
1 parent 49a1dc4 commit cc1f47d
Showing 1 changed file with 99 additions and 2 deletions.
101 changes: 99 additions & 2 deletions apps/browser/src/autofill/background/overlay.background.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ import { OverlayBackground } from "./overlay.background";

describe("OverlayBackground", () => {
const generatedPassword = "generated-password";
const generatedPasswordCallbackMock = jest.fn().mockResolvedValue(generatedPassword);
const addPasswordCallbackMock = jest.fn();
const mockUserId = Utils.newGuid() as UserId;
const sendResponse = jest.fn();
let accountService: FakeAccountService;
Expand Down Expand Up @@ -193,8 +195,8 @@ describe("OverlayBackground", () => {
fido2ActiveRequestManager,
inlineMenuFieldQualificationService,
themeStateService,
() => Promise.resolve(generatedPassword),
() => Promise.resolve(),
generatedPasswordCallbackMock,
addPasswordCallbackMock,
);
portKeyForTabSpy = overlayBackground["portKeyForTab"];
pageDetailsForTabSpy = overlayBackground["pageDetailsForTab"];
Expand Down Expand Up @@ -3208,6 +3210,101 @@ describe("OverlayBackground", () => {
});
});
});

describe("refreshGeneratedPassword", () => {
it("refreshes the generated password", async () => {
overlayBackground["generatedPassword"] = "populated";

sendPortMessage(listMessageConnectorSpy, { command: "refreshGeneratedPassword", portKey });
await flushPromises();

expect(generatedPasswordCallbackMock).toHaveBeenCalled();
});

it("sends a message to the list port indicating that the generated password should be updated", async () => {
sendPortMessage(listMessageConnectorSpy, { command: "refreshGeneratedPassword", portKey });
await flushPromises();

expect(listPortSpy.postMessage).toHaveBeenCalledWith({
command: "updateAutofillInlineMenuGeneratedPassword",
generatedPassword,
refreshPassword: true,
});
});
});

describe("fillGeneratedPassword", () => {
const focusedFieldData = createFocusedFieldDataMock({
inlineMenuFillType: InlineMenuFillType.PasswordGeneration,
});

beforeEach(() => {
globalThis.structuredClone = jest.fn((value) => value);
sendMockExtensionMessage(
{
command: "updateFocusedFieldData",
focusedFieldData,
},
sender,
);
overlayBackground["generatedPassword"] = generatedPassword;
overlayBackground["pageDetailsForTab"][sender.tab.id] = new Map([
[sender.frameId, createPageDetailMock()],
]);
});

describe("skipping filling the generated password", () => {
it("skips filling when the password has not been created", () => {
overlayBackground["generatedPassword"] = "";

sendPortMessage(listMessageConnectorSpy, { command: "fillGeneratedPassword", portKey });

expect(autofillService.doAutoFill).not.toHaveBeenCalled();
});

it("skips filling when the page details for the tab are not set", () => {
overlayBackground["pageDetailsForTab"][sender.tab.id] = undefined;

sendPortMessage(listMessageConnectorSpy, { command: "fillGeneratedPassword", portKey });

expect(autofillService.doAutoFill).not.toHaveBeenCalled();
});

it("skips filling when the page details for the tab does not contain a value", () => {
overlayBackground["pageDetailsForTab"][sender.tab.id] = new Map([]);

sendPortMessage(listMessageConnectorSpy, { command: "fillGeneratedPassword", portKey });

expect(autofillService.doAutoFill).not.toHaveBeenCalled();
});
});

it("filters the page details to only include the new password fields before filling", async () => {
await flushPromises();

sendPortMessage(listMessageConnectorSpy, { command: "fillGeneratedPassword", portKey });
await flushPromises();

expect(autofillService.doAutoFill).toHaveBeenCalledWith({
tab: sender.tab,
cipher: expect.any(Object),
pageDetails: [overlayBackground["pageDetailsForTab"][sender.tab.id].get(sender.frameId)],
fillNewPassword: true,
allowTotpAutofill: false,
});
});

it("triggers a delayed open of the inline menu", async () => {
jest.useFakeTimers();
jest.spyOn(overlayBackground as any, "openInlineMenu");

sendPortMessage(listMessageConnectorSpy, { command: "fillGeneratedPassword", portKey });
await flushPromises();
jest.advanceTimersByTime(400);

expect(overlayBackground["openInlineMenu"]).toHaveBeenCalled();
});
});
});

describe("handle web navigation on committed events", () => {
Expand Down

0 comments on commit cc1f47d

Please sign in to comment.