api.spec.jsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import $ from 'jquery';
  2. import {Client, Request, paramsToQueryArgs} from 'app/api';
  3. import GroupActions from 'app/actions/groupActions';
  4. import {PROJECT_MOVED} from 'app/constants/apiErrorCodes';
  5. jest.unmock('app/api');
  6. describe('api', function() {
  7. let sandbox;
  8. let api;
  9. beforeEach(function() {
  10. sandbox = sinon.sandbox.create();
  11. api = new Client();
  12. });
  13. afterEach(function() {
  14. sandbox.restore();
  15. });
  16. describe('paramsToQueryArgs()', function() {
  17. it('should convert itemIds properties to id array', function() {
  18. expect(
  19. paramsToQueryArgs({
  20. itemIds: [1, 2, 3],
  21. query: 'is:unresolved', // itemIds takes precedence
  22. })
  23. ).toEqual({id: [1, 2, 3]});
  24. });
  25. it('should extract query property if no itemIds', function() {
  26. expect(
  27. paramsToQueryArgs({
  28. query: 'is:unresolved',
  29. foo: 'bar',
  30. })
  31. ).toEqual({query: 'is:unresolved'});
  32. });
  33. it('should convert params w/o itemIds or query to undefined', function() {
  34. expect(
  35. paramsToQueryArgs({
  36. foo: 'bar',
  37. bar: 'baz', // paramsToQueryArgs ignores these
  38. })
  39. ).toBeUndefined();
  40. });
  41. it('should keep environment when query is provided', function() {
  42. expect(
  43. paramsToQueryArgs({
  44. query: 'is:unresolved',
  45. environment: 'production',
  46. })
  47. ).toEqual({query: 'is:unresolved', environment: 'production'});
  48. });
  49. it('should exclude environment when it is null/undefined', function() {
  50. expect(
  51. paramsToQueryArgs({
  52. query: 'is:unresolved',
  53. environment: null,
  54. })
  55. ).toEqual({query: 'is:unresolved'});
  56. });
  57. });
  58. describe('Client', function() {
  59. beforeEach(function() {
  60. sandbox.stub($, 'ajax');
  61. });
  62. describe('cancel()', function() {
  63. it('should abort any open XHR requests', function() {
  64. const req1 = new Request({
  65. abort: sinon.stub(),
  66. });
  67. const req2 = new Request({
  68. abort: sinon.stub(),
  69. });
  70. api.activeRequests = {
  71. 1: req1,
  72. 2: req2,
  73. };
  74. api.clear();
  75. expect(req1.xhr.abort.calledOnce).toBeTruthy();
  76. expect(req2.xhr.abort.calledOnce).toBeTruthy();
  77. });
  78. });
  79. });
  80. it('does not call success callback if 302 was returned because of a project slug change', function() {
  81. const successCb = jest.fn();
  82. api.activeRequests = {id: {alive: true}};
  83. api.wrapCallback('id', successCb)({
  84. responseJSON: {
  85. detail: {
  86. code: PROJECT_MOVED,
  87. message: '...',
  88. extra: {
  89. slug: 'new-slug',
  90. },
  91. },
  92. },
  93. });
  94. expect(successCb).not.toHaveBeenCalled();
  95. });
  96. it('handles error callback', function() {
  97. sandbox.stub(api, 'wrapCallback', (id, func) => func);
  98. const errorCb = jest.fn();
  99. const args = ['test', true, 1];
  100. api.handleRequestError(
  101. {
  102. id: 'test',
  103. path: 'test',
  104. requestOptions: {error: errorCb},
  105. },
  106. ...args
  107. );
  108. expect(errorCb).toHaveBeenCalledWith(...args);
  109. api.wrapCallback.restore();
  110. });
  111. it('handles undefined error callback', function() {
  112. expect(() =>
  113. api.handleRequestError(
  114. {
  115. id: 'test',
  116. path: 'test',
  117. requestOptions: {},
  118. },
  119. {},
  120. {}
  121. )
  122. ).not.toThrow();
  123. });
  124. describe('bulkUpdate()', function() {
  125. beforeEach(function() {
  126. sandbox.stub(api, '_wrapRequest');
  127. sandbox.stub(GroupActions, 'update'); // stub GroupActions.update call from api.update
  128. });
  129. it('should use itemIds as query if provided', function() {
  130. api.bulkUpdate({
  131. orgId: '1337',
  132. projectId: '1337',
  133. itemIds: [1, 2, 3],
  134. data: {status: 'unresolved'},
  135. query: 'is:resolved',
  136. });
  137. expect(api._wrapRequest.calledOnce).toBeTruthy();
  138. const requestArgs = api._wrapRequest.getCall(0).args[1];
  139. expect(requestArgs.query).toEqual({id: [1, 2, 3]});
  140. });
  141. it('should use query as query if itemIds are absent', function() {
  142. api.bulkUpdate({
  143. orgId: '1337',
  144. projectId: '1337',
  145. itemIds: null,
  146. data: {status: 'unresolved'},
  147. query: 'is:resolved',
  148. });
  149. expect(api._wrapRequest.calledOnce).toBeTruthy();
  150. const requestArgs = api._wrapRequest.getCall(0).args[1];
  151. expect(requestArgs.query).toEqual({query: 'is:resolved'});
  152. });
  153. });
  154. describe('merge()', function() {
  155. // TODO: this is totally copypasta from the test above. We need to refactor
  156. // these API methods/tests.
  157. beforeEach(function() {
  158. sandbox.stub(api, '_wrapRequest');
  159. sandbox.stub(GroupActions, 'merge'); // stub GroupActions.merge call from api.merge
  160. });
  161. it('should use itemIds as query if provided', function() {
  162. api.merge({
  163. orgId: '1337',
  164. projectId: '1337',
  165. itemIds: [1, 2, 3],
  166. data: {status: 'unresolved'},
  167. query: 'is:resolved',
  168. });
  169. expect(api._wrapRequest.calledOnce).toBeTruthy();
  170. const requestArgs = api._wrapRequest.getCall(0).args[1];
  171. expect(requestArgs.query).toEqual({id: [1, 2, 3]});
  172. });
  173. it('should use query as query if itemIds are absent', function() {
  174. api.merge({
  175. orgId: '1337',
  176. projectId: '1337',
  177. itemIds: null,
  178. data: {status: 'unresolved'},
  179. query: 'is:resolved',
  180. });
  181. expect(api._wrapRequest.calledOnce).toBeTruthy();
  182. const requestArgs = api._wrapRequest.getCall(0).args[1];
  183. expect(requestArgs.query).toEqual({query: 'is:resolved'});
  184. });
  185. });
  186. });