add pokerogue-savedata-api test coverage

This commit is contained in:
flx-sta 2024-10-09 09:40:11 -07:00
parent bf4f79379d
commit 087bedd47e
2 changed files with 56 additions and 1 deletions

View File

@ -35,7 +35,7 @@ export class PokerogueSavedataApi extends ApiBase {
return await response.text();
} catch (err) {
console.warn("Could not update all savedata!", err);
return null;
return "Unknown error";
}
}
}

View File

@ -0,0 +1,55 @@
import type { UpdateAllSavedataRequest } from "#app/@types/PokerogueSavedataApi";
import { PokerogueSavedataApi } from "#app/plugins/api/pokerogue-savedata-api";
import { getApiBaseUrl } from "#app/test/utils/testUtils";
import { http, HttpResponse } from "msw";
import { setupServer } from "msw/node";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
const apiBase = getApiBaseUrl();
const savedataApi = new PokerogueSavedataApi(apiBase);
const server = setupServer();
beforeAll(() => {
server.listen({ onUnhandledRequest: "error" });
});
afterAll(() => {
server.close();
});
afterEach(() => {
server.resetHandlers();
});
describe("Pokerogue Admin API", () => {
beforeEach(() => {
vi.spyOn(console, "warn");
});
describe("Update All", () => {
it("should return an empty string on SUCCESS", async () => {
server.use(http.post(`${apiBase}/savedata/updateall`, () => HttpResponse.text(null)));
const error = await savedataApi.updateAll({} as UpdateAllSavedataRequest);
expect(error).toBe("");
});
it("should return an error message on FAILURE", async () => {
server.use(http.post(`${apiBase}/savedata/updateall`, () => HttpResponse.text("Failed to update all!")));
const error = await savedataApi.updateAll({} as UpdateAllSavedataRequest);
expect(error).toBe("Failed to update all!");
});
it("should return 'Unknown error' and report a warning on ERROR", async () => {
server.use(http.post(`${apiBase}/savedata/updateall`, () => HttpResponse.error()));
const error = await savedataApi.updateAll({} as UpdateAllSavedataRequest);
expect(error).toBe("Unknown error");
expect(console.warn).toHaveBeenCalledWith("Could not update all savedata!", expect.any(Error));
});
});
});