mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-07 16:19:01 +00:00
51 lines
1.6 KiB
TypeScript
51 lines
1.6 KiB
TypeScript
|
|
import { describe, expect, it } from "vitest";
|
||
|
|
import { handleApiResponse, handlePaginatedResponse } from "@/api/handle-api-response";
|
||
|
|
|
||
|
|
describe("handleApiResponse", () => {
|
||
|
|
it("returns array payloads as-is", () => {
|
||
|
|
const items = [{ id: 1 }, { id: 2 }];
|
||
|
|
expect(handleApiResponse<typeof items>(items)).toEqual(items);
|
||
|
|
});
|
||
|
|
|
||
|
|
it("unwraps data, Data, and value wrappers", () => {
|
||
|
|
expect(handleApiResponse<{ id: number }>({ data: { id: 1 } })).toEqual({ id: 1 });
|
||
|
|
expect(handleApiResponse<{ id: number }>({ Data: { id: 2 } })).toEqual({ id: 2 });
|
||
|
|
expect(handleApiResponse<{ id: number }>({ value: { id: 3 } })).toEqual({ id: 3 });
|
||
|
|
});
|
||
|
|
|
||
|
|
it("returns object payload when no wrapper is present", () => {
|
||
|
|
const payload = { id: 99, name: "Test" };
|
||
|
|
expect(handleApiResponse(payload)).toEqual(payload);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe("handlePaginatedResponse", () => {
|
||
|
|
it("normalizes paginated ASP.NET payload", () => {
|
||
|
|
const result = handlePaginatedResponse<{ id: number }>(
|
||
|
|
{
|
||
|
|
Data: [{ id: 1 }, { id: 2 }],
|
||
|
|
TotalCount: 50,
|
||
|
|
PageNumber: 2,
|
||
|
|
PageSize: 25,
|
||
|
|
},
|
||
|
|
{ page: 2, pageSize: 25 },
|
||
|
|
);
|
||
|
|
|
||
|
|
expect(result).toEqual({
|
||
|
|
items: [{ id: 1 }, { id: 2 }],
|
||
|
|
totalCount: 50,
|
||
|
|
page: 2,
|
||
|
|
pageSize: 25,
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
it("handles plain arrays", () => {
|
||
|
|
const result = handlePaginatedResponse([{ id: 1 }], { page: 1, pageSize: 10 });
|
||
|
|
|
||
|
|
expect(result.items).toEqual([{ id: 1 }]);
|
||
|
|
expect(result.totalCount).toBe(1);
|
||
|
|
expect(result.page).toBe(1);
|
||
|
|
expect(result.pageSize).toBe(10);
|
||
|
|
});
|
||
|
|
});
|