Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
[fix]: fix code format
Browse files Browse the repository at this point in the history
  • Loading branch information
ariesjia committed Nov 10, 2023
1 parent 1b29900 commit 17ebd15
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
49 changes: 23 additions & 26 deletions svelte/src/libs/utils/delay.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
import { expect, describe, beforeEach, afterEach, test, vi } from 'vitest'
import withDelay from './delay'

describe('withDelay', () => {
import { expect, describe, beforeEach, afterEach, test, vi } from "vitest";
import withDelay from "./delay";

describe("withDelay", () => {
beforeEach(() => {
vi.useFakeTimers()
})
vi.useFakeTimers();
});

afterEach(() => {
vi.restoreAllMocks()
})
vi.restoreAllMocks();
});

test('should delay time to execute callback', async () => {
const callback = vi.fn()
test("should delay time to execute callback", async () => {
const callback = vi.fn();
const timer = 100;
const promise = withDelay(callback, timer)
expect(callback).not.toHaveBeenCalled()
vi.runAllTimers()
await promise
expect(callback).toHaveBeenCalled()
})


test('should return the result of callback', async () => {
const result = 'result'
const callback = vi.fn().mockImplementation(() => result)
const promise = withDelay(callback, 0)
vi.runAllTimers()
expect(await promise).toEqual(result)
})
const promise = withDelay(callback, timer);
expect(callback).not.toHaveBeenCalled();
vi.runAllTimers();
await promise;
expect(callback).toHaveBeenCalled();
});

})
test("should return the result of callback", async () => {
const result = "result";
const callback = vi.fn().mockImplementation(() => result);
const promise = withDelay(callback, 0);
vi.runAllTimers();
expect(await promise).toEqual(result);
});
});
2 changes: 1 addition & 1 deletion svelte/src/libs/utils/delay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
export const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

export default async function withDelay<T>(f: () => T, delayMs: number) {
await wait(delayMs)
await wait(delayMs);
return f();
}
1 change: 0 additions & 1 deletion svelte/src/libs/utils/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ export default async function withRetry<T>(
}
throw new Error(`Failed after ${maxRetries} retries`);
}

0 comments on commit 17ebd15

Please sign in to comment.