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

Browser - Add jest-preset-angular (#9412)

* add "jest-preset-angular/setup-jest" to browser app

* use flushPromises rather than await a dummy promise

* move the import of `page-script` into beforeAll rather than the describe scope

* invoke the sendMessage callback rather than relying on a promise comparison
This commit is contained in:
Nick Krantz 2024-05-31 13:21:45 -05:00 committed by GitHub
parent fb577a448e
commit 00c305dff3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 7 deletions

View File

@ -144,7 +144,7 @@ describe("AutofillInit", () => {
.mockResolvedValue(pageDetails); .mockResolvedValue(pageDetails);
const response = await autofillInit["handleExtensionMessage"](message, sender, sendResponse); const response = await autofillInit["handleExtensionMessage"](message, sender, sendResponse);
await Promise.resolve(response); await flushPromises();
expect(response).toBe(true); expect(response).toBe(true);
expect(sendResponse).toHaveBeenCalledWith(pageDetails); expect(sendResponse).toHaveBeenCalledWith(pageDetails);

View File

@ -37,14 +37,29 @@ describe("generateRandomCustomElementName", () => {
}); });
describe("sendExtensionMessage", () => { describe("sendExtensionMessage", () => {
it("sends a message to the extention", () => { it("sends a message to the extension", async () => {
const extensionMessageResponse = sendExtensionMessage("updateAutofillOverlayHidden", { const extensionMessagePromise = sendExtensionMessage("updateAutofillOverlayHidden", {
display: "none", display: "none",
}); });
jest.spyOn(chrome.runtime, "sendMessage");
expect(chrome.runtime.sendMessage).toHaveBeenCalled(); // Jest doesn't give anyway to select the typed overload of "sendMessage",
expect(extensionMessageResponse).toEqual(Promise.resolve({})); // 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(sendMessageSpy).toHaveBeenCalled();
const [latestCall] = sendMessageSpy.mock.calls;
const responseCallback = latestCall[1];
responseCallback("sendMessageResponse");
const response = await extensionMessagePromise;
expect(response).toEqual("sendMessageResponse");
}); });
}); });

View File

@ -53,7 +53,9 @@ describe("Fido2 page script with native WebAuthn support", () => {
const mockCredentialAssertResult = createAssertCredentialResultMock(); const mockCredentialAssertResult = createAssertCredentialResultMock();
setupMockedWebAuthnSupport(); setupMockedWebAuthnSupport();
beforeAll(() => {
require("./page-script"); require("./page-script");
});
afterEach(() => { afterEach(() => {
jest.resetModules(); jest.resetModules();

View File

@ -1,3 +1,5 @@
import "jest-preset-angular/setup-jest";
// Add chrome storage api // Add chrome storage api
const QUOTA_BYTES = 10; const QUOTA_BYTES = 10;
const storage = { const storage = {