Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0636f68

Browse files
committedJan 23, 2021
chore: update tests to use fake timers
1 parent 7ab36b7 commit 0636f68

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed
 

Diff for: ‎packages/vue-composable/__tests__/web/timeout.spec.ts

+21-13
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,8 @@ import { useTimeout } from "../../src";
22
import { createVue } from "../utils";
33
import { Ref, ref } from "../../src/api";
44

5-
function sleep(duration: number): Promise<boolean> {
6-
return new Promise((resolve) => {
7-
setTimeout(() => {
8-
resolve(true);
9-
}, duration);
10-
});
11-
}
12-
135
describe("timeout", () => {
6+
jest.useFakeTimers();
147
it("should be defined", () => {
158
expect(useTimeout).toBeDefined();
169
});
@@ -22,15 +15,19 @@ describe("timeout", () => {
2215
}, 1000);
2316

2417
expect(count).toBe(0);
25-
await sleep(1000);
18+
jest.advanceTimersByTime(900);
19+
// should not be resolved
20+
expect(count).toBe(0);
21+
22+
jest.advanceTimersByTime(100);
2623
expect(count).toBe(1);
2724
});
2825

2926
it("should set ready true after run callback", async () => {
3027
const { ready } = useTimeout(() => {}, 1000);
3128

3229
expect(ready.value).toBe(false);
33-
await sleep(1000);
30+
jest.advanceTimersByTime(1000);
3431
expect(ready.value).toBe(true);
3532
});
3633

@@ -45,7 +42,7 @@ describe("timeout", () => {
4542

4643
cancel();
4744

48-
await sleep(1000);
45+
jest.advanceTimersByTime(1000);
4946
expect(ready.value).toBe(null);
5047
expect(count).toBe(0);
5148
});
@@ -63,10 +60,21 @@ describe("timeout", () => {
6360
mount();
6461

6562
expect(ready.value).toBe(false);
66-
await sleep(1000);
67-
expect(ready.value).toBe(true);
63+
jest.advanceTimersByTime(500);
64+
expect(ready.value).toBe(false);
6865

6966
destroy();
7067
expect(ready.value).toBe(null);
7168
});
69+
70+
it("should default the delay to 0", () => {
71+
let count = 0;
72+
useTimeout(() => {
73+
count++;
74+
});
75+
76+
expect(count).toBe(0);
77+
jest.runOnlyPendingTimers();
78+
expect(count).toBe(1);
79+
});
7280
});

0 commit comments

Comments
 (0)
Please sign in to comment.