generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor : Moved to delay functionality to util
- Loading branch information
Showing
3 changed files
with
25 additions
and
3 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
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,5 @@ | ||
export function sleep(delay = 1000) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, delay); | ||
}); | ||
} |
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,18 @@ | ||
import { sleep } from "../../../src/utils/sleep"; | ||
jest.useFakeTimers(); | ||
|
||
describe("sleep function", () => { | ||
afterAll(() => jest.useRealTimers()); | ||
test("should resolve after the specified delay", async () => { | ||
const delay = 2000; | ||
const promise = sleep(delay); | ||
jest.advanceTimersByTime(delay); | ||
await expect(promise).resolves.toBeUndefined(); | ||
}); | ||
|
||
test("should resolve after default delay if no delay is provided", async () => { | ||
const promise = sleep(); | ||
jest.advanceTimersByTime(1000); | ||
await expect(promise).resolves.toBeUndefined(); | ||
}); | ||
}); |