selectCreatableField.spec.jsx 3.3 KB

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