From 026588cbf1c58d41040c524b6708f9533275696f Mon Sep 17 00:00:00 2001 From: Peter Holmberg Date: Mon, 29 Oct 2018 13:46:12 +0100 Subject: [PATCH] test and some refactoring --- .../app/features/org/OrgDetailsPage.test.tsx | 45 +++++ public/app/features/org/OrgDetailsPage.tsx | 43 +--- .../app/features/org/OrgPreferences.test.tsx | 28 +++ public/app/features/org/OrgPreferences.tsx | 185 ++++++++++-------- public/app/features/org/OrgProfile.test.tsx | 21 ++ public/app/features/org/OrgProfile.tsx | 2 +- .../OrgDetailsPage.test.tsx.snap | 36 ++++ .../OrgPreferences.test.tsx.snap | 125 ++++++++++++ .../__snapshots__/OrgProfile.test.tsx.snap | 46 +++++ public/app/types/acl.ts | 1 + 10 files changed, 411 insertions(+), 121 deletions(-) create mode 100644 public/app/features/org/OrgDetailsPage.test.tsx create mode 100644 public/app/features/org/OrgPreferences.test.tsx create mode 100644 public/app/features/org/OrgProfile.test.tsx create mode 100644 public/app/features/org/__snapshots__/OrgDetailsPage.test.tsx.snap create mode 100644 public/app/features/org/__snapshots__/OrgPreferences.test.tsx.snap create mode 100644 public/app/features/org/__snapshots__/OrgProfile.test.tsx.snap diff --git a/public/app/features/org/OrgDetailsPage.test.tsx b/public/app/features/org/OrgDetailsPage.test.tsx new file mode 100644 index 00000000000..2eb45fa368a --- /dev/null +++ b/public/app/features/org/OrgDetailsPage.test.tsx @@ -0,0 +1,45 @@ +import React from 'react'; +import { shallow } from 'enzyme'; +import { OrgDetailsPage, Props } from './OrgDetailsPage'; +import { NavModel, Organization, OrganizationPreferences } from '../../types'; + +const setup = (propOverrides?: object) => { + const props: Props = { + preferences: {} as OrganizationPreferences, + organization: {} as Organization, + navModel: {} as NavModel, + loadOrganization: jest.fn(), + loadOrganizationPreferences: jest.fn(), + loadStarredDashboards: jest.fn(), + setOrganizationName: jest.fn(), + updateOrganization: jest.fn(), + }; + + Object.assign(props, propOverrides); + + return shallow(); +}; + +describe('Render', () => { + it('should render component', () => { + const wrapper = setup(); + + expect(wrapper).toMatchSnapshot(); + }); + + it('should render organization and preferences', () => { + const wrapper = setup({ + organization: { + name: 'Cool org', + id: 1, + }, + preferences: { + homeDashboardId: 1, + theme: 'Default', + timezone: 'Default', + }, + }); + + expect(wrapper).toMatchSnapshot(); + }); +}); diff --git a/public/app/features/org/OrgDetailsPage.tsx b/public/app/features/org/OrgDetailsPage.tsx index a96bda831db..fa065ba4f66 100644 --- a/public/app/features/org/OrgDetailsPage.tsx +++ b/public/app/features/org/OrgDetailsPage.tsx @@ -9,30 +9,21 @@ import { loadOrganization, loadOrganizationPreferences, setOrganizationName, - setOrganizationTheme, - setOrganizationHomeDashboard, - setOrganizationTimezone, updateOrganization, - updateOrganizationPreferences, } from './state/actions'; import { loadStarredDashboards } from '../dashboard/state/actions'; -import { DashboardAcl, NavModel, Organization, OrganizationPreferences, StoreState } from 'app/types'; +import { NavModel, Organization, OrganizationPreferences, StoreState } from 'app/types'; import { getNavModel } from '../../core/selectors/navModel'; export interface Props { navModel: NavModel; organization: Organization; preferences: OrganizationPreferences; - starredDashboards: DashboardAcl[]; loadOrganization: typeof loadOrganization; loadOrganizationPreferences: typeof loadOrganizationPreferences; loadStarredDashboards: typeof loadStarredDashboards; setOrganizationName: typeof setOrganizationName; - setOrganizationHomeDashboard: typeof setOrganizationHomeDashboard; - setOrganizationTheme: typeof setOrganizationTheme; - setOrganizationTimezone: typeof setOrganizationTimezone; updateOrganization: typeof updateOrganization; - updateOrganizationPreferences: typeof updateOrganizationPreferences; } export class OrgDetailsPage extends PureComponent { @@ -50,24 +41,8 @@ export class OrgDetailsPage extends PureComponent { this.props.updateOrganization(); }; - onSubmitPreferences = () => { - this.props.updateOrganizationPreferences(); - }; - - onThemeChange = theme => { - this.props.setOrganizationTheme(theme); - }; - - onHomeDashboardChange = dashboardId => { - this.props.setOrganizationHomeDashboard(dashboardId); - }; - - onTimeZoneChange = timeZone => { - this.props.setOrganizationTimezone(timeZone); - }; - render() { - const { navModel, organization, preferences, starredDashboards } = this.props; + const { navModel, organization, preferences } = this.props; return (
@@ -82,14 +57,7 @@ export class OrgDetailsPage extends PureComponent { onSubmit={this.onUpdateOrganization} orgName={organization.name} /> - this.onHomeDashboardChange(dashboardId)} - onThemeChange={theme => this.onThemeChange(theme)} - onTimeZoneChange={timeZone => this.onTimeZoneChange(timeZone)} - onSubmit={this.onSubmitPreferences} - /> +
)} @@ -103,7 +71,6 @@ function mapStateToProps(state: StoreState) { navModel: getNavModel(state.navIndex, 'org-settings'), organization: state.organization.organization, preferences: state.organization.preferences, - starredDashboards: state.organization.starredDashboards, }; } @@ -112,11 +79,7 @@ const mapDispatchToProps = { loadOrganizationPreferences, loadStarredDashboards, setOrganizationName, - setOrganizationTheme, - setOrganizationHomeDashboard, - setOrganizationTimezone, updateOrganization, - updateOrganizationPreferences, }; export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(OrgDetailsPage)); diff --git a/public/app/features/org/OrgPreferences.test.tsx b/public/app/features/org/OrgPreferences.test.tsx new file mode 100644 index 00000000000..0f5d73eef48 --- /dev/null +++ b/public/app/features/org/OrgPreferences.test.tsx @@ -0,0 +1,28 @@ +import React from 'react'; +import { shallow } from 'enzyme'; +import { OrgPreferences, Props } from './OrgPreferences'; + +const setup = () => { + const props: Props = { + preferences: { + homeDashboardId: 1, + timezone: 'UTC', + theme: 'Default', + }, + starredDashboards: [{ id: 1, name: 'Standard dashboard' }], + setOrganizationTimezone: jest.fn(), + setOrganizationTheme: jest.fn(), + setOrganizationHomeDashboard: jest.fn(), + updateOrganizationPreferences: jest.fn(), + }; + + return shallow(); +}; + +describe('Render', () => { + it('should render component', () => { + const wrapper = setup(); + + expect(wrapper).toMatchSnapshot(); + }); +}); diff --git a/public/app/features/org/OrgPreferences.tsx b/public/app/features/org/OrgPreferences.tsx index e7f54e0498e..49331733921 100644 --- a/public/app/features/org/OrgPreferences.tsx +++ b/public/app/features/org/OrgPreferences.tsx @@ -1,92 +1,117 @@ -import React, { SFC } from 'react'; +import React, { PureComponent } from 'react'; +import { connect } from 'react-redux'; import Tooltip from '../../core/components/Tooltip/Tooltip'; import SimplePicker from '../../core/components/Picker/SimplePicker'; import { DashboardAcl, OrganizationPreferences } from 'app/types'; +import { + setOrganizationHomeDashboard, + setOrganizationTheme, + setOrganizationTimezone, + updateOrganizationPreferences, +} from './state/actions'; -interface Props { +export interface Props { preferences: OrganizationPreferences; starredDashboards: DashboardAcl[]; - onDashboardChange: (dashboardId: number) => void; - onTimeZoneChange: (timeZone: string) => void; - onThemeChange: (theme: string) => void; - onSubmit: () => void; + setOrganizationHomeDashboard: typeof setOrganizationHomeDashboard; + setOrganizationTheme: typeof setOrganizationTheme; + setOrganizationTimezone: typeof setOrganizationTimezone; + updateOrganizationPreferences: typeof updateOrganizationPreferences; } -const OrgPreferences: SFC = ({ - preferences, - starredDashboards, - onDashboardChange, - onSubmit, - onTimeZoneChange, - onThemeChange, -}) => { - const themes = [{ value: '', text: 'Default' }, { value: 'dark', text: 'Dark' }, { value: 'light', text: 'Light' }]; +const themes = [{ value: '', text: 'Default' }, { value: 'dark', text: 'Dark' }, { value: 'light', text: 'Light' }]; - const timezones = [ - { value: '', text: 'Default' }, - { value: 'browser', text: 'Local browser time' }, - { value: 'utc', text: 'UTC' }, - ]; +const timezones = [ + { value: '', text: 'Default' }, + { value: 'browser', text: 'Local browser time' }, + { value: 'utc', text: 'UTC' }, +]; - return ( -
{ - event.preventDefault(); - onSubmit(); - }} - > -

Preferences

-
- UI Theme - theme.value === preferences.theme)} - options={themes} - getOptionValue={i => i.value} - getOptionLabel={i => i.text} - onSelected={theme => onThemeChange(theme.value)} - width={20} - /> -
-
- - Home Dashboard - - - - - dashboard.id === preferences.homeDashboardId)} - getOptionValue={i => i.id} - getOptionLabel={i => i.title} - onSelected={(dashboard: DashboardAcl) => onDashboardChange(dashboard.id)} - options={starredDashboards} - placeholder="Chose default dashboard" - width={20} - /> -
-
- - timezone.value === preferences.timezone)} - getOptionValue={i => i.value} - getOptionLabel={i => i.text} - onSelected={timezone => onTimeZoneChange(timezone.value)} - options={timezones} - width={20} - /> -
-
- -
- - ); +export class OrgPreferences extends PureComponent { + onSubmitForm = event => { + event.preventDefault(); + this.props.updateOrganizationPreferences(); + }; + + render() { + const { + preferences, + starredDashboards, + setOrganizationHomeDashboard, + setOrganizationTimezone, + setOrganizationTheme, + } = this.props; + + starredDashboards.unshift({ id: 0, title: 'Default' }); + + return ( +
+

Preferences

+
+ UI Theme + theme.value === preferences.theme)} + options={themes} + getOptionValue={i => i.value} + getOptionLabel={i => i.text} + onSelected={theme => setOrganizationTheme(theme.value)} + width={20} + /> +
+
+ + Home Dashboard + + + + + dashboard.id === preferences.homeDashboardId)} + getOptionValue={i => i.id} + getOptionLabel={i => i.title} + onSelected={(dashboard: DashboardAcl) => setOrganizationHomeDashboard(dashboard.id)} + options={starredDashboards} + placeholder="Chose default dashboard" + width={20} + /> +
+
+ + timezone.value === preferences.timezone)} + getOptionValue={i => i.value} + getOptionLabel={i => i.text} + onSelected={timezone => setOrganizationTimezone(timezone.value)} + options={timezones} + width={20} + /> +
+
+ +
+ + ); + } +} + +function mapStateToProps(state) { + return { + preferences: state.organization.preferences, + starredDashboards: state.organization.starredDashboards, + }; +} + +const mapDispatchToProps = { + setOrganizationHomeDashboard, + setOrganizationTimezone, + setOrganizationTheme, + updateOrganizationPreferences, }; -export default OrgPreferences; +export default connect(mapStateToProps, mapDispatchToProps)(OrgPreferences); diff --git a/public/app/features/org/OrgProfile.test.tsx b/public/app/features/org/OrgProfile.test.tsx new file mode 100644 index 00000000000..d101eded13e --- /dev/null +++ b/public/app/features/org/OrgProfile.test.tsx @@ -0,0 +1,21 @@ +import React from 'react'; +import { shallow } from 'enzyme'; +import OrgProfile, { Props } from './OrgProfile'; + +const setup = () => { + const props: Props = { + orgName: 'Main org', + onSubmit: jest.fn(), + onOrgNameChange: jest.fn(), + }; + + return shallow(); +}; + +describe('Render', () => { + it('should render component', () => { + const wrapper = setup(); + + expect(wrapper).toMatchSnapshot(); + }); +}); diff --git a/public/app/features/org/OrgProfile.tsx b/public/app/features/org/OrgProfile.tsx index 32c14b94e17..22dfa7bb1ce 100644 --- a/public/app/features/org/OrgProfile.tsx +++ b/public/app/features/org/OrgProfile.tsx @@ -1,6 +1,6 @@ import React, { SFC } from 'react'; -interface Props { +export interface Props { orgName: string; onSubmit: () => void; onOrgNameChange: (orgName: string) => void; diff --git a/public/app/features/org/__snapshots__/OrgDetailsPage.test.tsx.snap b/public/app/features/org/__snapshots__/OrgDetailsPage.test.tsx.snap new file mode 100644 index 00000000000..28806d2bf1d --- /dev/null +++ b/public/app/features/org/__snapshots__/OrgDetailsPage.test.tsx.snap @@ -0,0 +1,36 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Render should render component 1`] = ` +
+ +
+ +
+
+`; + +exports[`Render should render organization and preferences 1`] = ` +
+ +
+
+ + +
+
+
+`; diff --git a/public/app/features/org/__snapshots__/OrgPreferences.test.tsx.snap b/public/app/features/org/__snapshots__/OrgPreferences.test.tsx.snap new file mode 100644 index 00000000000..4a0d0986e4f --- /dev/null +++ b/public/app/features/org/__snapshots__/OrgPreferences.test.tsx.snap @@ -0,0 +1,125 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Render should render component 1`] = ` +
+

+ Preferences +

+
+ + UI Theme + + +
+
+ + Home Dashboard + + + + + +
+
+ + +
+
+ +
+ +`; diff --git a/public/app/features/org/__snapshots__/OrgProfile.test.tsx.snap b/public/app/features/org/__snapshots__/OrgProfile.test.tsx.snap new file mode 100644 index 00000000000..b49b63e4532 --- /dev/null +++ b/public/app/features/org/__snapshots__/OrgProfile.test.tsx.snap @@ -0,0 +1,46 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Render should render component 1`] = ` +
+

+ Organization profile +

+
+
+
+ + Organization name + + +
+
+
+ +
+
+
+`; diff --git a/public/app/types/acl.ts b/public/app/types/acl.ts index 21f7bdac2d4..2c512f56c49 100644 --- a/public/app/types/acl.ts +++ b/public/app/types/acl.ts @@ -39,6 +39,7 @@ export interface DashboardAcl { name?: string; inherited?: boolean; sortRank?: number; + title?: string; } export interface DashboardPermissionInfo {