selectCreatableField.spec.jsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {changeInputValue, openMenu} from 'sentry-test/select-new';
  3. import {Form, SelectCreatableField} from 'sentry/components/deprecatedforms';
  4. describe('SelectCreatableField', function () {
  5. it('can add user input into select field when using options', function () {
  6. const wrapper = mountWithTheme(
  7. <SelectCreatableField options={[{value: 'foo', label: 'Foo'}]} name="fieldName" />
  8. );
  9. const input = wrapper.find('SelectControl input[type="text"]');
  10. changeInputValue(input, 'bar');
  11. wrapper.update();
  12. // Text is in input
  13. expect(wrapper.find('SelectControl input[type="text"]').props().value).toBe('bar');
  14. // Click on create option
  15. openMenu(wrapper, {control: true});
  16. wrapper.find('SelectControl Option Label').simulate('click');
  17. // Is active hidden input value
  18. expect(wrapper.find('SelectControl input[type="hidden"]').props().value).toEqual(
  19. 'bar'
  20. );
  21. });
  22. it('can add user input into select field when using choices', function () {
  23. const wrapper = mountWithTheme(
  24. <SelectCreatableField choices={['foo']} name="fieldName" />
  25. );
  26. const input = wrapper.find('SelectControl input[type="text"]');
  27. changeInputValue(input, 'bar');
  28. wrapper.update();
  29. // Text is in input
  30. expect(wrapper.find('SelectControl input[type="text"]').props().value).toBe('bar');
  31. // Click on create option
  32. openMenu(wrapper, {control: true});
  33. wrapper.find('SelectControl Option Label').simulate('click');
  34. // Is active hidden input value
  35. expect(wrapper.find('SelectControl input[type="hidden"]').props().value).toEqual(
  36. 'bar'
  37. );
  38. });
  39. it('can add user input into select field when using paired choices', function () {
  40. const wrapper = mountWithTheme(
  41. <SelectCreatableField choices={[['foo', 'foo']]} name="fieldName" />
  42. );
  43. const input = wrapper.find('SelectControl input[type="text"]');
  44. changeInputValue(input, 'bar');
  45. wrapper.update();
  46. // Text is in input
  47. expect(wrapper.find('SelectControl input[type="text"]').props().value).toBe('bar');
  48. // Click on create option
  49. openMenu(wrapper, {control: true});
  50. wrapper.find('SelectControl Option Label').simulate('click');
  51. // Is active hidden input value
  52. expect(wrapper.find('SelectControl input[type="hidden"]').props().value).toEqual(
  53. 'bar'
  54. );
  55. });
  56. it('with Form context', function () {
  57. const submitMock = jest.fn();
  58. const wrapper = mountWithTheme(
  59. <Form onSubmit={submitMock}>
  60. <SelectCreatableField choices={[['foo', 'foo']]} name="fieldName" />
  61. </Form>,
  62. {}
  63. );
  64. const input = wrapper.find('SelectControl input[type="text"]');
  65. changeInputValue(input, 'bar');
  66. wrapper.update();
  67. // Text is in input
  68. expect(wrapper.find('SelectControl input[type="text"]').props().value).toBe('bar');
  69. // Click on create option
  70. openMenu(wrapper, {control: true});
  71. wrapper.find('SelectControl Option Label').simulate('click');
  72. wrapper.find('Form').simulate('submit');
  73. expect(submitMock).toHaveBeenCalledWith(
  74. {
  75. fieldName: 'bar',
  76. },
  77. expect.anything(),
  78. expect.anything()
  79. );
  80. });
  81. });