56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
|
|
import { gymsApi } from "../gyms";
|
|
import { apiClient, withAuth } from "../client";
|
|
|
|
jest.mock("../client", () => ({
|
|
apiClient: {
|
|
get: jest.fn(),
|
|
patch: jest.fn(),
|
|
},
|
|
withAuth: jest.fn((token?: string | null) =>
|
|
token ? { headers: { Authorization: `Bearer ${token}` } } : {},
|
|
),
|
|
}));
|
|
|
|
describe("gymsApi", () => {
|
|
const getMock = apiClient.get as any;
|
|
const patchMock = apiClient.patch as any;
|
|
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it("returns array payload from getGyms", async () => {
|
|
getMock.mockResolvedValue({
|
|
data: [{ id: "gym_1", name: "Gym One" }],
|
|
});
|
|
|
|
const result = await gymsApi.getGyms("token_1");
|
|
|
|
expect(result).toEqual([{ id: "gym_1", name: "Gym One" }]);
|
|
expect(withAuth).toHaveBeenCalledWith("token_1");
|
|
});
|
|
|
|
it("returns nested data payload from getGyms", async () => {
|
|
getMock.mockResolvedValue({
|
|
data: { data: [{ id: "gym_2", name: "Gym Two" }] },
|
|
});
|
|
|
|
const result = await gymsApi.getGyms(null);
|
|
|
|
expect(result).toEqual([{ id: "gym_2", name: "Gym Two" }]);
|
|
});
|
|
|
|
it("patches selected gym for current user", async () => {
|
|
patchMock.mockResolvedValue({});
|
|
|
|
await gymsApi.updateUserGym("gym_2", "token_2");
|
|
|
|
expect(apiClient.patch).toHaveBeenCalledWith(
|
|
"/api/users/gym",
|
|
{ gymId: "gym_2" },
|
|
expect.any(Object),
|
|
);
|
|
});
|
|
});
|