Skip to content

Commit 88603dd

Browse files
Test: update recurring events tests for batched POST behavior
1 parent ec71b2a commit 88603dd

2 files changed

Lines changed: 14 additions & 13 deletions

File tree

backend/workers/createRecurringEvents.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ const fetchData = async (endpoint, URL, headerToSend, fetch) => {
2323

2424
/** POST
2525
* Creates a new event by making a POST request to the events API.
26-
* @param {Object} event - The event data to create.
26+
* @param {Object} eventArray - The events array data to create.
2727
* @returns {Promise<Object|null>} - The created event data or null on failure.
2828
*/
29-
const createEvents = async (event, URL, headerToSend, fetch) => {
30-
if (!event) return null;
29+
const createEvents = async (eventArray, URL, headerToSend, fetch) => {
30+
if (!eventArray) return null;
3131

3232
try {
3333
const res = await fetch(`${URL}/api/events/`, {
@@ -36,7 +36,7 @@ const createEvents = async (event, URL, headerToSend, fetch) => {
3636
'Content-Type': 'application/json',
3737
'x-customrequired-header': headerToSend,
3838
},
39-
body: JSON.stringify(event),
39+
body: JSON.stringify(eventArray),
4040
});
4141
if (!res.ok) throw new Error('Failed to create event');
4242
return await res.json();

backend/workers/createRecurringEvents.test.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const {
33
adjustToLosAngelesTime,
44
isSameUTCDate,
55
doesEventExist,
6-
createEvent,
6+
createEvents,
77
filterAndCreateEvents,
88
runTask,
99
scheduleTask,
@@ -189,7 +189,7 @@ describe('createRecurringEvents Module Tests', () => {
189189
expect(fetch).toHaveBeenCalledWith(
190190
`${mockURL}/api/events/`,
191191
expect.objectContaining({
192-
body: JSON.stringify(expectedEvent),
192+
body: JSON.stringify([expectedEvent]),
193193
}),
194194
);
195195

@@ -222,7 +222,7 @@ describe('createRecurringEvents Module Tests', () => {
222222
expect(fetch).toHaveBeenCalledWith(
223223
`${mockURL}/api/events/`,
224224
expect.objectContaining({
225-
body: JSON.stringify(expectedEvent),
225+
body: JSON.stringify([expectedEvent]),
226226
}),
227227
);
228228

@@ -256,7 +256,7 @@ describe('createRecurringEvents Module Tests', () => {
256256
expect(fetch).toHaveBeenCalledWith(
257257
`${mockURL}/api/events/`,
258258
expect.objectContaining({
259-
body: JSON.stringify(expectedEvent),
259+
body: JSON.stringify([expectedEvent]),
260260
}),
261261
);
262262

@@ -289,7 +289,7 @@ describe('createRecurringEvents Module Tests', () => {
289289
expect(fetch).toHaveBeenCalledWith(
290290
`${mockURL}/api/events/`,
291291
expect.objectContaining({
292-
body: JSON.stringify(expectedEvent),
292+
body: JSON.stringify([expectedEvent]),
293293
}),
294294
);
295295

@@ -330,31 +330,32 @@ describe('createRecurringEvents Module Tests', () => {
330330
});
331331
});
332332

333-
describe('createEvent', () => {
333+
describe('createEvents', () => {
334334
it('should create a new event via POST request', async () => {
335335
const mockEvent = { name: 'Event 1', date: '2023-11-02T19:00:00Z' };
336+
const mockEventArray = [mockEvent];
336337
fetch.mockResolvedValueOnce({
337338
ok: true,
338339
json: jest.fn().mockResolvedValue({ id: 1, ...mockEvent }),
339340
});
340341

341-
const result = await createEvent(mockEvent, mockURL, mockHeader, fetch);
342+
const result = await createEvents(mockEventArray, mockURL, mockHeader, fetch);
342343

343344
expect(fetch).toHaveBeenCalledWith(`${mockURL}/api/events/`, {
344345
method: 'POST',
345346
headers: {
346347
'Content-Type': 'application/json',
347348
'x-customrequired-header': mockHeader,
348349
},
349-
body: JSON.stringify(mockEvent),
350+
body: JSON.stringify(mockEventArray),
350351
});
351352
expect(result).toEqual({ id: 1, ...mockEvent });
352353
});
353354

354355
it('should return null if event creation fails', async () => {
355356
fetch.mockRejectedValueOnce(new Error('Network error'));
356357

357-
const result = await createEvent(null, mockURL, mockHeader, fetch);
358+
const result = await createEvents(null, mockURL, mockHeader, fetch);
358359

359360
expect(result).toBeNull();
360361
});

0 commit comments

Comments
 (0)