Add typescript support for helper functions (#598)
Co-authored-by: kolaente <k@knt.li> Reviewed-on: https://kolaente.dev/vikunja/frontend/pulls/598 Co-authored-by: konrad <konrad@kola-entertainments.de> Co-committed-by: konrad <konrad@kola-entertainments.de>
This commit is contained in:
@ -26,9 +26,9 @@ test('store list in history', () => {
|
||||
})
|
||||
|
||||
test('store only the last 5 lists in history', () => {
|
||||
let saved = null
|
||||
let saved: string | null = null
|
||||
Storage.prototype.getItem = jest.fn(() => saved)
|
||||
Storage.prototype.setItem = jest.fn((key, lists) => {
|
||||
Storage.prototype.setItem = jest.fn((key: string, lists: string) => {
|
||||
saved = lists
|
||||
})
|
||||
|
||||
@ -42,9 +42,9 @@ test('store only the last 5 lists in history', () => {
|
||||
})
|
||||
|
||||
test('don\'t store the same list twice', () => {
|
||||
let saved = null
|
||||
let saved: string | null = null
|
||||
Storage.prototype.getItem = jest.fn(() => saved)
|
||||
Storage.prototype.setItem = jest.fn((key, lists) => {
|
||||
Storage.prototype.setItem = jest.fn((key: string, lists: string) => {
|
||||
saved = lists
|
||||
})
|
||||
|
||||
@ -53,22 +53,10 @@ 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
|
||||
let saved: string | null = null
|
||||
Storage.prototype.getItem = jest.fn(() => saved)
|
||||
Storage.prototype.setItem = jest.fn((key, lists) => {
|
||||
Storage.prototype.setItem = jest.fn((key: string, lists: string) => {
|
||||
saved = lists
|
||||
})
|
||||
|
@ -1,4 +1,8 @@
|
||||
export const getHistory = () => {
|
||||
interface ListHistory {
|
||||
id: number;
|
||||
}
|
||||
|
||||
export function getHistory(): ListHistory[] {
|
||||
const savedHistory = localStorage.getItem('listHistory')
|
||||
if (savedHistory === null) {
|
||||
return []
|
||||
@ -7,17 +11,17 @@ export const getHistory = () => {
|
||||
return JSON.parse(savedHistory)
|
||||
}
|
||||
|
||||
export function saveListToHistory(list) {
|
||||
export function saveListToHistory(list: ListHistory) {
|
||||
const history = getHistory()
|
||||
|
||||
list.id = parseInt(list.id)
|
||||
// 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) {
|
||||
history.forEach((l, i) => {
|
||||
if (l.id === list.id) {
|
||||
history.splice(i, 1)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Add the new list to the beginning of the list
|
||||
history.unshift(list)
|
Reference in New Issue
Block a user