mirror of
https://github.com/bitwarden/browser.git
synced 2025-02-03 23:21:29 +01:00
[PM-5189] Adding jest tests for OverlayBackground methods
This commit is contained in:
parent
2963fc9b7d
commit
b6f3216fe2
@ -47,6 +47,7 @@ import {
|
|||||||
createAutofillPageDetailsMock,
|
createAutofillPageDetailsMock,
|
||||||
createPortSpyMock,
|
createPortSpyMock,
|
||||||
createFocusedFieldDataMock,
|
createFocusedFieldDataMock,
|
||||||
|
createPageDetailMock,
|
||||||
} from "../spec/autofill-mocks";
|
} from "../spec/autofill-mocks";
|
||||||
import {
|
import {
|
||||||
flushPromises,
|
flushPromises,
|
||||||
@ -55,6 +56,7 @@ import {
|
|||||||
triggerPortOnConnectEvent,
|
triggerPortOnConnectEvent,
|
||||||
triggerPortOnDisconnectEvent,
|
triggerPortOnDisconnectEvent,
|
||||||
triggerPortOnMessageEvent,
|
triggerPortOnMessageEvent,
|
||||||
|
triggerWebNavigationOnCommittedEvent,
|
||||||
} from "../spec/testing-utils";
|
} from "../spec/testing-utils";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@ -1281,6 +1283,33 @@ describe("OverlayBackground", () => {
|
|||||||
command: "fadeInAutofillInlineMenuIframe",
|
command: "fadeInAutofillInlineMenuIframe",
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("triggers a debounced reposition of the inline menu if the sender frame has a `null` sub frame offsets value", async () => {
|
||||||
|
jest.useFakeTimers();
|
||||||
|
const focusedFieldData = createFocusedFieldDataMock();
|
||||||
|
const sender = mock<chrome.runtime.MessageSender>({
|
||||||
|
tab: { id: focusedFieldData.tabId },
|
||||||
|
frameId: focusedFieldData.frameId,
|
||||||
|
});
|
||||||
|
sendMockExtensionMessage({ command: "updateFocusedFieldData", focusedFieldData }, sender);
|
||||||
|
overlayBackground["subFrameOffsetsForTab"][focusedFieldData.tabId] = new Map([
|
||||||
|
[focusedFieldData.frameId, null],
|
||||||
|
]);
|
||||||
|
tabsSendMessageSpy.mockImplementation();
|
||||||
|
jest.spyOn(overlayBackground as any, "repositionInlineMenu");
|
||||||
|
|
||||||
|
sendMockExtensionMessage(
|
||||||
|
{
|
||||||
|
command: "updateAutofillInlineMenuPosition",
|
||||||
|
overlayElement: AutofillOverlayElement.List,
|
||||||
|
},
|
||||||
|
sender,
|
||||||
|
);
|
||||||
|
await flushPromises();
|
||||||
|
jest.advanceTimersByTime(1000);
|
||||||
|
|
||||||
|
expect(overlayBackground["repositionInlineMenu"]).toHaveBeenCalled();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("toggleAutofillInlineMenuHidden message handler", () => {
|
describe("toggleAutofillInlineMenuHidden message handler", () => {
|
||||||
@ -1288,16 +1317,14 @@ describe("OverlayBackground", () => {
|
|||||||
await initOverlayElementPorts();
|
await initOverlayElementPorts();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns early if the display value is not provided", async () => {
|
it("returns early if the sender tab is not equal to the focused field tab", async () => {
|
||||||
const message = {
|
const sender = mock<chrome.runtime.MessageSender>({ tab: { id: 1 } });
|
||||||
command: "toggleAutofillInlineMenuHidden",
|
const focusedFieldData = createFocusedFieldDataMock({ tabId: 2 });
|
||||||
};
|
sendMockExtensionMessage({ command: "updateFocusedFieldData", focusedFieldData });
|
||||||
|
|
||||||
sendMockExtensionMessage(message);
|
sendMockExtensionMessage({ command: "toggleAutofillInlineMenuHidden" }, sender);
|
||||||
await flushPromises();
|
|
||||||
|
|
||||||
expect(buttonPortSpy.postMessage).not.toHaveBeenCalledWith(message);
|
expect(tabsSendMessageSpy).not.toHaveBeenCalled();
|
||||||
expect(listPortSpy.postMessage).not.toHaveBeenCalledWith(message);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("posts a message to the overlay button and list which hides the menu", async () => {
|
it("posts a message to the overlay button and list which hides the menu", async () => {
|
||||||
@ -1924,6 +1951,58 @@ describe("OverlayBackground", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("handle web navigation on committed events", () => {
|
||||||
|
describe("navigation event occurs in the top frame of the tab", () => {
|
||||||
|
it("removes the collected page details", async () => {
|
||||||
|
const sender = mock<chrome.webNavigation.WebNavigationFramedCallbackDetails>({
|
||||||
|
tabId: 1,
|
||||||
|
frameId: 0,
|
||||||
|
});
|
||||||
|
overlayBackground["pageDetailsForTab"][sender.tabId] = new Map([
|
||||||
|
[sender.frameId, createPageDetailMock()],
|
||||||
|
]);
|
||||||
|
|
||||||
|
triggerWebNavigationOnCommittedEvent(sender);
|
||||||
|
await flushPromises();
|
||||||
|
|
||||||
|
expect(overlayBackground["pageDetailsForTab"][sender.tabId]).toBe(undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("clears the sub frames associated with the tab", () => {
|
||||||
|
const sender = mock<chrome.webNavigation.WebNavigationFramedCallbackDetails>({
|
||||||
|
tabId: 1,
|
||||||
|
frameId: 0,
|
||||||
|
});
|
||||||
|
const subFrameId = 10;
|
||||||
|
overlayBackground["subFrameOffsetsForTab"][sender.tabId] = new Map([
|
||||||
|
[subFrameId, mock<SubFrameOffsetData>()],
|
||||||
|
]);
|
||||||
|
|
||||||
|
triggerWebNavigationOnCommittedEvent(sender);
|
||||||
|
|
||||||
|
expect(overlayBackground["subFrameOffsetsForTab"][sender.tabId]).toBe(undefined);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("navigation event occurs within sub frame", () => {
|
||||||
|
it("clears the sub frame offsets for the current frame", () => {
|
||||||
|
const sender = mock<chrome.webNavigation.WebNavigationFramedCallbackDetails>({
|
||||||
|
tabId: 1,
|
||||||
|
frameId: 1,
|
||||||
|
});
|
||||||
|
overlayBackground["subFrameOffsetsForTab"][sender.tabId] = new Map([
|
||||||
|
[sender.frameId, mock<SubFrameOffsetData>()],
|
||||||
|
]);
|
||||||
|
|
||||||
|
triggerWebNavigationOnCommittedEvent(sender);
|
||||||
|
|
||||||
|
expect(overlayBackground["subFrameOffsetsForTab"][sender.tabId].get(sender.frameId)).toBe(
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("handle port onConnect", () => {
|
describe("handle port onConnect", () => {
|
||||||
it("skips setting up the overlay port if the port connection is not for an overlay element", async () => {
|
it("skips setting up the overlay port if the port connection is not for an overlay element", async () => {
|
||||||
const port = createPortSpyMock("not-an-overlay-element");
|
const port = createPortSpyMock("not-an-overlay-element");
|
||||||
|
@ -647,7 +647,7 @@ export class OverlayBackground implements OverlayBackgroundInterface {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.cancelInlineMenuFadeIn();
|
this.cancelInlineMenuFadeInAndPositionUpdate();
|
||||||
|
|
||||||
await BrowserApi.tabSendMessage(
|
await BrowserApi.tabSendMessage(
|
||||||
sender.tab,
|
sender.tab,
|
||||||
@ -660,8 +660,7 @@ export class OverlayBackground implements OverlayBackgroundInterface {
|
|||||||
if (subFrameOffsetsForTab) {
|
if (subFrameOffsetsForTab) {
|
||||||
subFrameOffsets = subFrameOffsetsForTab.get(this.focusedFieldData.frameId);
|
subFrameOffsets = subFrameOffsetsForTab.get(this.focusedFieldData.frameId);
|
||||||
if (subFrameOffsets === null) {
|
if (subFrameOffsets === null) {
|
||||||
this.cancelUpdateInlineMenuPositionSubject.next();
|
this.repositionInlineMenuSubject.next(sender);
|
||||||
this.startUpdateInlineMenuPositionSubject.next(sender);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -114,6 +114,17 @@ export function triggerTabOnRemovedEvent(tabId: number, removeInfo: chrome.tabs.
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function triggerWebNavigationOnCommittedEvent(
|
||||||
|
details: chrome.webNavigation.WebNavigationFramedCallbackDetails,
|
||||||
|
) {
|
||||||
|
(chrome.webNavigation.onCommitted.addListener as unknown as jest.SpyInstance).mock.calls.forEach(
|
||||||
|
(call) => {
|
||||||
|
const callback = call[0];
|
||||||
|
callback(details);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function mockQuerySelectorAllDefinedCall() {
|
export function mockQuerySelectorAllDefinedCall() {
|
||||||
const originalDocumentQuerySelectorAll = document.querySelectorAll;
|
const originalDocumentQuerySelectorAll = document.querySelectorAll;
|
||||||
document.querySelectorAll = function (selector: string) {
|
document.querySelectorAll = function (selector: string) {
|
||||||
|
Loading…
Reference in New Issue
Block a user