Fix lists showing up multiple times in history

This commit is contained in:
kolaente 2021-07-17 19:10:05 +02:00
parent 0e5954cf96
commit be0bc5c84f
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
2 changed files with 15 additions and 0 deletions

View file

@ -10,6 +10,8 @@ export const getHistory = () => {
export function saveListToHistory(list) {
const history = getHistory()
list.id = parseInt(list.id)
// Remove the element if it already exists in history, preventing duplicates and essentially moving it to the beginning
for (const i in history) {
if (history[i].id === list.id) {
@ -17,6 +19,7 @@ export function saveListToHistory(list) {
}
}
// Add the new list to the beginning of the list
history.unshift(list)
if (history.length > 5) {

View file

@ -53,6 +53,18 @@ test('don\'t store the same list twice', () => {
expect(saved).toBe('[{"id":1}]')
})
test('don\'t store the same list twice with different id types', () => {
let saved = null
Storage.prototype.getItem = jest.fn(() => saved)
Storage.prototype.setItem = jest.fn((key, lists) => {
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', () => {
let saved = null
Storage.prototype.getItem = jest.fn(() => saved)