|
1 | 1 | import { describe, it, expect, beforeEach } from 'vitest'
|
| 2 | +import { createApp } from 'vue' |
2 | 3 | import { useLocalStorage } from '../src/composables/useLocalStorage'
|
3 | 4 |
|
4 | 5 | const storageKey = 'test'
|
5 | 6 |
|
6 |
| -describe('useLocalStorage', () => { |
7 |
| - beforeEach(function () { |
8 |
| - localStorage.clear() |
| 7 | +const withSetup = (composable) => { |
| 8 | + let result |
| 9 | + const app = createApp({ |
| 10 | + setup() { |
| 11 | + result = composable() |
| 12 | + |
| 13 | + return () => {} |
| 14 | + }, |
9 | 15 | })
|
| 16 | + |
| 17 | + app.mount(document.createElement('div')) |
| 18 | + |
| 19 | + return [result, app] |
| 20 | +} |
| 21 | + |
| 22 | +beforeEach(() => localStorage.clear()) |
| 23 | + |
| 24 | +describe('useLocalStorage', () => { |
10 | 25 | it('should work w/ default value', () => {
|
11 |
| - const storage = useLocalStorage(storageKey, 'default') |
| 26 | + const [storage, app] = withSetup(() => { |
| 27 | + return useLocalStorage(storageKey, 'default') |
| 28 | + }) |
| 29 | + |
12 | 30 | expect(storage.value).toBe('default')
|
13 | 31 | expect(localStorage.getItem(storageKey)).toBe('default')
|
| 32 | + |
| 33 | + app.unmount() |
14 | 34 | })
|
15 | 35 |
|
16 | 36 | it('should work w/ storage event', () => {
|
17 |
| - const storage = useLocalStorage(storageKey, 'default') |
| 37 | + const [storage, app] = withSetup(() => { |
| 38 | + return useLocalStorage(storageKey, 'default') |
| 39 | + }) |
18 | 40 | const storageEvent = new StorageEvent('storage', {
|
19 | 41 | key: storageKey,
|
20 | 42 | newValue: 'new value',
|
21 | 43 | })
|
| 44 | + |
22 | 45 | window.dispatchEvent(storageEvent)
|
| 46 | + |
23 | 47 | expect(storage.value).toBe('new value')
|
| 48 | + |
| 49 | + app.unmount() |
24 | 50 | })
|
25 | 51 | })
|
0 commit comments