mirror of
https://github.com/idrawjs/idraw
synced 2026-05-24 10:08:34 +00:00
test: add util/__tests__/lib/time.test.ts
This commit is contained in:
parent
a772697719
commit
d46081f278
1 changed files with 68 additions and 0 deletions
68
packages/util/__tests__/lib/time.test.ts
Normal file
68
packages/util/__tests__/lib/time.test.ts
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
import {
|
||||
delay, throttle, compose,
|
||||
} from '../../src/lib/time';
|
||||
|
||||
|
||||
describe('@idraw/util: lib/delay', () => {
|
||||
|
||||
test('delay', async () => {
|
||||
const start = Date.now();
|
||||
const time = 1000;
|
||||
await delay(time);
|
||||
const count = Date.now() - start;
|
||||
expect(count >= time).toStrictEqual(true);
|
||||
});
|
||||
|
||||
test('throttle', async () => {
|
||||
let count = 0;
|
||||
function doThrottle() {
|
||||
return new Promise((resolve) => {
|
||||
const func = throttle(function() {
|
||||
count ++;
|
||||
}, 100);
|
||||
let interval = setInterval(() => {
|
||||
if (count >= 5) {
|
||||
clearInterval(interval);
|
||||
resolve(null)
|
||||
}
|
||||
func();
|
||||
}, 5);
|
||||
})
|
||||
}
|
||||
await doThrottle();
|
||||
expect(count).toStrictEqual(5);
|
||||
});
|
||||
|
||||
|
||||
test('compose', async () => {
|
||||
let middleware = [];
|
||||
let context = {
|
||||
data: []
|
||||
};
|
||||
|
||||
middleware.push(async(ctx: any, next: any) => {
|
||||
ctx.data.push(1);
|
||||
await next();
|
||||
ctx.data.push(6);
|
||||
});
|
||||
|
||||
middleware.push(async(ctx: any, next: any) => {
|
||||
ctx.data.push(2);
|
||||
await next();
|
||||
ctx.data.push(5);
|
||||
});
|
||||
|
||||
middleware.push(async(ctx: any, next: any) => {
|
||||
ctx.data.push(3);
|
||||
await next();
|
||||
ctx.data.push(4);
|
||||
});
|
||||
|
||||
const fn = compose(middleware);
|
||||
|
||||
await fn(context);
|
||||
expect(context).toStrictEqual({data: [1, 2, 3, 4, 5, 6]});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Loading…
Reference in a new issue