Skip to content

Instantly share code, notes, and snippets.

@jamesfulford
Created January 6, 2020 17:11
Show Gist options
  • Save jamesfulford/cb21371ba8d282f396aa38ce737fb086 to your computer and use it in GitHub Desktop.
Save jamesfulford/cb21371ba8d282f396aa38ce737fb086 to your computer and use it in GitHub Desktop.
import { renderHook, act } from '@testing-library/react-hooks'
import { useFakeTimers, SinonFakeTimers } from 'sinon';
import { useTime } from '.';
describe('useTime (sinon clocks)', () => {
let clock: SinonFakeTimers;
beforeEach(() => { clock = useFakeTimers(); });
afterEach(() => { clock.restore(); });
it('should update the time based on refresh rate', () => {
const { result } = renderHook(() => useTime(100));
const initialTime = result.current;
act(() => {
clock.tick(99);
});
// Don't update yet
expect(initialTime).toEqual(result.current);
act(() => {
clock.tick(1);
});
// Updated
expect(initialTime).not.toEqual(result.current);
expect(result.current.diff(initialTime).milliseconds).toBe(100);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment