selectAsyncField.spec.jsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {selectByQuery, selectByValue} from 'sentry-test/select-new';
  3. import {Form, SelectAsyncField} from 'sentry/components/deprecatedforms';
  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. const wrapper = mountWithTheme(<SelectAsyncField url="/foo/bar/" name="fieldName" />);
  20. await selectByQuery(wrapper, 'baz', {control: true});
  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. const submitMock = jest.fn();
  34. const wrapper = mountWithTheme(
  35. <Form onSubmit={submitMock}>
  36. <SelectAsyncField url="/foo/bar/" name="fieldName" />
  37. </Form>,
  38. {}
  39. );
  40. await selectByQuery(wrapper, 'baz', {control: true});
  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. selectByValue(wrapper, 'baz', {control: true});
  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. });