From 055925e84c69e6f23ca9f5c81ba10ea525d5046e Mon Sep 17 00:00:00 2001 From: Cesar Gonzalez Date: Wed, 5 Jun 2024 12:37:17 -0500 Subject: [PATCH] [PM-5189] Implementing jest tests for the OverlayBackground --- .../background/overlay.background.spec.ts | 82 ++++++++++++++++++- .../autofill/background/overlay.background.ts | 11 +-- 2 files changed, 86 insertions(+), 7 deletions(-) diff --git a/apps/browser/src/autofill/background/overlay.background.spec.ts b/apps/browser/src/autofill/background/overlay.background.spec.ts index f8b1e657aac..ab01b6e7210 100644 --- a/apps/browser/src/autofill/background/overlay.background.spec.ts +++ b/apps/browser/src/autofill/background/overlay.background.spec.ts @@ -794,7 +794,7 @@ describe("OverlayBackground", () => { command: "openAutofillInlineMenu", isFocusingFieldElement: false, isOpeningFullAutofillInlineMenu: false, - authStatus: 2, + authStatus: AuthenticationStatus.Unlocked, }, { frameId: 0 }, ); @@ -814,7 +814,7 @@ describe("OverlayBackground", () => { command: "openAutofillInlineMenu", isFocusingFieldElement: false, isOpeningFullAutofillInlineMenu: false, - authStatus: 2, + authStatus: AuthenticationStatus.Unlocked, }, { frameId: 10 }, ); @@ -1154,6 +1154,84 @@ describe("OverlayBackground", () => { }); }); + describe("checkIsAutofillInlineMenuButtonVisible", () => { + it("sends a message to the top frame of the tab to identify if the inline menu button is visible", () => { + const sender = mock({ tab: { id: 1 } }); + + sendMockExtensionMessage({ command: "checkIsAutofillInlineMenuButtonVisible" }, sender); + + expect(tabsSendMessageSpy).toHaveBeenCalledWith( + sender.tab, + { command: "checkIsAutofillInlineMenuButtonVisible" }, + { frameId: 0 }, + ); + }); + }); + + describe("checkIsAutofillInlineMenuListVisible", () => { + it("sends a message to the top frame of the tab to identify if the inline menu list is visible", () => { + const sender = mock({ tab: { id: 1 } }); + + sendMockExtensionMessage({ command: "checkIsAutofillInlineMenuListVisible" }, sender); + + expect(tabsSendMessageSpy).toHaveBeenCalledWith( + sender.tab, + { command: "checkIsAutofillInlineMenuListVisible" }, + { frameId: 0 }, + ); + }); + }); + + describe("unlockCompleted", () => { + let updateOverlayCiphersSpy: jest.SpyInstance; + + beforeEach(async () => { + updateOverlayCiphersSpy = jest.spyOn(overlayBackground, "updateOverlayCiphers"); + await initOverlayElementPorts(); + }); + + it("updates the inline menu button auth status", async () => { + sendMockExtensionMessage({ command: "unlockCompleted" }); + await flushPromises(); + + expect(buttonPortSpy.postMessage).toHaveBeenCalledWith({ + command: "updateInlineMenuButtonAuthStatus", + authStatus: AuthenticationStatus.Unlocked, + }); + }); + + it("updates the overlay ciphers", async () => { + const updateOverlayCiphersSpy = jest.spyOn(overlayBackground, "updateOverlayCiphers"); + sendMockExtensionMessage({ command: "unlockCompleted" }); + await flushPromises(); + + expect(updateOverlayCiphersSpy).toHaveBeenCalled(); + }); + + it("opens the inline menu if a retry command is present in the message", async () => { + updateOverlayCiphersSpy.mockImplementation(); + getTabFromCurrentWindowIdSpy.mockResolvedValueOnce(createChromeTabMock({ id: 1 })); + sendMockExtensionMessage({ + command: "unlockCompleted", + data: { + commandToRetry: { message: { command: "openAutofillInlineMenu" } }, + }, + }); + await flushPromises(); + + expect(tabsSendMessageSpy).toHaveBeenCalledWith( + expect.any(Object), + { + command: "openAutofillInlineMenu", + isFocusingFieldElement: true, + isOpeningFullAutofillInlineMenu: false, + authStatus: AuthenticationStatus.Unlocked, + }, + { frameId: 0 }, + ); + }); + }); + describe("extension messages that trigger an update of the inline menu ciphers", () => { const extensionMessages = [ "addedCipher", diff --git a/apps/browser/src/autofill/background/overlay.background.ts b/apps/browser/src/autofill/background/overlay.background.ts index 1cf27dc9dc8..d514e8b201d 100644 --- a/apps/browser/src/autofill/background/overlay.background.ts +++ b/apps/browser/src/autofill/background/overlay.background.ts @@ -140,8 +140,6 @@ export class OverlayBackground implements OverlayBackgroundInterface { this.setupExtensionMessageListeners(); const env = await firstValueFrom(this.environmentService.environment$); this.iconsServerUrl = env.getIconsUrl(); - await this.getInlineMenuVisibility(); - await this.getAuthStatus(); } /** @@ -836,7 +834,6 @@ export class OverlayBackground implements OverlayBackgroundInterface { * @param message - Extension message received from the `unlockCompleted` command */ private async unlockCompleted(message: OverlayBackgroundExtensionMessage) { - await this.getAuthStatus(); await this.updateInlineMenuButtonAuthStatus(); await this.updateOverlayCiphers(); @@ -985,7 +982,9 @@ export class OverlayBackground implements OverlayBackgroundInterface { * * @param sender - The sender of the message */ - private async checkIsAutofillInlineMenuButtonVisible(sender: chrome.runtime.MessageSender) { + private async checkIsAutofillInlineMenuButtonVisible( + sender: chrome.runtime.MessageSender, + ): Promise { return await BrowserApi.tabSendMessage( sender.tab, { command: "checkIsAutofillInlineMenuButtonVisible" }, @@ -998,7 +997,9 @@ export class OverlayBackground implements OverlayBackgroundInterface { * * @param sender - The sender of the message */ - private async checkIsAutofillInlineMenuListVisible(sender: chrome.runtime.MessageSender) { + private async checkIsAutofillInlineMenuListVisible( + sender: chrome.runtime.MessageSender, + ): Promise { return await BrowserApi.tabSendMessage( sender.tab, { command: "checkIsAutofillInlineMenuListVisible" },