Skip to content

Commit

Permalink
[Enterprise Search] Update WS Overview logic to use new config data (#…
Browse files Browse the repository at this point in the history
…77122)

* Update AppLogic to use new data structure

* Update components to use AppLogic

* Remove unused props from logic file

* Fix failing tests

* Add tests to get 100% converage

The test added for app_logic will never happen but hey, 100 is 100.

Also added test to recent_activity for 100% coverage

* Use non-null assertion operator in logic file

TIL this is a thing

* Remove test for undefined
  • Loading branch information
scottybollinger committed Sep 10, 2020
1 parent 1d20c55 commit dc81836
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 90 deletions.
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,9 +48,7 @@ describe('AppLogic', () => {
it('sets values based on passed props', () => {
AppLogic.actions.initializeAppData(DEFAULT_INITIAL_APP_DATA);

expect(AppLogic.values).toEqual({
hasInitialized: true,
});
expect(AppLogic.values).toEqual(expectedLogicValues);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,26 @@
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;
}

export const AppLogic = kea<MakeLogicType<IAppValues, IAppActions>>({
actions: {
initializeAppData: ({ workplaceSearch }) => workplaceSearch,
initializeAppData: ({ workplaceSearch, isFederatedAuth }) => ({
workplaceSearch,
isFederatedAuth,
}),
},
reducers: {
hasInitialized: [
Expand All @@ -27,5 +35,23 @@ export const AppLogic = kea<MakeLogicType<IAppValues, IAppActions>>({
initializeAppData: () => true,
},
],
isFederatedAuth: [
true,
{
initializeAppData: (_, { isFederatedAuth }) => !!isFederatedAuth,
},
],
organization: [
{} as IOrganization,
{
initializeAppData: (_, { workplaceSearch }) => workplaceSearch!.organization,
},
],
account: [
{} as IAccount,
{
initializeAppData: (_, { workplaceSearch }) => workplaceSearch!.account,
},
],
},
});
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 },
} = 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,18 @@
import { kea, MakeLogicType } from 'kea';
import { HttpLogic } from '../../../shared/http';

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

import { IFeedActivity } from './recent_activity';

export interface IOverviewServerData {
hasUsers: boolean;
hasOrgSources: boolean;
canCreateContentSources: boolean;
canCreateInvitations: boolean;
isOldAccount: boolean;
sourcesCount: number;
pendingInvitationsCount: number;
accountsCount: number;
personalSourcesCount: number;
activityFeed: IFeedActivity[];
organization: IOrganization;
isFederatedAuth: boolean;
fpAccount: IAccount;
}

export interface IOverviewActions {
Expand All @@ -42,30 +36,6 @@ export const OverviewLogic = kea<MakeLogicType<IOverviewValues, IOverviewActions
initializeOverview: () => null,
},
reducers: {
organization: [
{} as IOrganization,
{
setServerData: (_, { organization }) => organization,
},
],
isFederatedAuth: [
true,
{
setServerData: (_, { isFederatedAuth }) => isFederatedAuth,
},
],
fpAccount: [
{} as IAccount,
{
setServerData: (_, { fpAccount }) => fpAccount,
},
],
canCreateInvitations: [
false,
{
setServerData: (_, { canCreateInvitations }) => canCreateInvitations,
},
],
hasUsers: [
false,
{
Expand Down
Loading

0 comments on commit dc81836

Please sign in to comment.