selectCreatableField.spec.jsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import React from 'react';
  2. import {mountWithTheme} from 'sentry-test/enzyme';
  3. import {Form, SelectCreatableField} from 'app/components/forms';
  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. TestStubs.routerContext()
  9. );
  10. wrapper
  11. .find('input[id="id-fieldName"]')
  12. .simulate('change', {target: {value: 'bar'}})
  13. .simulate('keyDown', {keyCode: 13});
  14. wrapper.update();
  15. // Is selected
  16. expect(wrapper.find('.Select-value-label').text()).toBe('bar');
  17. // Is in select menu
  18. expect(wrapper.find('Select').prop('options')).toEqual([
  19. expect.objectContaining({
  20. value: 'bar',
  21. label: 'bar',
  22. }),
  23. {
  24. value: 'foo',
  25. label: 'Foo',
  26. },
  27. ]);
  28. });
  29. it('can add user input into select field when using choices', function() {
  30. const wrapper = mountWithTheme(
  31. <SelectCreatableField choices={['foo']} name="fieldName" />,
  32. TestStubs.routerContext()
  33. );
  34. wrapper
  35. .find('input[id="id-fieldName"]')
  36. .simulate('change', {target: {value: 'bar'}})
  37. .simulate('keyDown', {keyCode: 13});
  38. wrapper.update();
  39. // Is selected
  40. expect(wrapper.find('.Select-value-label').text()).toBe('bar');
  41. // Is in select menu
  42. expect(wrapper.find('Select').prop('options')).toEqual([
  43. expect.objectContaining({
  44. value: 'bar',
  45. label: 'bar',
  46. }),
  47. {
  48. value: 'foo',
  49. label: 'foo',
  50. },
  51. ]);
  52. });
  53. it('can add user input into select field when using paired choices', function() {
  54. const wrapper = mountWithTheme(
  55. <SelectCreatableField choices={[['foo', 'foo']]} name="fieldName" />,
  56. TestStubs.routerContext()
  57. );
  58. wrapper
  59. .find('input[id="id-fieldName"]')
  60. .simulate('change', {target: {value: 'bar'}})
  61. .simulate('keyDown', {keyCode: 13});
  62. wrapper.update();
  63. // Is selected
  64. expect(wrapper.find('.Select-value-label').text()).toBe('bar');
  65. // Is in select menu
  66. expect(wrapper.find('Select').prop('options')).toEqual([
  67. expect.objectContaining({
  68. value: 'bar',
  69. label: 'bar',
  70. }),
  71. {
  72. value: 'foo',
  73. label: 'foo',
  74. },
  75. ]);
  76. });
  77. it('with Form context', function() {
  78. const submitMock = jest.fn();
  79. const wrapper = mountWithTheme(
  80. <Form onSubmit={submitMock}>
  81. <SelectCreatableField choices={[['foo', 'foo']]} name="fieldName" />
  82. </Form>,
  83. TestStubs.routerContext()
  84. );
  85. wrapper
  86. .find('input[id="id-fieldName"]')
  87. .simulate('change', {target: {value: 'bar'}})
  88. .simulate('keyDown', {keyCode: 13});
  89. wrapper.update();
  90. // Is selected
  91. expect(wrapper.find('.Select-value-label').text()).toBe('bar');
  92. // Is in select menu
  93. expect(wrapper.find('Select').prop('options')).toEqual([
  94. expect.objectContaining({
  95. value: 'bar',
  96. label: 'bar',
  97. }),
  98. {
  99. value: 'foo',
  100. label: 'foo',
  101. },
  102. ]);
  103. // SelectControl should have the value object, not just a simple value
  104. expect(wrapper.find('SelectControl').prop('value')).toEqual(
  105. expect.objectContaining({
  106. value: 'bar',
  107. label: 'bar',
  108. })
  109. );
  110. wrapper.find('Form').simulate('submit');
  111. expect(submitMock).toHaveBeenCalledWith(
  112. {
  113. fieldName: 'bar',
  114. },
  115. expect.anything(),
  116. expect.anything()
  117. );
  118. });
  119. });