-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💥 Fix use local storage for ssr (#129)
* Add useLocalStorage tests * Add check for window in useLocalStorage
- Loading branch information
Showing
7 changed files
with
120 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@usedapp/core": patch | ||
--- | ||
|
||
💥 Fix useLocalStorage for serverSide rendering |
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,7 +1,8 @@ | ||
{ | ||
"spec": "test/**/*.test.{ts,tsx}", | ||
"require": "ts-node/register", | ||
"watchExtensions": "ts", | ||
"extension": "ts", | ||
"timeout": 3000 | ||
"spec": "test/**/*.test.{ts,tsx}", | ||
"require": "ts-node/register", | ||
"watchExtensions": "ts", | ||
"extension": "ts", | ||
"timeout": 3000, | ||
"file": "./test/setup.ts" | ||
} |
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,87 @@ | ||
import { act, renderHook } from '@testing-library/react-hooks' | ||
import { expect } from 'chai' | ||
import { useLocalStorage } from '../../src/hooks/useLocalStorage' | ||
|
||
describe('useLocalStorage', () => { | ||
beforeEach(() => { | ||
window.localStorage.clear() | ||
}) | ||
|
||
function render() { | ||
let key = 'foo' | ||
const result = renderHook(() => useLocalStorage(key)) | ||
return { | ||
getValue: () => result.result.current[0], | ||
setValue: (value: any) => act(() => result.result.current[1](value)), | ||
setKey: (value: string) => { | ||
key = value | ||
result.rerender() | ||
}, | ||
} | ||
} | ||
|
||
it('returns undefined for empty storage', () => { | ||
const { getValue } = render() | ||
expect(getValue()).to.equal(undefined) | ||
}) | ||
|
||
it('parses existing values', () => { | ||
window.localStorage.setItem('foo', JSON.stringify({ a: 1 })) | ||
const { getValue } = render() | ||
expect(getValue()).to.deep.equal({ a: 1 }) | ||
}) | ||
|
||
it('caches results', () => { | ||
window.localStorage.setItem('foo', JSON.stringify({ a: 1 })) | ||
const { getValue } = render() | ||
expect(getValue()).to.deep.equal({ a: 1 }) | ||
window.localStorage.setItem('foo', JSON.stringify({ a: 2 })) | ||
expect(getValue()).to.deep.equal({ a: 1 }) | ||
}) | ||
|
||
it('returns undefined when cannot parse', () => { | ||
window.localStorage.setItem('foo', 'x{}y') | ||
const { getValue } = render() | ||
expect(getValue()).to.equal(undefined) | ||
}) | ||
|
||
it('modifies the localStorage and returns a the new value', () => { | ||
const { getValue, setValue } = render() | ||
expect(getValue()).to.equal(undefined) | ||
setValue({ a: 1 }) | ||
expect(window.localStorage.getItem('foo')).to.equal('{"a":1}') | ||
expect(getValue()).to.deep.equal({ a: 1 }) | ||
}) | ||
|
||
it('can remove the item by setting undefined', () => { | ||
window.localStorage.setItem('foo', 'true') | ||
const { getValue, setValue } = render() | ||
expect(getValue()).to.equal(true) | ||
setValue(undefined) | ||
expect(getValue()).to.equal(undefined) | ||
expect(window.localStorage.getItem('foo')).to.equal(null) | ||
}) | ||
|
||
it('can change keys', () => { | ||
window.localStorage.setItem('foo', 'true') | ||
const { getValue, setKey } = render() | ||
expect(getValue()).to.equal(true) | ||
setKey('bar') | ||
expect(getValue()).to.equal(undefined) | ||
expect(window.localStorage.getItem('foo')).to.equal('true') | ||
expect(window.localStorage.getItem('bar')).to.equal(null) | ||
}) | ||
|
||
it('can change keys and modify the other value', () => { | ||
window.localStorage.setItem('foo', 'true') | ||
window.localStorage.setItem('bar', 'false') | ||
const { getValue, setValue, setKey } = render() | ||
expect(getValue()).to.equal(true) | ||
setValue(123) | ||
setKey('bar') | ||
expect(getValue()).to.equal(false) | ||
setValue(456) | ||
expect(window.localStorage.getItem('foo')).to.equal('123') | ||
expect(window.localStorage.getItem('bar')).to.equal('456') | ||
}) | ||
}) |
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,2 @@ | ||
import 'jsdom-global/register' | ||
import 'mock-local-storage' |
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