dao-governance-framework/forum-network/src/tests/scripts/debounce.test.js

75 lines
2.1 KiB
JavaScript

import { Box } from '../../classes/display/box.js';
import { Scene } from '../../classes/display/scene.js';
import { Action } from '../../classes/display/action.js';
import { Actor } from '../../classes/display/actor.js';
import { debounce, delay, mochaRun } from '../../util.js';
describe('Debounce', () => {
let scene;
let caller;
let debouncer;
let method;
let call;
let execute;
before(() => {
const rootElement = document.getElementById('scene');
const rootBox = new Box('rootBox', rootElement).flex();
scene = new Scene('Debounce test', rootBox).withSequenceDiagram();
caller = new Actor('Caller', scene);
debouncer = new Actor('Debouncer', scene);
method = new Actor('Target method', scene);
call = new Action('call', scene);
execute = new Action('execute', scene);
});
it('Suppresses extra events that occur within the specified window', async () => {
let eventCount = 0;
const event = sinon.spy(async () => {
eventCount++;
await execute.log(debouncer, method, eventCount);
});
await scene.sequence.startSection();
await call.log(caller, debouncer, '1');
await debounce(event, 500);
await call.log(caller, debouncer, '2');
await debounce(event, 500);
await delay(500);
event.should.have.been.calledOnce;
await call.log(caller, debouncer, '3');
await debounce(event, 500);
await call.log(caller, debouncer, '4');
await debounce(event, 500);
eventCount.should.equal(2);
event.should.have.been.calledTwice;
await scene.sequence.endSection();
});
it('Propagates exceptions', async () => {
const event = sinon.spy(async () => {
await execute.log(debouncer, method, undefined, undefined, '-x');
throw new Error('An error occurs in the callback');
});
await scene.sequence.startSection();
try {
await call.log(caller, debouncer);
await debounce(event, 500);
} catch (e) {
event.should.have.been.calledOnce;
e.should.exist;
e.should.match(/An error occurs in the callback/);
}
await scene.sequence.endSection();
});
});
mochaRun();