formSource.spec.jsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import * as ActionCreators from 'sentry/actionCreators/formSearch';
  3. import FormSource from 'sentry/components/search/sources/formSource';
  4. import FormSearchStore from 'sentry/stores/formSearchStore';
  5. describe('FormSource', function () {
  6. let wrapper;
  7. const searchMap = [
  8. {
  9. title: 'Test Field',
  10. description: 'test-help',
  11. route: '/route/',
  12. field: {
  13. name: 'test-field',
  14. label: 'Test Field',
  15. help: 'test-help',
  16. },
  17. },
  18. {
  19. title: 'Foo Field',
  20. description: 'foo-help',
  21. route: '/foo/',
  22. field: {
  23. name: 'foo-field',
  24. label: 'Foo Field',
  25. help: 'foo-help',
  26. },
  27. },
  28. ];
  29. beforeEach(function () {
  30. jest.spyOn(ActionCreators, 'loadSearchMap').mockImplementation(() => {});
  31. FormSearchStore.loadSearchMap(searchMap);
  32. });
  33. afterEach(function () {
  34. ActionCreators.loadSearchMap.mockRestore();
  35. });
  36. it('can find a form field', async function () {
  37. const mock = jest.fn().mockReturnValue(null);
  38. wrapper = mountWithTheme(<FormSource query="te">{mock}</FormSource>);
  39. await tick();
  40. await tick();
  41. wrapper.update();
  42. expect(mock).toHaveBeenLastCalledWith(
  43. expect.objectContaining({
  44. results: [
  45. expect.objectContaining({
  46. item: {
  47. field: {
  48. label: 'Test Field',
  49. name: 'test-field',
  50. help: 'test-help',
  51. },
  52. title: 'Test Field',
  53. description: 'test-help',
  54. route: '/route/',
  55. resultType: 'field',
  56. sourceType: 'field',
  57. to: '/route/#test-field',
  58. },
  59. }),
  60. ],
  61. })
  62. );
  63. });
  64. it('does not find any form field', async function () {
  65. const mock = jest.fn().mockReturnValue(null);
  66. wrapper = mountWithTheme(<FormSource query="invalid">{mock}</FormSource>);
  67. await tick();
  68. wrapper.update();
  69. expect(mock).toHaveBeenCalledWith({
  70. isLoading: false,
  71. results: [],
  72. });
  73. });
  74. });