refactor ApiResponseError check into a function

This commit is contained in:
2025-01-22 14:16:02 -08:00
parent bbe2c4c7aa
commit 0fe279f069

View File

@@ -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); });
}); });
}); });