1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-01-09 19:28:06 +01:00

[PM-5189] Working through jest tests for the AutofillOverlayContentService

This commit is contained in:
Cesar Gonzalez 2024-06-07 14:31:19 -05:00
parent e3510a9a05
commit c5169c96ee
No known key found for this signature in database
GPG Key ID: 3381A5457F8CCECF

View File

@ -120,6 +120,7 @@ describe("AutofillOverlayContentService", () => {
) as ElementWithOpId<FormFieldElement>; ) as ElementWithOpId<FormFieldElement>;
autofillFieldElement.opid = "op-1"; autofillFieldElement.opid = "op-1";
jest.spyOn(autofillFieldElement, "addEventListener"); jest.spyOn(autofillFieldElement, "addEventListener");
jest.spyOn(autofillFieldElement, "removeEventListener");
autofillFieldData = createAutofillFieldMock({ autofillFieldData = createAutofillFieldMock({
opid: "username-field", opid: "username-field",
form: "validFormId", form: "validFormId",
@ -425,6 +426,23 @@ describe("AutofillOverlayContentService", () => {
expect(autofillOverlayContentService["storeModifiedFormElement"]).not.toHaveBeenCalled(); expect(autofillOverlayContentService["storeModifiedFormElement"]).not.toHaveBeenCalled();
}); });
it("sets the field as the most recently focused form field element", async () => {
autofillOverlayContentService["mostRecentlyFocusedField"] =
mock<ElementWithOpId<FormFieldElement>>();
await autofillOverlayContentService.setupAutofillInlineMenuListenerOnField(
autofillFieldElement,
autofillFieldData,
);
autofillFieldElement.dispatchEvent(new Event("input"));
await flushPromises();
expect(autofillOverlayContentService["mostRecentlyFocusedField"]).toEqual(
autofillFieldElement,
);
});
it("stores the field as a user filled field if the form field data indicates that it is for a username", async () => { it("stores the field as a user filled field if the form field data indicates that it is for a username", async () => {
await autofillOverlayContentService.setupAutofillInlineMenuListenerOnField( await autofillOverlayContentService.setupAutofillInlineMenuListenerOnField(
autofillFieldElement, autofillFieldElement,
@ -719,6 +737,70 @@ describe("AutofillOverlayContentService", () => {
}); });
}); });
}); });
describe("hidden form field focus event", () => {
it("sets up the inline menu listeners if the autofill field data is in the cache", async () => {
autofillFieldData.viewable = false;
await autofillOverlayContentService.setupAutofillInlineMenuListenerOnField(
autofillFieldElement,
autofillFieldData,
);
autofillFieldElement.dispatchEvent(new Event("focus"));
await flushPromises();
expect(autofillFieldElement.addEventListener).toHaveBeenCalledWith(
EVENTS.BLUR,
expect.any(Function),
);
expect(autofillFieldElement.addEventListener).toHaveBeenCalledWith(
EVENTS.KEYUP,
expect.any(Function),
);
expect(autofillFieldElement.addEventListener).toHaveBeenCalledWith(
EVENTS.INPUT,
expect.any(Function),
);
expect(autofillFieldElement.addEventListener).toHaveBeenCalledWith(
EVENTS.CLICK,
expect.any(Function),
);
expect(autofillFieldElement.addEventListener).toHaveBeenCalledWith(
EVENTS.FOCUS,
expect.any(Function),
);
expect(autofillFieldElement.removeEventListener).toHaveBeenCalled();
});
it("skips setting up the inline menu listeners if the autofill field data is not in the cache", async () => {
autofillFieldData.viewable = false;
await autofillOverlayContentService.setupAutofillInlineMenuListenerOnField(
autofillFieldElement,
autofillFieldData,
);
autofillOverlayContentService["formFieldElements"].delete(autofillFieldElement);
autofillFieldElement.dispatchEvent(new Event("focus"));
expect(autofillFieldElement.addEventListener).not.toHaveBeenCalledWith(
EVENTS.BLUR,
expect.any(Function),
);
expect(autofillFieldElement.addEventListener).not.toHaveBeenCalledWith(
EVENTS.KEYUP,
expect.any(Function),
);
expect(autofillFieldElement.addEventListener).not.toHaveBeenCalledWith(
EVENTS.INPUT,
expect.any(Function),
);
expect(autofillFieldElement.addEventListener).not.toHaveBeenCalledWith(
EVENTS.CLICK,
expect.any(Function),
);
expect(autofillFieldElement.removeEventListener).toHaveBeenCalled();
});
});
}); });
it("triggers the form field focused handler if the current active element in the document is the passed form field", async () => { it("triggers the form field focused handler if the current active element in the document is the passed form field", async () => {
@ -1476,5 +1558,24 @@ describe("AutofillOverlayContentService", () => {
); );
}); });
}); });
describe("checkMostRecentlyFocusedFieldHasValue", () => {
it("returns true if the most recently focused field has a truthy value", async () => {
autofillOverlayContentService["mostRecentlyFocusedField"] = mock<
ElementWithOpId<FormFieldElement>
>({ value: "test" });
sendMockExtensionMessage(
{
command: "checkMostRecentlyFocusedFieldHasValue",
},
mock<chrome.runtime.MessageSender>(),
sendResponseSpy,
);
await flushPromises();
expect(sendResponseSpy).toHaveBeenCalledWith(true);
});
});
}); });
}); });