fix(SPA): save article list pagination in query url

This commit is contained in:
Florian Sylvain committed 2026-10-13 16:20:37 +02:00
1 parent 54f27ac98f
commit 9d9453c1b3
7 files changed
+535 -2

No files matched your search

@@ -0,0 +1,55 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { readStoredPageSize, storePageSize } from '../pageSizeMemory'
const KEY = 'renewcms-page-size-articles'
const SIZES = [5, 25, 50, 100]
describe('pageSizeMemory', () => {
beforeEach(() => {
localStorage.clear()
})
afterEach(() => {
vi.unstubAllGlobals()
vi.restoreAllMocks()
})
it('reads null when nothing is stored', () => {
expect(readStoredPageSize(KEY, SIZES)).toBeNull()
})
it.each(['5', '25', '50', '100'])('reads the valid stored size %s', (raw) => {
localStorage.setItem(KEY, raw)
expect(readStoredPageSize(KEY, SIZES)).toBe(Number(raw))
})
it.each(['10', 'abc', '0', '-5', '25.5', '', '5.0', ' 5'])('treats %j as absent', (raw) => {
localStorage.setItem(KEY, raw)
expect(readStoredPageSize(KEY, SIZES)).toBeNull()
})
it('stores the bare number', () => {
storePageSize(KEY, 50)
expect(localStorage.getItem(KEY)).toBe('50')
})
it('reads as absent when getItem throws', () => {
vi.spyOn(Storage.prototype, 'getItem').mockImplementation(() => {
throw new Error('denied')
})
expect(readStoredPageSize(KEY, SIZES)).toBeNull()
})
it('swallows a setItem that throws', () => {
vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
throw new Error('quota')
})
expect(() => storePageSize(KEY, 50)).not.toThrow()
})
it('copes with localStorage being undefined', () => {
vi.stubGlobal('localStorage', undefined)
expect(readStoredPageSize(KEY, SIZES)).toBeNull()
expect(() => storePageSize(KEY, 50)).not.toThrow()
})
})
+18
View File
@@ -0,0 +1,18 @@
export const ARTICLES_PAGE_SIZE_KEY = 'renewcms-page-size-articles'
export function readStoredPageSize(key: string, validSizes: number[]) {
try {
const raw = localStorage.getItem(key)
return validSizes.find((size) => String(size) === raw) ?? null
} catch {
return null
}
}
export function storePageSize(key: string, size: number) {
try {
localStorage.setItem(key, String(size))
} catch {
return
}
}