-
Notifications
You must be signed in to change notification settings - Fork 14.1k
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
chore: Select component refactoring - SelectControl - Iteration 5 #16510
Changes from 30 commits
5e02cfd
8297208
0885f6c
8c3b96b
16341a7
5ed953c
67a2332
baa04dc
46be115
75e2dcb
c1a867f
9b0ec18
d89fabe
39d86c9
ffcb26b
a965366
3118a55
916fc29
a17164b
e48e428
738ee0d
fb13337
72810cd
bb7b470
7706a71
5854d0f
30e6b41
d69e33f
eb0f368
7bfcd93
5a94d56
3a5ee3f
353ae00
21dc30e
748af87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,8 +20,7 @@ | |
import React from 'react'; | ||
import sinon from 'sinon'; | ||
import { shallow } from 'enzyme'; | ||
import { Select, CreatableSelect } from 'src/components/Select'; | ||
import OnPasteSelect from 'src/components/Select/OnPasteSelect'; | ||
import { Select as SelectComponent } from 'src/components'; | ||
import SelectControl from 'src/explore/components/controls/SelectControl'; | ||
import { styledMount as mount } from 'spec/helpers/theming'; | ||
|
||
|
@@ -48,59 +47,35 @@ describe('SelectControl', () => { | |
wrapper = shallow(<SelectControl {...defaultProps} />); | ||
}); | ||
|
||
it('uses Select in onPasteSelect when freeForm=false', () => { | ||
wrapper = shallow(<SelectControl {...defaultProps} multi />); | ||
const select = wrapper.find(OnPasteSelect); | ||
expect(select.props().selectWrap).toBe(Select); | ||
}); | ||
|
||
it('uses Creatable in onPasteSelect when freeForm=true', () => { | ||
wrapper = shallow(<SelectControl {...defaultProps} multi freeForm />); | ||
const select = wrapper.find(OnPasteSelect); | ||
expect(select.props().selectWrap).toBe(CreatableSelect); | ||
}); | ||
|
||
it('calls props.onChange when select', () => { | ||
const select = wrapper.instance(); | ||
select.onChange({ value: 50 }); | ||
select.onChange(50); | ||
expect(defaultProps.onChange.calledWith(50)).toBe(true); | ||
}); | ||
|
||
it('returns all options on select all', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The "Select all" option has been removed as agreed with design and all the related tests have been removed as well |
||
const expectedValues = ['one', 'two']; | ||
const selectAllProps = { | ||
multi: true, | ||
allowAll: true, | ||
choices: expectedValues, | ||
name: 'row_limit', | ||
label: 'Row Limit', | ||
valueKey: 'value', | ||
onChange: sinon.spy(), | ||
}; | ||
wrapper.setProps(selectAllProps); | ||
wrapper.instance().onChange([{ meta: true, value: 'Select all' }]); | ||
expect(selectAllProps.onChange.calledWith(expectedValues)).toBe(true); | ||
}); | ||
|
||
describe('render', () => { | ||
it('renders with Select by default', () => { | ||
expect(wrapper.find(OnPasteSelect)).not.toExist(); | ||
expect(wrapper.findWhere(x => x.type() === Select)).toHaveLength(1); | ||
expect(wrapper.find(SelectComponent)).toExist(); | ||
}); | ||
|
||
it('renders with OnPasteSelect when multi', () => { | ||
it('renders as mode multiple', () => { | ||
wrapper.setProps({ multi: true }); | ||
expect(wrapper.find(OnPasteSelect)).toExist(); | ||
expect(wrapper.findWhere(x => x.type() === Select)).toHaveLength(0); | ||
expect(wrapper.find(SelectComponent)).toExist(); | ||
expect(wrapper.find(SelectComponent).prop('mode')).toBe('multiple'); | ||
}); | ||
|
||
it('renders with Creatable when freeForm', () => { | ||
it('renders with allowNewOptions when freeForm', () => { | ||
wrapper.setProps({ freeForm: true }); | ||
expect(wrapper.find(OnPasteSelect)).not.toExist(); | ||
expect(wrapper.findWhere(x => x.type() === CreatableSelect)).toHaveLength( | ||
1, | ||
); | ||
expect(wrapper.find(SelectComponent)).toExist(); | ||
expect(wrapper.find(SelectComponent).prop('allowNewOptions')).toBe(true); | ||
}); | ||
|
||
it('renders with allowNewOptions=false when freeForm=false', () => { | ||
wrapper.setProps({ freeForm: false }); | ||
expect(wrapper.find(SelectComponent)).toExist(); | ||
expect(wrapper.find(SelectComponent).prop('allowNewOptions')).toBe(false); | ||
}); | ||
|
||
describe('empty placeholder', () => { | ||
describe('withMulti', () => { | ||
it('does not show a placeholder if there are no choices', () => { | ||
|
@@ -161,16 +136,6 @@ describe('SelectControl', () => { | |
); | ||
expect(wrapper.html()).not.toContain('add something'); | ||
}); | ||
it('shows numbers of options as a placeholder by default', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The number of options and remaining options has been removed as agreed with design and all the related tests have been removed as well |
||
wrapper = mount(<SelectControl {...defaultProps} multi />); | ||
expect(wrapper.html()).toContain('2 option(s'); | ||
}); | ||
it('reduces the number of options in the placeholder by the value length', () => { | ||
wrapper = mount( | ||
<SelectControl {...defaultProps} multi value={['today']} />, | ||
); | ||
expect(wrapper.html()).toContain('1 option(s'); | ||
}); | ||
}); | ||
describe('when select is single', () => { | ||
it('does not render the placeholder when a selection has been made', () => { | ||
|
@@ -186,82 +151,12 @@ describe('SelectControl', () => { | |
}); | ||
}); | ||
|
||
describe('optionsRemaining', () => { | ||
describe('isMulti', () => { | ||
it('returns the options minus selected values', () => { | ||
const wrapper = mount( | ||
<SelectControl {...defaultProps} multi value={['today']} />, | ||
); | ||
expect(wrapper.instance().optionsRemaining()).toEqual(1); | ||
}); | ||
}); | ||
describe('is not multi', () => { | ||
it('returns the length of all options', () => { | ||
wrapper = mount( | ||
<SelectControl | ||
{...defaultProps} | ||
value={50} | ||
placeholder="add something" | ||
/>, | ||
); | ||
expect(wrapper.instance().optionsRemaining()).toEqual(2); | ||
}); | ||
}); | ||
describe('with Select all', () => { | ||
it('does not count it', () => { | ||
const props = { ...defaultProps, multi: true, allowAll: true }; | ||
const wrapper = mount(<SelectControl {...props} />); | ||
expect(wrapper.instance().getOptions(props).length).toEqual(3); | ||
expect(wrapper.instance().optionsRemaining()).toEqual(2); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getOptions', () => { | ||
it('returns the correct options', () => { | ||
wrapper.setProps(defaultProps); | ||
expect(wrapper.instance().getOptions(defaultProps)).toEqual(options); | ||
}); | ||
|
||
it('shows Select-All when enabled', () => { | ||
const selectAllProps = { | ||
choices: ['one', 'two'], | ||
name: 'name', | ||
freeForm: true, | ||
allowAll: true, | ||
multi: true, | ||
valueKey: 'value', | ||
}; | ||
wrapper.setProps(selectAllProps); | ||
expect(wrapper.instance().getOptions(selectAllProps)).toContainEqual({ | ||
label: 'Select all', | ||
meta: true, | ||
value: 'Select all', | ||
}); | ||
}); | ||
|
||
it('returns the correct options when freeform is set to true', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is not necessary with the new Select component as it is a standard behavior |
||
const freeFormProps = { | ||
choices: [], | ||
freeForm: true, | ||
value: ['one', 'two'], | ||
name: 'row_limit', | ||
label: 'Row Limit', | ||
valueKey: 'custom_value_key', | ||
onChange: sinon.spy(), | ||
}; | ||
// the last added option is at the top | ||
const expectedNewOptions = [ | ||
{ custom_value_key: 'two', label: 'two' }, | ||
{ custom_value_key: 'one', label: 'one' }, | ||
]; | ||
wrapper.setProps(freeFormProps); | ||
expect(wrapper.instance().getOptions(freeFormProps)).toEqual( | ||
expectedNewOptions, | ||
); | ||
}); | ||
}); | ||
|
||
describe('UNSAFE_componentWillReceiveProps', () => { | ||
it('sets state.options if props.choices has changed', () => { | ||
const updatedOptions = [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,7 @@ import { getClientErrorObject } from 'src/utils/getClientErrorObject'; | |
|
||
import CheckboxControl from 'src/explore/components/controls/CheckboxControl'; | ||
import TextControl from 'src/explore/components/controls/TextControl'; | ||
import SelectControl from 'src/explore/components/controls/SelectControl'; | ||
import { Select } from 'src/components'; | ||
import TextAreaControl from 'src/explore/components/controls/TextAreaControl'; | ||
import SelectAsyncControl from 'src/explore/components/controls/SelectAsyncControl'; | ||
import SpatialControl from 'src/explore/components/controls/SpatialControl'; | ||
|
@@ -113,7 +113,12 @@ const ColumnButtonWrapper = styled.div` | |
const checkboxGenerator = (d, onChange) => ( | ||
<CheckboxControl value={d} onChange={onChange} /> | ||
); | ||
const DATA_TYPES = ['STRING', 'NUMERIC', 'DATETIME', 'BOOLEAN']; | ||
const DATA_TYPES = [ | ||
{ value: 'STRING', label: 'STRING' }, | ||
{ value: 'NUMERIC', label: 'NUMERIC' }, | ||
{ value: 'DATETIME', label: 'DATETIME' }, | ||
{ value: 'BOOLEAN', label: 'BOOLEAN' }, | ||
]; | ||
Comment on lines
+124
to
+129
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not for this PR, but this probably shouldn't be a Select component at all, but rather a text field. These should be raw column types coming from the database, hence a typical |
||
|
||
const DATASOURCE_TYPES_ARR = [ | ||
{ key: 'physical', label: t('Physical (table or view)') }, | ||
|
@@ -199,7 +204,13 @@ function ColumnCollectionTable({ | |
fieldKey="type" | ||
label={t('Data type')} | ||
control={ | ||
<SelectControl choices={DATA_TYPES} name="type" freeForm /> | ||
<Select | ||
ariaLabel={t('Data type')} | ||
options={DATA_TYPES} | ||
name="type" | ||
allowNewOptions | ||
allowClear | ||
/> | ||
} | ||
/> | ||
)} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The OnPasteSelect is not used with the new Select component. These tests are included later checking whether
allowNewOptions
istrue
orfalse
based on the SelectControlfreeForm
option.