forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ScopedHistory.block (elastic#91099)
* implements ScopedHistory.block * add FTR tests * fix test plugin id * update generated doc * deprecates AppMountParameters.onAppLeave * typo fix * add new FTR test * fix added test
- Loading branch information
1 parent
391b3a8
commit 850e2f8
Showing
19 changed files
with
598 additions
and
19 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { OverlayStart } from '../overlays'; | ||
import { overlayServiceMock } from '../overlays/overlay_service.mock'; | ||
import { getUserConfirmationHandler, ConfirmHandler } from './navigation_confirm'; | ||
|
||
const nextTick = () => new Promise((resolve) => setImmediate(resolve)); | ||
|
||
describe('getUserConfirmationHandler', () => { | ||
let overlayStart: ReturnType<typeof overlayServiceMock.createStartContract>; | ||
let overlayPromise: Promise<OverlayStart>; | ||
let resolvePromise: Function; | ||
let rejectPromise: Function; | ||
let fallbackHandler: jest.MockedFunction<ConfirmHandler>; | ||
let handler: ConfirmHandler; | ||
|
||
beforeEach(() => { | ||
overlayStart = overlayServiceMock.createStartContract(); | ||
overlayPromise = new Promise((resolve, reject) => { | ||
resolvePromise = () => resolve(overlayStart); | ||
rejectPromise = () => reject('some error'); | ||
}); | ||
fallbackHandler = jest.fn().mockImplementation((message, callback) => { | ||
callback(true); | ||
}); | ||
|
||
handler = getUserConfirmationHandler({ | ||
overlayPromise, | ||
fallbackHandler, | ||
}); | ||
}); | ||
|
||
it('uses the fallback handler if the promise is not resolved yet', () => { | ||
const callback = jest.fn(); | ||
handler('foo', callback); | ||
|
||
expect(fallbackHandler).toHaveBeenCalledTimes(1); | ||
expect(fallbackHandler).toHaveBeenCalledWith('foo', callback); | ||
}); | ||
|
||
it('calls the callback with the value returned by the fallback handler', async () => { | ||
const callback = jest.fn(); | ||
handler('foo', callback); | ||
|
||
expect(fallbackHandler).toHaveBeenCalledTimes(1); | ||
expect(fallbackHandler).toHaveBeenCalledWith('foo', callback); | ||
|
||
expect(callback).toHaveBeenCalledTimes(1); | ||
expect(callback).toHaveBeenCalledWith(true); | ||
}); | ||
|
||
it('uses the overlay handler once the promise is resolved', async () => { | ||
resolvePromise(); | ||
await nextTick(); | ||
|
||
const callback = jest.fn(); | ||
handler('foo', callback); | ||
|
||
expect(fallbackHandler).not.toHaveBeenCalled(); | ||
|
||
expect(overlayStart.openConfirm).toHaveBeenCalledTimes(1); | ||
expect(overlayStart.openConfirm).toHaveBeenCalledWith('foo', expect.any(Object)); | ||
}); | ||
|
||
it('calls the callback with the value returned by `openConfirm`', async () => { | ||
overlayStart.openConfirm.mockResolvedValue(true); | ||
|
||
resolvePromise(); | ||
await nextTick(); | ||
|
||
const callback = jest.fn(); | ||
handler('foo', callback); | ||
|
||
await nextTick(); | ||
|
||
expect(callback).toHaveBeenCalledTimes(1); | ||
expect(callback).toHaveBeenCalledWith(true); | ||
}); | ||
|
||
it('uses the fallback handler if the promise rejects', async () => { | ||
rejectPromise(); | ||
await nextTick(); | ||
|
||
const callback = jest.fn(); | ||
handler('foo', callback); | ||
|
||
expect(fallbackHandler).toHaveBeenCalledTimes(1); | ||
expect(overlayStart.openConfirm).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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,62 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { OverlayStart } from 'kibana/public'; | ||
|
||
export type ConfirmHandlerCallback = (result: boolean) => void; | ||
export type ConfirmHandler = (message: string, callback: ConfirmHandlerCallback) => void; | ||
|
||
interface GetUserConfirmationHandlerParams { | ||
overlayPromise: Promise<OverlayStart>; | ||
fallbackHandler?: ConfirmHandler; | ||
} | ||
|
||
export const getUserConfirmationHandler = ({ | ||
overlayPromise, | ||
fallbackHandler = windowConfirm, | ||
}: GetUserConfirmationHandlerParams): ConfirmHandler => { | ||
let overlayConfirm: ConfirmHandler; | ||
|
||
overlayPromise.then( | ||
(overlay) => { | ||
overlayConfirm = getOverlayConfirmHandler(overlay); | ||
}, | ||
() => { | ||
// should never append, but even if it does, we don't need to do anything, | ||
// and will just use the default window confirm instead | ||
} | ||
); | ||
|
||
return (message: string, callback: ConfirmHandlerCallback) => { | ||
if (overlayConfirm) { | ||
overlayConfirm(message, callback); | ||
} else { | ||
fallbackHandler(message, callback); | ||
} | ||
}; | ||
}; | ||
|
||
const windowConfirm: ConfirmHandler = (message: string, callback: ConfirmHandlerCallback) => { | ||
const confirmed = window.confirm(message); | ||
callback(confirmed); | ||
}; | ||
|
||
const getOverlayConfirmHandler = (overlay: OverlayStart): ConfirmHandler => { | ||
return (message: string, callback: ConfirmHandlerCallback) => { | ||
overlay | ||
.openConfirm(message, { title: ' ', 'data-test-subj': 'navigationBlockConfirmModal' }) | ||
.then( | ||
(confirmed) => { | ||
callback(confirmed); | ||
}, | ||
() => { | ||
callback(false); | ||
} | ||
); | ||
}; | ||
}; |
Oops, something went wrong.