2017-02-08 14:44:43 +08:00
|
|
|
import throttleByAnimationFrame from '../throttleByAnimationFrame';
|
|
|
|
|
|
|
|
describe('Test utils function', () => {
|
2017-11-13 10:46:22 +08:00
|
|
|
beforeAll(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
2017-11-09 20:33:25 +08:00
|
|
|
afterAll(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2017-11-13 10:46:22 +08:00
|
|
|
|
2017-02-08 14:44:43 +08:00
|
|
|
it('throttle function should work', () => {
|
|
|
|
const callback = jest.fn();
|
|
|
|
const throttled = throttleByAnimationFrame(callback);
|
|
|
|
expect(callback).not.toBeCalled();
|
|
|
|
|
|
|
|
throttled();
|
|
|
|
throttled();
|
|
|
|
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(callback).toBeCalled();
|
|
|
|
expect(callback.mock.calls.length).toBe(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throttle function should be canceled', () => {
|
|
|
|
const callback = jest.fn();
|
|
|
|
const throttled = throttleByAnimationFrame(callback);
|
|
|
|
|
|
|
|
throttled();
|
|
|
|
throttled.cancel();
|
|
|
|
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(callback).not.toBeCalled();
|
|
|
|
});
|
|
|
|
});
|