Files
yara724api/src/helpers/list-query-v2.spec.ts
2026-09-27 14:40:29 +03:30

107 lines
3.3 KiB
TypeScript

import { applyListQueryV2 } from "./list-query-v2";
describe("applyListQueryV2", () => {
const rows = [
{ publicId: "B100", createdAt: "2026-01-01", status: "OPEN" },
{ publicId: "A200", createdAt: "2026-02-01", status: "DONE" },
{ publicId: "C050", createdAt: "2026-03-01", status: "OPEN" },
];
const getters = {
publicId: (r: (typeof rows)[0]) => r.publicId,
createdAt: (r: (typeof rows)[0]) => r.createdAt,
status: (r: (typeof rows)[0]) => r.status,
};
it("returns all rows when no pagination query", () => {
const r = applyListQueryV2(rows, getters, {});
expect(r.total).toBe(3);
expect(r.list).toHaveLength(3);
expect(r.page).toBeUndefined();
});
it("filters by search on publicId", () => {
const r = applyListQueryV2(rows, getters, { search: "a200" });
expect(r.total).toBe(1);
expect(r.list[0].publicId).toBe("A200");
});
it("filters nested party identities and normalizes Persian digits", () => {
const identityRows = [
{
publicId: "PHONE",
createdAt: "2026-01-01",
status: "OPEN",
parties: [
{
person: {
phoneNumber: "+98 912 123 4567",
nationalCodeOfDriver: "۰۰۱۲۳۴۵۶۷۸",
},
participants: [
{
phoneNumber: "0913-765-4321",
nationalCode: "۰۰۹۸۷۶۵۴۳۲۱",
},
],
},
],
},
{
publicId: "OTHER",
createdAt: "2026-01-02",
status: "OPEN",
parties: [{ person: { phoneNumber: "09351234567", nationalCode: "1111111111" } }],
},
];
const identityGetters = {
publicId: (r: (typeof identityRows)[0]) => r.publicId,
createdAt: (r: (typeof identityRows)[0]) => r.createdAt,
status: (r: (typeof identityRows)[0]) => r.status,
};
const byPhone = applyListQueryV2(identityRows, identityGetters, {
phoneNumber: "۰۹۱۲۱۲۳۴۵۶۷",
});
expect(byPhone.list.map((row) => row.publicId)).toEqual(["PHONE"]);
const byNationalCode = applyListQueryV2(identityRows, identityGetters, {
nationalCode: "0012345678",
});
expect(byNationalCode.list.map((row) => row.publicId)).toEqual(["PHONE"]);
const byParticipantPhone = applyListQueryV2(identityRows, identityGetters, {
phoneNumber: "09137654321",
});
expect(byParticipantPhone.list.map((row) => row.publicId)).toEqual(["PHONE"]);
const byParticipantNationalCode = applyListQueryV2(identityRows, identityGetters, {
nationalCode: "۰۰۹۸۷۶۵۴۳۲۱",
});
expect(byParticipantNationalCode.list.map((row) => row.publicId)).toEqual(["PHONE"]);
});
it("sorts by publicId ascending", () => {
const r = applyListQueryV2(rows, getters, {
sortBy: "publicId",
sortOrder: "asc",
});
expect(r.list.map((x) => x.publicId)).toEqual(["A200", "B100", "C050"]);
});
it("paginates when page and limit are set", () => {
const r = applyListQueryV2(rows, getters, {
sortBy: "publicId",
sortOrder: "asc",
page: 2,
limit: 1,
});
expect(r.total).toBe(3);
expect(r.list).toHaveLength(1);
expect(r.list[0].publicId).toBe("B100");
expect(r.page).toBe(2);
expect(r.limit).toBe(1);
expect(r.totalPages).toBe(3);
});
});