Skip to content

Commit 5c20e64

Browse files
committed
test: mock API calls
1 parent 35aa3e9 commit 5c20e64

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

src/App.test.tsx

+69
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,77 @@ import { BrowserRouter, Route, Routes } from "react-router";
33
import App from "./App";
44
import React from "react";
55
import userEvent from "@testing-library/user-event";
6+
import { IResponse } from "./types/speakers";
7+
import axios, { AxiosHeaders, AxiosResponse } from "axios";
8+
9+
jest.mock("axios");
10+
const mockedAxios = axios as jest.Mocked<typeof axios>;
11+
const axiosHeaders = new AxiosHeaders();
12+
const payload: AxiosResponse<IResponse[]> = {
13+
status: 200,
14+
statusText: "OK",
15+
headers: {},
16+
config: {
17+
headers: axiosHeaders,
18+
},
19+
data: [
20+
{
21+
id: "1",
22+
fullName: "John Smith",
23+
profilePicture: "https://example.com/john.jpg",
24+
tagLine: "Software engineer",
25+
bio: "I am a software engineer",
26+
sessions: [
27+
{
28+
id: 4567,
29+
name: "sample session",
30+
},
31+
],
32+
links: [
33+
{
34+
linkType: "Twitter",
35+
url: "https://twitter.com/johnsmith",
36+
title: "",
37+
},
38+
{
39+
linkType: "LinkedIn",
40+
url: "https://linkedin.com/in/johnsmith",
41+
title: "",
42+
},
43+
],
44+
},
45+
{
46+
id: "2",
47+
fullName: "Jane Doe",
48+
profilePicture: "https://example.com/jane.jpg",
49+
tagLine: "Data scientist",
50+
bio: "I am a data scientist",
51+
sessions: [],
52+
links: [
53+
{
54+
linkType: "Twitter",
55+
url: "https://twitter.com/janedoe",
56+
title: "",
57+
},
58+
{
59+
linkType: "LinkedIn",
60+
url: "https://linkedin.com/in/janedoe",
61+
title: "",
62+
},
63+
],
64+
},
65+
],
66+
};
667

768
describe("navigation pages", () => {
69+
beforeAll(() => {
70+
jest.mock("axios");
71+
mockedAxios.get.mockImplementation(() => Promise.resolve(payload));
72+
});
73+
beforeEach(() => {
74+
jest.clearAllMocks();
75+
});
76+
877
test("it render the HOME page", async () => {
978
render(
1079
<React.Suspense fallback={<span>Loading...</span>}>

0 commit comments

Comments
 (0)