69 lines
1.8 KiB
JavaScript
69 lines
1.8 KiB
JavaScript
// @vitest-environment jsdom
|
|
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
vi.mock("axios", () => {
|
|
return {
|
|
default: {
|
|
get: vi.fn(),
|
|
post: vi.fn(),
|
|
delete: vi.fn(),
|
|
put: vi.fn(),
|
|
},
|
|
};
|
|
});
|
|
|
|
import axios from "axios";
|
|
import { createOrderItem } from "@/components/shop/OrdersItems.vue";
|
|
|
|
describe("createOrderItem", () => {
|
|
beforeEach(() => {
|
|
localStorage.clear();
|
|
axios.post.mockReset();
|
|
});
|
|
|
|
it("returns null when token is missing", () => {
|
|
const result = createOrderItem(123, 10, 1);
|
|
|
|
expect(result).toBeNull();
|
|
expect(axios.post).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("rejects and does not call API when order id is missing or invalid", async () => {
|
|
localStorage.setItem("token", "test-token");
|
|
|
|
const invalidOrderIds = [null, undefined, 0, "0", -1, "abc"];
|
|
|
|
for (const invalidOrderId of invalidOrderIds) {
|
|
await expect(createOrderItem(invalidOrderId, 10, 1)).rejects.toThrow("Order ID is required");
|
|
}
|
|
|
|
expect(axios.post).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("calls API with normalized numeric order id when valid", async () => {
|
|
localStorage.setItem("token", "test-token");
|
|
axios.post.mockResolvedValue({ data: { success: true } });
|
|
|
|
await createOrderItem("51207", 77, 2, 99, "note", 1234);
|
|
|
|
expect(axios.post).toHaveBeenCalledTimes(1);
|
|
expect(axios.post).toHaveBeenCalledWith(
|
|
expect.stringContaining("/order/items"),
|
|
expect.objectContaining({
|
|
order_id: 51207,
|
|
product_id: 77,
|
|
quantity: 2,
|
|
related_item_id: 99,
|
|
notes: "note",
|
|
price: 1234,
|
|
}),
|
|
expect.objectContaining({
|
|
headers: expect.objectContaining({
|
|
Authorization: "Bearer test-token",
|
|
}),
|
|
})
|
|
);
|
|
});
|
|
});
|