Home Reference Source

application/__tests__/hocs/draftable-test.js

  1. jest.unmock('./../../hocs/draftable');
  2. jest.unmock('./../../services/base-service');
  3. jest.unmock('./../../services/error-service');
  4. jest.unmock('./../../services/draft-service');
  5.  
  6. jest.unmock('./../../util/strings');
  7. jest.unmock('./../../alt');
  8.  
  9. import React from 'react';
  10. import ReactDOM from 'react-dom';
  11. import TestUtils from 'react-addons-test-utils';
  12. import Immutable from 'immutable';
  13. import sinon from 'sinon';
  14. import AltTestingUtils from 'alt-utils/lib/AltTestingUtils';
  15. import {Input} from 'react-materialize';
  16.  
  17. import fetch from './../../util/mock-fetch';
  18.  
  19. import {DraftService, DraftsStore} from './../../services/draft-service';
  20. import {withDraftable} from './../../hocs/draftable';
  21.  
  22. class MockChild extends React.Component {
  23. constructor(props) {
  24. super(props);
  25. }
  26.  
  27. draftCb() {
  28. return;
  29. }
  30.  
  31. updateCb() {
  32. return;
  33. }
  34.  
  35. componentWillMount() {
  36. this.props.draftable.doesHaveDraft({'srn': 'tsn:123'}, this.draftCb);
  37. this.props.draftable.setDraftCreatedHook(this.updateCb);
  38. }
  39.  
  40. componentWillUnmount() {
  41.  
  42. }
  43.  
  44. render() {
  45. return (
  46. <div className='internalObject'></div>
  47. );
  48. }
  49. }
  50. const MockWrapped = withDraftable(MockChild);
  51.  
  52. describe('DraftableComponent', () => {
  53.  
  54. beforeEach(() => {
  55. jasmine.clock().uninstall();
  56. jasmine.clock().install();
  57. sinon.stub(window, 'fetch').returns(fetch([
  58. {
  59. id: 1,
  60. srn: 'srn:tsn:ceo-core:content/5432543-54325432',
  61. uuid: '5432543-54325432',
  62. slug: 'imma-test-slug',
  63. created_at: '2016-05-09 15:23:01',
  64. modified_at: '2016-05-09 15:34:16',
  65. published_at: null,
  66. version: 5
  67. }
  68. ]));
  69.  
  70. sinon.stub(MockChild.prototype, 'componentWillMount');
  71. });
  72.  
  73. afterEach(() => {
  74. window.fetch.restore();
  75. jasmine.clock().uninstall();
  76. MockChild.prototype.componentWillMount.restore();
  77. });
  78.  
  79. it('wraps component', () => {
  80. const wrapSpy = sinon.spy(MockWrapped.prototype, 'componentWillMount');
  81.  
  82. const el = TestUtils.renderIntoDocument(<MockWrapped />);
  83. expect(wrapSpy.called).toEqual(true);
  84. expect(el.interval).not.toEqual(false);
  85. MockWrapped.prototype.componentWillMount.restore();
  86. });
  87.  
  88. it('calls interval', () => {
  89. const wrapSpy = sinon.spy(MockWrapped.prototype, 'createDraft');
  90.  
  91. const el = TestUtils.renderIntoDocument(<MockWrapped />);
  92.  
  93. expect(wrapSpy.called).toEqual(false);
  94. expect(el.isDirty).toEqual(false);
  95.  
  96. jasmine.clock().tick(5001);
  97.  
  98. expect(wrapSpy.called).toEqual(true);
  99. expect(el.isDirty).toEqual(false);
  100.  
  101. MockWrapped.prototype.createDraft.restore();
  102. });
  103.  
  104. it('is dirty', (done) => {
  105. const wrapSpy = sinon.spy(MockWrapped.prototype, 'createDraft');
  106.  
  107. const el = TestUtils.renderIntoDocument(<MockWrapped />);
  108.  
  109. el.setState({'raw': {}});
  110.  
  111. expect(wrapSpy.called).toEqual(false);
  112. expect(el.isDirty).toEqual(false);
  113. expect(JSON.stringify(el.state.raw)).toEqual('{}');
  114.  
  115. el.onChange({'raw': {'foo': 'bar'}});
  116. expect(JSON.stringify(el.state.raw)).toEqual('{"foo":"bar"}');
  117. expect(el.isDirty).toEqual(true);
  118.  
  119. jasmine.clock().tick(5001);
  120. expect(wrapSpy.called).toEqual(true);
  121.  
  122. jasmine.clock().tick(101);
  123. expect(window.fetch.called).toEqual(true);
  124.  
  125. // actually wait for the return
  126. jasmine.clock().uninstall();
  127. setTimeout(() => {
  128. expect(el.isDirty).toEqual(false);
  129.  
  130. MockWrapped.prototype.createDraft.restore();
  131. done()
  132. }, 100);
  133. });
  134.  
  135. it('is not dirty', () => {
  136. const wrapSpy = sinon.spy(MockWrapped.prototype, 'createDraft');
  137.  
  138. const el = TestUtils.renderIntoDocument(<MockWrapped />);
  139.  
  140. el.setState({'raw': {'foo': 'bar'}});
  141.  
  142. expect(wrapSpy.called).toEqual(false);
  143. expect(el.isDirty).toEqual(false);
  144.  
  145. el.onChange({'raw': {'foo': 'bar'}});
  146. expect(el.isDirty).toEqual(false);
  147.  
  148. jasmine.clock().tick(5001);
  149. expect(wrapSpy.called).toEqual(true);
  150.  
  151. jasmine.clock().tick(101);
  152. expect(el.isDirty).toEqual(false);
  153. expect(window.fetch.called).toEqual(false);
  154.  
  155. MockWrapped.prototype.createDraft.restore();
  156. });
  157.  
  158. it('returns valid draft', (done) => {
  159. window.fetch.restore();
  160. MockChild.prototype.componentWillMount.restore();
  161.  
  162. sinon.stub(window, 'fetch').returns(fetch([
  163. {
  164. id: 1,
  165. content_raw: '{"foo":"bar"}'
  166. }
  167. ]));
  168.  
  169. const mockSpy = sinon.spy(MockChild.prototype, 'draftCb');
  170. const checkSpy = sinon.spy(MockWrapped.prototype, 'checkDraft');
  171.  
  172. jasmine.clock().uninstall();
  173. const el = TestUtils.renderIntoDocument(<MockWrapped />);
  174.  
  175. setTimeout(() => {
  176. expect(checkSpy.called).toEqual(true);
  177. expect(window.fetch.called).toEqual(true);
  178. expect(mockSpy.called).toEqual(true);
  179.  
  180. const ret = mockSpy.getCalls()[0].args[0];
  181. expect(ret.get('content_raw')).toEqual('{"foo":"bar"}');
  182.  
  183. MockChild.prototype.draftCb.restore();
  184. MockWrapped.prototype.checkDraft.restore();
  185.  
  186. sinon.stub(MockChild.prototype, 'componentWillMount');
  187. done();
  188. }, 100);
  189. });
  190.  
  191. it('returns empty draft', (done) => {
  192. window.fetch.restore();
  193. MockChild.prototype.componentWillMount.restore();
  194.  
  195. sinon.stub(window, 'fetch').returns(fetch([]));
  196.  
  197. const mockSpy = sinon.spy(MockChild.prototype, 'draftCb');
  198. const checkSpy = sinon.spy(MockWrapped.prototype, 'checkDraft');
  199.  
  200. jasmine.clock().uninstall();
  201. const el = TestUtils.renderIntoDocument(<MockWrapped />);
  202.  
  203. setTimeout(() => {
  204. expect(checkSpy.called).toEqual(true);
  205. expect(window.fetch.called).toEqual(true);
  206. expect(mockSpy.called).toEqual(true);
  207.  
  208. const ret = mockSpy.getCalls()[0].args[0];
  209. expect(ret).toEqual(false);
  210.  
  211. MockChild.prototype.draftCb.restore();
  212. MockWrapped.prototype.checkDraft.restore();
  213.  
  214. sinon.stub(MockChild.prototype, 'componentWillMount');
  215. done();
  216. }, 100);
  217. });
  218.  
  219. it('returns calls update callback', (done) => {
  220. window.fetch.restore();
  221. MockChild.prototype.componentWillMount.restore();
  222.  
  223. sinon.stub(window, 'fetch').returns(fetch([
  224. {
  225. id: 1,
  226. content_raw: '{"foo":"bar"}'
  227. }
  228. ]));
  229.  
  230. const mockSpy = sinon.spy(MockChild.prototype, 'updateCb');
  231.  
  232. const el = TestUtils.renderIntoDocument(<MockWrapped />);
  233. expect(mockSpy.called).toEqual(false);
  234.  
  235. el.onChange({'raw': {'foo': 'bar'}});
  236.  
  237. jasmine.clock().tick(5101);
  238.  
  239. // actually wait for the return
  240. jasmine.clock().uninstall();
  241. setTimeout(() => {
  242. expect(mockSpy.called).toEqual(true);
  243.  
  244. MockChild.prototype.updateCb.restore();
  245.  
  246. sinon.stub(MockChild.prototype, 'componentWillMount');
  247. done();
  248. }, 100);
  249. });
  250.  
  251. });