Skip to content

Commit 7bf2bbe

Browse files
Sam Sahbaei Razaviclaude
andcommitted
test: cart API — get, add item, update quantity, remove
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 3ba4fdf commit 7bf2bbe

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { describe, it, expect } from 'vitest';
2+
import request from 'supertest';
3+
import app from '../../../app';
4+
import { testProductId } from '../../../test/setup';
5+
6+
async function loginCustomer() {
7+
const res = await request(app).post('/api/auth/login').send({
8+
email: 'customer@test.com',
9+
password: 'password123',
10+
});
11+
return res.body.data.accessToken as string;
12+
}
13+
14+
describe('Cart API', () => {
15+
describe('GET /api/cart', () => {
16+
it('returns empty cart for new user', async () => {
17+
const token = await loginCustomer();
18+
const res = await request(app).get('/api/cart').set('Authorization', `Bearer ${token}`);
19+
expect(res.status).toBe(200);
20+
expect(res.body.data.cart.items).toEqual([]);
21+
});
22+
23+
it('requires authentication', async () => {
24+
const res = await request(app).get('/api/cart');
25+
expect(res.status).toBe(401);
26+
});
27+
});
28+
29+
describe('POST /api/cart/items', () => {
30+
it('adds a product to the cart', async () => {
31+
const token = await loginCustomer();
32+
const res = await request(app)
33+
.post('/api/cart/items')
34+
.set('Authorization', `Bearer ${token}`)
35+
.send({ productId: testProductId, quantity: 2 });
36+
expect(res.status).toBe(200);
37+
expect(res.body.data.cart.items[0].quantity).toBe(2);
38+
});
39+
40+
it('increments quantity when same product added again', async () => {
41+
const token = await loginCustomer();
42+
await request(app)
43+
.post('/api/cart/items')
44+
.set('Authorization', `Bearer ${token}`)
45+
.send({ productId: testProductId, quantity: 1 });
46+
await request(app)
47+
.post('/api/cart/items')
48+
.set('Authorization', `Bearer ${token}`)
49+
.send({ productId: testProductId, quantity: 2 });
50+
const cart = await request(app).get('/api/cart').set('Authorization', `Bearer ${token}`);
51+
expect(cart.body.data.cart.items[0].quantity).toBe(3);
52+
});
53+
54+
it('rejects quantity of 0', async () => {
55+
const token = await loginCustomer();
56+
const res = await request(app)
57+
.post('/api/cart/items')
58+
.set('Authorization', `Bearer ${token}`)
59+
.send({ productId: testProductId, quantity: 0 });
60+
expect(res.status).toBe(400);
61+
});
62+
});
63+
64+
describe('DELETE /api/cart/items/:id', () => {
65+
it('removes an item from the cart', async () => {
66+
const token = await loginCustomer();
67+
await request(app)
68+
.post('/api/cart/items')
69+
.set('Authorization', `Bearer ${token}`)
70+
.send({ productId: testProductId, quantity: 1 });
71+
const cartRes = await request(app).get('/api/cart').set('Authorization', `Bearer ${token}`);
72+
const itemId = cartRes.body.data.cart.items[0].id;
73+
74+
const res = await request(app)
75+
.delete(`/api/cart/items/${itemId}`)
76+
.set('Authorization', `Bearer ${token}`);
77+
expect(res.status).toBe(200);
78+
expect(res.body.data.cart.items).toHaveLength(0);
79+
});
80+
});
81+
});

0 commit comments

Comments
 (0)