-
Notifications
You must be signed in to change notification settings - Fork 919
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break down getTopNavConfig function and added unit tests
Signed-off-by: abbyhu2000 <[email protected]>
- Loading branch information
1 parent
32787cd
commit 4329340
Showing
3 changed files
with
496 additions
and
66 deletions.
There are no files selected for viewing
299 changes: 299 additions & 0 deletions
299
src/plugins/wizard/public/application/utils/get_top_nav_config.test.tsx
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,299 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { WizardServices } from '../../types'; | ||
import { | ||
addWizardDirectly, | ||
formulateSavedWizardVis, | ||
getTopNavConfig, | ||
TopNavConfigParams, | ||
validateSuccessfulSave, | ||
} from './get_top_nav_config'; | ||
import { createWizardServicesMock } from './mocks'; | ||
import { ApplicationStart, IToasts } from '../../../../../core/public'; | ||
import { EmbeddableStateTransfer } from '../../../../embeddable/public'; | ||
|
||
describe('getTopNavConfig', () => { | ||
let savedWizardVis: any; | ||
let mockServices: jest.Mocked<WizardServices>; | ||
let initialTopNavConfig: TopNavConfigParams; | ||
|
||
beforeEach(() => { | ||
mockServices = createWizardServicesMock(); | ||
savedWizardVis = { | ||
id: '1', | ||
title: 'save wizard wiz title', | ||
description: '', | ||
visualizationState: '', | ||
styleState: '', | ||
version: 0, | ||
copyOnSave: true, | ||
searchSourceFields: {}, | ||
}; | ||
}); | ||
|
||
describe('topNavConfig', () => { | ||
beforeEach(() => { | ||
initialTopNavConfig = { | ||
visualizationIdFromUrl: '', | ||
savedWizardVis, | ||
visualizationState: { | ||
searchField: '', | ||
}, | ||
styleState: {}, | ||
saveDisabledReason: 'invalid field', | ||
dispatch: jest.fn(), | ||
}; | ||
}); | ||
|
||
test('topNavConfig fields', async () => { | ||
const returnResult = getTopNavConfig(initialTopNavConfig, mockServices); | ||
const topNavConfig = returnResult[0]; | ||
expect(topNavConfig.id).toBe('save'); | ||
expect(topNavConfig.iconType).toBe('save'); | ||
expect(topNavConfig.emphasize).toBeFalsy(); | ||
expect(topNavConfig.description).toBe('Save Visualization'); | ||
expect(topNavConfig.className).toBe('saveButton'); | ||
expect(topNavConfig.label).toBe('save'); | ||
expect(topNavConfig.testId).toBe('wizardSaveButton'); | ||
expect(topNavConfig.disableButton).toBeTruthy(); | ||
expect(topNavConfig.tooltip).toBe('invalid field'); | ||
}); | ||
|
||
test('true emphasize', async () => { | ||
initialTopNavConfig.savedWizardVis.id = undefined; | ||
const returnResult = getTopNavConfig(initialTopNavConfig, mockServices); | ||
const topNavConfig = returnResult[0]; | ||
expect(topNavConfig.emphasize).toBeTruthy(); | ||
}); | ||
|
||
test('disableButton', async () => { | ||
initialTopNavConfig.saveDisabledReason = undefined; | ||
const returnResult = getTopNavConfig(initialTopNavConfig, mockServices); | ||
const topNavConfig = returnResult[0]; | ||
expect(topNavConfig.disableButton).toBeFalsy(); | ||
expect(topNavConfig.tooltip).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('formulateSavedWizardVis', () => { | ||
let indexPatterns: any; | ||
let visualizationState: any; | ||
let styleState: any; | ||
let newTitle: string; | ||
let newDescription: string; | ||
let newCopyOnSave: boolean; | ||
|
||
beforeEach(() => { | ||
indexPatterns = mockServices.data; | ||
visualizationState = {}; | ||
styleState = {}; | ||
newTitle = 'new title'; | ||
newDescription = 'new description'; | ||
newCopyOnSave = false; | ||
}); | ||
test('return null', async () => { | ||
savedWizardVis = null; | ||
const result = await formulateSavedWizardVis( | ||
savedWizardVis, | ||
indexPatterns, | ||
visualizationState, | ||
styleState, | ||
newTitle, | ||
newDescription, | ||
newCopyOnSave | ||
); | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
test('return true newlyCreated with null id', async () => { | ||
savedWizardVis.id = null; | ||
const result = await formulateSavedWizardVis( | ||
savedWizardVis, | ||
indexPatterns, | ||
visualizationState, | ||
styleState, | ||
newTitle, | ||
newDescription, | ||
newCopyOnSave | ||
); | ||
expect(result?.newlyCreated).toBeTruthy(); | ||
}); | ||
|
||
test('return true newlyCreated with true copyOnSave', async () => { | ||
newCopyOnSave = true; | ||
const result = await formulateSavedWizardVis( | ||
savedWizardVis, | ||
indexPatterns, | ||
visualizationState, | ||
styleState, | ||
newTitle, | ||
newDescription, | ||
newCopyOnSave | ||
); | ||
expect(result?.newlyCreated).toBeTruthy(); | ||
}); | ||
|
||
test('expect savedWizardVis has been formulated correctly', async () => { | ||
const result = await formulateSavedWizardVis( | ||
savedWizardVis, | ||
indexPatterns, | ||
visualizationState, | ||
styleState, | ||
newTitle, | ||
newDescription, | ||
newCopyOnSave | ||
); | ||
expect(typeof savedWizardVis.visualizationState).toBe('string'); | ||
expect(typeof savedWizardVis.styleState).toBe('string'); | ||
expect(savedWizardVis.title).toBe('new title'); | ||
expect(savedWizardVis.description).toBe('new description'); | ||
expect(savedWizardVis.copyOnSave).toBeFalsy(); | ||
expect(result?.currentTitle).toBe('save wizard wiz title'); | ||
}); | ||
}); | ||
|
||
describe('validateSuccessfulSave', () => { | ||
let originatingApp: string | undefined; | ||
let returnToOrigin: boolean; | ||
let newlyCreated: boolean; | ||
let stateTransfer: EmbeddableStateTransfer; | ||
let id: string | undefined; | ||
let application: ApplicationStart; | ||
let toastNotifications: IToasts; | ||
let dispatch: any; | ||
let visualizationIdFromUrl: string | undefined; | ||
let currentTitle: string; | ||
let history: any; | ||
|
||
beforeEach(() => { | ||
stateTransfer = mockServices.embeddable.getStateTransfer(); | ||
application = mockServices.application; | ||
id = '1'; | ||
toastNotifications = mockServices.toastNotifications; | ||
dispatch = jest.fn(); | ||
history = mockServices.history; | ||
}); | ||
|
||
test('create and add a new wizard to dashboard', () => { | ||
originatingApp = 'dashboard'; | ||
returnToOrigin = true; | ||
newlyCreated = true; | ||
const returnResult = addWizardDirectly( | ||
originatingApp, | ||
returnToOrigin, | ||
newlyCreated, | ||
stateTransfer, | ||
id, | ||
application | ||
); | ||
expect(stateTransfer.navigateToWithEmbeddablePackage).toBeCalledTimes(1); | ||
expect(returnResult?.skipDispatch).toBe(true); | ||
expect(returnResult?.setOriginatingAppUndefined).toBe(false); | ||
}); | ||
test('edit an existing wizard from the dashboard', () => { | ||
originatingApp = 'dashboard'; | ||
returnToOrigin = true; | ||
newlyCreated = false; | ||
const returnResult = addWizardDirectly( | ||
originatingApp, | ||
returnToOrigin, | ||
newlyCreated, | ||
stateTransfer, | ||
id, | ||
application | ||
); | ||
expect(stateTransfer.navigateToWithEmbeddablePackage).toBeCalledTimes(0); | ||
expect(application.navigateToApp).toBeCalledWith('dashboard'); | ||
expect(returnResult).toBeUndefined(); | ||
}); | ||
test('create wizard from visualization page', () => { | ||
originatingApp = 'visualization'; | ||
returnToOrigin = false; | ||
newlyCreated = true; | ||
const returnResult = addWizardDirectly( | ||
originatingApp, | ||
returnToOrigin, | ||
newlyCreated, | ||
stateTransfer, | ||
id, | ||
application | ||
); | ||
|
||
expect(returnResult?.skipDispatch).toBeFalsy(); | ||
expect(returnResult?.setOriginatingAppUndefined).toBeTruthy(); | ||
}); | ||
test('reset title if save not successful', () => { | ||
id = undefined; | ||
const returnResult = validateSuccessfulSave( | ||
id, | ||
toastNotifications, | ||
savedWizardVis, | ||
originatingApp, | ||
returnToOrigin, | ||
newlyCreated, | ||
stateTransfer, | ||
application, | ||
visualizationIdFromUrl, | ||
history, | ||
dispatch, | ||
currentTitle | ||
); | ||
|
||
expect(savedWizardVis.title).toBe(currentTitle); | ||
expect(returnResult).toBeUndefined(); | ||
}); | ||
|
||
test('skip dispatch when creating directly from dashboard', () => { | ||
originatingApp = 'dashboard'; | ||
returnToOrigin = true; | ||
newlyCreated = true; | ||
id = '1'; | ||
const returnResult = validateSuccessfulSave( | ||
id, | ||
toastNotifications, | ||
savedWizardVis, | ||
originatingApp, | ||
returnToOrigin, | ||
newlyCreated, | ||
stateTransfer, | ||
application, | ||
visualizationIdFromUrl, | ||
history, | ||
dispatch, | ||
currentTitle | ||
); | ||
|
||
expect(dispatch).toBeCalledTimes(0); | ||
expect(returnResult).toBeFalsy(); | ||
}); | ||
|
||
test('push history when id does not equal visualizatonIdFromUrl', () => { | ||
originatingApp = 'visualization'; | ||
returnToOrigin = false; | ||
newlyCreated = true; | ||
id = '1'; | ||
visualizationIdFromUrl = '2'; | ||
const returnResult = validateSuccessfulSave( | ||
id, | ||
toastNotifications, | ||
savedWizardVis, | ||
originatingApp, | ||
returnToOrigin, | ||
newlyCreated, | ||
stateTransfer, | ||
application, | ||
visualizationIdFromUrl, | ||
history, | ||
dispatch, | ||
currentTitle | ||
); | ||
|
||
expect(history.push).toBeCalled(); | ||
expect(dispatch).toBeCalledTimes(1); | ||
expect(returnResult).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.