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 13:04:37 -05:00
parent 055925e84c
commit 6c5a1a5748

View File

@@ -40,7 +40,7 @@ import {
createPortSpyMock, createPortSpyMock,
createFocusedFieldDataMock, createFocusedFieldDataMock,
} from "../spec/autofill-mocks"; } from "../spec/autofill-mocks";
import { flushPromises, sendMockExtensionMessage } from "../spec/testing-utils"; import { flushPromises, sendMockExtensionMessage, sendPortMessage } from "../spec/testing-utils";
import { import {
FocusedFieldData, FocusedFieldData,
@@ -76,10 +76,13 @@ describe("OverlayBackground", () => {
let subFrameOffsetsSpy: SubFrameOffsetsForTab; let subFrameOffsetsSpy: SubFrameOffsetsForTab;
let getFrameDetailsSpy: jest.SpyInstance; let getFrameDetailsSpy: jest.SpyInstance;
let tabsSendMessageSpy: jest.SpyInstance; let tabsSendMessageSpy: jest.SpyInstance;
let tabSendMessageDataSpy: jest.SpyInstance;
let sendMessageSpy: jest.SpyInstance; let sendMessageSpy: jest.SpyInstance;
let getTabFromCurrentWindowIdSpy: jest.SpyInstance; let getTabFromCurrentWindowIdSpy: jest.SpyInstance;
let buttonPortSpy: chrome.runtime.Port; let buttonPortSpy: chrome.runtime.Port;
let buttonMessageConnectorSpy: chrome.runtime.Port;
let listPortSpy: chrome.runtime.Port; let listPortSpy: chrome.runtime.Port;
let listMessageConnectorSpy: chrome.runtime.Port;
let getFrameCounter: number = 2; let getFrameCounter: number = 2;
async function initOverlayElementPorts(options = { initList: true, initButton: true }) { async function initOverlayElementPorts(options = { initList: true, initButton: true }) {
@@ -87,11 +90,17 @@ describe("OverlayBackground", () => {
if (initButton) { if (initButton) {
await overlayBackground["handlePortOnConnect"](createPortSpyMock(AutofillOverlayPort.Button)); await overlayBackground["handlePortOnConnect"](createPortSpyMock(AutofillOverlayPort.Button));
buttonPortSpy = overlayBackground["inlineMenuButtonPort"]; buttonPortSpy = overlayBackground["inlineMenuButtonPort"];
buttonMessageConnectorSpy = createPortSpyMock(AutofillOverlayPort.ButtonMessageConnector);
await overlayBackground["handlePortOnConnect"](buttonMessageConnectorSpy);
} }
if (initList) { if (initList) {
await overlayBackground["handlePortOnConnect"](createPortSpyMock(AutofillOverlayPort.List)); await overlayBackground["handlePortOnConnect"](createPortSpyMock(AutofillOverlayPort.List));
listPortSpy = overlayBackground["inlineMenuListPort"]; listPortSpy = overlayBackground["inlineMenuListPort"];
listMessageConnectorSpy = createPortSpyMock(AutofillOverlayPort.ListMessageConnector);
await overlayBackground["handlePortOnConnect"](listMessageConnectorSpy);
} }
return { buttonPortSpy, listPortSpy }; return { buttonPortSpy, listPortSpy };
@@ -149,6 +158,7 @@ describe("OverlayBackground", () => {
}); });
}); });
tabsSendMessageSpy = jest.spyOn(BrowserApi, "tabSendMessage"); tabsSendMessageSpy = jest.spyOn(BrowserApi, "tabSendMessage");
tabSendMessageDataSpy = jest.spyOn(BrowserApi, "tabSendMessageData");
sendMessageSpy = jest.spyOn(BrowserApi, "sendMessage"); sendMessageSpy = jest.spyOn(BrowserApi, "sendMessage");
getTabFromCurrentWindowIdSpy = jest.spyOn(BrowserApi, "getTabFromCurrentWindowId"); getTabFromCurrentWindowIdSpy = jest.spyOn(BrowserApi, "getTabFromCurrentWindowId");
@@ -1253,7 +1263,70 @@ describe("OverlayBackground", () => {
}); });
}); });
describe("inline menu button message handlers", () => {}); describe("inline menu button message handlers", () => {
let sender: chrome.runtime.MessageSender;
const portKey = "inlineMenuButtonPort";
let openUnlockPopoutSpy: jest.SpyInstance;
beforeEach(async () => {
sender = mock<chrome.runtime.MessageSender>({ tab: { id: 1 } });
portKeyForTabSpy[sender.tab.id] = portKey;
activeAccountStatusMock$.next(AuthenticationStatus.Unlocked);
await initOverlayElementPorts();
buttonMessageConnectorSpy.sender = sender;
openUnlockPopoutSpy = jest
.spyOn(overlayBackground as any, "openUnlockPopout")
.mockImplementation();
});
describe("autofillInlineMenuButtonClicked message handler", () => {
it("opens the unlock vault popout if the user auth status is not unlocked", async () => {
activeAccountStatusMock$.next(AuthenticationStatus.Locked);
tabsSendMessageSpy.mockImplementation();
sendPortMessage(buttonMessageConnectorSpy, {
command: "autofillInlineMenuButtonClicked",
portKey,
});
await flushPromises();
expect(tabsSendMessageSpy).toHaveBeenCalledWith(
sender.tab,
{ command: "closeInlineMenu", overlayElement: undefined },
{ frameId: 0 },
);
expect(tabSendMessageDataSpy).toBeCalledWith(
sender.tab,
"addToLockedVaultPendingNotifications",
{
commandToRetry: { message: { command: "openAutofillInlineMenu" }, sender },
target: "overlay.background",
},
);
expect(openUnlockPopoutSpy).toHaveBeenCalled();
});
it("opens the inline menu if the user auth status is unlocked", async () => {
getTabFromCurrentWindowIdSpy.mockResolvedValueOnce(sender.tab);
sendPortMessage(buttonMessageConnectorSpy, {
command: "autofillInlineMenuButtonClicked",
portKey,
});
await flushPromises();
expect(tabsSendMessageSpy).toHaveBeenCalledWith(
sender.tab,
{
command: "openAutofillInlineMenu",
isFocusingFieldElement: false,
isOpeningFullAutofillInlineMenu: true,
authStatus: AuthenticationStatus.Unlocked,
},
{ frameId: 0 },
);
});
});
});
describe("inline menu list message handlers", () => {}); describe("inline menu list message handlers", () => {});
}); });