Skip to content

Commit

Permalink
Merge pull request #655 from samvera-labs/skipped-tests
Browse files Browse the repository at this point in the history
Fix skipped tests for useLocalStorage
  • Loading branch information
cjcolvar authored Oct 8, 2024
2 parents 622ace4 + 3672439 commit a63d953
Showing 1 changed file with 29 additions and 19 deletions.
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'));
});
});

0 comments on commit a63d953

Please sign in to comment.