34 lines
1.2 KiB
TypeScript
34 lines
1.2 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { readCachedPaymentLink, writeCachedPaymentLink } from './paymentLinkCache'
|
|
|
|
function createMockStorage(): Storage {
|
|
const store = new Map<string, string>()
|
|
return {
|
|
getItem: (key: string) => store.get(key) ?? null,
|
|
setItem: (key: string, value: string) => void store.set(key, value),
|
|
removeItem: (key: string) => void store.delete(key),
|
|
clear: () => store.clear(),
|
|
key: () => null,
|
|
get length() { return store.size },
|
|
}
|
|
}
|
|
|
|
describe('payment link cache', () => {
|
|
it('returns null when nothing is cached', () => {
|
|
expect(readCachedPaymentLink(createMockStorage())).toBeNull()
|
|
})
|
|
|
|
it('round-trips a cached link', () => {
|
|
const storage = createMockStorage()
|
|
writeCachedPaymentLink(storage, 'https://paypal.me/alice')
|
|
expect(readCachedPaymentLink(storage)).toBe('https://paypal.me/alice')
|
|
})
|
|
|
|
it('overwrites a previously cached link', () => {
|
|
const storage = createMockStorage()
|
|
writeCachedPaymentLink(storage, 'https://paypal.me/alice')
|
|
writeCachedPaymentLink(storage, 'https://paypal.me/bob')
|
|
expect(readCachedPaymentLink(storage)).toBe('https://paypal.me/bob')
|
|
})
|
|
})
|