From be0bc5c84fcdc51b696c94d774b650f3017764e5 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sat, 17 Jul 2021 19:10:05 +0200 Subject: [PATCH] Fix lists showing up multiple times in history --- src/modules/listHistory.js | 3 +++ src/modules/listHistory.test.js | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/modules/listHistory.js b/src/modules/listHistory.js index 96acbe332..0a5c0bb73 100644 --- a/src/modules/listHistory.js +++ b/src/modules/listHistory.js @@ -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) { diff --git a/src/modules/listHistory.test.js b/src/modules/listHistory.test.js index d3d8b8423..dce26d7ce 100644 --- a/src/modules/listHistory.test.js +++ b/src/modules/listHistory.test.js @@ -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)