diff --git a/packages/nuxt/src/app/composables/state.ts b/packages/nuxt/src/app/composables/state.ts index 1c44302576..4b5c1ec97f 100644 --- a/packages/nuxt/src/app/composables/state.ts +++ b/packages/nuxt/src/app/composables/state.ts @@ -42,6 +42,8 @@ export function clearNuxtState ( ): void { const nuxtApp = useNuxtApp() const _allKeys = Object.keys(nuxtApp.payload.state) + .map(key => key.substring(useStateKeyPrefix.length)) + const _keys: string[] = !keys ? _allKeys : typeof keys === 'function' diff --git a/test/nuxt/composables.test.ts b/test/nuxt/composables.test.ts index 340b9a2c61..7922f25400 100644 --- a/test/nuxt/composables.test.ts +++ b/test/nuxt/composables.test.ts @@ -214,12 +214,50 @@ describe('useState', () => { useState('key', () => 'value') expect(Object.entries(useNuxtApp().payload.state)).toContainEqual(['$skey', 'value']) }) +}) - it.todo('clearNuxtState', () => { - const state = useState(() => 'test') +describe('clearNuxtState', () => { + it('clears state in payload for single key', () => { + const key = 'clearNuxtState-test' + const state = useState(key, () => 'test') expect(state.value).toBe('test') + clearNuxtState(key) + expect(state.value).toBeUndefined() + }) + + it('clears state in payload for array of keys', () => { + const key1 = 'clearNuxtState-test' + const key2 = 'clearNuxtState-test2' + const state1 = useState(key1, () => 'test') + const state2 = useState(key2, () => 'test') + expect(state1.value).toBe('test') + expect(state2.value).toBe('test') + clearNuxtState([key1, 'other']) + expect(state1.value).toBeUndefined() + expect(state2.value).toBe('test') + clearNuxtState([key1, key2]) + expect(state1.value).toBeUndefined() + expect(state2.value).toBeUndefined() + }) + + it('clears state in payload for function', () => { + const key = 'clearNuxtState-test' + const state = useState(key, () => 'test') + expect(state.value).toBe('test') + clearNuxtState(() => false) + expect(state.value).toBe('test') + clearNuxtState(k => k === key) + expect(state.value).toBeUndefined() + }) + + it('clears all state when no key is provided', () => { + const state1 = useState('clearNuxtState-test', () => 'test') + const state2 = useState('clearNuxtState-test2', () => 'test') + expect(state1.value).toBe('test') + expect(state2.value).toBe('test') clearNuxtState() - expect.soft(state.value).toBeUndefined() + expect(state1.value).toBeUndefined() + expect(state2.value).toBeUndefined() }) })