-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
551 additions
and
1,745 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 |
---|---|---|
@@ -1,35 +1,37 @@ | ||
import { describe, test, expect } from "vitest"; | ||
import { debounce } from "./debounce"; | ||
|
||
describe("Debounce", () => { | ||
const returns: number[] = []; | ||
const stack: number[] = []; | ||
|
||
test("should not run function with the same args for specific time", (done) => { | ||
const fn = (n: number) => { | ||
stack.push(n * 2); | ||
return n; | ||
}; | ||
const debouncedFn = debounce({ | ||
fn, | ||
name: "test", | ||
seconds: 3, | ||
mapper: (n) => `${n}`, | ||
}); | ||
|
||
const pusher = () => { | ||
[1, 2, 3].forEach((n) => { | ||
returns.push(debouncedFn(n)); | ||
test("should not run function with the same args for specific time", () => | ||
new Promise<void>((done) => { | ||
const fn = (n: number) => { | ||
stack.push(n * 2); | ||
return n; | ||
}; | ||
const debouncedFn = debounce({ | ||
fn, | ||
name: "test", | ||
seconds: 3, | ||
mapper: (n) => `${n}`, | ||
}); | ||
}; | ||
|
||
pusher(); | ||
setTimeout(pusher, 1000); // cached | ||
setTimeout(pusher, 2000); // cached | ||
setTimeout(pusher, 3500); // not cached | ||
setTimeout(() => { | ||
expect(returns).toEqual([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]); | ||
expect(stack).toEqual([2, 4, 6, 2, 4, 6]); | ||
done(); | ||
}, 4500); | ||
}); | ||
const pusher = () => { | ||
[1, 2, 3].forEach((n) => { | ||
returns.push(debouncedFn(n)); | ||
}); | ||
}; | ||
|
||
pusher(); | ||
setTimeout(pusher, 1000); // cached | ||
setTimeout(pusher, 2000); // cached | ||
setTimeout(pusher, 3500); // not cached | ||
setTimeout(() => { | ||
expect(returns).toEqual([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]); | ||
expect(stack).toEqual([2, 4, 6, 2, 4, 6]); | ||
done(); | ||
}, 4500); | ||
})); | ||
}); |
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
Oops, something went wrong.