|
| 1 | +import { beforeEach, describe, expect, it } from 'vitest' |
| 2 | +import { TANSTACK_DEVTOOLS_STATE } from '../utils/storage' |
| 3 | +import { getStateFromLocalStorage } from './devtools-context' |
| 4 | + |
| 5 | +describe('getStateFromLocalStorage', () => { |
| 6 | + beforeEach(() => { |
| 7 | + localStorage.clear() |
| 8 | + }) |
| 9 | + it('should return undefined when no data in localStorage', () => { |
| 10 | + const state = getStateFromLocalStorage() |
| 11 | + expect(state).toEqual(undefined) |
| 12 | + }) |
| 13 | + it('should return parsed state from localStorage and not remove valid plugins', () => { |
| 14 | + const mockState = { |
| 15 | + activePlugins: ['plugin1'], |
| 16 | + settings: { |
| 17 | + theme: 'dark', |
| 18 | + }, |
| 19 | + } |
| 20 | + localStorage.setItem(TANSTACK_DEVTOOLS_STATE, JSON.stringify(mockState)) |
| 21 | + const state = getStateFromLocalStorage([ |
| 22 | + { |
| 23 | + id: 'plugin1', |
| 24 | + render: () => {}, |
| 25 | + name: 'Plugin 1', |
| 26 | + }, |
| 27 | + ]) |
| 28 | + expect(state).toEqual(mockState) |
| 29 | + }) |
| 30 | + it('should filter out inactive plugins', () => { |
| 31 | + const mockState = { |
| 32 | + activePlugins: ['plugin1', 'plugin2'], |
| 33 | + settings: { |
| 34 | + theme: 'dark', |
| 35 | + }, |
| 36 | + } |
| 37 | + localStorage.setItem(TANSTACK_DEVTOOLS_STATE, JSON.stringify(mockState)) |
| 38 | + const plugins = [{ id: 'plugin1', render: () => {}, name: 'Plugin 1' }] |
| 39 | + const state = getStateFromLocalStorage(plugins) |
| 40 | + expect(state?.activePlugins).toEqual(['plugin1']) |
| 41 | + }) |
| 42 | + it('should return empty plugin state if all active plugins are invalid', () => { |
| 43 | + const mockState = { |
| 44 | + activePlugins: ['plugin1', 'plugin2'], |
| 45 | + settings: { |
| 46 | + theme: 'dark', |
| 47 | + }, |
| 48 | + } |
| 49 | + localStorage.setItem(TANSTACK_DEVTOOLS_STATE, JSON.stringify(mockState)) |
| 50 | + const plugins = [{ id: 'plugin3', render: () => {}, name: 'Plugin 3' }] |
| 51 | + const state = getStateFromLocalStorage(plugins) |
| 52 | + expect(state?.activePlugins).toEqual([]) |
| 53 | + }) |
| 54 | + it('should handle invalid JSON in localStorage gracefully', () => { |
| 55 | + localStorage.setItem(TANSTACK_DEVTOOLS_STATE, 'invalid json') |
| 56 | + const state = getStateFromLocalStorage() |
| 57 | + expect(state).toEqual(undefined) |
| 58 | + }) |
| 59 | +}) |
0 commit comments