You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.6 KiB

4 years ago
export const tests = []
const t = (f) => tests.push(f)
const add = (arr, el) => arr.push(el)
// it uses the array to better test the leading and trailing edge of the time limit
// so if the leading edge is true it will execute the callback
// if the trailing edge is true it will execute the callback before returning the array
const run = (callback, { delay, count }) =>
new Promise((r) => {
const arr = []
const inter = setInterval(() => callback(arr, 1), delay)
setTimeout(() => {
clearInterval(inter)
r(arr.length)
}, delay * count)
})
t(async ({ eq }) =>
// test with debounce wait limit inferior to wait time call (how much time we wait to the function be called again)
// it works concurrently
4 years ago
eq(
await Promise.all([
run(debounce(add, 50), { delay: 100, count: 5 }),
run(debounce(add, 20), { delay: 50, count: 10 }),
4 years ago
]),
[4, 9]
)
)
t(async ({ eq }) =>
// testing with wait limit superior to wait time call
// execution on the trailing edge, after wait limit has elapsed
eq(await run(debounce(add, 100), { delay: 50, count: 5 }), 0)
4 years ago
)
t(async ({ eq }) =>
// it works concurrently
4 years ago
eq(
await Promise.all([
run(opDebounce(add, 40), { delay: 20, count: 5 }),
run(opDebounce(add, 40), { delay: 20, count: 2 }),
4 years ago
]),
[0, 0]
)
4 years ago
)
t(async ({ eq }) =>
// leading edge as true
// it works concurrently
4 years ago
eq(
await Promise.all([
run(opDebounce(add, 200, { leading: true }), { delay: 70, count: 3 }),
run(opDebounce(add, 100, { leading: true }), { delay: 140, count: 3 }),
4 years ago
]),
[1, 2]
)
4 years ago
)
Object.freeze(tests)