-
Notifications
You must be signed in to change notification settings - Fork 844
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EuiSuperDatePicker] Add
onRefresh
handler (#1577)
- Loading branch information
Showing
6 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/components/date_picker/super_date_picker/async_interval.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export class AsyncInterval { | ||
timeoutId = null; | ||
isStopped = false; | ||
|
||
constructor(fn, refreshInterval) { | ||
this.setAsyncInterval(fn, refreshInterval); | ||
} | ||
|
||
setAsyncInterval = (fn, ms) => { | ||
if (!this.isStopped) { | ||
this.timeoutId = window.setTimeout(async () => { | ||
this.__pendingFn = await fn(); | ||
this.setAsyncInterval(fn, ms); | ||
}, ms); | ||
} | ||
}; | ||
|
||
stop = () => { | ||
this.isStopped = true; | ||
window.clearTimeout(this.timeoutId); | ||
}; | ||
} |
95 changes: 95 additions & 0 deletions
95
src/components/date_picker/super_date_picker/async_interval.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { AsyncInterval } from './async_interval'; | ||
import { times } from 'lodash'; | ||
|
||
describe('AsyncInterval', () => { | ||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
// Advances time and awaits any pending promises after every 100ms | ||
// This helper makes it easier to advance time without worrying | ||
// whether tasks are still lingering on the event loop | ||
async function andvanceTimerAndAwaitFn(instance, ms) { | ||
const iterations = times(Math.floor(ms / 100)); | ||
const remainder = ms % 100; | ||
// eslint-disable-next-line no-unused-vars | ||
for (const item of iterations) { | ||
await instance.__pendingFn; | ||
jest.advanceTimersByTime(100); | ||
await instance.__pendingFn; | ||
} | ||
jest.advanceTimersByTime(remainder); | ||
await instance.__pendingFn; | ||
} | ||
|
||
describe('when creating a 1000ms interval', async () => { | ||
let instance; | ||
let spy; | ||
beforeEach(() => { | ||
spy = jest.fn(); | ||
instance = new AsyncInterval(spy, 1000); | ||
}); | ||
|
||
it('should not call fn immediately', async () => { | ||
await andvanceTimerAndAwaitFn(instance, 0); | ||
expect(spy).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('should have called fn once after 1000ms', async () => { | ||
await andvanceTimerAndAwaitFn(instance, 1000); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should have called fn twice after 2000ms', async () => { | ||
await andvanceTimerAndAwaitFn(instance, 2000); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should have called fn three times after 3000ms', async () => { | ||
await andvanceTimerAndAwaitFn(instance, 3000); | ||
expect(spy).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it('should not call fn after stop has been invoked', async () => { | ||
await andvanceTimerAndAwaitFn(instance, 1000); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
instance.stop(); | ||
await andvanceTimerAndAwaitFn(instance, 1000); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe('when creating a 1000ms interval that calls a fn that takes 2000ms to complete', async () => { | ||
let instance; | ||
let spy; | ||
beforeEach(() => { | ||
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); | ||
spy = jest.fn(async () => await sleep(2000)); | ||
instance = new AsyncInterval(spy, 1000); | ||
}); | ||
|
||
it('should not call fn immediately', async () => { | ||
await andvanceTimerAndAwaitFn(instance, 0); | ||
expect(spy).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it('should have called fn once after 1000ms', async () => { | ||
await andvanceTimerAndAwaitFn(instance, 1000); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should have called fn twice after 4000ms', async () => { | ||
await andvanceTimerAndAwaitFn(instance, 4000); | ||
expect(spy).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should have called fn tree times after 7000ms', async () => { | ||
await andvanceTimerAndAwaitFn(instance, 7000); | ||
expect(spy).toHaveBeenCalledTimes(3); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters