-
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.
[Workplace Search] Add tests for remaining Sources components (#89026)
* Remove history params We already replace the history.push functionality with KibanaLogic.values.navigateToUrl but the history object was still being passed around. * Add org sources container tests * Add tests for source router * Clean up leftover history imports * Add tests for SourcesRouter * Quick refactor for cleaner existence check Optional chaining FTW * Refactor to simplify setInterval logic This commit does a refactor to move the logic for polling for status to the logic file. In doing this I realized that we were intializing sources in the SourcesView, when we are actually already initializing sources in the components that use this, which are OrganizationSources and PrivateSources, the top-level containers. Because of this, I was able to remove the useEffect entireley, as the flash messages are cleared between page transitions in Kibana and the initialization of the sources ahppens in the containers. * Add tests for SourcesView * Fix type issue
- Loading branch information
1 parent
c495093
commit 4281a34
Showing
9 changed files
with
337 additions
and
34 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
.../public/applications/workplace_search/views/content_sources/organization_sources.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,63 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import '../../../__mocks__/shallow_useeffect.mock'; | ||
|
||
import { setMockValues, setMockActions } from '../../../__mocks__'; | ||
|
||
import { shallow } from 'enzyme'; | ||
|
||
import React from 'react'; | ||
import { Redirect } from 'react-router-dom'; | ||
|
||
import { contentSources } from '../../__mocks__/content_sources.mock'; | ||
|
||
import { Loading } from '../../../shared/loading'; | ||
import { SourcesTable } from '../../components/shared/sources_table'; | ||
import { ViewContentHeader } from '../../components/shared/view_content_header'; | ||
|
||
import { ADD_SOURCE_PATH, getSourcesPath } from '../../routes'; | ||
|
||
import { OrganizationSources } from './organization_sources'; | ||
|
||
describe('OrganizationSources', () => { | ||
const initializeSources = jest.fn(); | ||
const setSourceSearchability = jest.fn(); | ||
|
||
const mockValues = { | ||
contentSources, | ||
dataLoading: false, | ||
}; | ||
|
||
beforeEach(() => { | ||
setMockActions({ | ||
initializeSources, | ||
setSourceSearchability, | ||
}); | ||
setMockValues({ ...mockValues }); | ||
}); | ||
|
||
it('renders', () => { | ||
const wrapper = shallow(<OrganizationSources />); | ||
|
||
expect(wrapper.find(SourcesTable)).toHaveLength(1); | ||
expect(wrapper.find(ViewContentHeader)).toHaveLength(1); | ||
}); | ||
|
||
it('returns loading when loading', () => { | ||
setMockValues({ ...mockValues, dataLoading: true }); | ||
const wrapper = shallow(<OrganizationSources />); | ||
|
||
expect(wrapper.find(Loading)).toHaveLength(1); | ||
}); | ||
|
||
it('returns redirect when no sources', () => { | ||
setMockValues({ ...mockValues, contentSources: [] }); | ||
const wrapper = shallow(<OrganizationSources />); | ||
|
||
expect(wrapper.find(Redirect).prop('to')).toEqual(getSourcesPath(ADD_SOURCE_PATH, true)); | ||
}); | ||
}); |
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
120 changes: 120 additions & 0 deletions
120
..._search/public/applications/workplace_search/views/content_sources/source_router.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,120 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import '../../../__mocks__/shallow_useeffect.mock'; | ||
|
||
import { setMockValues, setMockActions } from '../../../__mocks__'; | ||
|
||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import { useParams } from 'react-router-dom'; | ||
|
||
import { Route, Switch } from 'react-router-dom'; | ||
|
||
import { contentSources } from '../../__mocks__/content_sources.mock'; | ||
|
||
import { SetWorkplaceSearchChrome as SetPageChrome } from '../../../shared/kibana_chrome'; | ||
|
||
import { NAV } from '../../constants'; | ||
|
||
import { Loading } from '../../../shared/loading'; | ||
|
||
import { DisplaySettingsRouter } from './components/display_settings'; | ||
import { Overview } from './components/overview'; | ||
import { Schema } from './components/schema'; | ||
import { SchemaChangeErrors } from './components/schema/schema_change_errors'; | ||
import { SourceContent } from './components/source_content'; | ||
import { SourceSettings } from './components/source_settings'; | ||
|
||
import { SourceRouter } from './source_router'; | ||
|
||
describe('SourceRouter', () => { | ||
const initializeSource = jest.fn(); | ||
const contentSource = contentSources[1]; | ||
const customSource = contentSources[0]; | ||
const mockValues = { | ||
contentSource, | ||
dataLoading: false, | ||
}; | ||
|
||
beforeEach(() => { | ||
setMockActions({ | ||
initializeSource, | ||
}); | ||
setMockValues({ ...mockValues }); | ||
(useParams as jest.Mock).mockImplementationOnce(() => ({ | ||
sourceId: '1', | ||
})); | ||
}); | ||
|
||
it('returns Loading when loading', () => { | ||
setMockValues({ ...mockValues, dataLoading: true }); | ||
const wrapper = shallow(<SourceRouter />); | ||
|
||
expect(wrapper.find(Loading)).toHaveLength(1); | ||
}); | ||
|
||
it('renders source routes (standard)', () => { | ||
const wrapper = shallow(<SourceRouter />); | ||
|
||
expect(wrapper.find(Overview)).toHaveLength(1); | ||
expect(wrapper.find(SourceSettings)).toHaveLength(1); | ||
expect(wrapper.find(SourceContent)).toHaveLength(1); | ||
expect(wrapper.find(Switch)).toHaveLength(1); | ||
expect(wrapper.find(Route)).toHaveLength(3); | ||
}); | ||
|
||
it('renders source routes (custom)', () => { | ||
setMockValues({ ...mockValues, contentSource: customSource }); | ||
const wrapper = shallow(<SourceRouter />); | ||
|
||
expect(wrapper.find(DisplaySettingsRouter)).toHaveLength(1); | ||
expect(wrapper.find(Schema)).toHaveLength(1); | ||
expect(wrapper.find(SchemaChangeErrors)).toHaveLength(1); | ||
expect(wrapper.find(Route)).toHaveLength(6); | ||
}); | ||
|
||
it('handles breadcrumbs while loading (standard)', () => { | ||
setMockValues({ | ||
...mockValues, | ||
contentSource: {}, | ||
}); | ||
|
||
const loadingBreadcrumbs = ['Sources', '...']; | ||
|
||
const wrapper = shallow(<SourceRouter />); | ||
|
||
const overviewBreadCrumb = wrapper.find(SetPageChrome).at(0); | ||
const contentBreadCrumb = wrapper.find(SetPageChrome).at(1); | ||
const settingsBreadCrumb = wrapper.find(SetPageChrome).at(2); | ||
|
||
expect(overviewBreadCrumb.prop('trail')).toEqual([...loadingBreadcrumbs, NAV.OVERVIEW]); | ||
expect(contentBreadCrumb.prop('trail')).toEqual([...loadingBreadcrumbs, NAV.CONTENT]); | ||
expect(settingsBreadCrumb.prop('trail')).toEqual([...loadingBreadcrumbs, NAV.SETTINGS]); | ||
}); | ||
|
||
it('handles breadcrumbs while loading (custom)', () => { | ||
setMockValues({ | ||
...mockValues, | ||
contentSource: { serviceType: 'custom' }, | ||
}); | ||
|
||
const loadingBreadcrumbs = ['Sources', '...']; | ||
|
||
const wrapper = shallow(<SourceRouter />); | ||
|
||
const schemaBreadCrumb = wrapper.find(SetPageChrome).at(2); | ||
const schemaErrorsBreadCrumb = wrapper.find(SetPageChrome).at(3); | ||
const displaySettingsBreadCrumb = wrapper.find(SetPageChrome).at(4); | ||
|
||
expect(schemaBreadCrumb.prop('trail')).toEqual([...loadingBreadcrumbs, NAV.SCHEMA]); | ||
expect(schemaErrorsBreadCrumb.prop('trail')).toEqual([...loadingBreadcrumbs, NAV.SCHEMA]); | ||
expect(displaySettingsBreadCrumb.prop('trail')).toEqual([ | ||
...loadingBreadcrumbs, | ||
NAV.DISPLAY_SETTINGS, | ||
]); | ||
}); | ||
}); |
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
60 changes: 60 additions & 0 deletions
60
...search/public/applications/workplace_search/views/content_sources/sources_router.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,60 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import '../../../__mocks__/shallow_useeffect.mock'; | ||
|
||
import { setMockValues, setMockActions } from '../../../__mocks__'; | ||
|
||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
|
||
import { Route, Switch, Redirect } from 'react-router-dom'; | ||
|
||
import { ADD_SOURCE_PATH, PERSONAL_SOURCES_PATH, SOURCES_PATH, getSourcesPath } from '../../routes'; | ||
|
||
import { SourcesRouter } from './sources_router'; | ||
|
||
describe('SourcesRouter', () => { | ||
const resetSourcesState = jest.fn(); | ||
const mockValues = { | ||
account: { canCreatePersonalSources: true }, | ||
isOrganization: true, | ||
hasPlatinumLicense: true, | ||
}; | ||
|
||
beforeEach(() => { | ||
setMockActions({ | ||
resetSourcesState, | ||
}); | ||
setMockValues({ ...mockValues }); | ||
}); | ||
|
||
it('renders sources routes', () => { | ||
const TOTAL_ROUTES = 62; | ||
const wrapper = shallow(<SourcesRouter />); | ||
|
||
expect(wrapper.find(Switch)).toHaveLength(1); | ||
expect(wrapper.find(Route)).toHaveLength(TOTAL_ROUTES); | ||
}); | ||
|
||
it('redirects when nonplatinum license and accountOnly context', () => { | ||
setMockValues({ ...mockValues, hasPlatinumLicense: false }); | ||
const wrapper = shallow(<SourcesRouter />); | ||
|
||
expect(wrapper.find(Redirect).first().prop('from')).toEqual(ADD_SOURCE_PATH); | ||
expect(wrapper.find(Redirect).first().prop('to')).toEqual(SOURCES_PATH); | ||
}); | ||
|
||
it('redirects when cannot create sources', () => { | ||
setMockValues({ ...mockValues, account: { canCreatePersonalSources: false } }); | ||
const wrapper = shallow(<SourcesRouter />); | ||
|
||
expect(wrapper.find(Redirect).last().prop('from')).toEqual( | ||
getSourcesPath(ADD_SOURCE_PATH, false) | ||
); | ||
expect(wrapper.find(Redirect).last().prop('to')).toEqual(PERSONAL_SOURCES_PATH); | ||
}); | ||
}); |
Oops, something went wrong.