Skip to content

Commit

Permalink
test: add tests for useShallowRef
Browse files Browse the repository at this point in the history
  • Loading branch information
hlysine committed May 31, 2023
1 parent a1aa3b5 commit 045cfc0
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/__tests__/advanced.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import '@testing-library/jest-dom';
import { isRef, useShallowRef } from '..';
import 'jest-performance-testing';
import { renderHook } from '@testing-library/react';
import { isShallow } from '@vue/reactivity';

describe('useShallowRef', () => {
it('returns a valid ref', () => {
const { result } = renderHook(() => useShallowRef(1));
expect(result.current.value).toBe(1);
expect(isRef(result.current)).toBe(true);
expect(isShallow(result.current)).toBe(true);
});
it('keeps the same instance across re-render', () => {
const { result, rerender } = renderHook(() => useShallowRef(1));
const ref = result.current;

rerender();

expect(result.current).toBe(ref);
});
it('works with initializer function', () => {
const initializer = jest.fn(() => ({ count: 1 }));
const { result, rerender } = renderHook(() => useShallowRef(initializer));
const ref = result.current;
expect(result.current.value.count).toBe(1);
expect(initializer).toBeCalledTimes(1);

rerender();

expect(result.current.value.count).toBe(1);
expect(initializer).toBeCalledTimes(1);
expect(result.current).toBe(ref);
});
});

0 comments on commit 045cfc0

Please sign in to comment.