Skip to content

Commit de51ebe

Browse files
committed
Migrate LandingView test to Vitest
1 parent 888dfda commit de51ebe

File tree

1 file changed

+22
-33
lines changed

1 file changed

+22
-33
lines changed

src/app/[locale]/(redesign)/(public)/LandingView.test.tsx renamed to src/app/[locale]/(redesign)/(public)/LandingView.vitest.tsx

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,39 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
import { it, expect } from "@jest/globals";
5+
import { it, expect, vi, beforeEach, type MockedFunction } from "vitest";
66
import { composeStory } from "@storybook/react";
77
import { act, render, screen, within } from "@testing-library/react";
88
import { userEvent } from "@testing-library/user-event";
9-
import { axe } from "jest-axe";
9+
import { axe } from "vitest-axe";
1010
import { signIn, useSession } from "next-auth/react";
11-
import { useTelemetry as useTelemetryImported } from "../../../hooks/useTelemetry";
11+
import { useTelemetry } from "../../../hooks/useTelemetry";
1212
import Meta, { Landing, LandingFr, LandingDe } from "./LandingView.stories";
1313
import { deleteAllCookies } from "../../../functions/client/deleteAllCookies";
1414
import { mockIsIntersecting } from "react-intersection-observer/test-utils";
1515

16-
jest.mock("next-auth/react", () => {
16+
vi.mock("next-auth/react", () => {
1717
return {
18-
signIn: jest.fn(),
19-
useSession: jest.fn(() => {
18+
signIn: vi.fn(),
19+
useSession: vi.fn(() => {
2020
return {};
2121
}),
2222
};
2323
});
24-
jest.mock("next/navigation", () => ({
24+
vi.mock("next/navigation", () => ({
2525
useSearchParams: () => ({
26-
toString: jest.fn(),
26+
toString: vi.fn(),
2727
}),
2828
}));
2929

30-
jest.mock("../../../hooks/useTelemetry");
31-
// We need to override the types of `useTelemetry` here, because otherwise
32-
// Jest infers incorrect types in `toHaveBeenCalledWith`, and throws an error.
33-
// See https://github.com/jestjs/jest/issues/15703
34-
const useTelemetry = useTelemetryImported as () => (
35-
module: string,
36-
eventName: string,
37-
data: Record<string, string>,
38-
) => void;
39-
30+
vi.mock("../../../hooks/useTelemetry");
4031
beforeEach(() => {
4132
// For reasons that are unclear to me, the mock implementation defind in the
42-
// call to `jest.mock` above forgets the implementation. I've spent way too
33+
// call to `vi.mock` above forgets the implementation. I've spent way too
4334
// long debugging that already, so I'm settling for this :(
44-
const mockedUseSession = useSession as jest.Mock;
45-
mockedUseSession.mockReturnValue({});
35+
const mockedUseSession = useSession as MockedFunction<typeof useSession>;
36+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
37+
mockedUseSession.mockReturnValue({} as any);
4638

4739
// Make the rebrand announcement banner show up by default
4840
deleteAllCookies();
@@ -85,7 +77,7 @@ it("sends telemetry when a free scan CTA is shown in the viewport", async () =>
8577

8678
// jsdom will complain about not being able to navigate to a different page
8779
// after clicking the link; suppress that error, as it's not relevant to the test:
88-
jest.spyOn(console, "error").mockImplementation(() => undefined);
80+
vi.spyOn(console, "error").mockImplementation(() => undefined);
8981

9082
// The useViewTelemetry ref is attached to the form, not the button
9183
const submitButton = screen.getAllByRole("button", {
@@ -107,10 +99,7 @@ it("sends telemetry when a free scan CTA is shown in the viewport", async () =>
10799
});
108100

109101
it("does not show a 'Sign In' button in the header if the user is signed in", () => {
110-
const mockedUseSession = useSession as jest.Mock<
111-
ReturnType<typeof useSession>,
112-
Parameters<typeof useSession>
113-
>;
102+
const mockedUseSession = useSession as MockedFunction<typeof useSession>;
114103
mockedUseSession.mockReturnValue({
115104
data: {
116105
user: {
@@ -226,10 +215,10 @@ it("shows the french scanning for exposures illustration", () => {
226215
});
227216

228217
it("does not show a confirmaton message if the user has just deleted their account", () => {
229-
global.fetch = jest.fn().mockImplementation(() =>
218+
global.fetch = vi.fn().mockImplementation(() =>
230219
Promise.resolve({
231220
success: true,
232-
json: jest.fn(() => ({
221+
json: vi.fn(() => ({
233222
flowData: null,
234223
})),
235224
}),
@@ -245,10 +234,10 @@ it("does not show a confirmaton message if the user has just deleted their accou
245234
});
246235

247236
it("shows a confirmaton message if the user has just deleted their account", () => {
248-
global.fetch = jest.fn().mockImplementation(() =>
237+
global.fetch = vi.fn().mockImplementation(() =>
249238
Promise.resolve({
250239
success: true,
251-
json: jest.fn(() => ({
240+
json: vi.fn(() => ({
252241
flowData: null,
253242
})),
254243
}),
@@ -268,10 +257,10 @@ it("shows a confirmaton message if the user has just deleted their account", ()
268257
});
269258

270259
it("hides the 'account deletion' confirmation message when the user dismisses it", async () => {
271-
global.fetch = jest.fn().mockImplementation(() =>
260+
global.fetch = vi.fn().mockImplementation(() =>
272261
Promise.resolve({
273262
success: true,
274-
json: jest.fn(() => ({
263+
json: vi.fn(() => ({
275264
flowData: null,
276265
})),
277266
}),
@@ -342,5 +331,5 @@ it("opens the see all FAQ link into a new page", async () => {
342331
// jsdom will complain about not being able to navigate to a different page
343332
// after clicking the link; suppress that error, as it's not relevant to the
344333
// test:
345-
jest.spyOn(console, "error").mockImplementationOnce(() => undefined);
334+
vi.spyOn(console, "error").mockImplementationOnce(() => undefined);
346335
});

0 commit comments

Comments
 (0)