2020-04-22 19:07:51 +05:30
|
|
|
import $ from 'jquery';
|
|
|
|
import { assignIn } from 'lodash';
|
2022-07-16 23:28:13 +05:30
|
|
|
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
|
2020-04-22 19:07:51 +05:30
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
|
|
|
import SmartInterval from '~/smart_interval';
|
|
|
|
|
|
|
|
let interval;
|
|
|
|
|
|
|
|
describe('SmartInterval', () => {
|
|
|
|
const DEFAULT_MAX_INTERVAL = 100;
|
|
|
|
const DEFAULT_STARTING_INTERVAL = 5;
|
|
|
|
const DEFAULT_INCREMENT_FACTOR = 2;
|
|
|
|
|
|
|
|
function createDefaultSmartInterval(config) {
|
|
|
|
const defaultParams = {
|
|
|
|
callback: () => Promise.resolve(),
|
|
|
|
startingInterval: DEFAULT_STARTING_INTERVAL,
|
|
|
|
maxInterval: DEFAULT_MAX_INTERVAL,
|
|
|
|
incrementByFactorOf: DEFAULT_INCREMENT_FACTOR,
|
|
|
|
lazyStart: false,
|
|
|
|
immediateExecution: false,
|
|
|
|
hiddenInterval: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (config) {
|
|
|
|
assignIn(defaultParams, config);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new SmartInterval(defaultParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
interval.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Increment Interval', () => {
|
|
|
|
it('should increment the interval delay', () => {
|
|
|
|
interval = createDefaultSmartInterval();
|
|
|
|
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
|
|
|
|
return waitForPromises().then(() => {
|
|
|
|
const intervalConfig = interval.cfg;
|
|
|
|
const iterationCount = 4;
|
|
|
|
const maxIntervalAfterIterations =
|
|
|
|
intervalConfig.startingInterval * intervalConfig.incrementByFactorOf ** iterationCount;
|
|
|
|
const currentInterval = interval.getCurrentInterval();
|
|
|
|
|
|
|
|
// Provide some flexibility for performance of testing environment
|
|
|
|
expect(currentInterval).toBeGreaterThan(intervalConfig.startingInterval);
|
|
|
|
expect(currentInterval).toBeLessThanOrEqual(maxIntervalAfterIterations);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not increment past maxInterval', () => {
|
|
|
|
interval = createDefaultSmartInterval({ maxInterval: DEFAULT_STARTING_INTERVAL });
|
|
|
|
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
|
|
|
|
return waitForPromises().then(() => {
|
|
|
|
const currentInterval = interval.getCurrentInterval();
|
|
|
|
|
|
|
|
expect(currentInterval).toBe(interval.cfg.maxInterval);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not increment while waiting for callback', () => {
|
|
|
|
interval = createDefaultSmartInterval({
|
|
|
|
callback: () => new Promise($.noop),
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
|
|
|
|
return waitForPromises().then(() => {
|
|
|
|
const oneInterval = interval.cfg.startingInterval * DEFAULT_INCREMENT_FACTOR;
|
|
|
|
|
|
|
|
expect(interval.getCurrentInterval()).toEqual(oneInterval);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Public methods', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
interval = createDefaultSmartInterval();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should cancel an interval', () => {
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
|
|
|
|
interval.cancel();
|
|
|
|
|
|
|
|
return waitForPromises().then(() => {
|
|
|
|
const { intervalId } = interval.state;
|
|
|
|
const currentInterval = interval.getCurrentInterval();
|
|
|
|
const intervalLowerLimit = interval.cfg.startingInterval;
|
|
|
|
|
|
|
|
expect(intervalId).toBeUndefined();
|
|
|
|
expect(currentInterval).toBe(intervalLowerLimit);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should resume an interval', () => {
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
|
|
|
|
interval.cancel();
|
|
|
|
|
|
|
|
interval.resume();
|
|
|
|
|
|
|
|
return waitForPromises().then(() => {
|
|
|
|
const { intervalId } = interval.state;
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(intervalId).not.toBeUndefined();
|
2020-04-22 19:07:51 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('DOM Events', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
// This ensures DOM and DOM events are initialized for these specs.
|
2022-07-16 23:28:13 +05:30
|
|
|
setHTMLFixture('<div></div>');
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
interval = createDefaultSmartInterval();
|
|
|
|
});
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
|
|
|
});
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
it('should pause when page is not visible', () => {
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
|
|
|
|
return waitForPromises().then(() => {
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(interval.state.intervalId).not.toBeUndefined();
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
// simulates triggering of visibilitychange event
|
|
|
|
interval.onVisibilityChange({ target: { visibilityState: 'hidden' } });
|
|
|
|
|
|
|
|
expect(interval.state.intervalId).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should change to the hidden interval when page is not visible', () => {
|
|
|
|
interval.destroy();
|
|
|
|
|
|
|
|
const HIDDEN_INTERVAL = 1500;
|
|
|
|
interval = createDefaultSmartInterval({ hiddenInterval: HIDDEN_INTERVAL });
|
|
|
|
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
|
|
|
|
return waitForPromises().then(() => {
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(interval.state.intervalId).not.toBeUndefined();
|
2020-04-22 19:07:51 +05:30
|
|
|
expect(
|
|
|
|
interval.getCurrentInterval() >= DEFAULT_STARTING_INTERVAL &&
|
|
|
|
interval.getCurrentInterval() <= DEFAULT_MAX_INTERVAL,
|
2022-11-25 23:54:43 +05:30
|
|
|
).toBe(true);
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
// simulates triggering of visibilitychange event
|
|
|
|
interval.onVisibilityChange({ target: { visibilityState: 'hidden' } });
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(interval.state.intervalId).not.toBeUndefined();
|
2020-04-22 19:07:51 +05:30
|
|
|
expect(interval.getCurrentInterval()).toBe(HIDDEN_INTERVAL);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should resume when page is becomes visible at the previous interval', () => {
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
|
|
|
|
return waitForPromises().then(() => {
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(interval.state.intervalId).not.toBeUndefined();
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
// simulates triggering of visibilitychange event
|
|
|
|
interval.onVisibilityChange({ target: { visibilityState: 'hidden' } });
|
|
|
|
|
|
|
|
expect(interval.state.intervalId).toBeUndefined();
|
|
|
|
|
|
|
|
// simulates triggering of visibilitychange event
|
|
|
|
interval.onVisibilityChange({ target: { visibilityState: 'visible' } });
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(interval.state.intervalId).not.toBeUndefined();
|
2020-04-22 19:07:51 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should cancel on page unload', () => {
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
|
|
|
|
return waitForPromises().then(() => {
|
|
|
|
$(document).triggerHandler('beforeunload');
|
|
|
|
|
|
|
|
expect(interval.state.intervalId).toBeUndefined();
|
|
|
|
expect(interval.getCurrentInterval()).toBe(interval.cfg.startingInterval);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should execute callback before first interval', () => {
|
|
|
|
interval = createDefaultSmartInterval({ immediateExecution: true });
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(interval.cfg.immediateExecution).toBe(false);
|
2020-04-22 19:07:51 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|