-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #216 from bertmad3400/catchError
Catch error when writting to local storage
- Loading branch information
Showing
4 changed files
with
58 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* @vitest-environment jsdom | ||
* @vitest-environment-options { "storageQuota": "0" } | ||
*/ | ||
|
||
import { persisted } from '../index' | ||
import { expect, vi, beforeEach, describe, it } from 'vitest' | ||
|
||
beforeEach(() => localStorage.clear()) | ||
|
||
describe('persisted()', () => { | ||
|
||
it('logs error encountered when saving to local storage', () => { | ||
const consoleMock = vi.spyOn(console, 'error').mockImplementation(() => undefined); | ||
const store = persisted('myKey', 'myVal') | ||
|
||
store.set("myNewVal") | ||
|
||
expect(consoleMock).toHaveBeenCalledWith("Error when writing value from persisted store \"myKey\" to local", new DOMException) | ||
consoleMock.mockReset(); | ||
}) | ||
|
||
it('calls custom error function', () => { | ||
const mockFunc = vi.fn() | ||
|
||
const store = persisted('myKey2', 'myVal', { onError: mockFunc }) | ||
store.set("myNewVal") | ||
|
||
expect(mockFunc).toHaveBeenCalledOnce() | ||
}) | ||
}) |
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