release.spec.jsx 4.4 KB

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