fix throttle function not being able to run with argument(s)

This commit is contained in:
undergroundwires
2021-03-13 12:14:44 +01:00
parent 1f515e7be5
commit 1935db1019
2 changed files with 108 additions and 24 deletions

View File

@@ -5,6 +5,32 @@ import { EventSource } from '@/infrastructure/Events/EventSource';
import { IEventSubscription } from '@/infrastructure/Events/IEventSource';
describe('throttle', () => {
it('throws if callback is undefined', () => {
// arrange
const expectedError = 'undefined callback';
const callback = undefined;
// act
const act = () => throttle(callback, 500);
// assert
expect(act).to.throw(expectedError);
});
describe('throws if waitInMs is negative or zero', () => {
// arrange
const testCases = [
{ value: 0, expectedError: 'no delay to throttle' },
{ value: -2, expectedError: 'negative delay' },
];
const callback = () => { return; };
for (const testCase of testCases) {
it(`"${testCase.value}" throws "${testCase.expectedError}"`, () => {
// act
const waitInMs = testCase.value;
const act = () => throttle(callback, waitInMs);
// assert
expect(act).to.throw(testCase.expectedError);
});
}
});
it('should call the callback immediately', () => {
// arrange
const timer = new TimerMock();
@@ -21,16 +47,17 @@ describe('throttle', () => {
const timer = new TimerMock();
let totalRuns = 0;
const callback = () => totalRuns++;
const throttleFunc = throttle(callback, 500, timer);
const waitInMs = 500;
const throttleFunc = throttle(callback, waitInMs, timer);
// act
throttleFunc();
totalRuns--;
totalRuns--; // So we don't count the initial run
throttleFunc();
timer.tick(500);
timer.tickNext(waitInMs);
// assert
expect(totalRuns).to.equal(1);
});
it('calls the callback at most once at given time', () => {
it('should call the callback at most once at given time', () => {
// arrange
const timer = new TimerMock();
let totalRuns = 0;
@@ -40,11 +67,43 @@ describe('throttle', () => {
const throttleFunc = throttle(callback, waitInMs, timer);
// act
for (let i = 0; i < totalCalls; i++) {
timer.tick(waitInMs / totalCalls * i);
timer.setCurrentTime(waitInMs / totalCalls * i);
throttleFunc();
}
// assert
expect(totalRuns).to.equal(2); // initial and at the end
expect(totalRuns).to.equal(2); // one initial and one at the end
});
it('should call the callback as long as delay is waited', () => {
// arrange
const timer = new TimerMock();
let totalRuns = 0;
const callback = () => totalRuns++;
const waitInMs = 500;
const expectedTotalRuns = 10;
const throttleFunc = throttle(callback, waitInMs, timer);
// act
for (let i = 0; i < expectedTotalRuns; i++) {
throttleFunc();
timer.tickNext(waitInMs);
}
// assert
expect(totalRuns).to.equal(expectedTotalRuns);
});
it('should call arguments as expected', () => {
// arrange
const timer = new TimerMock();
const expected = [ 1, 2, 3 ];
const actual = new Array<number>();
const callback = (arg: number) => { actual.push(arg); };
const waitInMs = 500;
const throttleFunc = throttle(callback, waitInMs, timer);
// act
for (const arg of expected) {
throttleFunc(arg);
timer.tickNext(waitInMs);
}
// assert
expect(expected).to.deep.equal(actual);
});
});
@@ -69,7 +128,10 @@ class TimerMock implements ITimer {
public dateNow(): number {
return this.currentTime;
}
public tick(ms: number): void {
public tickNext(ms: number): void {
this.setCurrentTime(this.currentTime + ms);
}
public setCurrentTime(ms: number): void {
this.currentTime = ms;
this.timeChanged.notify(this.currentTime);
}