2022-04-23 13:06:34 +02:00
|
|
|
import {test, expect, vi} from 'vitest'
|
2021-07-20 18:03:38 +02:00
|
|
|
import {getHistory, removeListFromHistory, saveListToHistory} from './listHistory'
|
2021-07-06 22:22:57 +02:00
|
|
|
|
|
|
|
test('return an empty history when none was saved', () => {
|
2022-04-23 13:06:34 +02:00
|
|
|
Storage.prototype.getItem = vi.fn(() => null)
|
2021-07-06 22:22:57 +02:00
|
|
|
const h = getHistory()
|
|
|
|
expect(h).toStrictEqual([])
|
|
|
|
})
|
|
|
|
|
|
|
|
test('return a saved history', () => {
|
|
|
|
const saved = [{id: 1}, {id: 2}]
|
2022-04-23 13:06:34 +02:00
|
|
|
Storage.prototype.getItem = vi.fn(() => JSON.stringify(saved))
|
2021-07-06 22:22:57 +02:00
|
|
|
|
|
|
|
const h = getHistory()
|
|
|
|
expect(h).toStrictEqual(saved)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('store list in history', () => {
|
|
|
|
let saved = {}
|
2022-04-23 13:06:34 +02:00
|
|
|
Storage.prototype.getItem = vi.fn(() => null)
|
|
|
|
Storage.prototype.setItem = vi.fn((key, lists) => {
|
2021-07-06 22:22:57 +02:00
|
|
|
saved = lists
|
|
|
|
})
|
|
|
|
|
|
|
|
saveListToHistory({id: 1})
|
|
|
|
expect(saved).toBe('[{"id":1}]')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('store only the last 5 lists in history', () => {
|
2021-07-19 20:20:49 +02:00
|
|
|
let saved: string | null = null
|
2022-04-23 13:06:34 +02:00
|
|
|
Storage.prototype.getItem = vi.fn(() => saved)
|
|
|
|
Storage.prototype.setItem = vi.fn((key: string, lists: string) => {
|
2021-07-06 22:22:57 +02:00
|
|
|
saved = lists
|
|
|
|
})
|
|
|
|
|
|
|
|
saveListToHistory({id: 1})
|
|
|
|
saveListToHistory({id: 2})
|
|
|
|
saveListToHistory({id: 3})
|
|
|
|
saveListToHistory({id: 4})
|
|
|
|
saveListToHistory({id: 5})
|
|
|
|
saveListToHistory({id: 6})
|
|
|
|
expect(saved).toBe('[{"id":6},{"id":5},{"id":4},{"id":3},{"id":2}]')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('don\'t store the same list twice', () => {
|
2021-07-19 20:20:49 +02:00
|
|
|
let saved: string | null = null
|
2022-04-23 13:06:34 +02:00
|
|
|
Storage.prototype.getItem = vi.fn(() => saved)
|
|
|
|
Storage.prototype.setItem = vi.fn((key: string, lists: string) => {
|
2021-07-06 22:22:57 +02:00
|
|
|
saved = lists
|
|
|
|
})
|
|
|
|
|
|
|
|
saveListToHistory({id: 1})
|
|
|
|
saveListToHistory({id: 1})
|
|
|
|
expect(saved).toBe('[{"id":1}]')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('move a list to the beginning when storing it multiple times', () => {
|
2021-07-19 20:20:49 +02:00
|
|
|
let saved: string | null = null
|
2022-04-23 13:06:34 +02:00
|
|
|
Storage.prototype.getItem = vi.fn(() => saved)
|
|
|
|
Storage.prototype.setItem = vi.fn((key: string, lists: string) => {
|
2021-07-06 22:22:57 +02:00
|
|
|
saved = lists
|
|
|
|
})
|
|
|
|
|
|
|
|
saveListToHistory({id: 1})
|
|
|
|
saveListToHistory({id: 2})
|
|
|
|
saveListToHistory({id: 1})
|
|
|
|
expect(saved).toBe('[{"id":1},{"id":2}]')
|
|
|
|
})
|
2021-07-20 18:03:38 +02:00
|
|
|
|
|
|
|
test('remove list from history', () => {
|
|
|
|
let saved: string | null = '[{"id": 1}]'
|
2022-04-23 13:06:34 +02:00
|
|
|
Storage.prototype.getItem = vi.fn(() => null)
|
|
|
|
Storage.prototype.setItem = vi.fn((key: string, lists: string) => {
|
2021-07-20 18:03:38 +02:00
|
|
|
saved = lists
|
|
|
|
})
|
2022-04-23 13:06:34 +02:00
|
|
|
Storage.prototype.removeItem = vi.fn((key: string) => {
|
2021-07-20 18:03:38 +02:00
|
|
|
saved = null
|
|
|
|
})
|
|
|
|
|
|
|
|
removeListFromHistory({id: 1})
|
|
|
|
expect(saved).toBeNull()
|
|
|
|
})
|