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

test(@toss/react): wrote test code for useLocalStorageChangeDetector #415

Merged
Merged
Changes from 1 commit
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
Next Next commit
test(@toss/react): wrote test code for useLocalStorageChangeDetector
  • Loading branch information
원건우 committed Jan 10, 2024
commit 420c5ec1439cde52844132786e9c08fbe92c01f4
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { fireEvent, renderHook } from '@testing-library/react';
import { generateStorage } from '@toss/storage';
import { useLocalStorageChangeDetector } from './useLocalStorageChangeDetector';

const tossKey = 'tossKey';
const tossValue = 'tossValue';
const tossValue2 = 'tossValue2';

function mockVisibilityState(visibilityState: Document['visibilityState']) {
Object.defineProperty(document, 'visibilityState', { value: visibilityState, writable: true });
fireEvent(document, new Event('visibilitychange'));
}

const safeLocalStorage = generateStorage();
describe('useLocalStorageChangeDetector', () => {
beforeEach(() => {
safeLocalStorage.set(tossKey, tossValue);
});
describe('When it becomes visible, the value should be updated', () => {
it('When the clearStorage function is executed, the value should be null.', () => {
const { result } = renderHook(() => useLocalStorageChangeDetector(tossKey));
const [_, { clearStorage }] = result.current;

expect(result.current[0]).toBe(tossValue);

clearStorage();

mockVisibilityState('visible');

expect(result.current[0]).toBeNull();
});
it('The local storage value should be updated when it changes', () => {
const { result } = renderHook(() => useLocalStorageChangeDetector(tossKey));

expect(result.current[0]).toBe(tossValue);

safeLocalStorage.set(tossKey, tossValue2);

mockVisibilityState('visible');

expect(result.current[0]).toBe(tossValue2);
});
});
describe('The value should not be updated when it is hidden', () => {
it('The value should not be updated when the clearStorage function is executed', () => {
const { result } = renderHook(() => useLocalStorageChangeDetector(tossKey));
const [_, { clearStorage }] = result.current;

expect(result.current[0]).toBe(tossValue);

clearStorage();

mockVisibilityState('hidden');

expect(result.current[0]).toBe(tossValue);
});
it('The value should not be changed even when it becomes a local storage value', () => {
const { result } = renderHook(() => useLocalStorageChangeDetector(tossKey));

expect(result.current[0]).toBe(tossValue);

safeLocalStorage.set(tossKey, tossValue2);

mockVisibilityState('hidden');

expect(result.current[0]).toBe(tossValue);
});
});
});