1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-02-06 23:51:28 +01: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
No known key found for this signature in database
GPG Key ID: 3381A5457F8CCECF
4 changed files with 58 additions and 7 deletions

View File

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

View File

@ -46,7 +46,6 @@ class AutofillOverlayButton extends AutofillOverlayPageElement {
* @param authStatus - The authentication status of the user
* @param styleSheetUrl - The URL of the stylesheet 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
*/
private async initAutofillOverlayButton({

View File

@ -101,7 +101,7 @@ describe("AutofillOverlayList", () => {
const originalListOfElements =
autofillOverlayList["overlayListContainer"].querySelectorAll(".cipher-container");
autofillOverlayList["handleCiphersListScrollEvent"]();
window.dispatchEvent(new Event("scroll"));
jest.runAllTimers();
const updatedListOfElements =
@ -119,11 +119,11 @@ describe("AutofillOverlayList", () => {
"handleDebouncedScrollEvent",
);
autofillOverlayList["handleCiphersListScrollEvent"]();
window.dispatchEvent(new Event("scroll"));
jest.advanceTimersByTime(100);
autofillOverlayList["handleCiphersListScrollEvent"]();
window.dispatchEvent(new Event("scroll"));
jest.advanceTimersByTime(100);
autofillOverlayList["handleCiphersListScrollEvent"]();
window.dispatchEvent(new Event("scroll"));
jest.advanceTimersByTime(400);
expect(handleDebouncedScrollEventSpy).toHaveBeenCalledTimes(1);
@ -377,6 +377,60 @@ describe("AutofillOverlayList", () => {
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", () => {

View File

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