1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-11 10:10:25 +01:00

[PM-10654] Fixing broken jest tests (#10415)

This commit is contained in:
Cesar Gonzalez 2024-08-06 09:15:45 -05:00 committed by GitHub
parent 6faa9f58ed
commit 320e4f18ce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 27 additions and 17 deletions

View File

@ -61,10 +61,13 @@ describe("AutofillInit", () => {
autofillInit.init();
jest.advanceTimersByTime(250);
expect(chrome.runtime.sendMessage).toHaveBeenCalledWith({
command: "bgCollectPageDetails",
sender: "autofillInit",
});
expect(chrome.runtime.sendMessage).toHaveBeenCalledWith(
{
command: "bgCollectPageDetails",
sender: "autofillInit",
},
expect.any(Function),
);
});
it("registers a window load listener to collect the page details if the document is not in a `complete` ready state", () => {

View File

@ -37,10 +37,9 @@ describe("AutofillOverlayContentService", () => {
);
autofillInit = new AutofillInit(autofillOverlayContentService);
autofillInit.init();
sendExtensionMessageSpy = jest.spyOn(
autofillOverlayContentService as any,
"sendExtensionMessage",
);
sendExtensionMessageSpy = jest
.spyOn(autofillOverlayContentService as any, "sendExtensionMessage")
.mockResolvedValue(undefined);
Object.defineProperty(document, "readyState", {
value: defaultWindowReadyState,
writable: true,

View File

@ -16,10 +16,8 @@ describe("InlineMenuFieldQualificationService", () => {
forms: {},
fields: [],
});
chrome.runtime.sendMessage = jest.fn().mockImplementation((message) => ({
result: message.command === "getInlineMenuFieldQualificationFeatureFlag",
}));
inlineMenuFieldQualificationService = new InlineMenuFieldQualificationService();
inlineMenuFieldQualificationService["inlineMenuFieldQualificationFlagSet"] = true;
});
describe("isFieldForLoginForm", () => {

View File

@ -38,14 +38,24 @@ describe("generateRandomCustomElementName", () => {
describe("sendExtensionMessage", () => {
it("sends a message to the extension", async () => {
chrome.runtime.sendMessage = jest.fn().mockResolvedValue("sendMessageResponse");
const extensionMessagePromise = sendExtensionMessage("some-extension-message");
const response = await sendExtensionMessage("some-extension-message", { value: "value" });
// Jest doesn't give anyway to select the typed overload of "sendMessage",
// a cast is needed to get the correct spy type.
const sendMessageSpy = jest.spyOn(chrome.runtime, "sendMessage") as unknown as jest.SpyInstance<
void,
[message: string, responseCallback: (response: string) => void],
unknown
>;
expect(chrome.runtime.sendMessage).toHaveBeenCalledWith({
command: "some-extension-message",
value: "value",
});
expect(sendMessageSpy).toHaveBeenCalled();
const [latestCall] = sendMessageSpy.mock.calls;
const responseCallback = latestCall[1];
responseCallback("sendMessageResponse");
const response = await extensionMessagePromise;
expect(response).toEqual("sendMessageResponse");
});
});