Fix lists showing up multiple times in history
This commit is contained in:
parent
0e5954cf96
commit
be0bc5c84f
2 changed files with 15 additions and 0 deletions
|
@ -10,6 +10,8 @@ export const getHistory = () => {
|
||||||
export function saveListToHistory(list) {
|
export function saveListToHistory(list) {
|
||||||
const history = getHistory()
|
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
|
// Remove the element if it already exists in history, preventing duplicates and essentially moving it to the beginning
|
||||||
for (const i in history) {
|
for (const i in history) {
|
||||||
if (history[i].id === list.id) {
|
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)
|
history.unshift(list)
|
||||||
|
|
||||||
if (history.length > 5) {
|
if (history.length > 5) {
|
||||||
|
|
|
@ -53,6 +53,18 @@ test('don\'t store the same list twice', () => {
|
||||||
expect(saved).toBe('[{"id":1}]')
|
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', () => {
|
test('move a list to the beginning when storing it multiple times', () => {
|
||||||
let saved = null
|
let saved = null
|
||||||
Storage.prototype.getItem = jest.fn(() => saved)
|
Storage.prototype.getItem = jest.fn(() => saved)
|
||||||
|
|
Loading…
Reference in a new issue