57 lines
1.9 KiB
JavaScript
57 lines
1.9 KiB
JavaScript
import { expect, test, vi } from "vitest";
|
|
import { applyGiftPriceDefaults, applyGiftResourceSelection, fetchAllOptionPages } from "./useResourcePages.js";
|
|
|
|
test("fills gift name and id from selected gift resource", () => {
|
|
const form = {
|
|
coinPrice: "10",
|
|
giftId: "",
|
|
name: "",
|
|
resourceId: "",
|
|
};
|
|
|
|
const nextForm = applyGiftResourceSelection(form, {
|
|
coinPrice: 10,
|
|
name: "Rose",
|
|
priceType: "coin",
|
|
resourceCode: "gift_rose",
|
|
resourceId: 11,
|
|
});
|
|
|
|
expect(nextForm).toMatchObject({
|
|
coinPrice: "10",
|
|
giftId: "11",
|
|
name: "Rose",
|
|
resourceId: "11",
|
|
});
|
|
});
|
|
|
|
test("fills gift price without gift points", () => {
|
|
const form = {
|
|
coinPrice: "",
|
|
name: "Rose",
|
|
};
|
|
|
|
expect(applyGiftPriceDefaults(form, "100")).toMatchObject({
|
|
coinPrice: "100",
|
|
});
|
|
expect(applyGiftPriceDefaults(form, "101")).toMatchObject({
|
|
coinPrice: "101",
|
|
});
|
|
});
|
|
|
|
test("fetches all option pages instead of only the first resource page", async () => {
|
|
const fetcher = vi
|
|
.fn()
|
|
.mockResolvedValueOnce({ items: [{ resourceId: 1 }, { resourceId: 2 }], pageSize: 2, total: 5 })
|
|
.mockResolvedValueOnce({ items: [{ resourceId: 3 }, { resourceId: 4 }], pageSize: 2, total: 5 })
|
|
.mockResolvedValueOnce({ items: [{ resourceId: 5 }], pageSize: 2, total: 5 });
|
|
|
|
const result = await fetchAllOptionPages(fetcher, { status: "active" }, 2);
|
|
|
|
expect(fetcher).toHaveBeenNthCalledWith(1, { page: 1, page_size: 2, status: "active" });
|
|
expect(fetcher).toHaveBeenNthCalledWith(2, { page: 2, page_size: 2, status: "active" });
|
|
expect(fetcher).toHaveBeenNthCalledWith(3, { page: 3, page_size: 2, status: "active" });
|
|
expect(result.items.map((item) => item.resourceId)).toEqual([1, 2, 3, 4, 5]);
|
|
expect(result.total).toBe(5);
|
|
});
|