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

[Enterprise Search] Update WS Overview logic to use new config data #77122

Merged
merged 7 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -16,7 +16,28 @@ describe('AppLogic', () => {
});

const DEFAULT_VALUES = {
account: {},
hasInitialized: false,
isFederatedAuth: true,
organization: {},
};

const expectedLogicValues = {
account: {
canCreateInvitations: true,
canCreatePersonalSources: true,
groups: ['Default', 'Cats'],
id: 'some-id-string',
isAdmin: true,
isCurated: false,
viewedOnboardingPage: true,
},
hasInitialized: true,
isFederatedAuth: false,
organization: {
defaultOrgName: 'My Organization',
name: 'ACME Donuts',
},
};

it('has expected default values', () => {
Expand All @@ -27,8 +48,16 @@ describe('AppLogic', () => {
it('sets values based on passed props', () => {
AppLogic.actions.initializeAppData(DEFAULT_INITIAL_APP_DATA);

expect(AppLogic.values).toEqual(expectedLogicValues);
});

it('handles case where workplaceSearch undefined', () => {
AppLogic.actions.initializeAppData({ isFederatedAuth: false });

expect(AppLogic.values).toEqual({
hasInitialized: true,
...expectedLogicValues,
organization: {},
account: {},
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,29 @@
import { kea, MakeLogicType } from 'kea';

import { IInitialAppData } from '../../../common/types';
import { IWorkplaceSearchInitialData } from '../../../common/types/workplace_search';
import {
IOrganization,
IWorkplaceSearchInitialData,
IAccount,
} from '../../../common/types/workplace_search';

export interface IAppValues extends IWorkplaceSearchInitialData {
hasInitialized: boolean;
isFederatedAuth: boolean;
}
export interface IAppActions {
initializeAppData(props: IInitialAppData): void;
initializeAppData(props: IInitialAppData): IInitialAppData;
}

const emptyAccount = {} as IAccount;
const emptyOrg = {} as IOrganization;

export const AppLogic = kea<MakeLogicType<IAppValues, IAppActions>>({
actions: {
initializeAppData: ({ workplaceSearch }) => workplaceSearch,
initializeAppData: ({ workplaceSearch, isFederatedAuth }) => ({
workplaceSearch,
isFederatedAuth,
}),
},
reducers: {
hasInitialized: [
Expand All @@ -27,5 +38,23 @@ export const AppLogic = kea<MakeLogicType<IAppValues, IAppActions>>({
initializeAppData: () => true,
},
],
isFederatedAuth: [
true,
{
initializeAppData: (_, { isFederatedAuth }) => !!isFederatedAuth,
},
],
organization: [
emptyOrg,
{
initializeAppData: (_, { workplaceSearch }) => workplaceSearch?.organization || emptyOrg,
yakhinvadim marked this conversation as resolved.
Show resolved Hide resolved
},
],
account: [
emptyAccount,
{
initializeAppData: (_, { workplaceSearch }) => workplaceSearch?.account || emptyAccount,
},
],
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
* you may not use this file except in compliance with the Elastic License.
*/

export { setMockValues, mockValues, mockActions } from './overview_logic.mock';
export { setMockValues, mockOverviewValues, mockActions } from './overview_logic.mock';
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
*/

import { IOverviewValues } from '../overview_logic';
import { IAccount, IOrganization } from '../../../types';

export const mockValues = {
import { DEFAULT_INITIAL_APP_DATA } from '../../../../../../common/__mocks__';

const { workplaceSearch: mockAppValues } = DEFAULT_INITIAL_APP_DATA;

export const mockOverviewValues = {
accountsCount: 0,
activityFeed: [],
canCreateContentSources: false,
canCreateInvitations: false,
fpAccount: {} as IAccount,
hasOrgSources: false,
hasUsers: false,
isFederatedAuth: true,
isOldAccount: false,
organization: {} as IOrganization,
pendingInvitationsCount: 0,
personalSourcesCount: 0,
sourcesCount: 0,
Expand All @@ -28,6 +27,8 @@ export const mockActions = {
initializeOverview: jest.fn(() => ({})),
};

const mockValues = { ...mockOverviewValues, ...mockAppValues, isFederatedAuth: true };

jest.mock('kea', () => ({
...(jest.requireActual('kea') as object),
useActions: jest.fn(() => ({ ...mockActions })),
Expand All @@ -37,8 +38,5 @@ jest.mock('kea', () => ({
import { useValues } from 'kea';

export const setMockValues = (values: object) => {
(useValues as jest.Mock).mockImplementationOnce(() => ({
...mockValues,
...values,
}));
(useValues as jest.Mock).mockImplementation(() => ({ ...mockValues, ...values }));
};
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const account = {
canCreatePersonalSources: true,
groups: [],
isCurated: false,
canCreateInvitations: true,
};

describe('OnboardingSteps', () => {
Expand Down Expand Up @@ -60,9 +61,8 @@ describe('OnboardingSteps', () => {
describe('Users & Invitations', () => {
it('renders 0 users when not on federated auth', () => {
setMockValues({
canCreateInvitations: true,
isFederatedAuth: false,
fpAccount: account,
account,
accountsCount: 0,
hasUsers: false,
});
Expand All @@ -78,7 +78,7 @@ describe('OnboardingSteps', () => {
it('renders completed users state', () => {
setMockValues({
isFederatedAuth: false,
fpAccount: account,
account,
accountsCount: 1,
hasUsers: true,
});
Expand All @@ -90,14 +90,26 @@ describe('OnboardingSteps', () => {
});

it('disables link when the user cannot create invitations', () => {
setMockValues({ isFederatedAuth: false, canCreateInvitations: false });
setMockValues({
isFederatedAuth: false,
account: {
...account,
canCreateInvitations: false,
},
});
const wrapper = shallow(<OnboardingSteps />);
expect(wrapper.find(OnboardingCard).last().prop('actionPath')).toBe(undefined);
});
});

describe('Org Name', () => {
it('renders button to change name', () => {
setMockValues({
organization: {
name: 'foo',
defaultOrgName: 'foo',
},
});
const wrapper = shallow(<OnboardingSteps />);

const button = wrapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { ORG_SOURCES_PATH, USERS_PATH, ORG_SETTINGS_PATH } from '../../routes';

import { ContentSection } from '../../components/shared/content_section';

import { AppLogic } from '../../app_logic';
import { OverviewLogic } from './overview_logic';

import { OnboardingCard } from './onboarding_card';
Expand Down Expand Up @@ -58,16 +59,18 @@ const ONBOARDING_USERS_CARD_DESCRIPTION = i18n.translate(
);

export const OnboardingSteps: React.FC = () => {
const {
isFederatedAuth,
organization: { name, defaultOrgName },
account: { isCurated, canCreateInvitations },
Copy link
Contributor

@yakhinvadim yakhinvadim Sep 10, 2020

Choose a reason for hiding this comment

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

I know this is a tech update PR, so it's not a blocker in any way.
@scottybollinger Do you know if we still need these values or are they legacy and we can remove them (maybe in future PRs)?

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 don't know for sure but I am taking a note to find out. Thanks!

} = useValues(AppLogic);

const {
hasUsers,
hasOrgSources,
canCreateContentSources,
canCreateInvitations,
accountsCount,
sourcesCount,
fpAccount: { isCurated },
organization: { name, defaultOrgName },
isFederatedAuth,
} = useValues(OverviewLogic);

const accountsPath =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@ import { i18n } from '@kbn/i18n';
import { ContentSection } from '../../components/shared/content_section';
import { ORG_SOURCES_PATH, USERS_PATH } from '../../routes';

import { AppLogic } from '../../app_logic';
import { OverviewLogic } from './overview_logic';

import { StatisticCard } from './statistic_card';

export const OrganizationStats: React.FC = () => {
const {
sourcesCount,
pendingInvitationsCount,
accountsCount,
personalSourcesCount,
isFederatedAuth,
} = useValues(OverviewLogic);
const { isFederatedAuth } = useValues(AppLogic);

const { sourcesCount, pendingInvitationsCount, accountsCount, personalSourcesCount } = useValues(
OverviewLogic
);

return (
<ContentSection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useActions, useValues } from 'kea';
import { SetWorkplaceSearchChrome as SetPageChrome } from '../../../shared/kibana_chrome';
import { SendWorkplaceSearchTelemetry as SendTelemetry } from '../../../shared/telemetry';

import { AppLogic } from '../../app_logic';
import { OverviewLogic } from './overview_logic';

import { Loading } from '../../components/shared/loading';
Expand Down Expand Up @@ -44,15 +45,12 @@ const HEADER_DESCRIPTION = i18n.translate(
);

export const Overview: React.FC = () => {
const { initializeOverview } = useActions(OverviewLogic);

const {
dataLoading,
hasUsers,
hasOrgSources,
isOldAccount,
organization: { name: orgName, defaultOrgName },
} = useValues(OverviewLogic);
} = useValues(AppLogic);

const { initializeOverview } = useActions(OverviewLogic);
const { dataLoading, hasUsers, hasOrgSources, isOldAccount } = useValues(OverviewLogic);

useEffect(() => {
initializeOverview();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { resetContext } from 'kea';
jest.mock('../../../shared/http', () => ({ HttpLogic: { values: { http: { get: jest.fn() } } } }));
import { HttpLogic } from '../../../shared/http';

import { mockValues } from './__mocks__';
import { mockOverviewValues } from './__mocks__';
import { OverviewLogic } from './overview_logic';

describe('OverviewLogic', () => {
Expand All @@ -20,32 +20,19 @@ describe('OverviewLogic', () => {
});

it('has expected default values', () => {
expect(OverviewLogic.values).toEqual(mockValues);
expect(OverviewLogic.values).toEqual(mockOverviewValues);
});

describe('setServerData', () => {
const feed = [{ foo: 'bar' }] as any;
const account = {
id: '1243',
groups: ['Default'],
isAdmin: true,
isCurated: false,
canCreatePersonalSources: true,
viewedOnboardingPage: false,
};
const org = { name: 'ACME', defaultOrgName: 'Org' };

const data = {
accountsCount: 1,
activityFeed: feed,
canCreateContentSources: true,
canCreateInvitations: true,
fpAccount: account,
hasOrgSources: true,
hasUsers: true,
isFederatedAuth: false,
isOldAccount: true,
organization: org,
pendingInvitationsCount: 1,
personalSourcesCount: 1,
sourcesCount: 1,
Expand All @@ -60,10 +47,6 @@ describe('OverviewLogic', () => {
});

it('will set server values', () => {
expect(OverviewLogic.values.organization).toEqual(org);
expect(OverviewLogic.values.isFederatedAuth).toEqual(false);
expect(OverviewLogic.values.fpAccount).toEqual(account);
expect(OverviewLogic.values.canCreateInvitations).toEqual(true);
expect(OverviewLogic.values.hasUsers).toEqual(true);
expect(OverviewLogic.values.hasOrgSources).toEqual(true);
expect(OverviewLogic.values.canCreateContentSources).toEqual(true);
Expand Down
Loading