formSource.spec.jsx 2.0 KB

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