Skip to content
Source
Sign inSign up
TanStack/devtools
Open History to see the last commit

config.test.ts

130 lines3.7 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import {
3 DEFAULT_CONFIG,
4 loadConfig,
5 mergeConfig,
6 saveConfig,
7} from '../src/core/utils/config.utils'
8​
9describe('config', () => {
10 // Mock localStorage
11 const localStorageMock = (() => {
12 let store: Record<string, string> = {}
13 return {
14 getItem: vi.fn((key: string) => store[key] || null),
15 setItem: vi.fn((key: string, value: string) => {
16 store[key] = value
17 }),
18 removeItem: vi.fn((key: string) => {
19 delete store[key]
20 }),
21 clear: vi.fn(() => {
22 store = {}
23 }),
24 }
25 })()
26​
27 beforeEach(() => {
28 vi.stubGlobal('localStorage', localStorageMock)
29 localStorageMock.clear()
30 })
31​
32 afterEach(() => {
33 vi.restoreAllMocks()
34 })
35​
36 describe('DEFAULT_CONFIG', () => {
37 it('should have expected default values', () => {
38 expect(DEFAULT_CONFIG.threshold).toBe('serious')
39 expect(DEFAULT_CONFIG.ruleSet).toBe('wcag21aa')
40 expect(DEFAULT_CONFIG.showOverlays).toBe(true)
41 expect(DEFAULT_CONFIG.persistSettings).toBe(true)
42 })
43 })
44​
45 describe('loadConfig', () => {
46 it('should return DEFAULT_CONFIG when localStorage is empty', () => {
47 const config = loadConfig()
48 expect(config).toEqual(DEFAULT_CONFIG)
49 })
50​
51 it('should merge stored config with defaults', () => {
52 localStorageMock.setItem(
53 'tanstack-devtools-a11y-config',
54 JSON.stringify({ ruleSet: 'wcag22aa', showOverlays: false }),
55 )
56​
57 const config = loadConfig()
58 expect(config.ruleSet).toBe('wcag22aa')
59 expect(config.showOverlays).toBe(false)
60 expect(config.threshold).toBe('serious') // default preserved
61 })
62​
63 it('should return DEFAULT_CONFIG when stored JSON is invalid', () => {
64 localStorageMock.setItem(
65 'tanstack-devtools-a11y-config',
66 'invalid json{{{',
67 )
68 const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
69​
70 const config = loadConfig()
71 expect(config).toEqual(DEFAULT_CONFIG)
72 expect(warnSpy).toHaveBeenCalled()
73 })
74 })
75​
76 describe('saveConfig', () => {
77 it('should save config to localStorage', () => {
78 saveConfig({ ruleSet: 'section508' })
79​
80 expect(localStorageMock.setItem).toHaveBeenCalled()
81 const stored = JSON.parse(
82 localStorageMock.getItem('tanstack-devtools-a11y-config') || '{}',
83 )
84 expect(stored.ruleSet).toBe('section508')
85 })
86​
87 it('should merge with existing config', () => {
88 localStorageMock.setItem(
89 'tanstack-devtools-a11y-config',
90 JSON.stringify({ showOverlays: false }),
91 )
92​
93 saveConfig({ ruleSet: 'wcag22aa' })
94​
95 const stored = JSON.parse(
96 localStorageMock.getItem('tanstack-devtools-a11y-config') || '{}',
97 )
98 expect(stored.ruleSet).toBe('wcag22aa')
99 expect(stored.showOverlays).toBe(false)
100 })
101 })
102​
103 describe('mergeConfig', () => {
104 it('should merge user options with saved config when persistSettings is true', () => {
105 localStorageMock.setItem(
106 'tanstack-devtools-a11y-config',
107 JSON.stringify({ showOverlays: false }),
108 )
109​
110 const config = mergeConfig({ ruleSet: 'wcag22aa' })
111 expect(config.ruleSet).toBe('wcag22aa')
112 expect(config.showOverlays).toBe(false)
113 })
114​
115 it('should ignore saved config when persistSettings is false', () => {
116 localStorageMock.setItem(
117 'tanstack-devtools-a11y-config',
118 JSON.stringify({ showOverlays: false }),
119 )
120​
121 const config = mergeConfig({ persistSettings: false })
122 expect(config.showOverlays).toBe(true) // default, not saved
123 })
124​
125 it('should return defaults when no options provided', () => {
126 const config = mergeConfig()
127 expect(config).toEqual(DEFAULT_CONFIG)
128 })
129 })
130})