formSource.spec.jsx 1.9 KB

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