-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
62 additions
and
16 deletions.
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
64 changes: 50 additions & 14 deletions
64
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 |
---|---|---|
@@ -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); | ||
}); | ||
}); |
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