-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle inaccessible
localStorage
gracefully (#10)
* fix: cover the case were accessing `localStorage` can throw error in Node Internally in `jsdom` has a getter function defined for `localStorage` that can in some circumstances throw a runtime error that is not handled by the package The code has changed to use a function to qeury whether `localStorage` is available in the runtime context. If accessing `localStorage` global throws an error it returns an error otherwise `true` fixes #9 * fix(localStorage): merge tests when localStorage is undefined * chore(CookieStore): change function name to "supportsLocalStorage" Co-authored-by: Weyert de Boer <[email protected]> Co-authored-by: Artem Zakharchenko <[email protected]>
- Loading branch information
1 parent
ea8a387
commit 80abf96
Showing
4 changed files
with
89 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { default as store, PERSISTENCY_KEY } from './CookieStore' | ||
export * from './CookieStore' |
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,45 @@ | ||
import { Store, store } from '../src' | ||
|
||
const originalLocalStorage: Storage = localStorage | ||
|
||
beforeAll(() => { | ||
Object.defineProperty(window, 'localStorage', { | ||
value: undefined, | ||
writable: true, | ||
}) | ||
}) | ||
|
||
afterAll(() => { | ||
Object.defineProperty(window, 'localStorage', { | ||
value: originalLocalStorage, | ||
}) | ||
}) | ||
|
||
it('adds response cookies to the store even if "localStorage" is unavailable', () => { | ||
store.add( | ||
new Request('https://example.com'), | ||
new Response(null, { | ||
headers: new Headers({ | ||
'Set-Cookie': 'cookieName=abc-123', | ||
}), | ||
}), | ||
) | ||
store.persist() | ||
|
||
expect(store.getAll()).toEqual<Store>( | ||
new Map([ | ||
[ | ||
'https://example.com', | ||
new Map([ | ||
[ | ||
'cookieName', | ||
{ | ||
name: 'cookieName', | ||
value: 'abc-123', | ||
}, | ||
], | ||
]), | ||
], | ||
]), | ||
) | ||
}) |
This file was deleted.
Oops, something went wrong.