Skip to content

Commit

Permalink
style: 优化测试代码
Browse files Browse the repository at this point in the history
  • Loading branch information
bowencool committed Apr 4, 2022
1 parent a2c422c commit 1dcc43f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
24 changes: 12 additions & 12 deletions packages/concurrentAsync/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,40 @@ describe('concurrentAsync', () => {
});
test('invoked when', async () => {
jest.useFakeTimers();
jest.spyOn(global, 'setTimeout');
const rawFn = jest.fn(someAsyncTask);
// 0,200,400
// 100,300
const con2 = concurrentAsync(someAsyncTask, 2);
const con2 = concurrentAsync(rawFn, 2);
const promises = new Array(5).fill(0).map((_, i) => con2(i * 2, i * 100, i % 2 === 0));

expect(setTimeout).toHaveBeenCalledTimes(2);
expect(rawFn).toHaveBeenCalledTimes(2);

jest.advanceTimersByTime(1);
await expect(promises[0]).rejects.toBe(0);
// next task will be created util previous promise is resolved or rejected
expect(setTimeout).toHaveBeenCalledTimes(3); // the 3rd task has be created and the 4th hasn't been created yet
expect(rawFn).toHaveBeenCalledTimes(3); // the 3rd task has be created and the 4th hasn't been created yet

jest.advanceTimersByTime(50); // 51ms
expect(setTimeout).toHaveBeenCalledTimes(3); // no new task(4th) was created
expect(rawFn).toHaveBeenCalledTimes(3); // no new task(4th) was created

jest.advanceTimersByTime(50); // 101ms
expect(setTimeout).toHaveBeenCalledTimes(3); // no new task(4th) was created
expect(rawFn).toHaveBeenCalledTimes(3); // no new task(4th) was created
await expect(promises[1]).resolves.toBe(2); // the 2nd task was resolved
expect(setTimeout).toHaveBeenCalledTimes(4); // the 4th task was created
expect(rawFn).toHaveBeenCalledTimes(4); // the 4th task was created
jest.advanceTimersByTime(50); // 151ms
expect(setTimeout).toHaveBeenCalledTimes(4); // no new task(5th) was created
expect(rawFn).toHaveBeenCalledTimes(4); // no new task(5th) was created

jest.advanceTimersByTime(50); // 201ms
expect(setTimeout).toHaveBeenCalledTimes(4); // no new task(5th) was created
expect(rawFn).toHaveBeenCalledTimes(4); // no new task(5th) was created
await expect(promises[2]).rejects.toBe(4); // the 3rd task was resolved
expect(setTimeout).toHaveBeenCalledTimes(5); // the 5th task was created
expect(rawFn).toHaveBeenCalledTimes(5); // the 5th task was created

jest.advanceTimersByTime(200); // 401ms
await expect(promises[3]).resolves.toBe(6); // the 4th task was resolved
expect(setTimeout).toHaveBeenCalledTimes(5); // no more task was created
expect(rawFn).toHaveBeenCalledTimes(5); // no more task was created

jest.advanceTimersByTime(200); // 601ms
await expect(promises[4]).rejects.toBe(8); // the 5th task was resolved
expect(setTimeout).toHaveBeenCalledTimes(5); // no more task was created
expect(rawFn).toHaveBeenCalledTimes(5); // no more task was created
});
});
12 changes: 6 additions & 6 deletions packages/debounceAsync/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ describe('debounceAsync', () => {
jest.useFakeTimers();
const rawFn = jest.fn(someAsyncTask);
const debounced = debounceAsync(rawFn, 10);
const cb = jest.fn();
const resolveOrReject = jest.fn();
new Array(5).fill(0).map((_, i) =>
debounced(i * 2, 10, i % 2 === 0)
.then(cb)
.catch(cb),
.then(resolveOrReject)
.catch(resolveOrReject),
);

expect(rawFn).not.toHaveBeenCalled();
expect(cb).not.toHaveBeenCalled();
expect(resolveOrReject).not.toHaveBeenCalled();
jest.advanceTimersByTime(500);
await flushPromises();
expect(rawFn).toHaveBeenCalledTimes(1);
expect(rawFn).lastCalledWith(8, 10, true);
expect(cb).toHaveBeenCalledTimes(1);
expect(cb).lastCalledWith(8);
expect(resolveOrReject).toHaveBeenCalledTimes(1);
expect(resolveOrReject).lastCalledWith(8);
});
});

0 comments on commit 1dcc43f

Please sign in to comment.