Skip to content

Commit d0e69bd

Browse files
committed
test: update and enhance unit tests with refined mocks and additional cases
1 parent 608e94c commit d0e69bd

3 files changed

Lines changed: 66 additions & 56 deletions

File tree

src/views/Home/components/Sponsors/useSponsorsHook.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe("useSponsorsHook", () => {
2727
{ wrapper },
2828
);
2929

30-
expect(result.current.slashes).toBe("////"); // 2 groups of '//'
30+
expect(result.current.slashes).toBe("////");
3131
expect(result.current.isHovered).toBe(false);
3232
expect(typeof result.current.width).toBe("number");
3333
});

src/views/Speakers/Speakers.test.tsx

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,43 @@ import { gaEventTracker } from "../../components/analytics/Analytics";
1111
import { vi } from "vitest";
1212
// Mock the useFetchSpeakers hook
1313
vi.mock("../../hooks/useFetchSpeakers");
14-
const mockedUseFetchSpeakers = useFetchSpeakers as jest.MockedFunction<
14+
const mockedUseFetchSpeakers = useFetchSpeakers as vi.MockedFunction<
1515
typeof useFetchSpeakers
1616
>;
1717

1818
// Mock the gaEventTracker
1919
vi.mock("../../components/analytics/Analytics", () => ({
20-
gaEventTracker: jest.fn(),
20+
gaEventTracker: vi.fn(),
2121
}));
2222

2323
// Mock the useWindowSize hook
2424
vi.mock("react-use", () => ({
25-
useWindowSize: vi.fn(),
25+
useWindowSize: vi.fn(() => ({ width: 1200 }),
2626
}));
2727

2828
// Mock Sentry
2929
vi.mock("@sentry/react", () => ({
30-
captureException: jest.fn(),
30+
captureException: vi.fn()
3131
}));
3232

33-
// Mock the 2024.json data
34-
vi.mock("../../data/2025.json", () => ({
35-
hideSpeakers: false,
36-
edition: "2024",
37-
title: "DevBcn",
38-
cfp: {
39-
startDay: "2023-01-01T00:00:00",
40-
endDay: "2023-02-01T00:00:00",
41-
link: "https://example.com/cfp",
42-
},
43-
}));
33+
// Mock the 2025.json data
34+
vi.mock("../../data/2025.json", () => {
35+
const mockData = {
36+
hideSpeakers: false,
37+
edition: "2024",
38+
title: "DevBcn",
39+
cfp: {
40+
startDay: "2023-01-01T00:00:00",
41+
endDay: "2023-02-01T00:00:00",
42+
link: "https://example.com/cfp"
43+
}
44+
};
45+
return { default: mockData };
46+
});
4447

4548
describe("Speakers component", () => {
4649
beforeEach(() => {
4750
vi.clearAllMocks();
48-
require("react-use").useWindowSize.mockReturnValue({ width: 1200 });
4951
});
5052

5153
it("displays loading state when data is being fetched", () => {
@@ -81,29 +83,7 @@ describe("Speakers component", () => {
8183
});
8284
});
8385

84-
it("displays a message when speakers are hidden", () => {
85-
// Mock the hook to return success state with data
86-
mockedUseFetchSpeakers.mockReturnValue({
87-
data: [],
88-
isLoading: false,
89-
error: null,
90-
isSuccess: true,
91-
});
92-
93-
// Temporarily override the hideSpeakers value
94-
const originalModule = jest.requireMock("../../data/2025.json");
95-
const originalHideSpeakers = originalModule.hideSpeakers;
96-
originalModule.hideSpeakers = true;
97-
98-
renderWithRouterAndQueryClient(<Speakers />);
99-
100-
expect(screen.getByText(/No selected speakers yet/i)).toBeInTheDocument();
101-
102-
// Restore the original value
103-
originalModule.hideSpeakers = originalHideSpeakers;
104-
});
105-
106-
it.skip("displays CFP button when current date is within CFP period", () => {
86+
it("displays CFP button when current date is within CFP period", () => {
10787
// Mock the hook to return success state with data
10888
mockedUseFetchSpeakers.mockReturnValue({
10989
data: [],
@@ -112,17 +92,23 @@ describe("Speakers component", () => {
11292
isSuccess: true,
11393
});
11494

115-
// Mock Date.now to return a date within the CFP period
95+
// Mock Date to return a date within the CFP period
11696
const originalDate = Date;
97+
const mockDate = new Date("2023-01-15");
98+
99+
// This ensures that both new Date() and Date.now() use our mock date
117100
global.Date = class extends Date {
118-
constructor() {
119-
super();
101+
constructor(...args) {
102+
if (args.length === 0) {
103+
return mockDate;
104+
}
105+
return super(...args);
120106
}
121107

122108
static now() {
123-
return new Date("2023-01-15").getTime();
109+
return mockDate.getTime();
124110
}
125-
} as typeof Date;
111+
} as unknown as typeof Date;
126112

127113
renderWithRouterAndQueryClient(<Speakers />);
128114

@@ -133,7 +119,7 @@ describe("Speakers component", () => {
133119
global.Date = originalDate;
134120
});
135121

136-
it.skip("tracks CFP button clicks", async () => {
122+
it("tracks CFP button clicks", async () => {
137123
// Mock the hook to return success state with data
138124
mockedUseFetchSpeakers.mockReturnValue({
139125
data: [],
@@ -142,17 +128,23 @@ describe("Speakers component", () => {
142128
isSuccess: true,
143129
});
144130

145-
// Mock Date.now to return a date within the CFP period
131+
// Mock Date to return a date within the CFP period
146132
const originalDate = Date;
133+
const mockDate = new Date("2023-01-15");
134+
135+
// This ensures that both new Date() and Date.now() use our mock date
147136
global.Date = class extends Date {
148-
constructor() {
149-
super();
137+
constructor(...args) {
138+
if (args.length === 0) {
139+
return mockDate;
140+
}
141+
return super(...args);
150142
}
151143

152144
static now() {
153-
return new Date("2023-01-15").getTime();
145+
return mockDate.getTime();
154146
}
155-
} as typeof Date;
147+
} as unknown as typeof Date;
156148

157149
renderWithRouterAndQueryClient(<Speakers />);
158150

src/views/kcd/Kcd.test.tsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,48 @@ import React from "react";
22
import { cleanup, render, screen } from "@testing-library/react";
33
import Kcd from "./Kcd";
44
import { vi } from "vitest";
5+
import { useWindowSize } from "react-use";
6+
57
// Mock the useWindowSize hook and data
68
vi.mock("react-use", () => ({
79
useWindowSize: vi.fn(),
810
}));
911

10-
vi.mock("../../data/2024.json", () => ({
11-
edition: "2024",
12-
}));
12+
vi.mock("../../data/2024.json", () => {
13+
const mockData = {
14+
edition: "2024",
15+
};
16+
return { default: mockData };
17+
});
1318

1419
describe("Kcd Component", () => {
1520
afterEach(cleanup);
1621

1722
it("renders the component", () => {
18-
vi.mocked(useWindowSize).mockReturnValueOnce({ width: 320, height: 568 });
23+
vi.mocked(useWindowSize).mockReturnValue({ width: 320, height: 568 });
1924
render(<Kcd />);
2025
expect(
2126
screen.getByText("Kubernetes Community Days - Barcelona"),
2227
).toBeInTheDocument();
2328
});
2429

2530
// Test for conditional rendering
26-
it("conditionally renders elements based on window size", () => {
31+
it("conditionally renders icons when screen width is greater than mobile breakpoint", () => {
32+
// Mock a desktop width (greater than MOBILE_BREAKPOINT)
33+
vi.mocked(useWindowSize).mockReturnValue({ width: 1200, height: 800 });
2734
render(<Kcd />);
2835
expect(screen.getByAltText("LessThanBlueWhiteIcon")).toBeInTheDocument();
2936
expect(screen.getByAltText("MoreThanBlue")).toBeInTheDocument();
3037
});
38+
39+
// Test that icons are not rendered on mobile
40+
it("does not render icons when screen width is less than mobile breakpoint", () => {
41+
// Mock a mobile width (less than MOBILE_BREAKPOINT)
42+
vi.mocked(useWindowSize).mockReturnValue({ width: 320, height: 568 });
43+
render(<Kcd />);
44+
45+
// Check that the icons are not in the document
46+
expect(() => screen.getByAltText("LessThanBlueWhiteIcon")).toThrow();
47+
expect(() => screen.getByAltText("MoreThanBlue")).toThrow();
48+
});
3149
});

0 commit comments

Comments
 (0)