Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix initializeWithValue default not respected, fix docs and tests #1003

Merged
merged 5 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/useLocalStorageValue/__docs__/example.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const Example: React.FC<ExampleProps> = ({
}) => {
const lsVal = useLocalStorageValue(key, {
defaultValue,
initializeWithValue: true,
});

return (
Expand Down
11 changes: 6 additions & 5 deletions src/useLocalStorageValue/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Example } from './example.stories'
import { ImportPath } from '../../__docs__/ImportPath'
import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs'
import {Example} from './example.stories'
import {ImportPath} from '../../__docs__/ImportPath'
import {ArgsTable, Canvas, Meta, Story} from '@storybook/addon-docs'

<Meta title="Side-effect/useLocalStorageValue" component={Example} />

Expand All @@ -18,8 +18,9 @@ Manages a single LocalStorage key.
> Does not allow usage of `null` value, since JSON allows serializing `null` values - it would be
> impossible to separate null value fom 'no such value' API result which is also `null`.
> Due to support of SSR this hook returns undefined on first render even if value is there, to avoid
> this behavior set the `initializeWithValue` option to true.
> If you are doing SSR, set `initializeWithValue` to `false` in order for this hook to return
> `undefined` on first render. The LocalStorage value will be fetched client-side when effects
> are executed.
#### Example

Expand Down
2 changes: 1 addition & 1 deletion src/useSessionStorageValue/__docs__/example.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const Example: React.FC<ExampleProps> = ({
key = 'react-hookz-ss-test',
defaultValue = '@react-hookz is awesome',
}) => {
const ssVal = useSessionStorageValue(key, { defaultValue, initializeWithValue: true });
const ssVal = useSessionStorageValue(key, { defaultValue });

return (
<div>
Expand Down
11 changes: 6 additions & 5 deletions src/useSessionStorageValue/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Example } from './example.stories'
import { ImportPath } from '../../__docs__/ImportPath'
import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs'
import {Example} from './example.stories'
import {ImportPath} from '../../__docs__/ImportPath'
import {ArgsTable, Canvas, Meta, Story} from '@storybook/addon-docs'

<Meta title="Side-effect/useSessionStorageValue" component={Example} />

Expand All @@ -18,8 +18,9 @@ Manages a single SessionStorage key.
> Does not allow usage of `null` value, since JSON allows serializing `null` values - it would be
> impossible to separate null value fom 'no such value' API result which is also `null`.
> Due to support of SSR this hook returns undefined on first render even if value is there, to avoid
> this behavior set the `initializeWithValue` option to true.
> If you are doing SSR, set `initializeWithValue` to `false` in order for this hook to return
> `undefined` on first render. The SessionStorage value will be fetched client-side when effects
> are executed.
#### Example

Expand Down
93 changes: 53 additions & 40 deletions src/useStorageValue/__tests__/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,58 +12,71 @@ describe('useStorageValue', () => {
expect(result.error).toBeUndefined();
});

it('should not fetch value from storage on init', () => {
const storage = newStorage();
const { result } = renderHook(() => useStorageValue(storage, 'foo'));
describe('if initializeWithValue set to false', () => {
it('should not fetch value from storage on init', () => {
const storage = newStorage();
const { result } = renderHook(() =>
useStorageValue(storage, 'foo', { initializeWithValue: false })
);

expect(result.current.value).toBe(undefined);
expect(storage.getItem).not.toHaveBeenCalled();
});
expect(result.current.value).toBe(undefined);
expect(storage.getItem).not.toHaveBeenCalled();
});

it('should not fetch value from storage on .fetch() call', () => {
const storage = newStorage();
const { result } = renderHook(() => useStorageValue(storage, 'foo'));
it('should not fetch value from storage on .fetch() call', () => {
const storage = newStorage();
const { result } = renderHook(() =>
useStorageValue(storage, 'foo', { initializeWithValue: false })
);

expect(result.current.value).toBe(undefined);
act(() => {
result.current.fetch();
expect(result.current.value).toBe(undefined);
act(() => {
result.current.fetch();
});
expect(result.current.value).toBe(undefined);
expect(storage.getItem).not.toHaveBeenCalled();
});
expect(result.current.value).toBe(undefined);
expect(storage.getItem).not.toHaveBeenCalled();
});

it('should not set storage value on .set() call', () => {
const storage = newStorage();
const { result } = renderHook(() => useStorageValue<string>(storage, 'foo'));
it('should not set storage value on .set() call', () => {
const storage = newStorage();
const { result } = renderHook(() =>
useStorageValue<string>(storage, 'foo', { initializeWithValue: false })
);

expect(result.current.value).toBe(undefined);
act(() => {
result.current.set('bar');
expect(result.current.value).toBe(undefined);
act(() => {
result.current.set('bar');
});
expect(result.current.value).toBe(undefined);
expect(storage.setItem).not.toHaveBeenCalled();
});
expect(result.current.value).toBe(undefined);
expect(storage.setItem).not.toHaveBeenCalled();
});

it('should not call storage`s removeItem on .remove() call', () => {
const storage = newStorage();
const { result } = renderHook(() => useStorageValue<string>(storage, 'foo'));
it('should not call storage`s removeItem on .remove() call', () => {
const storage = newStorage();
const { result } = renderHook(() =>
useStorageValue<string>(storage, 'foo', { initializeWithValue: false })
);

act(() => {
result.current.remove();
act(() => {
result.current.remove();
});
expect(storage.removeItem).not.toHaveBeenCalled();
});
expect(storage.removeItem).not.toHaveBeenCalled();
});

it('should not set state to default value on item remove', () => {
const storage = newStorage(() => '"bar"');
const { result } = renderHook(() =>
useStorageValue<string>(storage, 'foo', { defaultValue: 'default value' })
);
it('should not set state to default value on item remove', () => {
const storage = newStorage(() => '"bar"');
const { result } = renderHook(() =>
useStorageValue<string>(storage, 'foo', {
defaultValue: 'default value',
initializeWithValue: false,
})
);

expect(result.current.value).toBe(undefined);
act(() => {
result.current.remove();
expect(result.current.value).toBe(undefined);
act(() => {
result.current.remove();
});
expect(result.current.value).toBe(undefined);
});
expect(result.current.value).toBe(undefined);
});
});
6 changes: 4 additions & 2 deletions src/useStorageValue/useStorageValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ export function useStorageValue<

const isFirstMount = useFirstMountState();
const [state, setState] = useState<Type | null | undefined>(
options?.initializeWithValue && isFirstMount ? storageActions.current.fetch() : undefined
optionsRef.current?.initializeWithValue && isFirstMount
xobotyi marked this conversation as resolved.
Show resolved Hide resolved
? storageActions.current.fetch()
: undefined
);
const stateRef = useSyncedRef(state);

Expand All @@ -172,7 +174,7 @@ export function useStorageValue<
}, [key]);

useEffect(() => {
if (!options?.initializeWithValue) {
if (!optionsRef.current.initializeWithValue) {
xobotyi marked this conversation as resolved.
Show resolved Hide resolved
stateActions.current.fetch();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down