Files
splitt-the-bill/app/utils/visitedPots.test.ts
T
anbraten cbe1b42f6e
tinyedge/deploy Deployed by TinyEdge
🐛 add env to useDb and apply prettier
2026-08-07 13:28:26 +02:00

106 lines
2.7 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { readVisitedPots, recordVisitedPot } from './visitedPots';
function createMockStorage(): Storage {
const store = new Map<string, string>();
return {
getItem: (key: string) => store.get(key) ?? null,
setItem: (key: string, value: string) => void store.set(key, value),
removeItem: (key: string) => void store.delete(key),
clear: () => store.clear(),
key: () => null,
get length() {
return store.size;
},
};
}
describe('visited pots storage', () => {
it('returns an empty list when nothing is stored', () => {
expect(readVisitedPots(createMockStorage())).toEqual([]);
});
it('records a visited pot', () => {
const storage = createMockStorage();
recordVisitedPot(storage, {
slug: 'abc',
name: 'Lisbon Trip',
currency: 'EUR',
});
expect(readVisitedPots(storage)).toEqual([
expect.objectContaining({
slug: 'abc',
name: 'Lisbon Trip',
currency: 'EUR',
}),
]);
});
it('moves a re-visited pot to the front instead of duplicating it', () => {
const storage = createMockStorage();
recordVisitedPot(storage, {
slug: 'abc',
name: 'Lisbon Trip',
currency: 'EUR',
});
recordVisitedPot(storage, {
slug: 'def',
name: 'Ski Weekend',
currency: 'CHF',
});
recordVisitedPot(storage, {
slug: 'abc',
name: 'Lisbon Trip',
currency: 'EUR',
});
const pots = readVisitedPots(storage);
expect(pots).toHaveLength(2);
expect(pots[0]?.slug).toBe('abc');
});
it('updates the stored name and currency on revisit', () => {
const storage = createMockStorage();
recordVisitedPot(storage, {
slug: 'abc',
name: 'Old Name',
currency: 'EUR',
});
recordVisitedPot(storage, {
slug: 'abc',
name: 'New Name',
currency: 'USD',
});
expect(readVisitedPots(storage)).toEqual([
expect.objectContaining({
slug: 'abc',
name: 'New Name',
currency: 'USD',
}),
]);
});
it('ignores malformed JSON', () => {
const storage = createMockStorage();
storage.setItem('stb:visited-pots', '{not json');
expect(readVisitedPots(storage)).toEqual([]);
});
it('caps the list at 20 entries, keeping the most recent', () => {
const storage = createMockStorage();
for (let i = 0; i < 25; i++) {
recordVisitedPot(storage, {
slug: `pot-${i}`,
name: `Pot ${i}`,
currency: 'EUR',
});
}
const pots = readVisitedPots(storage);
expect(pots).toHaveLength(20);
expect(pots[0]?.slug).toBe('pot-24');
expect(pots.some((p) => p.slug === 'pot-0')).toBe(false);
});
});