Skip to content
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

[App Search] Update EngineRouter & EngineNav to use EngineLogic #83138

Merged
merged 6 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@
margin-top: $euiSizeXS;
}
}

.appSearchNavIcons {
// EUI override
&.euiFlexItem {
flex-grow: 0;
flex-direction: row;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,68 @@
*/

import '../../../__mocks__/react_router_history.mock';
import { setMockValues } from '../../../__mocks__/kea.mock';
import { unmountHandler } from '../../../__mocks__/shallow_useeffect.mock';
import { setMockValues, setMockActions } from '../../../__mocks__/kea.mock';

import React from 'react';
import { shallow, mount } from 'enzyme';
import { Switch, useParams } from 'react-router-dom';
import { shallow } from 'enzyme';
import { Switch, Redirect, useParams } from 'react-router-dom';
import { EuiBadge } from '@elastic/eui';

jest.mock('../../../shared/flash_messages', () => ({
setQueuedErrorMessage: jest.fn(),
}));
import { setQueuedErrorMessage } from '../../../shared/flash_messages';

import { EngineRouter, EngineNav } from './';

describe('EngineRouter', () => {
const values = { dataLoading: false, engineNotFound: false, myRole: {} };
const actions = { setEngineName: jest.fn(), initializeEngine: jest.fn(), clearEngine: jest.fn() };

beforeEach(() => {
setMockValues(values);
setMockActions(actions);
});

describe('useEffect', () => {
beforeEach(() => {
(useParams as jest.Mock).mockReturnValue({ engineName: 'some-engine' });
shallow(<EngineRouter />);
});

it('sets engineName based on the current route parameters', () => {
expect(actions.setEngineName).toHaveBeenCalledWith('some-engine');
});

it('initializes/fetches engine API data', () => {
expect(actions.initializeEngine).toHaveBeenCalled();
});

it('clears engine on unmount', () => {
unmountHandler();
expect(actions.clearEngine).toHaveBeenCalled();
});
});

it('redirects to engines list and flashes an error if the engine param was not found', () => {
(useParams as jest.Mock).mockReturnValue({ engineName: '404-engine' });
setMockValues({ ...values, engineNotFound: true });
const wrapper = shallow(<EngineRouter />);

expect(wrapper.find(Redirect).prop('to')).toEqual('/engines');
expect(setQueuedErrorMessage).toHaveBeenCalledWith(
"No engine with name '404-engine' could be found."
);
});

it('does not render if async data is still loading', () => {
setMockValues({ ...values, dataLoading: true });
const wrapper = shallow(<EngineRouter />);
expect(wrapper.isEmptyRender()).toBe(true);
});

it('renders a default engine overview', () => {
setMockValues({ myRole: {} });
const wrapper = shallow(<EngineRouter />);

expect(wrapper.find(Switch)).toHaveLength(1);
Expand All @@ -32,106 +82,153 @@ describe('EngineRouter', () => {
});

describe('EngineNav', () => {
const values = { myRole: {}, engineName: 'some-engine', dataLoading: false, engine: {} };

beforeEach(() => {
(useParams as jest.Mock).mockReturnValue({ engineName: 'some-engine' });
setMockValues(values);
});

it('does not render if async data is still loading', () => {
setMockValues({ ...values, dataLoading: true });
const wrapper = shallow(<EngineNav />);
expect(wrapper.isEmptyRender()).toBe(true);
});

it('does not render without an engine name', () => {
setMockValues({ myRole: {} });
(useParams as jest.Mock).mockReturnValue({ engineName: '' });
setMockValues({ ...values, engineName: '' });
const wrapper = shallow(<EngineNav />);
expect(wrapper.isEmptyRender()).toBe(true);
});

it('renders an engine label', () => {
setMockValues({ myRole: {} });
const wrapper = mount(<EngineNav />);
it('renders an engine label and badges', () => {
setMockValues({ ...values, isSampleEngine: false, isMetaEngine: false });
const wrapper = shallow(<EngineNav />);
const label = wrapper.find('[data-test-subj="EngineLabel"]').find('.eui-textTruncate');

expect(label.text()).toEqual('SOME-ENGINE');
expect(wrapper.find(EuiBadge)).toHaveLength(0);

const label = wrapper.find('[data-test-subj="EngineLabel"]').last();
expect(label.text()).toEqual(expect.stringContaining('SOME-ENGINE'));
setMockValues({ ...values, isSampleEngine: true });
wrapper.setProps({}); // Re-render
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a PR coming immediately after this one that creates a new rerender(wrapper); test helper so we don't need all these clarification comments.

expect(wrapper.find(EuiBadge).prop('children')).toEqual('SAMPLE ENGINE');

// TODO: Test sample & meta engine conditional rendering
expect(label.find(EuiBadge).text()).toEqual('SAMPLE ENGINE');
setMockValues({ ...values, isMetaEngine: true });
wrapper.setProps({}); // Re-render
expect(wrapper.find(EuiBadge).prop('children')).toEqual('META ENGINE');
});

it('renders a default engine overview link', () => {
setMockValues({ myRole: {} });
const wrapper = shallow(<EngineNav />);
expect(wrapper.find('[data-test-subj="EngineOverviewLink"]')).toHaveLength(1);
});

it('renders an analytics link', () => {
setMockValues({ myRole: { canViewEngineAnalytics: true } });
setMockValues({ ...values, myRole: { canViewEngineAnalytics: true } });
const wrapper = shallow(<EngineNav />);
expect(wrapper.find('[data-test-subj="EngineAnalyticsLink"]')).toHaveLength(1);
});

it('renders a documents link', () => {
setMockValues({ myRole: { canViewEngineDocuments: true } });
setMockValues({ ...values, myRole: { canViewEngineDocuments: true } });
const wrapper = shallow(<EngineNav />);
expect(wrapper.find('[data-test-subj="EngineDocumentsLink"]')).toHaveLength(1);
});

it('renders a schema link', () => {
setMockValues({ myRole: { canViewEngineSchema: true } });
setMockValues({ ...values, myRole: { canViewEngineSchema: true } });
const wrapper = shallow(<EngineNav />);
expect(wrapper.find('[data-test-subj="EngineSchemaLink"]')).toHaveLength(1);
});

it('renders schema nav icons', () => {
const myRole = { canViewEngineSchema: true };
const wrapper = shallow(<EngineNav />);

setMockValues({ ...values, myRole, hasUnconfirmedSchemaFields: true });
JasonStoltz marked this conversation as resolved.
Show resolved Hide resolved
wrapper.setProps({}); // Re-render
expect(wrapper.find('[data-test-subj="EngineNavSchemaUnconfirmedFields"]')).toHaveLength(1);

// TODO: Schema warning icon
setMockValues({ ...values, myRole, hasSchemaConflicts: true });
wrapper.setProps({}); // Re-render
expect(wrapper.find('[data-test-subj="EngineNavSchemaConflicts"]')).toHaveLength(1);
});

// TODO: Unskip when EngineLogic is migrated
it.skip('renders a crawler link', () => {
setMockValues({ myRole: { canViewEngineCrawler: true } });
it('renders a crawler link', () => {
const myRole = { canViewEngineCrawler: true };
setMockValues({ ...values, myRole });
const wrapper = shallow(<EngineNav />);
expect(wrapper.find('[data-test-subj="EngineCrawlerLink"]')).toHaveLength(1);

// TODO: Test that the crawler link does NOT show up for meta/sample engines
// Does not render for meta engines
JasonStoltz marked this conversation as resolved.
Show resolved Hide resolved
setMockValues({ ...values, myRole, isMetaEngine: true });
wrapper.setProps({}); // Re-render
expect(wrapper.find('[data-test-subj="EngineCrawlerLink"]')).toHaveLength(0);

// Does not render for sample engine
setMockValues({ ...values, myRole, isSampleEngine: true });
wrapper.setProps({}); // Re-render
expect(wrapper.find('[data-test-subj="EngineCrawlerLink"]')).toHaveLength(0);
});

// TODO: Unskip when EngineLogic is migrated
it.skip('renders a meta engine source engines link', () => {
setMockValues({ myRole: { canViewMetaEngineSourceEngines: true } });
it('renders a meta engine source engines link', () => {
const myRole = { canViewMetaEngineSourceEngines: true };
setMockValues({ ...values, myRole, isMetaEngine: true });
const wrapper = shallow(<EngineNav />);
expect(wrapper.find('[data-test-subj="MetaEngineEnginesLink"]')).toHaveLength(1);

// TODO: Test that the crawler link does NOT show up for non-meta engines
// Does not render if engine is not a meta-engine
JasonStoltz marked this conversation as resolved.
Show resolved Hide resolved
setMockValues({ ...values, myRole, isMetaEngine: false });
wrapper.setProps({}); // Re-render
expect(wrapper.find('[data-test-subj="MetaEngineEnginesLink"]')).toHaveLength(0);
});

it('renders a relevance tuning link', () => {
setMockValues({ myRole: { canManageEngineRelevanceTuning: true } });
setMockValues({ ...values, myRole: { canManageEngineRelevanceTuning: true } });
const wrapper = shallow(<EngineNav />);
expect(wrapper.find('[data-test-subj="EngineRelevanceTuningLink"]')).toHaveLength(1);
});

it('renders relevance tuning nav icons', () => {
setMockValues({
...values,
myRole: { canManageEngineRelevanceTuning: true },
engine: {
unsearchedUnconfirmedFields: true,
invalidBoosts: true,
},
});
const wrapper = shallow(<EngineNav />);

// TODO: Boost error icon
expect(wrapper.find('[data-test-subj="EngineNavRelevanceTuningInvalidBoosts"]')).toHaveLength(1); // prettier-ignore
JasonStoltz marked this conversation as resolved.
Show resolved Hide resolved
expect(wrapper.find('[data-test-subj="EngineNavRelevanceTuningUnsearchedFields"]')).toHaveLength(1); // prettier-ignore
});

it('renders a synonyms link', () => {
setMockValues({ myRole: { canManageEngineSynonyms: true } });
setMockValues({ ...values, myRole: { canManageEngineSynonyms: true } });
const wrapper = shallow(<EngineNav />);
expect(wrapper.find('[data-test-subj="EngineSynonymsLink"]')).toHaveLength(1);
});

it('renders a curations link', () => {
setMockValues({ myRole: { canManageEngineCurations: true } });
setMockValues({ ...values, myRole: { canManageEngineCurations: true } });
const wrapper = shallow(<EngineNav />);
expect(wrapper.find('[data-test-subj="EngineCurationsLink"]')).toHaveLength(1);
});

it('renders a results settings link', () => {
setMockValues({ myRole: { canManageEngineResultSettings: true } });
setMockValues({ ...values, myRole: { canManageEngineResultSettings: true } });
const wrapper = shallow(<EngineNav />);
expect(wrapper.find('[data-test-subj="EngineResultSettingsLink"]')).toHaveLength(1);
});

it('renders a Search UI link', () => {
setMockValues({ myRole: { canManageEngineSearchUi: true } });
setMockValues({ ...values, myRole: { canManageEngineSearchUi: true } });
const wrapper = shallow(<EngineNav />);
expect(wrapper.find('[data-test-subj="EngineSearchUILink"]')).toHaveLength(1);
});

it('renders an API logs link', () => {
setMockValues({ myRole: { canViewEngineApiLogs: true } });
setMockValues({ ...values, myRole: { canViewEngineApiLogs: true } });
const wrapper = shallow(<EngineNav />);
expect(wrapper.find('[data-test-subj="EngineAPILogsLink"]')).toHaveLength(1);
});
Expand Down
Loading