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

[7.x] [App Search] Load curation settings at root /curations/ path (#115690) #115803

Merged
merged 1 commit into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -107,17 +107,6 @@ describe('CurationsLogic', () => {

describe('listeners', () => {
describe('loadCurations', () => {
it('should set dataLoading state', () => {
mount({ dataLoading: false });

CurationsLogic.actions.loadCurations();

expect(CurationsLogic.values).toEqual({
...DEFAULT_VALUES,
dataLoading: true,
});
});

it('should make an API call and set curations & meta state', async () => {
http.get.mockReturnValueOnce(Promise.resolve(MOCK_CURATIONS_RESPONSE));
mount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export const CurationsLogic = kea<MakeLogicType<CurationsValues, CurationsAction
dataLoading: [
true,
{
loadCurations: () => true,
onCurationsLoad: () => false,
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,120 @@
* 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 enabled', () => {
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,20 +5,53 @@
* 2.0.
*/

import React from 'react';
import React, { useEffect } 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 @@ -126,14 +126,14 @@ describe('Curations', () => {

describe('loading state', () => {
it('renders a full-page loading state on initial page load', () => {
setMockValues({ ...values, dataLoading: true, curations: [] });
setMockValues({ ...values, dataLoading: true });
const wrapper = shallow(<Curations />);

expect(wrapper.prop('isLoading')).toEqual(true);
});

it('does not re-render a full-page loading state after initial page load (uses component-level loading state instead)', () => {
setMockValues({ ...values, dataLoading: true, curations: [{}] });
it('does not re-render a full-page loading state when data is loaded', () => {
setMockValues({ ...values, dataLoading: false });
const wrapper = shallow(<Curations />);

expect(wrapper.prop('isLoading')).toEqual(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import { getCurationsBreadcrumbs } from '../utils';

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

export const Curations: React.FC = () => {
const { dataLoading, curations, meta, selectedPageTab } = useValues(CurationsLogic);
const { dataLoading: curationsDataLoading, meta, selectedPageTab } = useValues(CurationsLogic);
const { loadCurations, onSelectPageTab } = useActions(CurationsLogic);
const { hasPlatinumLicense } = useValues(LicensingLogic);
const { dataLoading: curationsSettingsDataLoading } = useValues(CurationsSettingsLogic);

const OVERVIEW_TAB = {
label: i18n.translate(
Expand Down Expand Up @@ -92,7 +93,7 @@ export const Curations: React.FC = () => {
],
tabs: pageTabs,
}}
isLoading={dataLoading && !curations.length}
isLoading={curationsSettingsDataLoading || curationsDataLoading}
>
{selectedPageTab === 'overview' && <CurationsOverview />}
{selectedPageTab === 'history' && <CurationsHistory />}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,40 @@ import { SuggestionsTable } from '../components/suggestions_table';

import { CurationsOverview } from './curations_overview';

const MOCK_VALUES = {
// CurationsSettingsLogic
curationsSettings: {
enabled: true,
},
// CurationsLogic
curations: [
{
id: 'cur-id-1',
},
{
id: 'cur-id-2',
},
],
// LicensingLogics
hasPlatinumLicense: true,
};

describe('CurationsOverview', () => {
beforeEach(() => {
jest.clearAllMocks();
setMockValues(MOCK_VALUES);
});

it('renders an empty message when there are no curations', () => {
setMockValues({ curations: [] });
setMockValues({ ...MOCK_VALUES, curations: [] });
const wrapper = shallow(<CurationsOverview />);

expect(wrapper.find(EmptyState).exists()).toBe(true);
});

it('renders a curations table when there are curations present', () => {
setMockValues({
...MOCK_VALUES,
curations: [
{
id: 'cur-id-1',
Expand All @@ -47,15 +67,36 @@ describe('CurationsOverview', () => {
expect(wrapper.find(CurationsTable)).toHaveLength(1);
});

it('renders a suggestions table when the user has a platinum license', () => {
setMockValues({ curations: [], hasPlatinumLicense: true });
it('renders a suggestions table when the user has a platinum license and curations suggestions enabled', () => {
setMockValues({
...MOCK_VALUES,
hasPlatinumLicense: true,
curationsSettings: {
enabled: true,
},
});
const wrapper = shallow(<CurationsOverview />);

expect(wrapper.find(SuggestionsTable).exists()).toBe(true);
});

it('doesn\t render a suggestions table when the user has no platinum license', () => {
setMockValues({ curations: [], hasPlatinumLicense: false });
setMockValues({
...MOCK_VALUES,
hasPlatinumLicense: false,
});
const wrapper = shallow(<CurationsOverview />);

expect(wrapper.find(SuggestionsTable).exists()).toBe(false);
});

it('doesn\t render a suggestions table when the user has disabled suggestions', () => {
setMockValues({
...MOCK_VALUES,
curationsSettings: {
enabled: false,
},
});
const wrapper = shallow(<CurationsOverview />);

expect(wrapper.find(SuggestionsTable).exists()).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ import { CurationsTable, EmptyState } from '../components';
import { SuggestionsTable } from '../components/suggestions_table';
import { CurationsLogic } from '../curations_logic';

import { CurationsSettingsLogic } from './curations_settings';

export const CurationsOverview: React.FC = () => {
const { curations } = useValues(CurationsLogic);
const { hasPlatinumLicense } = useValues(LicensingLogic);

const shouldShowSuggestions = hasPlatinumLicense;
const {
curationsSettings: { enabled },
} = useValues(CurationsSettingsLogic);

const shouldShowSuggestions = enabled && hasPlatinumLicense;

return (
<>
Expand Down
Loading