forked from hazem/yslootahrobotics
116 lines
4.0 KiB
TypeScript
116 lines
4.0 KiB
TypeScript
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
import { pricingStore } from '@/store/usePricingStore';
|
|
|
|
const mockStorage: Record<string, string> = {};
|
|
|
|
vi.stubGlobal('localStorage', {
|
|
getItem: vi.fn((key: string) => mockStorage[key] ?? null),
|
|
setItem: vi.fn((key: string, value: string) => { mockStorage[key] = value; }),
|
|
removeItem: vi.fn((key: string) => { delete mockStorage[key]; }),
|
|
clear: vi.fn(() => { Object.keys(mockStorage).forEach((k) => delete mockStorage[k]); }),
|
|
});
|
|
|
|
describe('usePricingStore', () => {
|
|
beforeEach(() => {
|
|
Object.keys(mockStorage).forEach((k) => delete mockStorage[k]);
|
|
pricingStore.getState().resetPrices();
|
|
});
|
|
|
|
describe('Default Prices', () => {
|
|
it('should have 5 default pricing items', () => {
|
|
const items = pricingStore.getState().items;
|
|
expect(items).toHaveLength(5);
|
|
});
|
|
|
|
it('should have correct default base price', () => {
|
|
const base = pricingStore.getState().items.find((i) => i.id === 'base');
|
|
expect(base).toBeDefined();
|
|
expect(base!.price).toBe(250000);
|
|
});
|
|
|
|
it('should have correct default attire prices', () => {
|
|
const items = pricingStore.getState().items;
|
|
const kandura = items.find((i) => i.id === 'emarati-kandura');
|
|
const vest = items.find((i) => i.id === 'industrial-vest');
|
|
const suit = items.find((i) => i.id === 'business-suit');
|
|
|
|
expect(kandura!.price).toBe(15000);
|
|
expect(vest!.price).toBe(8500);
|
|
expect(suit!.price).toBe(12000);
|
|
});
|
|
|
|
it('should have correct default custom color price', () => {
|
|
const color = pricingStore.getState().items.find((i) => i.id === 'custom-color');
|
|
expect(color!.price).toBe(3500);
|
|
});
|
|
});
|
|
|
|
describe('updatePrice', () => {
|
|
it('should update a single item price', () => {
|
|
pricingStore.getState().updatePrice('base', 300000);
|
|
|
|
const base = pricingStore.getState().items.find((i) => i.id === 'base');
|
|
expect(base!.price).toBe(300000);
|
|
});
|
|
|
|
it('should not affect other items when updating one', () => {
|
|
pricingStore.getState().updatePrice('base', 300000);
|
|
|
|
const kandura = pricingStore.getState().items.find((i) => i.id === 'emarati-kandura');
|
|
expect(kandura!.price).toBe(15000);
|
|
});
|
|
|
|
it('should persist to localStorage after update', () => {
|
|
pricingStore.getState().updatePrice('base', 999999);
|
|
|
|
expect(localStorage.setItem).toHaveBeenCalled();
|
|
const stored = JSON.parse(mockStorage['lootah-pricing']);
|
|
const base = stored.find((i: { id: string }) => i.id === 'base');
|
|
expect(base.price).toBe(999999);
|
|
});
|
|
});
|
|
|
|
describe('resetPrices', () => {
|
|
it('should restore all prices to defaults', () => {
|
|
pricingStore.getState().updatePrice('base', 1);
|
|
pricingStore.getState().updatePrice('emarati-kandura', 2);
|
|
|
|
pricingStore.getState().resetPrices();
|
|
|
|
const items = pricingStore.getState().items;
|
|
expect(items.find((i) => i.id === 'base')!.price).toBe(250000);
|
|
expect(items.find((i) => i.id === 'emarati-kandura')!.price).toBe(15000);
|
|
});
|
|
});
|
|
|
|
describe('hydrate', () => {
|
|
it('should load prices from localStorage', () => {
|
|
mockStorage['lootah-pricing'] = JSON.stringify([
|
|
{ id: 'base', label: 'G1 Robot Base', price: 500000 },
|
|
]);
|
|
|
|
pricingStore.getState().hydrate();
|
|
|
|
const base = pricingStore.getState().items.find((i) => i.id === 'base');
|
|
expect(base!.price).toBe(500000);
|
|
expect(pricingStore.getState().isHydrated).toBe(true);
|
|
});
|
|
|
|
it('should keep defaults for items not in localStorage', () => {
|
|
mockStorage['lootah-pricing'] = JSON.stringify([
|
|
{ id: 'base', label: 'G1 Robot Base', price: 500000 },
|
|
]);
|
|
|
|
pricingStore.getState().hydrate();
|
|
|
|
const kandura = pricingStore.getState().items.find((i) => i.id === 'emarati-kandura');
|
|
expect(kandura!.price).toBe(15000);
|
|
});
|
|
|
|
it('should set isHydrated even with no stored data', () => {
|
|
pricingStore.getState().hydrate();
|
|
expect(pricingStore.getState().isHydrated).toBe(true);
|
|
});
|
|
});
|
|
});
|