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

[PM-5189] Working through jest tests for OverlayBackground

This commit is contained in:
Cesar Gonzalez 2024-06-04 17:47:04 -05:00
parent 07713511e4
commit 4ecbfb3d49
No known key found for this signature in database
GPG Key ID: 3381A5457F8CCECF
2 changed files with 124 additions and 26 deletions

View File

@ -30,6 +30,7 @@ import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.servi
import { BrowserApi } from "../../platform/browser/browser-api";
import { BrowserPlatformUtilsService } from "../../platform/services/platform-utils/browser-platform-utils.service";
import { AutofillOverlayElement } from "../enums/autofill-overlay.enum";
import { AutofillService } from "../services/abstractions/autofill.service";
import { createChromeTabMock, createAutofillPageDetailsMock } from "../spec/autofill-mocks";
import { flushPromises, sendMockExtensionMessage } from "../spec/testing-utils";
@ -259,7 +260,6 @@ describe("OverlayBackground", () => {
});
describe("re-positioning the inline menu within sub frames", () => {
describe("rebuildSubFrameOffsets", () => {
const tabId = 1;
const topFrameId = 0;
const middleFrameId = 10;
@ -286,6 +286,7 @@ describe("OverlayBackground", () => {
);
});
describe("rebuildSubFrameOffsets", () => {
it("skips rebuilding sub frame offsets if the sender contains the currently focused field", () => {
const sender = mock<chrome.runtime.MessageSender>({ tab, frameId: bottomFrameId });
@ -318,7 +319,7 @@ describe("OverlayBackground", () => {
it("triggers an update of the inline menu position after rebuilding sub frames", async () => {
jest.useFakeTimers();
overlayBackground["updateInlineMenuPositionTimeout"] = 1;
overlayBackground["updateInlineMenuPositionTimeout"] = setTimeout(jest.fn, 650);
const sender = mock<chrome.runtime.MessageSender>({ tab, frameId: middleFrameId });
jest.spyOn(overlayBackground as any, "updateInlineMenuPositionAfterSubFrameRebuild");
@ -332,6 +333,96 @@ describe("OverlayBackground", () => {
});
});
describe("updateInlineMenuPositionAfterSubFrameRebuild", () => {});
describe("updateInlineMenuPositionAfterSubFrameRebuild", () => {
let sender: chrome.runtime.MessageSender;
async function flushInlineMenuUpdatePromises() {
await flushPromises();
jest.advanceTimersByTime(650);
await flushPromises();
}
beforeEach(() => {
sender = mock<chrome.runtime.MessageSender>({ tab, frameId: middleFrameId });
jest.useFakeTimers();
overlayBackground["isFieldCurrentlyFocused"] = true;
});
it("skips updating the position of either inline menu element if a field is not currently focused", async () => {
overlayBackground["isFieldCurrentlyFocused"] = false;
sendMockExtensionMessage({ command: "rebuildSubFrameOffsets" }, sender);
await flushInlineMenuUpdatePromises();
expect(tabsSendMessageSpy).not.toHaveBeenCalledWith(
sender.tab,
{
command: "appendInlineMenuElementsToDom",
overlayElement: AutofillOverlayElement.Button,
},
{ frameId: 0 },
);
expect(tabsSendMessageSpy).not.toHaveBeenCalledWith(
sender.tab,
{
command: "appendInlineMenuElementsToDom",
overlayElement: AutofillOverlayElement.List,
},
{ frameId: 0 },
);
});
it("updates the position of the inline menu elements", async () => {
sendMockExtensionMessage({ command: "rebuildSubFrameOffsets" }, sender);
await flushInlineMenuUpdatePromises();
expect(tabsSendMessageSpy).toHaveBeenCalledWith(
sender.tab,
{
command: "appendInlineMenuElementsToDom",
overlayElement: AutofillOverlayElement.Button,
},
{ frameId: 0 },
);
expect(tabsSendMessageSpy).toHaveBeenCalledWith(
sender.tab,
{
command: "appendInlineMenuElementsToDom",
overlayElement: AutofillOverlayElement.List,
},
{ frameId: 0 },
);
});
it("skips updating the inline menu list if the focused field has a value and the user status is not unlocked", async () => {
overlayBackground["userAuthStatus"] = AuthenticationStatus.Locked;
tabsSendMessageSpy.mockImplementation((_tab, message, _options) => {
if (message.command === "checkMostRecentlyFocusedFieldHasValue") {
return Promise.resolve(true);
}
return Promise.resolve();
});
sendMockExtensionMessage({ command: "rebuildSubFrameOffsets" }, sender);
await flushInlineMenuUpdatePromises();
expect(tabsSendMessageSpy).toHaveBeenCalledWith(
sender.tab,
{
command: "appendInlineMenuElementsToDom",
overlayElement: AutofillOverlayElement.Button,
},
{ frameId: 0 },
);
expect(tabsSendMessageSpy).not.toHaveBeenCalledWith(
sender.tab,
{
command: "appendInlineMenuElementsToDom",
overlayElement: AutofillOverlayElement.List,
},
{ frameId: 0 },
);
});
});
});
});

View File

@ -371,6 +371,13 @@ export class OverlayBackground implements OverlayBackgroundInterface {
);
}
/**
* Handles updating the inline menu's position after rebuilding the sub frames
* for the provided tab. Will skip repositioning the inline menu if the field
* is not currently focused, or if the focused field has a value.
*
* @param sender - The sender of the message
*/
private async updateInlineMenuPositionAfterSubFrameRebuild(sender: chrome.runtime.MessageSender) {
if (!this.isFieldCurrentlyFocused) {
return;