selectField.spec.jsx 2.5 KB

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