forked from Movalabs-crew/mova-store
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproducts.test.ts
More file actions
293 lines (248 loc) · 9.95 KB
/
Copy pathproducts.test.ts
File metadata and controls
293 lines (248 loc) · 9.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import { describe, it, expect, vi, beforeEach } from "vitest";
// Mock supabase before importing lib/products
const mockStorageFrom = {
upload: vi.fn(),
getPublicUrl: vi.fn(),
remove: vi.fn(),
};
const mockFrom = vi.fn();
vi.mock("../../lib/supabase", () => ({
supabase: {
from: (table: string) => mockFrom(table),
storage: {
from: (bucket: string) => mockStorageFrom,
},
},
}));
import {
mapProduct,
listProducts,
getProductById,
uploadProductImage,
createProduct,
updateProduct,
deleteProduct,
} from "../../lib/products";
describe("lib/products data layer", () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe("mapProduct", () => {
it("returns null when passed null or undefined", () => {
expect(mapProduct(null)).toBeNull();
expect(mapProduct(undefined)).toBeNull();
});
it("coerces price strings to Number and preserves product fields", () => {
const raw = {
id: "prod-123",
name: "Running Shoe",
price: "49.99",
img: "https://example.com/shoe.jpg",
created_at: "2026-09-01T00:00:00Z",
};
const mapped = mapProduct(raw);
expect(mapped).toEqual({
id: "prod-123",
name: "Running Shoe",
price: 49.99,
img: "https://example.com/shoe.jpg",
created_at: "2026-09-01T00:00:00Z",
});
expect(typeof mapped?.price).toBe("number");
});
it("handles already numeric prices correctly", () => {
const raw = {
id: 1,
name: "Shirt",
price: 25,
img: "https://example.com/shirt.jpg",
};
const mapped = mapProduct(raw);
expect(mapped?.price).toBe(25);
});
});
describe("listProducts", () => {
it("fetches products ordered by created_at desc and maps each product", async () => {
const fakeData = [
{ id: "1", name: "Shoe", price: "100", img: "/img1.jpg" },
{ id: "2", name: "Hat", price: "20.5", img: "/img2.jpg" },
];
const mockOrder = vi.fn().mockResolvedValue({ data: fakeData, error: null });
const mockSelect = vi.fn().mockReturnValue({ order: mockOrder });
mockFrom.mockReturnValue({ select: mockSelect });
const res = await listProducts();
expect(mockFrom).toHaveBeenCalledWith("products");
expect(mockSelect).toHaveBeenCalledWith("*");
expect(mockOrder).toHaveBeenCalledWith("created_at", { ascending: false });
expect(res).toEqual([
{ id: "1", name: "Shoe", price: 100, img: "/img1.jpg" },
{ id: "2", name: "Hat", price: 20.5, img: "/img2.jpg" },
]);
});
it("throws error when Supabase select fails", async () => {
const mockOrder = vi.fn().mockResolvedValue({
data: null,
error: new Error("Database error"),
});
const mockSelect = vi.fn().mockReturnValue({ order: mockOrder });
mockFrom.mockReturnValue({ select: mockSelect });
await expect(listProducts()).rejects.toThrow("Database error");
});
});
describe("getProductById", () => {
it("fetches a single product by ID and maps it", async () => {
const fakeProduct = { id: "p1", name: "Bag", price: "75.00", img: "/bag.jpg" };
const mockMaybeSingle = vi.fn().mockResolvedValue({ data: fakeProduct, error: null });
const mockEq = vi.fn().mockReturnValue({ maybeSingle: mockMaybeSingle });
const mockSelect = vi.fn().mockReturnValue({ eq: mockEq });
mockFrom.mockReturnValue({ select: mockSelect });
const result = await getProductById("p1");
expect(mockFrom).toHaveBeenCalledWith("products");
expect(mockSelect).toHaveBeenCalledWith("*");
expect(mockEq).toHaveBeenCalledWith("id", "p1");
expect(result).toEqual({
id: "p1",
name: "Bag",
price: 75,
img: "/bag.jpg",
});
});
it("returns null when product is not found", async () => {
const mockMaybeSingle = vi.fn().mockResolvedValue({ data: null, error: null });
const mockEq = vi.fn().mockReturnValue({ maybeSingle: mockMaybeSingle });
const mockSelect = vi.fn().mockReturnValue({ eq: mockEq });
mockFrom.mockReturnValue({ select: mockSelect });
const result = await getProductById("p-missing");
expect(result).toBeNull();
});
it("throws error when Supabase lookup fails", async () => {
const mockMaybeSingle = vi.fn().mockResolvedValue({
data: null,
error: new Error("Query timeout"),
});
const mockEq = vi.fn().mockReturnValue({ maybeSingle: mockMaybeSingle });
const mockSelect = vi.fn().mockReturnValue({ eq: mockEq });
mockFrom.mockReturnValue({ select: mockSelect });
await expect(getProductById("p1")).rejects.toThrow("Query timeout");
});
});
describe("uploadProductImage", () => {
it("uploads file with timestamped name and returns public URL", async () => {
const mockFile = new File(["dummy content"], "test-product.png", {
type: "image/png",
});
mockStorageFrom.upload.mockResolvedValue({
data: { path: "12345.png" },
error: null,
});
mockStorageFrom.getPublicUrl.mockReturnValue({
data: { publicUrl: "https://dummy.supabase.co/storage/v1/object/public/products/12345.png" },
});
const url = await uploadProductImage(mockFile);
expect(mockStorageFrom.upload).toHaveBeenCalledTimes(1);
const [fileName, fileArg, options] = mockStorageFrom.upload.mock.calls[0];
expect(fileName).toMatch(/^\d+-[a-z0-9]+\.png$/);
expect(fileArg).toBe(mockFile);
expect(options).toEqual({
cacheControl: "3600",
upsert: false,
contentType: "image/png",
});
expect(mockStorageFrom.getPublicUrl).toHaveBeenCalledWith(fileName);
expect(url).toBe("https://dummy.supabase.co/storage/v1/object/public/products/12345.png");
});
it("throws error when storage upload fails", async () => {
const mockFile = new File(["dummy"], "fail.png", { type: "image/png" });
mockStorageFrom.upload.mockResolvedValue({
data: null,
error: new Error("Storage quota exceeded"),
});
await expect(uploadProductImage(mockFile)).rejects.toThrow("Storage quota exceeded");
});
});
describe("createProduct", () => {
it("inserts a new product and returns the mapped created product", async () => {
const input = { name: "New Shoe", price: 120, img: "/shoe.png" };
const returnedRow = { id: "p-new", ...input, price: "120" };
const mockSingle = vi.fn().mockResolvedValue({ data: returnedRow, error: null });
const mockSelect = vi.fn().mockReturnValue({ single: mockSingle });
const mockInsert = vi.fn().mockReturnValue({ select: mockSelect });
mockFrom.mockReturnValue({ insert: mockInsert });
const res = await createProduct(input);
expect(mockFrom).toHaveBeenCalledWith("products");
expect(mockInsert).toHaveBeenCalledWith([input]);
expect(res).toEqual({
id: "p-new",
name: "New Shoe",
price: 120,
img: "/shoe.png",
});
});
it("throws error when insert fails", async () => {
const mockSingle = vi.fn().mockResolvedValue({
data: null,
error: new Error("Insert constraint error"),
});
const mockSelect = vi.fn().mockReturnValue({ single: mockSingle });
const mockInsert = vi.fn().mockReturnValue({ select: mockSelect });
mockFrom.mockReturnValue({ insert: mockInsert });
await expect(createProduct({ name: "Bad", price: 10 })).rejects.toThrow(
"Insert constraint error"
);
});
});
describe("updateProduct", () => {
it("updates product fields by id and returns mapped product", async () => {
const updates = { name: "Updated Shoe", price: 130 };
const returnedRow = { id: "p-1", img: "/shoe.png", ...updates, price: "130" };
const mockSingle = vi.fn().mockResolvedValue({ data: returnedRow, error: null });
const mockSelect = vi.fn().mockReturnValue({ single: mockSingle });
const mockEq = vi.fn().mockReturnValue({ select: mockSelect });
const mockUpdate = vi.fn().mockReturnValue({ eq: mockEq });
mockFrom.mockReturnValue({ update: mockUpdate });
const res = await updateProduct("p-1", updates);
expect(mockFrom).toHaveBeenCalledWith("products");
expect(mockUpdate).toHaveBeenCalledWith(updates);
expect(mockEq).toHaveBeenCalledWith("id", "p-1");
expect(res).toEqual({
id: "p-1",
img: "/shoe.png",
name: "Updated Shoe",
price: 130,
});
});
it("throws error when update fails", async () => {
const mockSingle = vi.fn().mockResolvedValue({
data: null,
error: new Error("Update failed"),
});
const mockSelect = vi.fn().mockReturnValue({ single: mockSingle });
const mockEq = vi.fn().mockReturnValue({ select: mockSelect });
const mockUpdate = vi.fn().mockReturnValue({ eq: mockEq });
mockFrom.mockReturnValue({ update: mockUpdate });
await expect(updateProduct("p-1", { price: 99 })).rejects.toThrow("Update failed");
});
});
describe("deleteProduct", () => {
it("deletes a product by id", async () => {
const mockEq = vi.fn().mockResolvedValue({ data: null, error: null });
const mockDelete = vi.fn().mockReturnValue({ eq: mockEq });
mockFrom.mockReturnValue({ delete: mockDelete });
await deleteProduct("p-del");
expect(mockFrom).toHaveBeenCalledWith("products");
expect(mockDelete).toHaveBeenCalledTimes(1);
expect(mockEq).toHaveBeenCalledWith("id", "p-del");
});
it("throws error when delete fails", async () => {
const mockEq = vi.fn().mockResolvedValue({
data: null,
error: new Error("Foreign key constraint violation"),
});
const mockDelete = vi.fn().mockReturnValue({ eq: mockEq });
mockFrom.mockReturnValue({ delete: mockDelete });
await expect(deleteProduct("p-del")).rejects.toThrow(
"Foreign key constraint violation"
);
});
});
});