mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 16:00:32 +00:00
refactor ApiResponseError check into a function
This commit is contained in:
@@ -29,7 +29,11 @@ function updateGlobalFetchMockJson(
|
|||||||
* @param obj
|
* @param obj
|
||||||
*/
|
*/
|
||||||
function resetGlobalFetchMockJson() {
|
function resetGlobalFetchMockJson() {
|
||||||
updateGlobalFetchMockJson({})
|
updateGlobalFetchMockJson({});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function assertAsyncCallbackThrowsApiResponseError(callback: () => Promise<any>) {
|
||||||
|
await expect(callback).rejects.toThrow(ApiResponseError);
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("ApiBasedRepositoryLoader", () => {
|
describe("ApiBasedRepositoryLoader", () => {
|
||||||
@@ -58,18 +62,17 @@ describe("ApiBasedRepositoryLoader", () => {
|
|||||||
it("throws the correct error if the API response contains no data", async () => {
|
it("throws the correct error if the API response contains no data", async () => {
|
||||||
updateGlobalFetchMockJson(fetchSystemDataFailedResponse);
|
updateGlobalFetchMockJson(fetchSystemDataFailedResponse);
|
||||||
|
|
||||||
// Jest is so confusing
|
await assertAsyncCallbackThrowsApiResponseError(async () => {
|
||||||
await expect(async () => {
|
|
||||||
await loader.fetchAndUpdateSystemData();
|
await loader.fetchAndUpdateSystemData();
|
||||||
}).rejects.toThrow(ApiResponseError);
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("throws the correct error if HTTP status code is not 200", async () => {
|
it("throws the correct error if HTTP status code is not 200", async () => {
|
||||||
updateGlobalFetchMockJson(fetchSystemDataFailedResponse, 400);
|
updateGlobalFetchMockJson(fetchSystemDataFailedResponse, 400);
|
||||||
|
|
||||||
await expect(async () => {
|
await assertAsyncCallbackThrowsApiResponseError(async () => {
|
||||||
await loader.fetchAndUpdateSystemData();
|
await loader.fetchAndUpdateSystemData();
|
||||||
}).rejects.toThrow(ApiResponseError);
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user