1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 15:53:27 +00:00

[PM-5189] Implementing jest tests for the OverlayBackground

This commit is contained in:
Cesar Gonzalez
2024-06-05 12:37:17 -05:00
parent b51586f362
commit 055925e84c
2 changed files with 86 additions and 7 deletions

View File

@@ -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<chrome.runtime.MessageSender>({ 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<chrome.runtime.MessageSender>({ 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",

View File

@@ -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<boolean> {
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<boolean> {
return await BrowserApi.tabSendMessage(
sender.tab,
{ command: "checkIsAutofillInlineMenuListVisible" },