1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-18 17:23:37 +00:00

[PM-5189] Replacing tests for AutofillOverlayPageElement to work through a public interface more explicitly

This commit is contained in:
Cesar Gonzalez
2024-04-16 11:15:39 -05:00
parent 9003bf3331
commit f63d864784
4 changed files with 58 additions and 7 deletions

View File

@@ -14,7 +14,6 @@ type InitAutofillOverlayListMessage = OverlayListMessage & {
theme: string; theme: string;
translations: Record<string, string>; translations: Record<string, string>;
ciphers?: OverlayCipherData[]; ciphers?: OverlayCipherData[];
messageConnectorUrl: string;
portKey: string; portKey: string;
}; };

View File

@@ -46,7 +46,6 @@ class AutofillOverlayButton extends AutofillOverlayPageElement {
* @param authStatus - The authentication status of the user * @param authStatus - The authentication status of the user
* @param styleSheetUrl - The URL of the stylesheet to apply to the page * @param styleSheetUrl - The URL of the stylesheet to apply to the page
* @param translations - The translations to apply to the page * @param translations - The translations to apply to the page
* @param messageConnectorUrl - The URL of the message connector to use
* @param portKey - Background generated key that allows the port to communicate with the background * @param portKey - Background generated key that allows the port to communicate with the background
*/ */
private async initAutofillOverlayButton({ private async initAutofillOverlayButton({

View File

@@ -101,7 +101,7 @@ describe("AutofillOverlayList", () => {
const originalListOfElements = const originalListOfElements =
autofillOverlayList["overlayListContainer"].querySelectorAll(".cipher-container"); autofillOverlayList["overlayListContainer"].querySelectorAll(".cipher-container");
autofillOverlayList["handleCiphersListScrollEvent"](); window.dispatchEvent(new Event("scroll"));
jest.runAllTimers(); jest.runAllTimers();
const updatedListOfElements = const updatedListOfElements =
@@ -119,11 +119,11 @@ describe("AutofillOverlayList", () => {
"handleDebouncedScrollEvent", "handleDebouncedScrollEvent",
); );
autofillOverlayList["handleCiphersListScrollEvent"](); window.dispatchEvent(new Event("scroll"));
jest.advanceTimersByTime(100); jest.advanceTimersByTime(100);
autofillOverlayList["handleCiphersListScrollEvent"](); window.dispatchEvent(new Event("scroll"));
jest.advanceTimersByTime(100); jest.advanceTimersByTime(100);
autofillOverlayList["handleCiphersListScrollEvent"](); window.dispatchEvent(new Event("scroll"));
jest.advanceTimersByTime(400); jest.advanceTimersByTime(400);
expect(handleDebouncedScrollEventSpy).toHaveBeenCalledTimes(1); expect(handleDebouncedScrollEventSpy).toHaveBeenCalledTimes(1);
@@ -377,6 +377,60 @@ describe("AutofillOverlayList", () => {
expect((firstCipherItem as HTMLElement).focus).toBeCalled(); expect((firstCipherItem as HTMLElement).focus).toBeCalled();
}); });
}); });
describe("blur event", () => {
it("posts a message to the parent window indicating that the overlay has lost focus", () => {
postWindowMessage(createInitAutofillOverlayListMessageMock({ portKey }));
globalThis.dispatchEvent(new Event("blur"));
expect(globalThis.parent.postMessage).toHaveBeenCalledWith(
{ command: "overlayPageBlurred", portKey },
"*",
);
});
});
describe("keydown event", () => {
beforeEach(() => {
postWindowMessage(createInitAutofillOverlayListMessageMock({ portKey }));
});
it("skips redirecting keyboard focus when a KeyDown event triggers and the key is not a `Tab` or `Escape` key", () => {
globalThis.document.dispatchEvent(new KeyboardEvent("keydown", { code: "test" }));
expect(globalThis.parent.postMessage).not.toHaveBeenCalled();
});
it("redirects the overlay focus out to the previous element on KeyDown of the `Tab+Shift` keys", () => {
globalThis.document.dispatchEvent(
new KeyboardEvent("keydown", { code: "Tab", shiftKey: true }),
);
expect(globalThis.parent.postMessage).toHaveBeenCalledWith(
{ command: "redirectOverlayFocusOut", direction: "previous", portKey },
"*",
);
});
it("redirects the overlay focus out to the next element on KeyDown of the `Tab` key", () => {
globalThis.document.dispatchEvent(new KeyboardEvent("keydown", { code: "Tab" }));
expect(globalThis.parent.postMessage).toHaveBeenCalledWith(
{ command: "redirectOverlayFocusOut", direction: "next", portKey },
"*",
);
});
it("redirects the overlay focus out to the current element on KeyDown of the `Escape` key", () => {
globalThis.document.dispatchEvent(new KeyboardEvent("keydown", { code: "Escape" }));
expect(globalThis.parent.postMessage).toHaveBeenCalledWith(
{ command: "redirectOverlayFocusOut", direction: "current", portKey },
"*",
);
});
});
}); });
describe("handleResizeObserver", () => { describe("handleResizeObserver", () => {

View File

@@ -204,7 +204,6 @@ function createInitAutofillOverlayListMessageMock(
styleSheetUrl: "https://jest-testing-website.com", styleSheetUrl: "https://jest-testing-website.com",
theme: ThemeType.Light, theme: ThemeType.Light,
authStatus: AuthenticationStatus.Unlocked, authStatus: AuthenticationStatus.Unlocked,
messageConnectorUrl: "https://jest-testing-website.com/message-connector",
portKey: "portKey", portKey: "portKey",
ciphers: [ ciphers: [
createAutofillOverlayCipherDataMock(1, { createAutofillOverlayCipherDataMock(1, {