Skip to content

Commit 3bfbaad

Browse files
committed
test: fixing tests
1 parent 508f0ec commit 3bfbaad

File tree

10 files changed

+30
-166
lines changed

10 files changed

+30
-166
lines changed

package-lock.json

Lines changed: 1 addition & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"@egjs/react-flicking": "^4.12.0",
99
"@sentry/react": "^9.14.0",
1010
"@sentry/tracing": "^7.120.3",
11+
"@types/jest": "^29.5.14",
1112
"@types/node": "^22.15.3",
1213
"@types/react": "^18.3.5",
1314
"@types/react-dom": "^18.3.0",
@@ -69,7 +70,6 @@
6970
"@testing-library/react-hooks": "^7.0.2",
7071
"@testing-library/user-event": "^14.6.0",
7172
"@types/google-map-react": "^2.1.10",
72-
"@types/jest": "^29.5.13",
7373
"@types/react-router-dom": "^5.3.3",
7474
"@types/styled-components": "^5.1.34",
7575
"@typescript-eslint/eslint-plugin": "^8.32.0",

src/2024/Speakers/Speakers2024.test.tsx

Lines changed: 13 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,23 @@ import Speakers2024 from "./Speakers2024";
44
import {
55
createMockSpeakers,
66
renderWithRouterAndQueryClient,
7-
} from "../../utils/testing/speakerTestUtils";
7+
} from "@utils/testing/speakerTestUtils";
88
import { useFetchSpeakers } from "@hooks/useFetchSpeakers";
9-
import userEvent from "@testing-library/user-event";
9+
import { useWindowSize } from "react-use";
10+
import { type MockedFunction, vi } from "vitest";
1011

11-
import { vi } from "vitest";
12-
import { gaEventTracker } from "@components/analytics/Analytics";
12+
vi.mock("@hooks/useFetchSpeakers");
1313

14-
vi.mock("../../hooks/useFetchSpeakers");
15-
16-
vi.mock("../../components/analytics/Analytics", () => ({
17-
gaEventTracker: jest.fn(),
14+
vi.mock("@components/analytics/Analytics", () => ({
15+
gaEventTracker: vi.fn(),
1816
}));
1917
vi.mock("react-use", () => ({
2018
useWindowSize: vi.fn(),
2119
}));
2220
vi.mock("@sentry/react", () => ({
23-
captureException: jest.fn(),
21+
captureException: vi.fn(),
2422
}));
25-
vi.mock("../../data/2024.json", () => ({
23+
vi.mock("@data/2024.json", () => ({
2624
hideSpeakers: false,
2725
edition: "2024",
2826
title: "DevBcn",
@@ -33,15 +31,17 @@ vi.mock("../../data/2024.json", () => ({
3331
},
3432
}));
3533

36-
const mockedUseFetchSpeakers = useFetchSpeakers as jest.MockedFunction<
34+
const mockedUseFetchSpeakers = useFetchSpeakers as MockedFunction<
3735
typeof useFetchSpeakers
3836
>;
3937

4038
describe("Speakers2024 component", () => {
4139
beforeEach(() => {
4240
vi.clearAllMocks();
43-
// eslint-disable-next-line @typescript-eslint/no-require-imports
44-
require("react-use").useWindowSize.mockReturnValue({ width: 1200 });
41+
(useWindowSize as MockedFunction<typeof useWindowSize>).mockReturnValue({
42+
width: 1200,
43+
height: 0,
44+
});
4545
});
4646

4747
it("displays loading state when data is being fetched", () => {
@@ -99,70 +99,6 @@ describe("Speakers2024 component", () => {
9999
originalModule.hideSpeakers = originalHideSpeakers;
100100
});
101101

102-
it.skip("displays CFP button when current date is within CFP period", () => {
103-
// Mock the hook to return success state with data
104-
mockedUseFetchSpeakers.mockReturnValue({
105-
data: [],
106-
isLoading: false,
107-
error: null,
108-
isSuccess: true,
109-
});
110-
111-
// Mock Date to return a date within the CFP period
112-
const originalDate = Date;
113-
const mockDate = new Date("2023-01-15");
114-
global.Date = class extends Date {
115-
constructor() {
116-
return mockDate;
117-
}
118-
119-
static now() {
120-
return mockDate.getTime();
121-
}
122-
} as unknown as typeof Date;
123-
124-
renderWithRouterAndQueryClient(<Speakers2024 />);
125-
126-
const cfpButton = screen.getByText(/Apply to be a Speaker/i);
127-
expect(cfpButton).toBeInTheDocument();
128-
129-
// Restore original Date
130-
global.Date = originalDate;
131-
});
132-
133-
it.skip("tracks CFP button clicks", async () => {
134-
// Mock the hook to return success state with data
135-
mockedUseFetchSpeakers.mockReturnValue({
136-
data: [],
137-
isLoading: false,
138-
error: null,
139-
isSuccess: true,
140-
});
141-
142-
// Mock Date to return a date within the CFP period
143-
const originalDate = Date;
144-
const mockDate = new Date("2023-01-15");
145-
global.Date = class extends Date {
146-
constructor() {
147-
return mockDate;
148-
}
149-
150-
static now() {
151-
return mockDate.getTime();
152-
}
153-
} as unknown as typeof Date;
154-
155-
renderWithRouterAndQueryClient(<Speakers2024 />);
156-
157-
const cfpButton = screen.getByText(/Apply to be a Speaker/i);
158-
await userEvent.click(cfpButton);
159-
160-
expect(gaEventTracker).toHaveBeenCalledWith("CFP", "CFP");
161-
162-
// Restore original Date
163-
global.Date = originalDate;
164-
});
165-
166102
it("calls useFetchSpeakers with the correct year", () => {
167103
// Mock the hook to return loading state
168104
mockedUseFetchSpeakers.mockReturnValue({

src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import {
4747
ROUTE_TALKS,
4848
ROUTE_TRAVEL,
4949
ROUTE_WORKSHOPS,
50-
} from "./constants/routes";
50+
} from "@constants/routes";
5151

5252
import Footer from "./components/Footer/Footer";
5353
import HomeWrapper from "./views/Home/HomeWrapper";
@@ -57,7 +57,7 @@ import SpeakerDetailContainer from "./views/SpeakerDetail/SpeakerDetailContainer
5757
import { styled } from "styled-components";
5858
import React, { FC } from "react";
5959
import { CookieConsent } from "react-cookie-consent";
60-
import { Color } from "./styles/colors";
60+
import { Color } from "@styles/colors";
6161
import Loading from "./components/Loading/Loading";
6262
import { QueryClient, QueryClientProvider } from "react-query";
6363
import Talks from "./views/Talks/Talks";

src/components/UI/Button.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Button from "./Button";
33
import { vi } from "vitest";
44

55
vi.mock("../../components/analytics/Analytics", () => ({
6-
gaEventTracker: jest.fn(),
6+
gaEventTracker: vi.fn(),
77
}));
88

99
describe("Button", () => {

src/setupTests.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
// jest-dom adds custom jest matchers for asserting on DOM nodes.
2-
// allows you to do things like:
3-
// expect(element).toHaveTextContent(/react/i)
4-
// learn more: https://github.com/testing-library/jest-dom
51
import "@testing-library/jest-dom";
6-
import { TextEncoder } from 'util';
7-
import { vi } from 'vitest';
2+
import { TextEncoder } from "util";
3+
import { vi } from "vitest";
84

95
global.TextEncoder = TextEncoder;
106
window.scrollTo = vi.fn();

src/utils/testing/speakerTestUtils.tsx

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import React, { Suspense } from "react";
22
import { BrowserRouter, Route, Routes } from "react-router";
3-
import { ISpeaker } from "../../types/speakers";
3+
import { ISpeaker } from "@types/speakers";
44
import { QueryClient, QueryClientProvider } from "react-query";
55
import { render, RenderOptions, RenderResult } from "@testing-library/react";
66

7-
// Re-export everything from testing-library
87
export * from "@testing-library/react";
98

10-
// Create mock speaker data
119
export const createMockSpeaker = (overrides = {}): ISpeaker => ({
1210
id: "1",
1311
fullName: "John Smith",
@@ -35,7 +33,6 @@ export const createMockSpeaker = (overrides = {}): ISpeaker => ({
3533
...overrides,
3634
});
3735

38-
// Create an array of mock speakers
3936
export const createMockSpeakers = (count: number): ISpeaker[] => {
4037
return Array.from({ length: count }, (_, i) =>
4138
createMockSpeaker({
@@ -47,7 +44,6 @@ export const createMockSpeakers = (count: number): ISpeaker[] => {
4744
);
4845
};
4946

50-
// Create a custom render function that includes the BrowserRouter
5147
export function renderWithRouter(
5248
ui: React.ReactElement,
5349
options?: Omit<RenderOptions, "wrapper">,
@@ -65,7 +61,6 @@ export function renderWithRouter(
6561
return render(ui, { wrapper, ...options });
6662
}
6763

68-
// Create a custom render function that includes both BrowserRouter and QueryClientProvider
6964
export function renderWithRouterAndQueryClient(
7065
ui: React.ReactElement,
7166
options?: Omit<RenderOptions, "wrapper">,
@@ -92,18 +87,3 @@ export function renderWithRouterAndQueryClient(
9287

9388
return render(ui, { wrapper, ...options });
9489
}
95-
96-
// Mock the useFetchSpeakers hook
97-
export const mockUseFetchSpeakers = (
98-
data: ISpeaker[] | null = null,
99-
isLoading = false,
100-
error: Error | null = null,
101-
isSuccess = !isLoading && !error,
102-
) => {
103-
return {
104-
data,
105-
isLoading,
106-
error,
107-
isSuccess,
108-
};
109-
};

src/views/Speakers/Speakers.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import {
88
import { useFetchSpeakers } from "../../hooks/useFetchSpeakers";
99
import userEvent from "@testing-library/user-event";
1010
import { gaEventTracker } from "../../components/analytics/Analytics";
11-
import { vi } from "vitest";
11+
import { type MockedFunction, vi } from "vitest";
1212
// Mock the useFetchSpeakers hook
1313
vi.mock("../../hooks/useFetchSpeakers");
14-
const mockedUseFetchSpeakers = useFetchSpeakers as vi.MockedFunction<
14+
const mockedUseFetchSpeakers = useFetchSpeakers as MockedFunction<
1515
typeof useFetchSpeakers
1616
>;
1717

src/views/kcd/Kcd.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import { StyledSectionsSeparator } from "../CodeOfConduct/CodeOfConduct";
1010
import { keyframes, styled } from "styled-components";
1111
import data from "@data/2024.json";
1212
import { MOBILE_BREAKPOINT } from "@constants/BreakPoints";
13-
import LessThanBlue from "../../assets/images/MoreThanBlueWhiteIcon.svg";
14-
import MoreThanBlue from "../../assets/images/LessThanBlueWhiteIcon.svg";
13+
import LessThanBlue from "@assets/images/MoreThanBlueWhiteIcon.svg";
14+
import MoreThanBlue from "@assets/images/LessThanBlueWhiteIcon.svg";
1515
import { useWindowSize } from "react-use";
16-
import youtube from "../../assets/images/youtube.svg";
17-
import linkedinIcon from "../../assets/images/linkedinIcon.svg";
18-
import twitterIcon from "../../assets/images/twitterIcon.svg";
16+
import youtube from "@assets/images/youtube.svg";
17+
import linkedinIcon from "@assets/images/linkedinIcon.svg";
18+
import twitterIcon from "@assets/images/twitterIcon.svg";
1919
import { useDocumentTitleUpdater } from "@hooks/useDocumentTitleUpdate";
2020

2121
const StyledParagraph = styled.p`

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
"@types/*": [
5454
"types/*"
5555
]
56-
}
56+
},
57+
"types": ["@testing-library/jest-dom"]
5758
},
5859
"include": [
5960
"src"

0 commit comments

Comments
 (0)