feat: complete testing, security, dynamic content, and devops overhaul

This commit is contained in:
parsa committed 2026-05-27 02:00:14 +03:30
1 parent 467532713c
commit 40ff169dc2
52 files changed
+3918 -104

No files matched your search

+142
View File
@@ -0,0 +1,142 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { useCartStore } from '../cartStore';
import { orderService } from '../../services/orderService';
vi.mock('../../services/orderService', () => ({
orderService: {
createOrder: vi.fn(),
},
}));
const mockProduct = {
id: 'prod-1',
artNo: 'art-1',
name: 'Canhydrox GAG',
scientificTagline: 'Joint helper',
description: 'Joints',
price: '۱,۰۰۰ تومان',
priceValue: 1000,
category: 'joints',
unit: 'tablet',
packageSize: 100,
main_ingredients: [],
dosage_logic: '1',
benefits: '',
symptoms: [],
suitableFor: 'سگ' as any,
calculateDosage: () => ({ quantity: 1, unit: 'tablet', description: '' }),
analysis: {},
feedingAdvice: '',
specialist: null as any,
image: '',
};
describe('cartStore', () => {
beforeEach(() => {
vi.clearAllMocks();
useCartStore.setState({
items: [],
isSubscribed: false,
charityDonation: 0,
coupon: null,
orders: [],
});
});
it('should add item to cart', () => {
const store = useCartStore.getState();
store.addItem(mockProduct, 2);
const state = useCartStore.getState();
expect(state.items).toHaveLength(1);
expect(state.items[0].product.id).toBe('prod-1');
expect(state.items[0].quantity).toBe(2);
});
it('should increment quantity if item already in cart', () => {
const store = useCartStore.getState();
store.addItem(mockProduct, 2);
store.addItem(mockProduct, 3);
const state = useCartStore.getState();
expect(state.items).toHaveLength(1);
expect(state.items[0].quantity).toBe(5);
});
it('should remove item from cart', () => {
const store = useCartStore.getState();
store.addItem(mockProduct, 2);
store.removeItem('prod-1');
const state = useCartStore.getState();
expect(state.items).toHaveLength(0);
});
it('should update quantity', () => {
const store = useCartStore.getState();
store.addItem(mockProduct, 2);
store.updateQuantity('prod-1', 4);
const state = useCartStore.getState();
expect(state.items[0].quantity).toBe(4);
});
it('should remove item if update quantity is <= 0', () => {
const store = useCartStore.getState();
store.addItem(mockProduct, 2);
store.updateQuantity('prod-1', 0);
const state = useCartStore.getState();
expect(state.items).toHaveLength(0);
});
it('should apply discount coupon', () => {
const store = useCartStore.getState();
const success = store.applyCoupon('CANINA2024');
expect(success).toBe(true);
const state = useCartStore.getState();
expect(state.coupon).toEqual({ code: 'CANINA2024', discount: 0.1 });
});
it('should calculate subtotal, discount and total', () => {
const store = useCartStore.getState();
store.addItem(mockProduct, 2); // 2 * 1000 = 2000
store.applyCoupon('CANINA2024'); // 10% discount
expect(useCartStore.getState().getSubtotal()).toBe(2000);
expect(useCartStore.getState().getDiscount()).toBe(200);
expect(useCartStore.getState().getTotal()).toBe(1800);
});
it('should create order successfully via orderService', async () => {
const mockBackendOrder = {
id: 'backend-order-id',
createdAt: '2026-05-26T18:00:00.000Z',
totalAmount: '2000.00',
charityDonation: '0.00',
status: 'processing',
trackingNumber: 'TRK-12345',
};
vi.mocked(orderService.createOrder).mockResolvedValue(mockBackendOrder);
const store = useCartStore.getState();
store.addItem(mockProduct, 2);
const orderId = await store.addOrder({
items: store.items,
total: 2000,
charityDonation: 0,
petId: 'pet-id',
});
expect(orderService.createOrder).toHaveBeenCalled();
expect(orderId).toBe('backend-order-id');
const state = useCartStore.getState();
expect(state.orders).toHaveLength(1);
expect(state.orders[0].id).toBe('backend-order-id');
expect(state.orders[0].total).toBe(2000);
});
});
+62
View File
@@ -0,0 +1,62 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { useSettingsStore } from '../settingsStore';
import api from '../../services/api';
vi.mock('../../services/api', () => ({
default: {
get: vi.fn(),
},
}));
describe('settingsStore', () => {
beforeEach(() => {
vi.clearAllMocks();
useSettingsStore.setState({
texts: {},
scientificTerms: {},
isLoading: false,
});
});
it('should return fallback if key is not found', () => {
const text = useSettingsStore.getState().getText('non-existent', 'Fallback Text');
expect(text).toBe('Fallback Text');
});
it('should return value if key is found', () => {
useSettingsStore.setState({
texts: { 'test-key': 'Real Value' },
});
const text = useSettingsStore.getState().getText('test-key', 'Fallback Text');
expect(text).toBe('Real Value');
});
it('should fetch settings successfully and map array to object', async () => {
const mockUiTexts = [
{ key: 'hero_title', value: 'Welcome to Canina' },
{ key: 'hero_desc', value: 'Best supplements' },
];
const mockTerms = [
{ key: 'mussel', term: 'Mussel', definition: 'NZ Green Mussel', wikiId: 'general' },
];
vi.mocked(api.get).mockImplementation((url: string) => {
if (url === '/settings/ui-texts') {
return Promise.resolve({ data: mockUiTexts });
}
if (url === '/settings/scientific-terms') {
return Promise.resolve({ data: mockTerms });
}
return Promise.reject(new Error('Unknown URL'));
});
const store = useSettingsStore.getState();
await store.fetchSettings();
const state = useSettingsStore.getState();
expect(state.texts['hero_title']).toBe('Welcome to Canina');
expect(state.texts['hero_desc']).toBe('Best supplements');
expect(state.scientificTerms['mussel']).toEqual(mockTerms[0]);
expect(state.isLoading).toBe(false);
});
});
+133
View File
@@ -0,0 +1,133 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { useUserStore } from '../userStore';
import { authService } from '../../services/authService';
import api from '../../services/api';
vi.mock('../../services/authService', () => ({
authService: {
updateProfile: vi.fn(),
logout: vi.fn(),
getProfile: vi.fn(),
},
}));
vi.mock('../../services/api', () => ({
default: {
post: vi.fn(),
patch: vi.fn(),
delete: vi.fn(),
},
}));
describe('userStore', () => {
beforeEach(() => {
vi.clearAllMocks();
useUserStore.setState({
role: 'User_Guest',
isLoggedIn: false,
profile: {
firstName: '',
lastName: '',
email: '',
mobile: '',
walletBalance: 0,
charityDonationTotal: 0,
addresses: [],
transactions: [],
},
});
});
it('should logout correctly', () => {
useUserStore.setState({
role: 'User_PetOwner',
isLoggedIn: true,
profile: {
firstName: 'Ali',
lastName: 'Ahmadi',
email: 'ali@example.com',
mobile: '0912',
walletBalance: 100,
charityDonationTotal: 10,
addresses: [],
transactions: [],
},
});
useUserStore.getState().logout();
expect(authService.logout).toHaveBeenCalled();
const state = useUserStore.getState();
expect(state.isLoggedIn).toBe(false);
expect(state.role).toBe('User_Guest');
expect(state.profile.firstName).toBe('');
});
it('should fetch profile successfully', async () => {
const mockProfileData = {
firstName: 'Ali',
lastName: 'Ahmadi',
email: 'ali@example.com',
mobile: '09123456789',
role: 'User_PetOwner',
walletBalance: '1500.00',
charityDonationTotal: '50.00',
addresses: [],
pets: [],
walletTransactions: [],
};
vi.mocked(authService.getProfile).mockResolvedValue(mockProfileData);
const store = useUserStore.getState();
await store.fetchProfile();
expect(authService.getProfile).toHaveBeenCalled();
const state = useUserStore.getState();
expect(state.isLoggedIn).toBe(true);
expect(state.role).toBe('User_PetOwner');
expect(state.profile.firstName).toBe('Ali');
expect(state.profile.walletBalance).toBe(1500);
});
it('should update profile correctly', async () => {
const mockUpdatedData = {
firstName: 'Ali Updated',
lastName: 'Ahmadi',
email: 'ali@example.com',
mobile: '09123456789',
};
vi.mocked(authService.updateProfile).mockResolvedValue(mockUpdatedData);
const store = useUserStore.getState();
await store.updateProfile({ firstName: 'Ali Updated' });
expect(authService.updateProfile).toHaveBeenCalledWith({ firstName: 'Ali Updated' });
const state = useUserStore.getState();
expect(state.profile.firstName).toBe('Ali Updated');
});
it('should add address and call fetchProfile', async () => {
const address = {
id: 'addr-1',
title: 'Home',
receptorName: 'Ali',
phone: '0912',
province: 'Teh',
city: 'Teh',
detail: 'Det',
zipCode: '123',
isDefault: false,
};
const fetchSpy = vi.spyOn(useUserStore.getState(), 'fetchProfile').mockResolvedValue(undefined);
vi.mocked(api.post).mockResolvedValue({ data: { success: true } });
const store = useUserStore.getState();
await store.addAddress(address);
expect(api.post).toHaveBeenCalled();
expect(fetchSpy).toHaveBeenCalled();
});
});