mirror of
https://github.com/finos/SymphonyElectron.git
synced 2024-11-28 19:53:53 -06:00
a839c86d83
* Electron-66 - Added more unit tests for config, getRegistry & throttle * Electron-66 - Added some more unit tests and enabled activity detection unit test
112 lines
3.0 KiB
JavaScript
112 lines
3.0 KiB
JavaScript
const throttle = require('../../js/utils/throttle.js');
|
|
|
|
describe('throttle tests', function() {
|
|
var now, origNow;
|
|
beforeEach(function() {
|
|
origNow = Date.now;
|
|
// mock date func
|
|
Date.now = function() { return now };
|
|
now = 10000;
|
|
});
|
|
|
|
it('expect to be called only once when called more than once in 1 second period',
|
|
function() {
|
|
jest.useFakeTimers();
|
|
|
|
const callback = jest.fn();
|
|
const throttledCB = throttle(1000, callback);
|
|
|
|
expect(callback).not.toBeCalled();
|
|
|
|
throttledCB();
|
|
expect(callback.mock.calls.length).toBe(1);
|
|
|
|
throttledCB();
|
|
expect(callback.mock.calls.length).toBe(1);
|
|
|
|
now += 1000;
|
|
jest.runTimersToTime(1000);
|
|
expect(callback.mock.calls.length).toBe(2);
|
|
|
|
throttledCB();
|
|
expect(callback.mock.calls.length).toBe(2);
|
|
|
|
now += 900;
|
|
jest.runTimersToTime(900);
|
|
expect(callback.mock.calls.length).toBe(2);
|
|
|
|
now += 100;
|
|
jest.runTimersToTime(100);
|
|
expect(callback.mock.calls.length).toBe(3);
|
|
});
|
|
|
|
it('expect to be called twice when call spacing > 1 sec', function() {
|
|
const callback = jest.fn();
|
|
const throttledCB = throttle(1000, callback);
|
|
|
|
expect(callback).not.toBeCalled();
|
|
|
|
throttledCB();
|
|
expect(callback.mock.calls.length).toBe(1);
|
|
|
|
now += 1000;
|
|
|
|
throttledCB();
|
|
expect(callback.mock.calls.length).toBe(2);
|
|
});
|
|
|
|
it('expect clearTimeout to be invoked', function() {
|
|
const callback = jest.fn();
|
|
const throttledCB = throttle(1000, callback);
|
|
|
|
expect(callback).not.toBeCalled();
|
|
|
|
throttledCB();
|
|
expect(callback.mock.calls.length).toBe(1);
|
|
expect(clearTimeout.mock.calls.length).toBe(0);
|
|
|
|
now -= 1000;
|
|
throttledCB();
|
|
expect(callback.mock.calls.length).toBe(1);
|
|
|
|
now += 1000;
|
|
throttledCB();
|
|
expect(callback.mock.calls.length).toBe(1);
|
|
expect(clearTimeout.mock.calls.length).toBe(1);
|
|
});
|
|
|
|
describe('expect to throw exception', function() {
|
|
it('when calling throttle with time equal to zero', function(done) {
|
|
try {
|
|
throttle(0, function() {});
|
|
} catch(error) {
|
|
expect(error.message).toBeDefined();
|
|
done();
|
|
}
|
|
});
|
|
|
|
it('when calling throttle with time less than zero', function(done) {
|
|
try {
|
|
throttle(-1, function() {});
|
|
} catch(error) {
|
|
expect(error.message).toBeDefined();
|
|
done();
|
|
}
|
|
});
|
|
|
|
it('when calling throttle without a function callback', function(done) {
|
|
try {
|
|
throttle(1, 'not a func');
|
|
} catch(error) {
|
|
expect(error.message).toBeDefined();
|
|
done();
|
|
}
|
|
});
|
|
});
|
|
|
|
afterEach(function() {
|
|
// restore orig
|
|
Date.now = origNow;
|
|
})
|
|
});
|