-
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.
- Loading branch information
1 parent
d1653bc
commit 5076baf
Showing
5 changed files
with
340 additions
and
11 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
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,60 @@ | ||
/* | ||
* 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: ' ' }).then( | ||
(confirmed) => { | ||
callback(confirmed); | ||
}, | ||
() => { | ||
callback(false); | ||
} | ||
); | ||
}; | ||
}; |
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.