-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
772c77a
commit a6cfa78
Showing
5 changed files
with
169 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { StrictResponse, http } from 'msw'; | ||
import { getPaginatedSlice } from '../utilities/pagination'; | ||
import { | ||
makeNotFoundResponse, | ||
makePaginatedResponse, | ||
makeResponse, | ||
} from '../utilities/response'; | ||
import { DateTime } from 'luxon'; | ||
|
||
import type { APIErrorResponse } from '../utilities/response'; | ||
import type { MockContext } from '../mockContext'; | ||
import type { Event } from '@linode/api-v4'; | ||
import type { MockEventProgressHandler } from '../mockContext'; | ||
|
||
/** | ||
* Filters events by their `created` date. | ||
* | ||
* Events with `created` dates in the future are filtered out. | ||
* | ||
* @param event - Event to filter. | ||
* | ||
* @returns `true` if event's created date is in the past, `false` otherwise. | ||
*/ | ||
const filterEventsByCreatedTime = ( | ||
eventQueueItem: [Event, MockEventProgressHandler | null] | ||
): boolean => DateTime.fromISO(eventQueueItem[0].created) <= DateTime.now(); | ||
|
||
/** | ||
* Simulates event progression by executing a callback that may mutate the event or context. | ||
* | ||
* | ||
*/ | ||
const progressEvent = ( | ||
eventQueueItem: [Event, MockEventProgressHandler | null], | ||
context: MockContext | ||
) => { | ||
const [event, handler] = eventQueueItem; | ||
if (handler) { | ||
const result = handler(event, context); | ||
// If handler responds with `true`, replace the handler with `null` to prevent further execution. | ||
if (result) { | ||
eventQueueItem[1] = null; | ||
} | ||
} | ||
}; | ||
|
||
export const getEvents = (mockContext: MockContext) => [ | ||
http.get('*/v4*/events', ({ request }) => { | ||
const url = new URL(request.url); | ||
|
||
const pageNumber = Number(url.searchParams.get('page')) || 1; | ||
const pageSize = Number(url.searchParams.get('page_size')) || 25; | ||
const totalPages = Math.max( | ||
Math.ceil(mockContext.regions.length / pageSize), | ||
1 | ||
); | ||
|
||
const events = mockContext.eventQueue | ||
.filter(filterEventsByCreatedTime) | ||
.map((queuedEvent) => { | ||
progressEvent(queuedEvent, mockContext); | ||
return queuedEvent[0]; | ||
}); | ||
|
||
const pageSlice = getPaginatedSlice(events, pageNumber, pageSize); | ||
|
||
return makePaginatedResponse(pageSlice, pageNumber, totalPages); | ||
}), | ||
http.get( | ||
'*/v4*/events/:id', | ||
({ params }): StrictResponse<Event | APIErrorResponse> => { | ||
const id = Number(params.id); | ||
const event = mockContext.eventQueue.find( | ||
(eventQueueItem) => eventQueueItem[0].id === id | ||
); | ||
|
||
if (!event) { | ||
return makeNotFoundResponse(); | ||
} | ||
|
||
progressEvent(event, mockContext); | ||
return makeResponse(event[0]); | ||
} | ||
), | ||
]; | ||
|
||
export const updateEvents = (mockContext: MockContext) => [ | ||
// Marks all events up to and including the event with the given ID as seen. | ||
http.post('*/v4*/events/:id/seen', ({ params }) => { | ||
const id = Number(params.id); | ||
|
||
mockContext.eventQueue.forEach((eventQueueItem) => { | ||
if (eventQueueItem[0].id <= id) { | ||
eventQueueItem[0].seen = true; | ||
} | ||
}); | ||
|
||
// API-v4 responds with a 200 and empty object regardless of whether the | ||
// requested event actually exists (or belongs to the requesting user). | ||
return makeResponse({}); | ||
}), | ||
// Marks all events up to and including the event with the given ID as read. | ||
http.post('*/v4*/events/:id/read', ({ params }) => { | ||
const id = Number(params.id); | ||
|
||
mockContext.eventQueue.forEach((eventQueueItem) => { | ||
if (eventQueueItem[0].id <= id) { | ||
eventQueueItem[0].read = true; | ||
} | ||
}); | ||
|
||
// API-v4 responds with a 200 and empty object regardless of whether the | ||
// requested event actually exists (or belongs to the requesting user). | ||
return makeResponse({}); | ||
}), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters