mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-12-22 17:14:22 +00:00
Implement LocalStorageMock for testing
This commit is contained in:
@@ -1,10 +1,44 @@
|
||||
import '@testing-library/jest-dom'
|
||||
import { afterEach, vi } from 'vitest'
|
||||
import { afterEach, beforeEach } from 'vitest'
|
||||
|
||||
// Mock fetch globally since your app uses fetch
|
||||
global.fetch = vi.fn()
|
||||
// Create a working localStorage implementation for tests
|
||||
// This ensures localStorage works in both CLI and VSCode test runner
|
||||
class LocalStorageMock implements Storage {
|
||||
private store: Map<string, string> = new Map()
|
||||
|
||||
// Clean up after each test
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks()
|
||||
get length(): number {
|
||||
return this.store.size
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.store.clear()
|
||||
}
|
||||
|
||||
getItem(key: string): string | null {
|
||||
return this.store.get(key) ?? null
|
||||
}
|
||||
|
||||
key(index: number): string | null {
|
||||
return Array.from(this.store.keys())[index] ?? null
|
||||
}
|
||||
|
||||
removeItem(key: string): void {
|
||||
this.store.delete(key)
|
||||
}
|
||||
|
||||
setItem(key: string, value: string): void {
|
||||
this.store.set(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
// Replace global localStorage
|
||||
global.localStorage = new LocalStorageMock()
|
||||
|
||||
// Clean up before each test
|
||||
beforeEach(() => {
|
||||
localStorage.clear()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
localStorage.clear()
|
||||
})
|
||||
Reference in New Issue
Block a user