-
Notifications
You must be signed in to change notification settings - Fork 893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Dashboard De-Angular] Add more unit tests for utils folder #4641
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
45 changes: 45 additions & 0 deletions
45
src/plugins/dashboard/public/application/utils/breadcrumbs.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,45 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Any modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
import { ViewMode } from 'src/plugins/embeddable/public'; | ||
import { setBreadcrumbsForExistingDashboard, setBreadcrumbsForNewDashboard } from './breadcrumbs'; | ||
|
||
describe('breadcrumbs for ', () => { | ||
test('new dashboard in view mode', () => { | ||
const text = setBreadcrumbsForNewDashboard(ViewMode.VIEW, false); | ||
expect(text[1].text).toBe('New Dashboard'); | ||
}); | ||
|
||
test('new dashboard in edit mode without unsaved changes', () => { | ||
const text = setBreadcrumbsForNewDashboard(ViewMode.EDIT, false); | ||
expect(text[1].text).toBe('Editing New Dashboard'); | ||
}); | ||
|
||
test('new dashboard in edit mode with unsaved changes', () => { | ||
const text = setBreadcrumbsForNewDashboard(ViewMode.EDIT, true); | ||
expect(text[1].text).toBe('Editing New Dashboard (unsaved)'); | ||
}); | ||
|
||
test('existing dashboard in view mode', () => { | ||
const text = setBreadcrumbsForExistingDashboard('dashboard name', ViewMode.VIEW, false); | ||
expect(text[1].text).toBe('dashboard name'); | ||
}); | ||
|
||
test('existing dashboard in edit mode without unsaved changes', () => { | ||
const text = setBreadcrumbsForExistingDashboard('dashboard name', ViewMode.EDIT, false); | ||
expect(text[1].text).toBe('Editing dashboard name'); | ||
}); | ||
|
||
test('existing dashboard in edit mode with unsaved changes', () => { | ||
const text = setBreadcrumbsForExistingDashboard('dashboard name', ViewMode.EDIT, true); | ||
expect(text[1].text).toBe('Editing dashboard name (unsaved)'); | ||
}); | ||
}); |
159 changes: 159 additions & 0 deletions
159
src/plugins/dashboard/public/application/utils/create_dashboard_app_state.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,159 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Any modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
import { IOsdUrlStateStorage } from 'src/plugins/opensearch_dashboards_utils/public'; | ||
import { createDashboardGlobalAndAppState, updateStateUrl } from './create_dashboard_app_state'; | ||
import { migrateAppState } from './migrate_app_state'; | ||
import { dashboardAppStateStub } from './stubs'; | ||
import { createDashboardServicesMock } from './mocks'; | ||
import { SavedObjectDashboard } from '../..'; | ||
import { syncQueryStateWithUrl } from 'src/plugins/data/public'; | ||
import { ViewMode } from 'src/plugins/embeddable/public'; | ||
|
||
const mockStartStateSync = jest.fn(); | ||
const mockStopStateSync = jest.fn(); | ||
const mockStopQueryStateSync = jest.fn(); | ||
|
||
jest.mock('../../../../opensearch_dashboards_utils/public', () => ({ | ||
createStateContainer: jest.fn(() => 'stateContainer'), | ||
syncState: jest.fn(() => ({ | ||
start: mockStartStateSync, | ||
stop: mockStopStateSync, | ||
})), | ||
})); | ||
|
||
jest.mock('../../../../data/public', () => ({ | ||
syncQueryStateWithUrl: jest.fn(() => ({ | ||
stop: mockStopQueryStateSync, | ||
})), | ||
})); | ||
|
||
jest.mock('./migrate_app_state', () => ({ | ||
migrateAppState: jest.fn(() => 'migratedAppState'), | ||
})); | ||
|
||
const { createStateContainer, syncState } = jest.requireMock( | ||
'../../../../opensearch_dashboards_utils/public' | ||
); | ||
|
||
const osdUrlStateStorage = ({ | ||
set: jest.fn(), | ||
get: jest.fn(() => ({ linked: false })), | ||
flush: jest.fn(), | ||
} as unknown) as IOsdUrlStateStorage; | ||
|
||
describe('createDashboardGlobalAndAppState', () => { | ||
const mockServices = createDashboardServicesMock(); | ||
|
||
const savedDashboardInstance = { | ||
id: '', | ||
timeRestore: true, | ||
lastSavedTitle: 'title', | ||
searchSource: {}, | ||
getQuery: () => {}, | ||
getFilters: () => {}, | ||
} as SavedObjectDashboard; | ||
|
||
const { | ||
stateContainer, | ||
stopStateSync, | ||
stopSyncingQueryServiceStateWithUrl, | ||
} = createDashboardGlobalAndAppState({ | ||
stateDefaults: dashboardAppStateStub, | ||
osdUrlStateStorage, | ||
services: mockServices, | ||
savedDashboardInstance, | ||
}); | ||
const transitions = createStateContainer.mock.calls[0][1]; | ||
|
||
test('should initialize dashboard app state', () => { | ||
expect(osdUrlStateStorage.get).toHaveBeenCalledWith('_a'); | ||
expect(migrateAppState).toHaveBeenCalledWith( | ||
{ | ||
...dashboardAppStateStub, | ||
linked: false, | ||
}, | ||
mockServices.opensearchDashboardsVersion, | ||
mockServices.usageCollection | ||
); | ||
expect(osdUrlStateStorage.set).toHaveBeenCalledWith('_a', 'migratedAppState', { | ||
replace: true, | ||
}); | ||
expect(createStateContainer).toHaveBeenCalled(); | ||
expect(syncState).toHaveBeenCalled(); | ||
expect(syncQueryStateWithUrl).toHaveBeenCalled(); | ||
expect(mockStartStateSync).toHaveBeenCalled(); | ||
}); | ||
|
||
test('should return the stateContainer and stopStateSync and stopSyncingQueryServiceStateWithUrl', () => { | ||
expect(stateContainer).toBe('stateContainer'); | ||
stopStateSync(); | ||
stopSyncingQueryServiceStateWithUrl(); | ||
expect(stopStateSync).toHaveBeenCalledTimes(1); | ||
expect(stopSyncingQueryServiceStateWithUrl).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
describe('stateContainer transitions', () => { | ||
test('set', () => { | ||
const newQuery = { query: '', language: '' }; | ||
expect(transitions.set(dashboardAppStateStub)('query', newQuery)).toEqual({ | ||
...dashboardAppStateStub, | ||
query: newQuery, | ||
}); | ||
}); | ||
|
||
test('setOption', () => { | ||
const newOptions = { | ||
hidePanelTitles: true, | ||
}; | ||
expect( | ||
transitions.setOption(dashboardAppStateStub)('hidePanelTitles', newOptions.hidePanelTitles) | ||
).toEqual({ | ||
...dashboardAppStateStub, | ||
options: { | ||
...dashboardAppStateStub.options, | ||
...newOptions, | ||
}, | ||
}); | ||
}); | ||
|
||
test('setDashboard', () => { | ||
const newDashboard = { | ||
fullScreenMode: true, | ||
title: 'new title', | ||
description: 'New Dashboard Test Description', | ||
timeRestore: true, | ||
query: { query: '', language: 'kuery' }, | ||
viewMode: ViewMode.VIEW, | ||
}; | ||
expect(transitions.setDashboard(dashboardAppStateStub)(newDashboard)).toEqual({ | ||
...dashboardAppStateStub, | ||
...newDashboard, | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('updateStateUrl', () => { | ||
const dashboardAppState = { | ||
...dashboardAppStateStub, | ||
viewMode: ViewMode.VIEW, | ||
}; | ||
updateStateUrl({ osdUrlStateStorage, state: dashboardAppState, replace: true }); | ||
|
||
test('update URL to not contain panels', () => { | ||
const { panels, ...statesWithoutPanels } = dashboardAppState; | ||
expect(osdUrlStateStorage.set).toHaveBeenCalledWith('_a', statesWithoutPanels, { | ||
replace: true, | ||
}); | ||
expect(osdUrlStateStorage.flush).toHaveBeenCalledWith({ replace: true }); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!