Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 添加 after1000ms 以及测试,并 Mock console 的 API
- Loading branch information
haixiangyan
authored and
haixiangyan
committed
Apr 28, 2022
1 parent
316025d
commit 5fdd793
Showing
3 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type AnyFunction = (...args: any[]) => any; | ||
|
||
const after1000ms = (callback: AnyFunction) => { | ||
console.log("准备计时"); | ||
setTimeout(() => { | ||
console.log("午时已到"); | ||
callback && callback(); | ||
}, 1000); | ||
}; | ||
|
||
export default after1000ms; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import after1000ms from "utils/after1000ms"; | ||
|
||
describe("after1000ms", () => { | ||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
it("可以在 1000ms 后自动执行函数", () => { | ||
jest.spyOn(global, "setTimeout"); | ||
const callback = jest.fn(); | ||
|
||
after1000ms(callback); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(callback).toHaveBeenCalled(); | ||
expect(setTimeout).toHaveBeenCalledTimes(1); | ||
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 1000); | ||
}); | ||
}); |