withRelease.spec.jsx 2.7 KB

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