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 skipped tests for useLocalStorage #655

Merged
merged 1 commit into from
Oct 8, 2024
Merged
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
48 changes: 29 additions & 19 deletions src/services/local-storage.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
import * as util from './utility-helpers';
import { useLocalStorage } from '@Services/local-storage';
import * as React from 'react';
import { render } from '@testing-library/react';
import { renderHook, act } from '@testing-library/react';
import { useLocalStorage } from './local-storage';

describe.skip('local storage', () => {
describe('useLocalStorage', () => {
test('default value', () => {
let settingRef = React.createRef();
describe('useLocalStorage', () => {
beforeEach(() => {
localStorage.clear();
});

function TestComponent() {
const [setting, setSetting] = useLocalStorage('setting', {});
test('initializes with default value', () => {
const { result } = renderHook(() => useLocalStorage('key', 'defaultValue'));
const [value] = result.current;
expect(value).toBe('defaultValue');
});

React.useEffect(() => {
settingRef.currrent = setting;
});
test('initializes with the value in localStorage if it exists', () => {
// Setup: set value in localStorage => existing value
localStorage.setItem('key', JSON.stringify('storedValue'));
const { result } = renderHook(() => useLocalStorage('key', 'defaultValue'));
const [value] = result.current;
expect(value).toBe('storedValue');
});

return null;
}
render(<TestComponent/>);
test('updates localStorage when value is changed', () => {
const { result } = renderHook(() => useLocalStorage('key', 'defaultValue'));
const [defValue] = result.current;
expect(defValue).toBe('defaultValue');

expect(settingRef.current).toEqual({});
setSetting({'key': 'value'})
expect(setting).toEqual({'key': 'value'});
// Update value
act(() => {
const [, setValue] = result.current;
setValue('newValue');
});
const [newValue] = result.current;
expect(newValue).toBe('newValue');
expect(localStorage.getItem('key')).toBe(JSON.stringify('newValue'));
});
});