| 1 |
import { test } from 'node:test'; |
| 2 |
import assert from 'node:assert/strict'; |
| 3 |
import { cancelDebounce, debounce, debounceMs, revertMs } from './timing.ts'; |
| 4 |
|
| 5 |
// No document under `node --test`, so every duration reads its fallback, which |
| 6 |
// is the crate's current value. This is the pin: `makeover-timing` moving a |
| 7 |
// number and this file not following is a failing test rather than a page that |
| 8 |
// quietly disagrees with the stylesheet beside it. |
| 9 |
test('the fallbacks are makeover-timing 0.1.1', () => { |
| 10 |
assert.equal(revertMs(), 1500); |
| 11 |
assert.equal(debounceMs(), 150); |
| 12 |
}); |
| 13 |
|
| 14 |
test('a key runs once however many times it is called', async () => { |
| 15 |
let runs = 0; |
| 16 |
debounce('k', () => (runs += 1), 5); |
| 17 |
debounce('k', () => (runs += 1), 5); |
| 18 |
debounce('k', () => (runs += 1), 5); |
| 19 |
await new Promise((done) => setTimeout(done, 30)); |
| 20 |
assert.equal(runs, 1); |
| 21 |
}); |
| 22 |
|
| 23 |
test('two keys are two calls', async () => { |
| 24 |
const ran: string[] = []; |
| 25 |
debounce('a', () => ran.push('a'), 5); |
| 26 |
debounce('b', () => ran.push('b'), 5); |
| 27 |
await new Promise((done) => setTimeout(done, 30)); |
| 28 |
assert.deepEqual(ran.sort(), ['a', 'b']); |
| 29 |
}); |
| 30 |
|
| 31 |
test('cancelling drops the call that was waiting', async () => { |
| 32 |
let ran = false; |
| 33 |
debounce('c', () => (ran = true), 5); |
| 34 |
cancelDebounce('c'); |
| 35 |
await new Promise((done) => setTimeout(done, 30)); |
| 36 |
assert.equal(ran, false); |
| 37 |
}); |
| 38 |
|