Skip to content

Commit

Permalink
Revert "[App Search] Load curation settings at root /curations/ path (#…
Browse files Browse the repository at this point in the history
…115690)" (#116462) (#116497)

Co-authored-by: Byron Hulcher <[email protected]>
  • Loading branch information
kibanamachine and byronhulcher authored Oct 27, 2021
1 parent 43fe3c6 commit f14942a
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,120 +5,20 @@
* 2.0.
*/

import '../../../__mocks__/shallow_useeffect.mock';
import '../../../__mocks__/react_router';
import { setMockActions, setMockValues } from '../../../__mocks__/kea_logic';
import '../../__mocks__/engine_logic.mock';

import React from 'react';
import { Route, Switch } from 'react-router-dom';

import { shallow } from 'enzyme';

import { LogRetentionOptions } from '../log_retention';

import { CurationsRouter } from './';

const MOCK_VALUES = {
// CurationsSettingsLogic
dataLoading: false,
curationsSettings: {
enabled: true,
mode: 'automatic',
},
// LogRetentionLogic
logRetention: {
[LogRetentionOptions.Analytics]: {
enabled: true,
},
},
// LicensingLogic
hasPlatinumLicense: true,
};

const MOCK_ACTIONS = {
// CurationsSettingsLogic
loadCurationsSettings: jest.fn(),
onSkipLoadingCurationsSettings: jest.fn(),
// LogRetentionLogic
fetchLogRetention: jest.fn(),
};

describe('CurationsRouter', () => {
beforeEach(() => {
jest.clearAllMocks();
setMockActions(MOCK_ACTIONS);
});

it('renders', () => {
const wrapper = shallow(<CurationsRouter />);

expect(wrapper.find(Switch)).toHaveLength(1);
expect(wrapper.find(Route)).toHaveLength(4);
});

it('loads log retention settings', () => {
setMockValues(MOCK_VALUES);
shallow(<CurationsRouter />);

expect(MOCK_ACTIONS.fetchLogRetention).toHaveBeenCalled();
});

describe('when the user has no platinum license', () => {
beforeEach(() => {
setMockValues({
...MOCK_VALUES,
hasPlatinumLicense: false,
});
});

it('it does not fetch log retention', () => {
shallow(<CurationsRouter />);
expect(MOCK_ACTIONS.fetchLogRetention).toHaveBeenCalledTimes(0);
});
});

describe('loading curation settings based on log retention', () => {
it('loads curation settings when log retention is enabled', () => {
setMockValues({
...MOCK_VALUES,
logRetention: {
[LogRetentionOptions.Analytics]: {
enabled: true,
},
},
});

shallow(<CurationsRouter />);

expect(MOCK_ACTIONS.loadCurationsSettings).toHaveBeenCalledTimes(1);
});

it('skips loading curation settings when log retention is disabled', () => {
setMockValues({
...MOCK_VALUES,
logRetention: {
[LogRetentionOptions.Analytics]: {
enabled: false,
},
},
});

shallow(<CurationsRouter />);

expect(MOCK_ACTIONS.onSkipLoadingCurationsSettings).toHaveBeenCalledTimes(1);
});

it('takes no action if log retention has not yet been loaded', () => {
setMockValues({
...MOCK_VALUES,
logRetention: null,
});

shallow(<CurationsRouter />);

expect(MOCK_ACTIONS.loadCurationsSettings).toHaveBeenCalledTimes(0);
expect(MOCK_ACTIONS.onSkipLoadingCurationsSettings).toHaveBeenCalledTimes(0);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,20 @@
* 2.0.
*/

import React, { useEffect } from 'react';
import React from 'react';
import { Route, Switch } from 'react-router-dom';

import { useValues, useActions } from 'kea';

import { LicensingLogic } from '../../../shared/licensing';
import {
ENGINE_CURATIONS_PATH,
ENGINE_CURATIONS_NEW_PATH,
ENGINE_CURATION_PATH,
ENGINE_CURATION_SUGGESTION_PATH,
} from '../../routes';
import { LogRetentionLogic, LogRetentionOptions } from '../log_retention';

import { Curation } from './curation';
import { Curations, CurationCreation, CurationSuggestion } from './views';
import { CurationsSettingsLogic } from './views/curations_settings';

export const CurationsRouter: React.FC = () => {
// We need to loadCurationsSettings here so they are available across all views

const { hasPlatinumLicense } = useValues(LicensingLogic);

const { loadCurationsSettings, onSkipLoadingCurationsSettings } =
useActions(CurationsSettingsLogic);

const { logRetention } = useValues(LogRetentionLogic);
const { fetchLogRetention } = useActions(LogRetentionLogic);

const analyticsDisabled = !logRetention?.[LogRetentionOptions.Analytics].enabled;

useEffect(() => {
if (hasPlatinumLicense) {
fetchLogRetention();
}
}, [hasPlatinumLicense]);

useEffect(() => {
if (logRetention) {
if (!analyticsDisabled) {
loadCurationsSettings();
} else {
onSkipLoadingCurationsSettings();
}
}
}, [logRetention]);

return (
<Switch>
<Route exact path={ENGINE_CURATIONS_PATH}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,15 @@ import { getCurationsBreadcrumbs } from '../utils';

import { CurationsHistory } from './curations_history/curations_history';
import { CurationsOverview } from './curations_overview';
import { CurationsSettings, CurationsSettingsLogic } from './curations_settings';
import { CurationsSettings } from './curations_settings';

export const Curations: React.FC = () => {
const { dataLoading: curationsDataLoading, meta, selectedPageTab } = useValues(CurationsLogic);
const { dataLoading, meta, selectedPageTab } = useValues(CurationsLogic);
const { loadCurations, onSelectPageTab } = useActions(CurationsLogic);
const {
engine: { search_relevance_suggestions_active: searchRelevanceSuggestionsActive },
} = useValues(EngineLogic);

const { dataLoading: curationsSettingsDataLoading } = useValues(CurationsSettingsLogic);

const suggestionsEnabled = searchRelevanceSuggestionsActive;

const OVERVIEW_TAB = {
Expand Down Expand Up @@ -82,8 +80,6 @@ export const Curations: React.FC = () => {
loadCurations();
}, [meta.page.current]);

const isLoading = curationsSettingsDataLoading || curationsDataLoading;

return (
<AppSearchPageTemplate
pageChrome={getCurationsBreadcrumbs()}
Expand All @@ -98,9 +94,9 @@ export const Curations: React.FC = () => {
{CREATE_NEW_CURATION_TITLE}
</EuiButtonTo>,
],
tabs: isLoading ? undefined : pageTabs,
tabs: dataLoading ? undefined : pageTabs,
}}
isLoading={isLoading}
isLoading={dataLoading}
>
{selectedPageTab === 'overview' && <CurationsOverview />}
{selectedPageTab === 'history' && <CurationsHistory />}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { shallow, ShallowWrapper } from 'enzyme';

import { EuiButtonEmpty, EuiCallOut, EuiSwitch } from '@elastic/eui';

import { mountWithIntl } from '@kbn/test/jest';

import { Loading } from '../../../../../shared/loading';
import { EuiButtonTo } from '../../../../../shared/react_router_helpers';
import { DataPanel } from '../../../data_panel';
Expand Down Expand Up @@ -44,6 +46,8 @@ const MOCK_VALUES = {

const MOCK_ACTIONS = {
// CurationsSettingsLogic
loadCurationsSettings: jest.fn(),
onSkipLoadingCurationsSettings: jest.fn(),
toggleCurationsEnabled: jest.fn(),
toggleCurationsMode: jest.fn(),
// LogRetentionLogic
Expand All @@ -56,6 +60,14 @@ describe('CurationsSettings', () => {
setMockActions(MOCK_ACTIONS);
});

it('loads curations and log retention settings on load', () => {
setMockValues(MOCK_VALUES);
mountWithIntl(<CurationsSettings />);

expect(MOCK_ACTIONS.loadCurationsSettings).toHaveBeenCalled();
expect(MOCK_ACTIONS.fetchLogRetention).toHaveBeenCalled();
});

it('contains a switch to toggle curations settings', () => {
let wrapper: ShallowWrapper;

Expand Down Expand Up @@ -154,6 +166,50 @@ describe('CurationsSettings', () => {
expect(wrapper.is(Loading)).toBe(true);
});

describe('loading curation settings based on log retention', () => {
it('loads curation settings when log retention is enabled', () => {
setMockValues({
...MOCK_VALUES,
logRetention: {
[LogRetentionOptions.Analytics]: {
enabled: true,
},
},
});

shallow(<CurationsSettings />);

expect(MOCK_ACTIONS.loadCurationsSettings).toHaveBeenCalledTimes(1);
});

it('skips loading curation settings when log retention is enabled', () => {
setMockValues({
...MOCK_VALUES,
logRetention: {
[LogRetentionOptions.Analytics]: {
enabled: false,
},
},
});

shallow(<CurationsSettings />);

expect(MOCK_ACTIONS.onSkipLoadingCurationsSettings).toHaveBeenCalledTimes(1);
});

it('takes no action if log retention has not yet been loaded', () => {
setMockValues({
...MOCK_VALUES,
logRetention: null,
});

shallow(<CurationsSettings />);

expect(MOCK_ACTIONS.loadCurationsSettings).toHaveBeenCalledTimes(0);
expect(MOCK_ACTIONS.onSkipLoadingCurationsSettings).toHaveBeenCalledTimes(0);
});
});

describe('when the user has no platinum license', () => {
beforeEach(() => {
setMockValues({
Expand All @@ -162,6 +218,11 @@ describe('CurationsSettings', () => {
});
});

it('it does not fetch log retention', () => {
shallow(<CurationsSettings />);
expect(MOCK_ACTIONS.fetchLogRetention).toHaveBeenCalledTimes(0);
});

it('shows a CTA to upgrade your license when the user when the user', () => {
const wrapper = shallow(<CurationsSettings />);
expect(wrapper.is(DataPanel)).toBe(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React from 'react';
import React, { useEffect } from 'react';

import { useActions, useValues } from 'kea';

Expand Down Expand Up @@ -43,12 +43,34 @@ export const CurationsSettings: React.FC = () => {
curationsSettings: { enabled, mode },
dataLoading,
} = useValues(CurationsSettingsLogic);
const { toggleCurationsEnabled, toggleCurationsMode } = useActions(CurationsSettingsLogic);
const {
loadCurationsSettings,
onSkipLoadingCurationsSettings,
toggleCurationsEnabled,
toggleCurationsMode,
} = useActions(CurationsSettingsLogic);

const { isLogRetentionUpdating, logRetention } = useValues(LogRetentionLogic);
const { fetchLogRetention } = useActions(LogRetentionLogic);

const analyticsDisabled = !logRetention?.[LogRetentionOptions.Analytics].enabled;

useEffect(() => {
if (hasPlatinumLicense) {
fetchLogRetention();
}
}, [hasPlatinumLicense]);

useEffect(() => {
if (logRetention) {
if (!analyticsDisabled) {
loadCurationsSettings();
} else {
onSkipLoadingCurationsSettings();
}
}
}, [logRetention]);

if (!hasPlatinumLicense)
return (
<DataPanel
Expand Down

0 comments on commit f14942a

Please sign in to comment.