Home Reference Source

application/__tests__/services/base-service-test.js

  1. jest.unmock('./../../services/base-service');
  2. jest.unmock('./../../util/mock-fetch');
  3. jest.unmock('./../../util/strings');
  4.  
  5. jest.unmock('./../../alt');
  6.  
  7. import React from 'react';
  8. import ReactDOM from 'react-dom';
  9. import TestUtils from 'react-addons-test-utils';
  10. import Immutable from 'immutable';
  11. import sinon from 'sinon';
  12. import AltTestingUtils from 'alt-utils/lib/AltTestingUtils';
  13.  
  14. import alt from './../../alt';
  15. import BaseService from './../../services/base-service';
  16. import fetch from './../../util/mock-fetch';
  17.  
  18. describe('BaseService', () => {
  19.  
  20. it('calls get', (done) => {
  21. sinon.stub(window, 'fetch').returns(fetch([{
  22. id: 1,
  23. user_id: 2,
  24. content_id: 3,
  25. content: 'this is a test'
  26. }]));
  27.  
  28. const base = new BaseService;
  29.  
  30. base.get('/foo/fraz', {'content': 'fraz'}).then(() => {
  31. expect(window.fetch.called).toEqual(true);
  32. expect(window.fetch.getCalls()[0].args[0]).toEqual('/foo/fraz?content=fraz');
  33.  
  34. const params = window.fetch.getCalls()[0].args[1];
  35. expect(params.headers.Authorization).toEqual('Basic ' + btoa('1:'));
  36.  
  37. window.fetch.restore();
  38. done();
  39. });
  40. });
  41.  
  42. it('calls post', (done) => {
  43. sinon.stub(window, 'fetch').returns(fetch([{
  44. id: 1,
  45. user_id: 2,
  46. content_id: 3,
  47. content: 'this is a test'
  48. }]));
  49.  
  50. const base = new BaseService;
  51.  
  52. base.post('/foo/fraz', {'content': 'fraz'}).then(() => {
  53. expect(window.fetch.called).toEqual(true);
  54. expect(window.fetch.getCalls()[0].args[0]).toEqual('/foo/fraz');
  55.  
  56. const params = window.fetch.getCalls()[0].args[1];
  57. expect(params.headers.Authorization).toEqual('Basic ' + btoa('1:'));
  58. expect(params.method).toEqual('post');
  59. expect(params.body).toEqual('{"content":"fraz"}');
  60.  
  61. window.fetch.restore();
  62. done();
  63. });
  64. });
  65.  
  66. it('calls post with params', (done) => {
  67. sinon.stub(window, 'fetch').returns(fetch([{
  68. id: 1,
  69. user_id: 2,
  70. content_id: 3,
  71. content: 'this is a test'
  72. }]));
  73.  
  74. const base = new BaseService;
  75.  
  76. base.post('/foo/fraz', {'content': 'fraz'}, {'getparam': 'foo'}).then(() => {
  77. expect(window.fetch.called).toEqual(true);
  78. expect(window.fetch.getCalls()[0].args[0]).toEqual('/foo/fraz?getparam=foo');
  79.  
  80. const params = window.fetch.getCalls()[0].args[1];
  81. expect(params.headers.Authorization).toEqual('Basic ' + btoa('1:'));
  82. expect(params.method).toEqual('post');
  83. expect(params.body).toEqual('{"content":"fraz"}');
  84.  
  85. window.fetch.restore();
  86. done();
  87. });
  88. });
  89.  
  90. it('calls put', (done) => {
  91. sinon.stub(window, 'fetch').returns(fetch([{
  92. id: 1,
  93. user_id: 2,
  94. content_id: 3,
  95. content: 'this is a test'
  96. }]));
  97.  
  98. const base = new BaseService;
  99.  
  100. base.put('/foo/fraz', {'content': 'fraz'}).then(() => {
  101. expect(window.fetch.called).toEqual(true);
  102. expect(window.fetch.getCalls()[0].args[0]).toEqual('/foo/fraz');
  103.  
  104. const params = window.fetch.getCalls()[0].args[1];
  105. expect(params.headers.Authorization).toEqual('Basic ' + btoa('1:'));
  106. expect(params.method).toEqual('put');
  107. expect(params.body).toEqual('{"content":"fraz"}');
  108.  
  109. window.fetch.restore();
  110. done();
  111. });
  112. });
  113.  
  114. it('calls delete', (done) => {
  115. sinon.stub(window, 'fetch').returns(fetch([{
  116. id: 1,
  117. user_id: 2,
  118. content_id: 3,
  119. content: 'this is a test'
  120. }]));
  121.  
  122. const base = new BaseService;
  123.  
  124. base.delete('/foo/fraz').then(() => {
  125. expect(window.fetch.called).toEqual(true);
  126. expect(window.fetch.getCalls()[0].args[0]).toEqual('/foo/fraz');
  127.  
  128. const params = window.fetch.getCalls()[0].args[1];
  129. expect(params.headers.Authorization).toEqual('Basic ' + btoa('1:'));
  130. expect(params.method).toEqual('delete');
  131.  
  132. window.fetch.restore();
  133. done();
  134. });
  135. });
  136.  
  137. it('throws exception on get', (done) => {
  138. sinon.stub(window, 'fetch').returns(fetch([{
  139. id: 1,
  140. user_id: 2,
  141. content_id: 3,
  142. content: 'this is a test'
  143. }], 400));
  144.  
  145. const base = new BaseService;
  146.  
  147. base.get('/foo/fraz').then(() => {
  148. expect(window.fetch.called).toEqual(true);
  149. }).catch((e) => {
  150. expect(e);
  151. window.fetch.restore();
  152. done();
  153. });
  154. });
  155.  
  156. it('throws exception on post', (done) => {
  157. sinon.stub(window, 'fetch').returns(fetch([{
  158. id: 1,
  159. user_id: 2,
  160. content_id: 3,
  161. content: 'this is a test'
  162. }], 400));
  163.  
  164. const base = new BaseService;
  165.  
  166. base.post('/foo/fraz').then(() => {
  167. expect(window.fetch.called).toEqual(true);
  168. }).catch((e) => {
  169. expect(e);
  170. window.fetch.restore();
  171. done();
  172. });
  173. });
  174.  
  175. it('throws exception on put', (done) => {
  176. sinon.stub(window, 'fetch').returns(fetch([{
  177. id: 1,
  178. user_id: 2,
  179. content_id: 3,
  180. content: 'this is a test'
  181. }], 400));
  182.  
  183. const base = new BaseService;
  184.  
  185. base.put('/foo/fraz').then(() => {
  186. expect(window.fetch.called).toEqual(true);
  187. }).catch((e) => {
  188. expect(e);
  189. window.fetch.restore();
  190. done();
  191. });
  192. });
  193.  
  194. it('throws exception on delete', (done) => {
  195. sinon.stub(window, 'fetch').returns(fetch([{
  196. id: 1,
  197. user_id: 2,
  198. content_id: 3,
  199. content: 'this is a test'
  200. }], 400));
  201.  
  202. const base = new BaseService;
  203.  
  204. base.delete('/foo/fraz').then(() => {
  205. expect(window.fetch.called).toEqual(true);
  206. }).catch((e) => {
  207. expect(e);
  208. window.fetch.restore();
  209. done();
  210. });
  211. });
  212. });