From ee66836d08235de538bd0dc3ae2277fb7616d3f4 Mon Sep 17 00:00:00 2001 From: Scotty Bollinger Date: Wed, 26 Aug 2020 18:16:48 -0500 Subject: [PATCH] [Enterprise Search] Adds app logic file to Workplace Search (#76009) (#76040) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add new Workplace Search initial data properties * Add app logic * Refactor index to match App Search Adds the easier-to-read ComponentConfigured and ComponentUnconfigured FCs with a ternary in the root compoenent * Remove ‘Logic’ from interface names * Extract initial data from WS into interface This allows for breaking apart the app-specific data and also having an interface to extend in the app_logic file * Destructuring FTW --- .../common/__mocks__/initial_app_data.ts | 2 + .../enterprise_search/common/types/index.ts | 7 +- .../common/types/workplace_search.ts | 7 ++ .../workplace_search/app_logic.test.ts | 35 +++++++++ .../workplace_search/app_logic.ts | 32 ++++++++ .../workplace_search/index.test.tsx | 77 ++++++++++++++----- .../applications/workplace_search/index.tsx | 38 +++++---- .../lib/enterprise_search_config_api.test.ts | 4 + .../lib/enterprise_search_config_api.ts | 2 + 9 files changed, 165 insertions(+), 39 deletions(-) create mode 100644 x-pack/plugins/enterprise_search/public/applications/workplace_search/app_logic.test.ts create mode 100644 x-pack/plugins/enterprise_search/public/applications/workplace_search/app_logic.ts diff --git a/x-pack/plugins/enterprise_search/common/__mocks__/initial_app_data.ts b/x-pack/plugins/enterprise_search/common/__mocks__/initial_app_data.ts index 79e1efc425b4e..2d31be65dd30e 100644 --- a/x-pack/plugins/enterprise_search/common/__mocks__/initial_app_data.ts +++ b/x-pack/plugins/enterprise_search/common/__mocks__/initial_app_data.ts @@ -29,6 +29,8 @@ export const DEFAULT_INITIAL_APP_DATA = { }, }, workplaceSearch: { + canCreateInvitations: true, + isFederatedAuth: false, organization: { name: 'ACME Donuts', defaultOrgName: 'My Organization', diff --git a/x-pack/plugins/enterprise_search/common/types/index.ts b/x-pack/plugins/enterprise_search/common/types/index.ts index 52e468b741a07..008afb234a376 100644 --- a/x-pack/plugins/enterprise_search/common/types/index.ts +++ b/x-pack/plugins/enterprise_search/common/types/index.ts @@ -5,17 +5,14 @@ */ import { IAccount as IAppSearchAccount } from './app_search'; -import { IAccount as IWorkplaceSearchAccount, IOrganization } from './workplace_search'; +import { IWorkplaceSearchInitialData } from './workplace_search'; export interface IInitialAppData { readOnlyMode?: boolean; ilmEnabled?: boolean; configuredLimits?: IConfiguredLimits; appSearch?: IAppSearchAccount; - workplaceSearch?: { - organization: IOrganization; - fpAccount: IWorkplaceSearchAccount; - }; + workplaceSearch?: IWorkplaceSearchInitialData; } export interface IConfiguredLimits { diff --git a/x-pack/plugins/enterprise_search/common/types/workplace_search.ts b/x-pack/plugins/enterprise_search/common/types/workplace_search.ts index fd8fa6daf81ac..bc4e39b0788d9 100644 --- a/x-pack/plugins/enterprise_search/common/types/workplace_search.ts +++ b/x-pack/plugins/enterprise_search/common/types/workplace_search.ts @@ -17,3 +17,10 @@ export interface IOrganization { name: string; defaultOrgName: string; } + +export interface IWorkplaceSearchInitialData { + canCreateInvitations: boolean; + isFederatedAuth: boolean; + organization: IOrganization; + fpAccount: IAccount; +} diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/app_logic.test.ts b/x-pack/plugins/enterprise_search/public/applications/workplace_search/app_logic.test.ts new file mode 100644 index 0000000000000..bc31b7df5d971 --- /dev/null +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/app_logic.test.ts @@ -0,0 +1,35 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { resetContext } from 'kea'; + +import { DEFAULT_INITIAL_APP_DATA } from '../../../common/__mocks__'; +import { AppLogic } from './app_logic'; + +describe('AppLogic', () => { + beforeEach(() => { + resetContext({}); + AppLogic.mount(); + }); + + const DEFAULT_VALUES = { + hasInitialized: false, + }; + + it('has expected default values', () => { + expect(AppLogic.values).toEqual(DEFAULT_VALUES); + }); + + describe('initializeAppData()', () => { + it('sets values based on passed props', () => { + AppLogic.actions.initializeAppData(DEFAULT_INITIAL_APP_DATA); + + expect(AppLogic.values).toEqual({ + hasInitialized: true, + }); + }); + }); +}); diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/app_logic.ts b/x-pack/plugins/enterprise_search/public/applications/workplace_search/app_logic.ts new file mode 100644 index 0000000000000..b7116f02663c1 --- /dev/null +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/app_logic.ts @@ -0,0 +1,32 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { kea } from 'kea'; + +import { IInitialAppData } from '../../../common/types'; +import { IWorkplaceSearchInitialData } from '../../../common/types/workplace_search'; +import { IKeaLogic } from '../shared/types'; + +export interface IAppValues extends IWorkplaceSearchInitialData { + hasInitialized: boolean; +} +export interface IAppActions { + initializeAppData(props: IInitialAppData): void; +} + +export const AppLogic = kea({ + actions: (): IAppActions => ({ + initializeAppData: ({ workplaceSearch }) => workplaceSearch, + }), + reducers: () => ({ + hasInitialized: [ + false, + { + initializeAppData: () => true, + }, + ], + }), +}) as IKeaLogic; diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/index.test.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/index.test.tsx index a0d9352ee9f82..39280ad6f4be4 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/index.test.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/index.test.tsx @@ -10,39 +10,76 @@ import '../__mocks__/kea.mock'; import React, { useContext } from 'react'; import { Redirect } from 'react-router-dom'; import { shallow } from 'enzyme'; -import { useValues } from 'kea'; +import { useValues, useActions } from 'kea'; -import { Overview } from './views/overview'; +import { SetupGuide } from './views/setup_guide'; import { ErrorState } from './views/error_state'; +import { Overview } from './views/overview'; -import { WorkplaceSearch } from './'; +import { WorkplaceSearch, WorkplaceSearchUnconfigured, WorkplaceSearchConfigured } from './'; -describe('Workplace Search', () => { - it('redirects to Setup Guide when enterpriseSearchUrl is not set', () => { - (useContext as jest.Mock).mockImplementationOnce(() => ({ - config: { host: '' }, - })); +describe('WorkplaceSearch', () => { + it('renders WorkplaceSearchUnconfigured when config.host is not set', () => { + (useContext as jest.Mock).mockImplementationOnce(() => ({ config: { host: '' } })); const wrapper = shallow(); - expect(wrapper.find(Redirect)).toHaveLength(1); - expect(wrapper.find(Overview)).toHaveLength(0); + expect(wrapper.find(WorkplaceSearchUnconfigured)).toHaveLength(1); }); - it('renders the Overview when enterpriseSearchUrl is set', () => { - (useContext as jest.Mock).mockImplementationOnce(() => ({ - config: { host: 'https://foo.bar' }, - })); + it('renders WorkplaceSearchConfigured when config.host set', () => { + (useContext as jest.Mock).mockImplementationOnce(() => ({ config: { host: 'some.url' } })); const wrapper = shallow(); + expect(wrapper.find(WorkplaceSearchConfigured)).toHaveLength(1); + }); +}); + +describe('WorkplaceSearchUnconfigured', () => { + it('renders the Setup Guide and redirects to the Setup Guide', () => { + const wrapper = shallow(); + + expect(wrapper.find(SetupGuide)).toHaveLength(1); + expect(wrapper.find(Redirect)).toHaveLength(1); + }); +}); + +describe('WorkplaceSearchConfigured', () => { + beforeEach(() => { + // Mock resets + (useValues as jest.Mock).mockImplementation(() => ({})); + (useActions as jest.Mock).mockImplementation(() => ({ initializeAppData: () => {} })); + }); + + it('renders with layout', () => { + const wrapper = shallow(); + expect(wrapper.find(Overview)).toHaveLength(1); - expect(wrapper.find(Redirect)).toHaveLength(0); }); - it('renders ErrorState when the app cannot connect to Enterprise Search', () => { - (useValues as jest.Mock).mockImplementationOnce(() => ({ errorConnecting: true })); - const wrapper = shallow(); + it('initializes app data with passed props', () => { + const initializeAppData = jest.fn(); + (useActions as jest.Mock).mockImplementation(() => ({ initializeAppData })); + + shallow(); + + expect(initializeAppData).toHaveBeenCalledWith({ readOnlyMode: true }); + }); + + it('does not re-initialize app data', () => { + const initializeAppData = jest.fn(); + (useActions as jest.Mock).mockImplementation(() => ({ initializeAppData })); + (useValues as jest.Mock).mockImplementation(() => ({ hasInitialized: true })); + + shallow(); + + expect(initializeAppData).not.toHaveBeenCalled(); + }); + + it('renders ErrorState', () => { + (useValues as jest.Mock).mockImplementation(() => ({ errorConnecting: true })); + + const wrapper = shallow(); - expect(wrapper.find(ErrorState).exists()).toBe(true); - expect(wrapper.find(Overview)).toHaveLength(0); + expect(wrapper.find(ErrorState)).toHaveLength(2); }); }); diff --git a/x-pack/plugins/enterprise_search/public/applications/workplace_search/index.tsx b/x-pack/plugins/enterprise_search/public/applications/workplace_search/index.tsx index 8582a003c6fa8..c0a51d5670a14 100644 --- a/x-pack/plugins/enterprise_search/public/applications/workplace_search/index.tsx +++ b/x-pack/plugins/enterprise_search/public/applications/workplace_search/index.tsx @@ -4,13 +4,14 @@ * you may not use this file except in compliance with the Elastic License. */ -import React, { useContext } from 'react'; +import React, { useContext, useEffect } from 'react'; import { Route, Redirect, Switch } from 'react-router-dom'; -import { useValues } from 'kea'; +import { useActions, useValues } from 'kea'; import { IInitialAppData } from '../../../common/types'; import { KibanaContext, IKibanaContext } from '../index'; import { HttpLogic, IHttpLogicValues } from '../shared/http'; +import { AppLogic, IAppActions, IAppValues } from './app_logic'; import { Layout } from '../shared/layout'; import { WorkplaceSearchNav } from './components/layout/nav'; @@ -20,21 +21,19 @@ import { SetupGuide } from './views/setup_guide'; import { ErrorState } from './views/error_state'; import { Overview } from './views/overview'; -export const WorkplaceSearch: React.FC = () => { +export const WorkplaceSearch: React.FC = (props) => { const { config } = useContext(KibanaContext) as IKibanaContext; + return !config.host ? : ; +}; + +export const WorkplaceSearchConfigured: React.FC = (props) => { + const { hasInitialized } = useValues(AppLogic) as IAppValues; + const { initializeAppData } = useActions(AppLogic) as IAppActions; const { errorConnecting } = useValues(HttpLogic) as IHttpLogicValues; - if (!config.host) - return ( - - - - - - - - - ); + useEffect(() => { + if (!hasInitialized) initializeAppData(props); + }, [hasInitialized]); return ( @@ -61,3 +60,14 @@ export const WorkplaceSearch: React.FC = () => { ); }; + +export const WorkplaceSearchUnconfigured: React.FC = () => ( + + + + + + + + +); diff --git a/x-pack/plugins/enterprise_search/server/lib/enterprise_search_config_api.test.ts b/x-pack/plugins/enterprise_search/server/lib/enterprise_search_config_api.test.ts index c26ada77f504f..323f79e63bc6f 100644 --- a/x-pack/plugins/enterprise_search/server/lib/enterprise_search_config_api.test.ts +++ b/x-pack/plugins/enterprise_search/server/lib/enterprise_search_config_api.test.ts @@ -47,6 +47,8 @@ describe('callEnterpriseSearchConfigAPI', () => { onboarding_complete: true, }, workplace_search: { + can_create_invitations: true, + is_federated_auth: false, organization: { name: 'ACME Donuts', default_org_name: 'My Organization', @@ -136,6 +138,8 @@ describe('callEnterpriseSearchConfigAPI', () => { }, }, workplaceSearch: { + canCreateInvitations: false, + isFederatedAuth: false, organization: { name: undefined, defaultOrgName: undefined, diff --git a/x-pack/plugins/enterprise_search/server/lib/enterprise_search_config_api.ts b/x-pack/plugins/enterprise_search/server/lib/enterprise_search_config_api.ts index 1dbec76806ba8..c9cbec15169d9 100644 --- a/x-pack/plugins/enterprise_search/server/lib/enterprise_search_config_api.ts +++ b/x-pack/plugins/enterprise_search/server/lib/enterprise_search_config_api.ts @@ -90,6 +90,8 @@ export const callEnterpriseSearchConfigAPI = async ({ }, }, workplaceSearch: { + canCreateInvitations: !!data?.settings?.workplace_search?.can_create_invitations, + isFederatedAuth: !!data?.settings?.workplace_search?.is_federated_auth, organization: { name: data?.settings?.workplace_search?.organization?.name, defaultOrgName: data?.settings?.workplace_search?.organization?.default_org_name,