Skip to content

Commit

Permalink
Merge pull request #58 from lihongxun945/feat/raf-interval
Browse files Browse the repository at this point in the history
feat: 增加rafInterval
  • Loading branch information
Genluo committed Mar 23, 2022
2 parents dd06396 + e173380 commit c541b12
Show file tree
Hide file tree
Showing 4 changed files with 31,660 additions and 178 deletions.
50 changes: 50 additions & 0 deletions __test__/time/rafInterval.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { time } from '../../src/index';

const { setRAFInterval, cancelRAFInterval: cancelRequestInterval } = time;

describe('RequestInterval', () => {
jest.useFakeTimers('modern');
test('setRequestInterval, cancelRequestInterval should be defined', () => {
expect(setRAFInterval).toBeDefined();
expect(cancelRequestInterval).toBeDefined();
});
test('should do nothing with undefined callback', () => {
// @ts-ignore
const id = setRAFInterval(undefined, 100);
expect(typeof id.id).toEqual(typeof 1);
cancelRequestInterval(id);
});
test('should work when delay is undefined', () => {
const callback = jest.fn();
const id = setRAFInterval(callback);
expect(callback).not.toBeCalled();
jest.advanceTimersByTime(50);
expect(callback).toHaveBeenCalled();
cancelRequestInterval(id);
});
test('should not call until time is up', () => {
const callback = jest.fn();
const id = setRAFInterval(callback, 100);
expect(callback).not.toBeCalled();
jest.advanceTimersByTime(200);
expect(callback).toHaveBeenCalledTimes(1);
cancelRequestInterval(id);
});
test('should call 3 times', () => {
const callback = jest.fn();
const id = setRAFInterval(callback, 100);
expect(callback).not.toBeCalled();
jest.advanceTimersByTime(350);
expect(callback).toHaveBeenCalledTimes(3);
cancelRequestInterval(id);
});
test('cancelRequestInterval should work', () => {
const callback = jest.fn();
const id = setRAFInterval(callback, 100);
jest.advanceTimersByTime(150);
expect(callback).toHaveBeenCalledTimes(1);
cancelRequestInterval(id);
jest.advanceTimersByTime(200);
expect(callback).toHaveBeenCalledTimes(1);
});
});
Loading

0 comments on commit c541b12

Please sign in to comment.