Skip to content

Commit

Permalink
test(core/utils): add unit test for callUntilEnd, callPromiseUntilEnd
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaodong2008 committed Oct 12, 2024
1 parent f5c60c1 commit ff66152
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/core/__tests__/unit/utils/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { callPromiseUntilEnd, callUntilEnd } from "@/main";
import { expect, test } from "vitest";

test("Call until end", async () => {
const startT = Date.now();
let i = 0;
await callUntilEnd(() => {
expect(Date.now() - startT).toBeGreaterThanOrEqual(100 * i);
return ++i === 10;
}, 100);

expect(Date.now() - startT).toBeGreaterThanOrEqual(1000);
expect(i).toBe(10);
});

test("Call promise until end", async () => {
const startT = Date.now();
let i = 0;
await callPromiseUntilEnd(async () => {
return new Promise((resolve) =>
setTimeout(() => {
i++;
expect(Date.now() - startT).toBeGreaterThanOrEqual(100 * i * 2);
console.log(Date.now() - startT, i);

resolve(i >= 10);
}, 100)
);
}, 100);

expect(Date.now() - startT).toBeGreaterThanOrEqual(2000);
expect(i).toBe(10);
});

0 comments on commit ff66152

Please sign in to comment.