withRelease.spec.jsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import {render, waitFor} from 'sentry-test/reactTestingLibrary';
  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 function () {
  31. const Component = jest.fn(() => null);
  32. const Container = withRelease(Component);
  33. render(
  34. <Container
  35. api={api}
  36. organization={organization}
  37. projectSlug={projectSlug}
  38. releaseVersion={releaseVersion}
  39. />
  40. );
  41. await waitFor(() =>
  42. expect(Component).toHaveBeenCalledWith(
  43. expect.objectContaining({
  44. release: mockData,
  45. releaseLoading: false,
  46. releaseError: undefined,
  47. deploys: [mockData],
  48. deploysLoading: false,
  49. deploysError: undefined,
  50. }),
  51. {}
  52. )
  53. );
  54. });
  55. it('prevents repeated calls', function () {
  56. const Component = jest.fn(() => null);
  57. const Container = withRelease(Component);
  58. jest.spyOn(api, 'requestPromise');
  59. jest.spyOn(Container.prototype, 'fetchRelease');
  60. jest.spyOn(Container.prototype, 'fetchDeploys');
  61. // Mount and run component
  62. render(
  63. <Container
  64. api={api}
  65. organization={organization}
  66. projectSlug={projectSlug}
  67. releaseVersion={releaseVersion}
  68. />
  69. );
  70. // Mount and run duplicates
  71. render(
  72. <Container
  73. api={api}
  74. organization={organization}
  75. projectSlug={projectSlug}
  76. releaseVersion={releaseVersion}
  77. />
  78. );
  79. render(
  80. <Container
  81. api={api}
  82. organization={organization}
  83. projectSlug={projectSlug}
  84. releaseVersion={releaseVersion}
  85. />
  86. );
  87. expect(api.requestPromise).toHaveBeenCalledTimes(2); // 1 for fetchRelease, 1 for fetchDeploys
  88. expect(Container.prototype.fetchRelease).toHaveBeenCalledTimes(3);
  89. expect(Container.prototype.fetchDeploys).toHaveBeenCalledTimes(3);
  90. });
  91. });