release.spec.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import {getProjectRelease, getReleaseDeploys} from 'sentry/actionCreators/release';
  2. import ReleaseStore, {getReleaseStoreKey} from 'sentry/stores/releaseStore';
  3. describe('ReleaseActionCreator', function () {
  4. const orgSlug = 'myOrg';
  5. const projectSlug = 'myProject';
  6. const releaseVersion = 'myRelease';
  7. const releaseKey = getReleaseStoreKey(projectSlug, releaseVersion);
  8. const api = new MockApiClient();
  9. const mockData = {id: '1'};
  10. let mockResponse;
  11. describe('getProjectRelease', () => {
  12. const releaseUrl = `/projects/${orgSlug}/${projectSlug}/releases/${encodeURIComponent(
  13. releaseVersion
  14. )}/`;
  15. beforeEach(() => {
  16. MockApiClient.clearMockResponses();
  17. mockResponse = MockApiClient.addMockResponse({
  18. url: releaseUrl,
  19. body: mockData,
  20. });
  21. ReleaseStore.reset();
  22. jest.restoreAllMocks();
  23. jest.spyOn(ReleaseStore, 'loadRelease');
  24. jest.spyOn(ReleaseStore, 'loadReleaseSuccess');
  25. // XXX(leedongwei): We cannot spy on ReleaseStore at all
  26. // See repositories.spec.jsx beforeEach method for the reason
  27. });
  28. it('fetches a Release and emits actions', async () => {
  29. getProjectRelease(api, {orgSlug, projectSlug, releaseVersion});
  30. expect(ReleaseStore.loadRelease).toHaveBeenCalledWith(
  31. orgSlug,
  32. projectSlug,
  33. releaseVersion
  34. );
  35. expect(ReleaseStore.loadReleaseSuccess).not.toHaveBeenCalled();
  36. await tick(); // Run Store.loadRelease and fire Action.loadReleaseSuccess
  37. await tick(); // Run Store.loadReleaseSuccess
  38. expect(mockResponse).toHaveBeenCalledWith(releaseUrl, expect.anything());
  39. expect(ReleaseStore.loadReleaseSuccess).toHaveBeenCalledWith(
  40. projectSlug,
  41. releaseVersion,
  42. mockData
  43. );
  44. expect(ReleaseStore.state.release[releaseKey]).toEqual(mockData);
  45. expect(ReleaseStore.state.releaseLoading[releaseKey]).toEqual(false);
  46. expect(ReleaseStore.state.releaseError[releaseKey]).toEqual(undefined);
  47. });
  48. it('short-circuits the JS event loop when fetching Release', () => {
  49. expect(ReleaseStore.state.releaseLoading[releaseKey]).toEqual(undefined);
  50. getProjectRelease(api, {orgSlug, projectSlug, releaseVersion});
  51. expect(ReleaseStore.loadRelease).toHaveBeenCalled();
  52. // expect(ReleaseStore.loadRelease).not.toHaveBeenCalled(); // See above for comment on ReleaseStore
  53. expect(ReleaseStore.state.releaseLoading[releaseKey]).toEqual(true);
  54. });
  55. });
  56. describe('getReleaseDeploys', () => {
  57. const deploysUrl = `/organizations/${orgSlug}/releases/${encodeURIComponent(
  58. releaseVersion
  59. )}/deploys/`;
  60. beforeEach(() => {
  61. MockApiClient.clearMockResponses();
  62. mockResponse = MockApiClient.addMockResponse({
  63. url: deploysUrl,
  64. body: [mockData],
  65. });
  66. ReleaseStore.reset();
  67. jest.restoreAllMocks();
  68. jest.spyOn(ReleaseStore, 'loadDeploys');
  69. jest.spyOn(ReleaseStore, 'loadDeploysSuccess');
  70. });
  71. it('fetch Deploys and emit an action', async () => {
  72. getReleaseDeploys(api, {orgSlug, projectSlug, releaseVersion});
  73. expect(ReleaseStore.loadDeploys).toHaveBeenCalledWith(
  74. orgSlug,
  75. projectSlug,
  76. releaseVersion
  77. );
  78. expect(ReleaseStore.loadDeploysSuccess).not.toHaveBeenCalled();
  79. await tick(); // Run Store.loadDeploys and fire Action.loadDeploysSuccess
  80. await tick(); // Run Store.loadDeploysSuccess
  81. expect(mockResponse).toHaveBeenCalledWith(deploysUrl, expect.anything());
  82. expect(ReleaseStore.loadDeploysSuccess).toHaveBeenCalledWith(
  83. projectSlug,
  84. releaseVersion,
  85. [mockData]
  86. );
  87. expect(ReleaseStore.state.deploys[releaseKey]).toEqual([mockData]);
  88. expect(ReleaseStore.state.deploysLoading[releaseKey]).toEqual(false);
  89. expect(ReleaseStore.state.deploysError[releaseKey]).toEqual(undefined);
  90. });
  91. it('short-circuits the JS event loop when fetching Deploys', () => {
  92. expect(ReleaseStore.state.deploysLoading[releaseKey]).toEqual(undefined);
  93. getReleaseDeploys(api, {orgSlug, projectSlug, releaseVersion});
  94. expect(ReleaseStore.loadDeploys).toHaveBeenCalled();
  95. // expect(ReleaseStore.loadDeploys).not.toHaveBeenCalled(); // See above for comment on ReleaseStore
  96. expect(ReleaseStore.state.deploysLoading[releaseKey]).toEqual(true);
  97. });
  98. });
  99. });