-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(editor): Fix local storage flags defaulting to undefined string (#…
…7603) useStorage takes the default value `undefined` and sets it in local storage.. also returns "undefined" as string which breaks onboarding flows Github issue / Community forum post (link here to close automatically):
- Loading branch information
Showing
12 changed files
with
97 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { nextTick } from 'vue'; | ||
import { useStorage } from './useStorage'; | ||
|
||
describe('useStorage', () => { | ||
beforeEach(() => { | ||
localStorage.clear(); | ||
}); | ||
|
||
it('should initialize with null if no value is stored in localStorage', () => { | ||
const key = 'test-key'; | ||
const data = useStorage(key); | ||
|
||
expect(data.value).toBeNull(); | ||
}); | ||
|
||
it('should initialize with the stored value if it exists in localStorage', () => { | ||
const key = 'test-key'; | ||
const value = 'test-value'; | ||
localStorage.setItem(key, value); | ||
|
||
const data = useStorage(key); | ||
expect(data.value).toBe(value); | ||
}); | ||
|
||
it('should update localStorage when the data ref is updated', async () => { | ||
const key = 'test-key'; | ||
const value = 'test-value'; | ||
const data = useStorage(key); | ||
|
||
data.value = value; | ||
await nextTick(); | ||
|
||
expect(localStorage.getItem(key)).toBe(value); | ||
}); | ||
|
||
it('should remove the key from localStorage when the data ref is set to null', async () => { | ||
const key = 'test-key'; | ||
const value = 'test-value'; | ||
localStorage.setItem(key, value); | ||
|
||
const data = useStorage(key); | ||
|
||
data.value = null; | ||
await nextTick(); | ||
|
||
expect(localStorage.getItem(key)).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useStorage as useStorageComposable } from '@vueuse/core'; | ||
import type { Ref } from 'vue'; | ||
|
||
export function useStorage(key: string): Ref<string | null> { | ||
const data = useStorageComposable(key, null, undefined, { writeDefaults: false }); | ||
|
||
// bug in 1.15.1 | ||
if (data.value === 'undefined') { | ||
data.value = null; | ||
} | ||
|
||
return data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters