selectField.spec.jsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import React from 'react';
  2. import {mount, shallow} from 'enzyme';
  3. import {Form, SelectField} from 'app/components/forms';
  4. import {selectByValue} from '../../../helpers/select';
  5. describe('SelectField', function() {
  6. it('renders without form context', function() {
  7. let wrapper = mount(
  8. <SelectField
  9. options={[{label: 'a', value: 'a'}, {label: 'b', value: 'b'}]}
  10. name="fieldName"
  11. value="a"
  12. />
  13. );
  14. expect(wrapper).toMatchSnapshot();
  15. });
  16. it('renders with flat choices', function() {
  17. let wrapper = shallow(<SelectField choices={['a', 'b', 'c']} name="fieldName" />, {
  18. context: {
  19. form: {
  20. data: {
  21. fieldName: 'fieldValue',
  22. },
  23. errors: {},
  24. },
  25. },
  26. });
  27. expect(wrapper).toMatchSnapshot();
  28. });
  29. it('renders with paired choices', function() {
  30. let wrapper = shallow(
  31. <SelectField
  32. choices={[['a', 'abc'], ['b', 'bcd'], ['c', 'cde']]}
  33. name="fieldName"
  34. />,
  35. {
  36. context: {
  37. form: {
  38. data: {
  39. fieldName: 'fieldValue',
  40. },
  41. errors: {},
  42. },
  43. },
  44. }
  45. );
  46. expect(wrapper).toMatchSnapshot();
  47. });
  48. it('can change value and submit', function() {
  49. let mock = jest.fn();
  50. let wrapper = mount(
  51. <Form onSubmit={mock}>
  52. <SelectField
  53. options={[{label: 'a', value: 'a'}, {label: 'b', value: 'b'}]}
  54. name="fieldName"
  55. />
  56. </Form>
  57. );
  58. selectByValue(wrapper, 'a', {name: 'fieldName'});
  59. wrapper.find('Form').simulate('submit');
  60. expect(mock).toHaveBeenCalledWith(
  61. {fieldName: 'a'},
  62. expect.anything(),
  63. expect.anything()
  64. );
  65. });
  66. describe('Multiple', function() {
  67. it('selects multiple values and submits', function() {
  68. let mock = jest.fn();
  69. let wrapper = mount(
  70. <Form onSubmit={mock}>
  71. <SelectField
  72. multiple
  73. options={[{label: 'a', value: 'a'}, {label: 'b', value: 'b'}]}
  74. name="fieldName"
  75. />
  76. </Form>
  77. );
  78. selectByValue(wrapper, 'a', {name: 'fieldName'});
  79. wrapper.find('Form').simulate('submit');
  80. expect(mock).toHaveBeenCalledWith(
  81. {fieldName: ['a']},
  82. expect.anything(),
  83. expect.anything()
  84. );
  85. });
  86. });
  87. });