-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/828 create tag component in elements (#866)
* feat: #828 Add dropdown select component using rc-select * put tag component into formik and testing * fix lint Co-authored-by: Vu Nguyen <[email protected]>
- Loading branch information
Showing
7 changed files
with
590 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/elements/src/components/DropdownSelect/__test__/__snapshots__/index.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Dropdown-select should match a snapshot 1`] = ` | ||
<div | ||
className="field pb-4" | ||
> | ||
<div | ||
className="control" | ||
> | ||
<Field | ||
name="test" | ||
> | ||
<Component /> | ||
</Field> | ||
</div> | ||
</div> | ||
`; |
105 changes: 105 additions & 0 deletions
105
packages/elements/src/components/DropdownSelect/__test__/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import * as React from 'react' | ||
import { shallow, mount } from 'enzyme' | ||
import { DropdownSelect, DropdownSelectProps } from '../index' | ||
import { Formik, Form, FormikErrors } from 'formik' | ||
import toJson from 'enzyme-to-json' | ||
import { act } from 'react-dom/test-utils' | ||
|
||
const mockedOptions = [ | ||
{ label: 'a', value: 'a' }, | ||
{ label: 'b', value: 'b' }, | ||
] | ||
const dropdownSelectProps: DropdownSelectProps = { | ||
name: 'demo', | ||
labelText: 'demo', | ||
} | ||
|
||
const createFormikWrapper = () => { | ||
const wrapper = mount( | ||
<Formik onSubmit={jest.fn()} initialValues={{ demo: 'b' }}> | ||
{() => ( | ||
<Form> | ||
<div className="column is-half-desktop"> | ||
<DropdownSelect name="demo" mode="tags" labelText="Demo" options={mockedOptions} /> | ||
</div> | ||
</Form> | ||
)} | ||
</Formik>, | ||
) | ||
|
||
return wrapper | ||
} | ||
|
||
const ErrorFomrikComponent = () => { | ||
return ( | ||
<section className="section"> | ||
<Formik | ||
validate={values => { | ||
const errors: FormikErrors<any> = { | ||
demo: '', | ||
} | ||
if (values.demo === 'b') { | ||
errors.demo = 'Required' | ||
return errors | ||
} | ||
return errors | ||
}} | ||
initialValues={{ demo: 'a' }} | ||
onSubmit={jest.fn()} | ||
> | ||
{() => ( | ||
<Form> | ||
<div className="column is-half-desktop"> | ||
<DropdownSelect name="demo" mode="tags" labelText="Demo" options={mockedOptions} /> | ||
</div> | ||
</Form> | ||
)} | ||
</Formik> | ||
</section> | ||
) | ||
} | ||
|
||
describe('Dropdown-select', () => { | ||
it('should match a snapshot', () => { | ||
expect(toJson(shallow(<DropdownSelect {...dropdownSelectProps} />))).toMatchSnapshot() | ||
}) | ||
|
||
describe('should work when integrating with Formik', () => { | ||
it('Render error correctly', async () => { | ||
const wrapper = mount(<ErrorFomrikComponent />) | ||
const select = wrapper.find('Select') | ||
await act(async () => { | ||
select.simulate('change', { target: { name: 'demo', value: 'b' } }) | ||
}) | ||
|
||
wrapper.update() | ||
|
||
expect(wrapper.find('Select').props().value).toBe('b') | ||
}) | ||
}) | ||
|
||
it('Render label correctly', () => { | ||
const wrapper = createFormikWrapper() | ||
const label = wrapper.find('label').first() | ||
expect(label.text()).toBe('Demo') | ||
}) | ||
|
||
it('Map value correctly from formik', async () => { | ||
const wrapper = createFormikWrapper() | ||
|
||
await act(async () => { | ||
wrapper.find('Select').simulate('change', { | ||
target: { | ||
name: 'demo', | ||
value: 'a', | ||
}, | ||
}) | ||
}) | ||
wrapper.update() | ||
expect(wrapper.find('Select').prop('value')).toEqual('a') | ||
}) | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
}) |
29 changes: 29 additions & 0 deletions
29
packages/elements/src/components/DropdownSelect/dropdown-select.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react' | ||
import { Form, Formik } from 'formik' | ||
|
||
import { storiesOf } from '@storybook/react' | ||
import { DropdownSelect } from '.' | ||
import { action } from '@storybook/addon-actions' | ||
|
||
const options = [ | ||
{ value: 'jack', label: 'Jack' }, | ||
{ value: 'lucy', label: 'Lucy' }, | ||
{ value: 'yiminghe', label: 'Yiminghe' }, | ||
] | ||
|
||
storiesOf('DropdownSelect', module).add('Primary', () => { | ||
return ( | ||
<section className="section"> | ||
<Formik | ||
initialValues={{ value: '' }} | ||
onSubmit={values => { | ||
action('Form Values' + values) | ||
}} | ||
> | ||
<Form> | ||
<DropdownSelect name="dropdown-select-field" mode="tags" labelText="Dropdown Select" options={options} /> | ||
</Form> | ||
</Formik> | ||
</section> | ||
) | ||
}) |
Oops, something went wrong.