-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Fixes bugs with autoplay and refresh * Fix typecheck Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
- Loading branch information
1 parent
b57d6b7
commit b327278
Showing
5 changed files
with
351 additions
and
26 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
145 changes: 145 additions & 0 deletions
145
x-pack/legacy/plugins/canvas/public/state/middleware/__tests__/workpad_autoplay.test.ts
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,145 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
jest.mock('../../../lib/app_state'); | ||
jest.mock('../../../lib/router_provider'); | ||
|
||
import { workpadAutoplay } from '../workpad_autoplay'; | ||
import { setAutoplayInterval } from '../../../lib/app_state'; | ||
import { createTimeInterval } from '../../../lib/time_interval'; | ||
// @ts-ignore Untyped local | ||
import { routerProvider } from '../../../lib/router_provider'; | ||
|
||
const next = jest.fn(); | ||
const dispatch = jest.fn(); | ||
const getState = jest.fn(); | ||
const routerMock = { navigateTo: jest.fn() }; | ||
routerProvider.mockReturnValue(routerMock); | ||
|
||
const middleware = workpadAutoplay({ dispatch, getState })(next); | ||
|
||
const workpadState = { | ||
persistent: { | ||
workpad: { | ||
id: 'workpad-id', | ||
pages: ['page1', 'page2', 'page3'], | ||
page: 0, | ||
}, | ||
}, | ||
}; | ||
|
||
const autoplayState = { | ||
...workpadState, | ||
transient: { | ||
autoplay: { | ||
inFlight: false, | ||
enabled: true, | ||
interval: 5000, | ||
}, | ||
fullscreen: true, | ||
}, | ||
}; | ||
|
||
const autoplayDisabledState = { | ||
...workpadState, | ||
transient: { | ||
autoplay: { | ||
inFlight: false, | ||
enabled: false, | ||
interval: 5000, | ||
}, | ||
}, | ||
}; | ||
|
||
const action = {}; | ||
|
||
describe('workpad autoplay middleware', () => { | ||
beforeEach(() => { | ||
dispatch.mockClear(); | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('app state', () => { | ||
it('sets the app state to the interval from state when enabled', () => { | ||
getState.mockReturnValue(autoplayState); | ||
middleware(action); | ||
|
||
expect(setAutoplayInterval).toBeCalledWith( | ||
createTimeInterval(autoplayState.transient.autoplay.interval) | ||
); | ||
}); | ||
|
||
it('sets the app state to null when not enabled', () => { | ||
getState.mockReturnValue(autoplayDisabledState); | ||
middleware(action); | ||
|
||
expect(setAutoplayInterval).toBeCalledWith(null); | ||
}); | ||
}); | ||
|
||
describe('autoplay navigation', () => { | ||
it('navigates forward after interval', () => { | ||
jest.useFakeTimers(); | ||
getState.mockReturnValue(autoplayState); | ||
middleware(action); | ||
|
||
jest.advanceTimersByTime(autoplayState.transient.autoplay.interval + 1); | ||
|
||
expect(routerMock.navigateTo).toBeCalledWith('loadWorkpad', { | ||
id: workpadState.persistent.workpad.id, | ||
page: workpadState.persistent.workpad.page + 2, // (index + 1) + 1 more for 1 indexed page number | ||
}); | ||
|
||
jest.useRealTimers(); | ||
}); | ||
|
||
it('navigates from last page back to front', () => { | ||
jest.useFakeTimers(); | ||
const onLastPageState = { ...autoplayState }; | ||
onLastPageState.persistent.workpad.page = onLastPageState.persistent.workpad.pages.length - 1; | ||
|
||
getState.mockReturnValue(autoplayState); | ||
middleware(action); | ||
|
||
jest.advanceTimersByTime(autoplayState.transient.autoplay.interval + 1); | ||
|
||
expect(routerMock.navigateTo).toBeCalledWith('loadWorkpad', { | ||
id: workpadState.persistent.workpad.id, | ||
page: 1, | ||
}); | ||
|
||
jest.useRealTimers(); | ||
}); | ||
|
||
it('continues autoplaying', () => { | ||
jest.useFakeTimers(); | ||
getState.mockReturnValue(autoplayState); | ||
middleware(action); | ||
|
||
jest.advanceTimersByTime(autoplayState.transient.autoplay.interval * 2 + 1); | ||
expect(routerMock.navigateTo).toBeCalledTimes(2); | ||
jest.useRealTimers(); | ||
}); | ||
|
||
it('does not reset timer between middleware calls', () => { | ||
jest.useFakeTimers(); | ||
|
||
getState.mockReturnValue(autoplayState); | ||
middleware(action); | ||
|
||
// Advance until right before timeout | ||
jest.advanceTimersByTime(autoplayState.transient.autoplay.interval - 1); | ||
|
||
// Run middleware again | ||
middleware(action); | ||
|
||
// Advance timer | ||
jest.advanceTimersByTime(1); | ||
|
||
expect(routerMock.navigateTo).toBeCalled(); | ||
}); | ||
}); | ||
}); |
161 changes: 161 additions & 0 deletions
161
x-pack/legacy/plugins/canvas/public/state/middleware/__tests__/workpad_refresh.test.ts
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,161 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
jest.mock('../../../legacy'); | ||
jest.mock('ui/new_platform'); // actions/elements has some dependencies on ui/new_platform. | ||
jest.mock('../../../lib/app_state'); | ||
|
||
import { workpadRefresh } from '../workpad_refresh'; | ||
import { inFlightComplete } from '../../actions/resolved_args'; | ||
// @ts-ignore untyped local | ||
import { setRefreshInterval } from '../../actions/workpad'; | ||
import { setRefreshInterval as setAppStateRefreshInterval } from '../../../lib/app_state'; | ||
|
||
import { createTimeInterval } from '../../../lib/time_interval'; | ||
|
||
const next = jest.fn(); | ||
const dispatch = jest.fn(); | ||
const getState = jest.fn(); | ||
|
||
const middleware = workpadRefresh({ dispatch, getState })(next); | ||
|
||
const refreshState = { | ||
transient: { | ||
refresh: { | ||
interval: 5000, | ||
}, | ||
}, | ||
}; | ||
|
||
const noRefreshState = { | ||
transient: { | ||
refresh: { | ||
interval: 0, | ||
}, | ||
}, | ||
}; | ||
|
||
const inFlightState = { | ||
transient: { | ||
refresh: { | ||
interval: 5000, | ||
}, | ||
inFlight: true, | ||
}, | ||
}; | ||
|
||
describe('workpad refresh middleware', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('onInflightComplete', () => { | ||
it('refreshes if interval gt 0', () => { | ||
jest.useFakeTimers(); | ||
getState.mockReturnValue(refreshState); | ||
|
||
middleware(inFlightComplete()); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(dispatch).toHaveBeenCalled(); | ||
}); | ||
|
||
it('does not reset interval if another action occurs', () => { | ||
jest.useFakeTimers(); | ||
getState.mockReturnValue(refreshState); | ||
|
||
middleware(inFlightComplete()); | ||
|
||
jest.advanceTimersByTime(refreshState.transient.refresh.interval - 1); | ||
|
||
expect(dispatch).not.toHaveBeenCalled(); | ||
middleware(inFlightComplete()); | ||
|
||
jest.advanceTimersByTime(1); | ||
|
||
expect(dispatch).toHaveBeenCalled(); | ||
}); | ||
|
||
it('does not refresh if interval is 0', () => { | ||
jest.useFakeTimers(); | ||
getState.mockReturnValue(noRefreshState); | ||
|
||
middleware(inFlightComplete()); | ||
|
||
jest.runAllTimers(); | ||
expect(dispatch).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('setRefreshInterval', () => { | ||
it('does nothing if refresh interval is unchanged', () => { | ||
getState.mockReturnValue(refreshState); | ||
|
||
jest.useFakeTimers(); | ||
const interval = 1; | ||
middleware(setRefreshInterval(interval)); | ||
jest.runAllTimers(); | ||
|
||
expect(setAppStateRefreshInterval).not.toBeCalled(); | ||
}); | ||
|
||
it('sets the app refresh interval', () => { | ||
getState.mockReturnValue(noRefreshState); | ||
next.mockImplementation(() => { | ||
getState.mockReturnValue(refreshState); | ||
}); | ||
|
||
jest.useFakeTimers(); | ||
const interval = 1; | ||
middleware(setRefreshInterval(interval)); | ||
|
||
expect(setAppStateRefreshInterval).toBeCalledWith(createTimeInterval(interval)); | ||
jest.runAllTimers(); | ||
}); | ||
|
||
it('starts a refresh for the new interval', () => { | ||
getState.mockReturnValue(refreshState); | ||
jest.useFakeTimers(); | ||
|
||
const interval = 1000; | ||
|
||
middleware(inFlightComplete()); | ||
|
||
jest.runTimersToTime(refreshState.transient.refresh.interval - 1); | ||
expect(dispatch).not.toBeCalled(); | ||
|
||
getState.mockReturnValue(noRefreshState); | ||
next.mockImplementation(() => { | ||
getState.mockReturnValue(refreshState); | ||
}); | ||
middleware(setRefreshInterval(interval)); | ||
jest.runTimersToTime(1); | ||
|
||
expect(dispatch).not.toBeCalled(); | ||
|
||
jest.runTimersToTime(interval); | ||
expect(dispatch).toBeCalled(); | ||
}); | ||
}); | ||
|
||
describe('inFlight in progress', () => { | ||
it('requeues the refresh when inflight is active', () => { | ||
jest.useFakeTimers(); | ||
getState.mockReturnValue(inFlightState); | ||
|
||
middleware(inFlightComplete()); | ||
jest.runTimersToTime(refreshState.transient.refresh.interval); | ||
|
||
expect(dispatch).not.toBeCalled(); | ||
|
||
getState.mockReturnValue(refreshState); | ||
jest.runAllTimers(); | ||
|
||
expect(dispatch).toBeCalled(); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.