withRelease.spec.jsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import ReleaseStore from 'sentry/stores/releaseStore';
  3. import withRelease from 'sentry/utils/withRelease';
  4. describe('withRelease HoC', function () {
  5. const organization = TestStubs.Organization();
  6. const orgSlug = organization.slug;
  7. const projectSlug = 'myProject';
  8. const releaseVersion = 'myRelease';
  9. const releaseUrl = `/projects/${orgSlug}/${projectSlug}/releases/${encodeURIComponent(
  10. releaseVersion
  11. )}/`;
  12. const deployUrl = `/organizations/${orgSlug}/releases/${encodeURIComponent(
  13. releaseVersion
  14. )}/deploys/`;
  15. const api = new MockApiClient();
  16. const mockData = {id: '1'};
  17. beforeEach(() => {
  18. MockApiClient.clearMockResponses();
  19. MockApiClient.addMockResponse({
  20. url: releaseUrl,
  21. body: mockData,
  22. });
  23. MockApiClient.addMockResponse({
  24. url: deployUrl,
  25. body: [mockData],
  26. });
  27. jest.restoreAllMocks();
  28. ReleaseStore.reset();
  29. });
  30. it('adds release/deploys prop', async () => {
  31. const Component = () => null;
  32. const Container = withRelease(Component);
  33. const wrapper = mountWithTheme(
  34. <Container
  35. api={api}
  36. organization={organization}
  37. projectSlug={projectSlug}
  38. releaseVersion={releaseVersion}
  39. />
  40. );
  41. await tick(); // Run Store.loadEtc
  42. await tick(); // Run Store.loadEtcSuccess
  43. wrapper.update();
  44. const mountedComponent = wrapper.find(Component);
  45. expect(mountedComponent.prop('release')).toEqual(mockData);
  46. expect(mountedComponent.prop('releaseLoading')).toEqual(false);
  47. expect(mountedComponent.prop('releaseError')).toEqual(undefined);
  48. expect(mountedComponent.prop('deploys')).toEqual([mockData]);
  49. expect(mountedComponent.prop('deploysLoading')).toEqual(false);
  50. expect(mountedComponent.prop('deploysError')).toEqual(undefined);
  51. });
  52. it('prevents repeated calls', async () => {
  53. const Component = () => null;
  54. const Container = withRelease(Component);
  55. jest.spyOn(api, 'requestPromise');
  56. jest.spyOn(Container.prototype, 'fetchRelease');
  57. jest.spyOn(Container.prototype, 'fetchDeploys');
  58. // Mount and run component
  59. mountWithTheme(
  60. <Container
  61. api={api}
  62. organization={organization}
  63. projectSlug={projectSlug}
  64. releaseVersion={releaseVersion}
  65. />
  66. );
  67. await tick();
  68. await tick();
  69. // Mount and run duplicates
  70. mountWithTheme(
  71. <Container
  72. api={api}
  73. organization={organization}
  74. projectSlug={projectSlug}
  75. releaseVersion={releaseVersion}
  76. />
  77. );
  78. await tick();
  79. mountWithTheme(
  80. <Container
  81. api={api}
  82. organization={organization}
  83. projectSlug={projectSlug}
  84. releaseVersion={releaseVersion}
  85. />
  86. );
  87. await tick();
  88. expect(api.requestPromise).toHaveBeenCalledTimes(2); // 1 for fetchRelease, 1 for fetchDeploys
  89. expect(Container.prototype.fetchRelease).toHaveBeenCalledTimes(3);
  90. expect(Container.prototype.fetchDeploys).toHaveBeenCalledTimes(3);
  91. });
  92. });