Skip to content

Instantly share code, notes, and snippets.

@aphofstede
Created April 13, 2018 11:10
Show Gist options
  • Save aphofstede/7cc26561a9616a470b166c3353bdbc03 to your computer and use it in GitHub Desktop.
Save aphofstede/7cc26561a9616a470b166c3353bdbc03 to your computer and use it in GitHub Desktop.
debounce with fake timer test
import expect from 'expect'
import sinon from 'sinon'
import _ from 'lodash'
describe.only ('Debounce', () => {
let clock
beforeEach(() => {
clock = sinon.useFakeTimers();
})
afterEach(() => {
clock.restore()
})
it ('can stub clock for debounce', function () {
let spy = sinon.spy();
_.debounce(spy, 1000)();
expect(spy.callCount).toEqual(0);
clock.tick(1);
expect(spy.callCount).toEqual(0);
clock.tick(998);
expect(spy.callCount).toEqual(0);
clock.tick(1);
expect(spy.callCount).toEqual(1);
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment