selectAsyncField.spec.jsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React from 'react';
  2. import {mount} from 'enzyme';
  3. import {Form, SelectAsyncField} from 'app/components/forms';
  4. describe('SelectAsyncField', function() {
  5. let api;
  6. beforeEach(function() {
  7. api = MockApiClient.addMockResponse({
  8. url: '/foo/bar/',
  9. query: {
  10. autocomplete_query: 'baz',
  11. autocomplete_field: 'fieldName',
  12. },
  13. body: {
  14. fieldName: [{id: 'baz', text: 'Baz Label'}],
  15. },
  16. });
  17. });
  18. it('supports autocomplete arguments from an integration', async function() {
  19. let wrapper = mount(<SelectAsyncField url="/foo/bar/" name="fieldName" />);
  20. wrapper.find('input[id="id-fieldName"]').simulate('change', {target: {value: 'baz'}});
  21. expect(api).toHaveBeenCalled();
  22. await tick();
  23. wrapper.update();
  24. // Is in select menu
  25. expect(wrapper.find('Select').prop('options')).toEqual([
  26. expect.objectContaining({
  27. value: 'baz',
  28. label: 'Baz Label',
  29. }),
  30. ]);
  31. });
  32. it('with Form context', async function() {
  33. let submitMock = jest.fn();
  34. let wrapper = mount(
  35. <Form onSubmit={submitMock}>
  36. <SelectAsyncField url="/foo/bar/" name="fieldName" />
  37. </Form>,
  38. {}
  39. );
  40. wrapper.find('input[id="id-fieldName"]').simulate('change', {target: {value: 'baz'}});
  41. await tick();
  42. wrapper.update();
  43. // Is in select menu
  44. expect(wrapper.find('Select').prop('options')).toEqual([
  45. expect.objectContaining({
  46. value: 'baz',
  47. label: 'Baz Label',
  48. }),
  49. ]);
  50. // Select item
  51. wrapper.find('input[id="id-fieldName"]').simulate('keyDown', {keyCode: 13});
  52. // SelectControl MUST have the value object, not just a simple value
  53. // otherwise it means that selecting an item that has been populated in the menu by
  54. // an async request will not work (nothing will appear selected).
  55. expect(wrapper.find('SelectControl').prop('value')).toEqual({
  56. value: 'baz',
  57. label: expect.anything(),
  58. });
  59. wrapper.find('Form').simulate('submit');
  60. expect(submitMock).toHaveBeenCalledWith(
  61. {
  62. fieldName: 'baz',
  63. },
  64. expect.anything(),
  65. expect.anything()
  66. );
  67. });
  68. });