Skip to content

Commit 0e0ad79

Browse files
committed
🔒 jest testing 2 apis
1 parent 2a251f9 commit 0e0ad79

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

Diff for: backend/controllers/eventbritecontroller.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
const axios = require("axios");
22

3-
4-
async function getOrganizations(apiToken) {
3+
let getOrganizations = async (token) => {
54
try {
65
const url = "https://www.eventbriteapi.com/v3/users/me/organizations/";
7-
8-
const response = await axios.get(url, {
6+
let response = await axios.get(url, {
97
headers: {
10-
Authorization: `Bearer ${apiToken}`,
8+
Authorization: `Bearer ${token}`,
119
},
1210
});
13-
14-
1511
return response.data;
16-
} catch (error) {
17-
console.error("Error fetching organizations from Eventbrite API:", error);
18-
throw new Error("Failed to fetch organizations from Eventbrite API");
12+
} catch (e) {
13+
throw new Error("Error fetching organizations from Eventbrite API");
1914
}
20-
}
21-
15+
};
2216

17+
let getEventById = async (eventId) => {
18+
try {
19+
const url = `https://www.eventbriteapi.com/v3/events/${eventId}/?token=${process.env.API_TOKEN}`;
20+
let response = await axios.get(url);
21+
return response.data;
22+
} catch (e) {
23+
throw new Error("Error fetching event from Eventbrite API");
24+
}
25+
};
2326

2427
module.exports = {
2528
getOrganizations,
29+
getEventById,
2630
};

Diff for: backend/tests/user.test.js

+42-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
const axios = require("axios");
2-
const { getOrganizations } = require("../controllers/eventbritecontroller");
2+
const {
3+
getOrganizations,
4+
getEventById,
5+
} = require("../controllers/eventbritecontroller");
36

47
jest.mock("axios");
58

69
describe("Eventbrite API", () => {
710
it("should fetch organizations from Eventbrite API", async () => {
8-
911
const mockResponse = {
1012
data: [
1113
{
@@ -19,9 +21,8 @@ describe("Eventbrite API", () => {
1921
],
2022
};
2123

22-
axios.get.mockResolvedValue(mockResponse);
24+
axios.get.mockResolvedValue(mockResponse);
2325

24-
2526
const response = await getOrganizations("KCIV4OKXGX3FO6FDGVYC");
2627

2728
expect(axios.get).toHaveBeenCalledWith(
@@ -36,7 +37,43 @@ describe("Eventbrite API", () => {
3637
expect(response).toEqual(mockResponse.data);
3738
});
3839

40+
it("should fetch a specific event from Eventbrite API", async () => {
41+
const eventId = "688487002157";
42+
const mockResponse = {
43+
data: {
44+
id: eventId,
45+
name: "Event Name",
46+
description: { text: "Event description" },
47+
start: { utc: "2023-07-31T15:00:00Z" },
48+
end: { utc: "2023-07-31T18:00:00Z" },
49+
},
50+
};
3951

40-
});
52+
axios.get.mockResolvedValue(mockResponse);
4153

54+
const response = await getEventById(eventId);
55+
56+
expect(axios.get).toHaveBeenCalledWith(
57+
`https://www.eventbriteapi.com/v3/events/${eventId}/?token=${process.env.API_TOKEN}`
58+
);
59+
60+
expect(response).toEqual(mockResponse.data);
61+
});
4262

63+
it("should handle errors from Eventbrite API when fetching organizations", async () => {
64+
axios.get.mockRejectedValue(new Error("API error"));
65+
66+
await expect(getOrganizations("INVALID_TOKEN")).rejects.toThrow(
67+
"Error fetching organizations from Eventbrite API"
68+
);
69+
});
70+
71+
it("should handle errors from Eventbrite API when fetching a specific event", async () => {
72+
const eventId = "688487002157";
73+
axios.get.mockRejectedValue(new Error("API error"));
74+
75+
await expect(getEventById(eventId)).rejects.toThrow(
76+
"Error fetching event from Eventbrite API"
77+
);
78+
});
79+
});

0 commit comments

Comments
 (0)