This commit is contained in:
@@ -1,67 +1,105 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { readVisitedPots, recordVisitedPot } from './visitedPots'
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { readVisitedPots, recordVisitedPot } from './visitedPots';
|
||||
|
||||
function createMockStorage(): Storage {
|
||||
const store = new Map<string, string>()
|
||||
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 },
|
||||
}
|
||||
get length() {
|
||||
return store.size;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe('visited pots storage', () => {
|
||||
it('returns an empty list when nothing is stored', () => {
|
||||
expect(readVisitedPots(createMockStorage())).toEqual([])
|
||||
})
|
||||
expect(readVisitedPots(createMockStorage())).toEqual([]);
|
||||
});
|
||||
|
||||
it('records a visited pot', () => {
|
||||
const storage = createMockStorage()
|
||||
recordVisitedPot(storage, { slug: 'abc', name: 'Lisbon Trip', currency: 'EUR' })
|
||||
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' }),
|
||||
])
|
||||
})
|
||||
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 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')
|
||||
})
|
||||
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' })
|
||||
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' }),
|
||||
])
|
||||
})
|
||||
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([])
|
||||
})
|
||||
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()
|
||||
const storage = createMockStorage();
|
||||
for (let i = 0; i < 25; i++) {
|
||||
recordVisitedPot(storage, { slug: `pot-${i}`, name: `Pot ${i}`, currency: 'EUR' })
|
||||
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)
|
||||
})
|
||||
})
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user