test(store): add Vitest unit test suite for cartStore discount, items, and coupon calculations [TASK-106]
This commit is contained in:
1 parent
228734c523
commit
01f4a7cf57
4 files changed
+133
-57
No files matched your search
@@ -2,9 +2,18 @@ import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { useCartStore } from '../cartStore';
|
||||
import { orderService } from '../../services/orderService';
|
||||
|
||||
import api from '../../services/api';
|
||||
|
||||
vi.mock('../../services/api', () => ({
|
||||
default: {
|
||||
post: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('../../services/orderService', () => ({
|
||||
orderService: {
|
||||
createOrder: vi.fn(),
|
||||
validateCoupon: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
@@ -90,19 +99,28 @@ describe('cartStore', () => {
|
||||
expect(state.items).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('should apply discount coupon', () => {
|
||||
it('should apply discount coupon', async () => {
|
||||
vi.mocked(api.post).mockResolvedValue({
|
||||
data: { discountValue: 0.1 }
|
||||
});
|
||||
|
||||
const store = useCartStore.getState();
|
||||
const success = store.applyCoupon('CANINO2024');
|
||||
store.addItem(mockProduct, 2); // 2 * 1000 = 2000
|
||||
const success = await store.applyCoupon('CANINA10');
|
||||
expect(success).toBe(true);
|
||||
|
||||
const state = useCartStore.getState();
|
||||
expect(state.coupon).toEqual({ code: 'CANINO2024', discount: 0.1 });
|
||||
expect(state.coupon).toEqual({ code: 'CANINA10', discount: 0.1 });
|
||||
});
|
||||
|
||||
it('should calculate subtotal, discount and total', () => {
|
||||
it('should calculate subtotal, discount and total', async () => {
|
||||
vi.mocked(api.post).mockResolvedValue({
|
||||
data: { discountValue: 0.1 }
|
||||
});
|
||||
|
||||
const store = useCartStore.getState();
|
||||
store.addItem(mockProduct, 2); // 2 * 1000 = 2000
|
||||
store.applyCoupon('CANINO2024'); // 10% discount
|
||||
await store.applyCoupon('CANINA10'); // 10% discount
|
||||
|
||||
expect(useCartStore.getState().getSubtotal()).toBe(2000);
|
||||
expect(useCartStore.getState().getDiscount()).toBe(200);
|
||||
|
||||
Reference in new issue
Block a user