mirror of
https://github.com/tompro/sattle.git
synced 2026-08-27 07:15:59 +00:00
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
// Shared test scaffolding: an in-memory localStorage (Node has none outside
|
|
// a browser) plus the mock-mint helpers every suite uses.
|
|
|
|
import {vi} from 'vitest'
|
|
import {parseJsonArray, parseJsonObject, parseJsonObjectArray} from './jsonParsing'
|
|
|
|
export {parseJsonArray, parseJsonObject, parseJsonObjectArray}
|
|
|
|
export class LocalStorageStub {
|
|
private map = new Map<string, string>()
|
|
getItem = (key: string): string | null => this.map.get(key) ?? null
|
|
setItem = (key: string, value: string): void => {
|
|
this.map.set(key, String(value))
|
|
}
|
|
removeItem = (key: string): void => {
|
|
this.map.delete(key)
|
|
}
|
|
clear = (): void => {
|
|
this.map.clear()
|
|
}
|
|
get length(): number {
|
|
return this.map.size
|
|
}
|
|
key = (index: number): string | null => [...this.map.keys()][index] ?? null
|
|
}
|
|
|
|
export const stubLocalStorage = (): LocalStorageStub => {
|
|
const stub = new LocalStorageStub()
|
|
vi.stubGlobal('localStorage', stub)
|
|
return stub
|
|
}
|
|
|
|
export const requiredValue = <T>(
|
|
value: T | null | undefined,
|
|
message = 'Expected test value to be present',
|
|
): T => {
|
|
if (value === null || value === undefined) throw new TypeError(message)
|
|
return value
|
|
}
|
|
|
|
export const requiredString = (value: unknown, message = 'Expected a string value'): string => {
|
|
if (typeof value !== 'string') throw new TypeError(message)
|
|
return value
|
|
}
|