33 lines
1.4 KiB
JavaScript
33 lines
1.4 KiB
JavaScript
import { readPurchaseDevice, rememberPurchaseDevice } from '@/utils/purchase-device-preference'
|
|
|
|
describe('purchase device preference', () => {
|
|
beforeEach(() => { localStorage.clear() })
|
|
afterEach(() => { jest.restoreAllMocks() })
|
|
|
|
it('isolates users and remembers an explicit empty selection', () => {
|
|
expect(rememberPurchaseDevice(1, 10)).toBe(true)
|
|
expect(rememberPurchaseDevice(2, 20)).toBe(true)
|
|
expect(readPurchaseDevice(1)).toBe(10)
|
|
expect(readPurchaseDevice(2)).toBe(20)
|
|
expect(rememberPurchaseDevice(1, '')).toBe(true)
|
|
expect(readPurchaseDevice(1)).toBeNull()
|
|
expect(readPurchaseDevice(2)).toBe(20)
|
|
})
|
|
|
|
it('does not share a fallback key when user identity is missing', () => {
|
|
expect(rememberPurchaseDevice(null, 10)).toBe(false)
|
|
expect(readPurchaseDevice(null)).toBeNull()
|
|
expect(localStorage.length).toBe(0)
|
|
})
|
|
|
|
it('ignores corrupt preferences and tolerates unavailable browser storage', () => {
|
|
rememberPurchaseDevice(1, 10)
|
|
localStorage.setItem(localStorage.key(0), '{broken')
|
|
expect(readPurchaseDevice(1)).toBeNull()
|
|
jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => { throw new Error('disabled') })
|
|
jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => { throw new Error('disabled') })
|
|
expect(readPurchaseDevice(1)).toBeNull()
|
|
expect(rememberPurchaseDevice(1, 10)).toBe(false)
|
|
})
|
|
})
|