Skip to content

Commit

Permalink
Merge pull request #7 from mbondyra/pr/54477
Browse files Browse the repository at this point in the history
Reactification of management/section/settings, Fix updating config prop
  • Loading branch information
mattkime authored Jan 21, 2020
2 parents 859b32a + 73ec86c commit 5a8f4bf
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ import {
UiSettingsType,
} from '../../../../../../../core/server/';
import { FieldSetting } from './types';

import { AdvancedSettings } from './advanced_settings';

jest.mock('ui/new_platform', () => ({
npStart: mockConfig(),
}));

jest.mock('./components/field', () => ({
Field: () => {
return 'field';
Expand All @@ -48,178 +51,183 @@ jest.mock('./components/search', () => ({
},
}));

const defaultConfig: Partial<FieldSetting> = {
displayName: 'defaultName',
requiresPageReload: false,
isOverridden: false,
ariaName: 'ariaName',
readOnly: false,
isCustom: false,
defVal: 'defVal',
type: 'string' as UiSettingsType,
category: ['category'],
};
function mockConfig() {
const defaultConfig: Partial<FieldSetting> = {
displayName: 'defaultName',
requiresPageReload: false,
isOverridden: false,
ariaName: 'ariaName',
readOnly: false,
isCustom: false,
defVal: 'defVal',
type: 'string' as UiSettingsType,
category: ['category'],
};

const config = {
set: (key: string, value: any) => Promise.resolve(true),
remove: (key: string) => Promise.resolve(true),
isCustom: (key: string) => false,
isOverridden: (key: string) => Boolean(config.getAll()[key].isOverridden),
getRegistered: () => ({} as Readonly<Record<string, UiSettingsParams>>),
overrideLocalDefault: (key: string, value: any) => {},
getUpdate$: () =>
new Observable<{
key: string;
newValue: any;
oldValue: any;
}>(),
isDeclared: (key: string) => true,
isDefault: (key: string) => true,
const config = {
set: (key: string, value: any) => Promise.resolve(true),
remove: (key: string) => Promise.resolve(true),
isCustom: (key: string) => false,
isOverridden: (key: string) => Boolean(config.getAll()[key].isOverridden),
getRegistered: () => ({} as Readonly<Record<string, UiSettingsParams>>),
overrideLocalDefault: (key: string, value: any) => {},
getUpdate$: () =>
new Observable<{
key: string;
newValue: any;
oldValue: any;
}>(),
isDeclared: (key: string) => true,
isDefault: (key: string) => true,

getSaved$: () =>
new Observable<{
key: string;
newValue: any;
oldValue: any;
}>(),
getSaved$: () =>
new Observable<{
key: string;
newValue: any;
oldValue: any;
}>(),

getUpdateErrors$: () => new Observable<Error>(),
get: (key: string, defaultOverride?: any): any => config.getAll()[key] || defaultOverride,
get$: (key: string) => new Observable<any>(config.get(key)),
getAll: (): Readonly<Record<string, UiSettingsParams & UserProvidedValues>> => {
return {
'test:array:setting': {
...defaultConfig,
value: ['default_value'],
name: 'Test array setting',
description: 'Description for Test array setting',
category: ['elasticsearch'],
},
'test:boolean:setting': {
...defaultConfig,
value: true,
name: 'Test boolean setting',
description: 'Description for Test boolean setting',
category: ['elasticsearch'],
},
'test:image:setting': {
...defaultConfig,
value: null,
name: 'Test image setting',
description: 'Description for Test image setting',
type: 'image',
},
'test:json:setting': {
...defaultConfig,
value: '{"foo": "bar"}',
name: 'Test json setting',
description: 'Description for Test json setting',
type: 'json',
},
'test:markdown:setting': {
...defaultConfig,
value: '',
name: 'Test markdown setting',
description: 'Description for Test markdown setting',
type: 'markdown',
},
'test:number:setting': {
...defaultConfig,
value: 5,
name: 'Test number setting',
description: 'Description for Test number setting',
},
'test:select:setting': {
...defaultConfig,
value: 'orange',
name: 'Test select setting',
description: 'Description for Test select setting',
type: 'select',
options: ['apple', 'orange', 'banana'],
},
'test:string:setting': {
...defaultConfig,
...{
value: null,
name: 'Test string setting',
description: 'Description for Test string setting',
type: 'string',
isCustom: true,
getUpdateErrors$: () => new Observable<Error>(),
get: (key: string, defaultOverride?: any): any => config.getAll()[key] || defaultOverride,
get$: (key: string) => new Observable<any>(config.get(key)),
getAll: (): Readonly<Record<string, UiSettingsParams & UserProvidedValues>> => {
return {
'test:array:setting': {
...defaultConfig,
value: ['default_value'],
name: 'Test array setting',
description: 'Description for Test array setting',
category: ['elasticsearch'],
},
},
'test:readonlystring:setting': {
...defaultConfig,
...{
value: null,
name: 'Test readonly string setting',
description: 'Description for Test readonly string setting',
type: 'string',
readOnly: true,
'test:boolean:setting': {
...defaultConfig,
value: true,
name: 'Test boolean setting',
description: 'Description for Test boolean setting',
category: ['elasticsearch'],
},
},
'test:customstring:setting': {
...defaultConfig,
...{
'test:image:setting': {
...defaultConfig,
value: null,
name: 'Test custom string setting',
description: 'Description for Test custom string setting',
name: 'Test image setting',
description: 'Description for Test image setting',
type: 'image',
},
'test:json:setting': {
...defaultConfig,
value: '{"foo": "bar"}',
name: 'Test json setting',
description: 'Description for Test json setting',
type: 'json',
},
'test:markdown:setting': {
...defaultConfig,
value: '',
name: 'Test markdown setting',
description: 'Description for Test markdown setting',
type: 'markdown',
},
'test:number:setting': {
...defaultConfig,
value: 5,
name: 'Test number setting',
description: 'Description for Test number setting',
},
'test:select:setting': {
...defaultConfig,
value: 'orange',
name: 'Test select setting',
description: 'Description for Test select setting',
type: 'select',
options: ['apple', 'orange', 'banana'],
},
'test:string:setting': {
...defaultConfig,
...{
value: null,
name: 'Test string setting',
description: 'Description for Test string setting',
type: 'string',
isCustom: true,
},
},
'test:readonlystring:setting': {
...defaultConfig,
...{
value: null,
name: 'Test readonly string setting',
description: 'Description for Test readonly string setting',
type: 'string',
readOnly: true,
},
},
'test:customstring:setting': {
...defaultConfig,
...{
value: null,
name: 'Test custom string setting',
description: 'Description for Test custom string setting',
type: 'string',
isCustom: true,
},
},
'test:isOverridden:string': {
...defaultConfig,
isOverridden: true,
value: 'foo',
name: 'An overridden string',
description: 'Description for overridden string',
type: 'string',
isCustom: true,
},
},
'test:isOverridden:string': {
...defaultConfig,
isOverridden: true,
value: 'foo',
name: 'An overridden string',
description: 'Description for overridden string',
type: 'string',
},
'test:isOverridden:number': {
...defaultConfig,
isOverridden: true,
value: 1234,
name: 'An overridden number',
description: 'Description for overridden number',
type: 'number',
},
'test:isOverridden:json': {
...defaultConfig,
isOverridden: true,
value: dedent`
{
"foo": "bar"
}
`,
name: 'An overridden json',
description: 'Description for overridden json',
type: 'json',
},
'test:isOverridden:select': {
...defaultConfig,
isOverridden: true,
value: 'orange',
name: 'Test overridden select setting',
description: 'Description for overridden select setting',
type: 'select',
options: ['apple', 'orange', 'banana'],
},
};
},
};
'test:isOverridden:number': {
...defaultConfig,
isOverridden: true,
value: 1234,
name: 'An overridden number',
description: 'Description for overridden number',
type: 'number',
},
'test:isOverridden:json': {
...defaultConfig,
isOverridden: true,
value: dedent`
{
"foo": "bar"
}
`,
name: 'An overridden json',
description: 'Description for overridden json',
type: 'json',
},
'test:isOverridden:select': {
...defaultConfig,
isOverridden: true,
value: 'orange',
name: 'Test overridden select setting',
description: 'Description for overridden select setting',
type: 'select',
options: ['apple', 'orange', 'banana'],
},
};
},
};
return {
core: {
uiSettings: config,
},
};
}

describe('AdvancedSettings', () => {
it('should render specific setting if given setting key', async () => {
const component = shallow(
<AdvancedSettings config={config} query="test:string:setting" enableSaving={true} />
);
const component = shallow(<AdvancedSettings query="test:string:setting" enableSaving={true} />);

expect(component).toMatchSnapshot();
});

it('should render read-only when saving is disabled', async () => {
const component = shallow(
<AdvancedSettings config={config} query="test:string:setting" enableSaving={false} />
<AdvancedSettings query="test:string:setting" enableSaving={false} />
);

expect(component).toMatchSnapshot();
Expand Down
Loading

0 comments on commit 5a8f4bf

Please sign in to comment.