Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed Feb 22, 2019
1 parent 3e88d7c commit dc76393
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
10 changes: 9 additions & 1 deletion src-docs/src/views/date_picker/super_date_picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function MyCustomQuickSelectPanel({ applyTime }) {
export default class extends Component {

state = {
refreshInterval: 1000,
isPaused: false,
recentlyUsedRanges: [],
isLoading: false,
showUpdateButton: true,
Expand All @@ -52,7 +54,13 @@ export default class extends Component {
}

onRefresh = ({ start, end, refreshInterval }) => {
console.log(start, end, refreshInterval);
console.log('onRefresh was called');
return new Promise((resolve) => {

setTimeout(resolve, 100);
}).then(() => {
console.log(start, end, refreshInterval);
});
}

onStartInputChange = e => {
Expand Down
64 changes: 50 additions & 14 deletions src/components/date_picker/super_date_picker/async_interval.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,67 @@
import { AsyncInterval } from './async_interval';
import { times } from 'lodash';

jest.useFakeTimers();
describe('AsyncInterval', () => {

async function waitFor(instance, ms) {
jest.advanceTimersByTime(ms);
await instance.__pendingFn;
}
beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.useRealTimers();
});

async function andvanceTimerAndAwaitFn(instance, ms) {
const iterations = Math.floor(ms / 100);
const remainder = ms % 100;
// eslint-disable-next-line no-unused-vars
for (const item of times(iterations)) {
await instance.__pendingFn;
jest.advanceTimersByTime(100);
await instance.__pendingFn;
}
jest.advanceTimersByTime(remainder);
await instance.__pendingFn;
}

describe('AsyncInterval', () => {
test('should call fn 3 times', async () => {
const spy = jest.fn();
const instance = new AsyncInterval(spy, 1000);
await waitFor(instance, 2000);
await waitFor(instance, 2000);
await waitFor(instance, 2000);

const refrestInterval = 1000;
const instance = new AsyncInterval(spy, refrestInterval);
await andvanceTimerAndAwaitFn(instance, 3000);
expect(spy).toHaveBeenCalledTimes(3);
});

test('should not call fn after stop has been called', async () => {
const spy = jest.fn();
const instance = new AsyncInterval(spy, 1000);
await waitFor(instance, 2000);

await andvanceTimerAndAwaitFn(instance, 1000);
expect(spy).toHaveBeenCalledTimes(1);

await andvanceTimerAndAwaitFn(instance, 1000);
expect(spy).toHaveBeenCalledTimes(2);

instance.stop();
await waitFor(instance, 2000);
await waitFor(instance, 2000);
await andvanceTimerAndAwaitFn(instance, 1000);
expect(spy).toHaveBeenCalledTimes(2);
});

test('should wait invoking next refresh interval until promise has resolved', async () => {
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const spy = jest.fn(async () => await sleep(500));
const instance = new AsyncInterval(spy, 1000);

await andvanceTimerAndAwaitFn(instance, 0);
expect(spy).toHaveBeenCalledTimes(0);

await andvanceTimerAndAwaitFn(instance, 1000);
expect(spy).toHaveBeenCalledTimes(1);

await andvanceTimerAndAwaitFn(instance, 1500);
expect(spy).toHaveBeenCalledTimes(2);

await andvanceTimerAndAwaitFn(instance, 1500);
expect(spy).toHaveBeenCalledTimes(3);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export class EuiSuperDatePicker extends Component {
onRefreshChange: PropTypes.func,

/**
* Callback for when the refresh interval is fired
* Callback for when the refresh interval is fired. Called with { start, end, refreshInterval }
* If a promise is returned, the next refresh interval will not start until the promise has resolved.
* If the promise rejects the refresh interval will stop and the error thrown
*/
onRefresh: PropTypes.func,

Expand Down

0 comments on commit dc76393

Please sign in to comment.