savehandler.spec.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import expect = require('expect.js');
  4. import { UUID } from '@phosphor/coreutils';
  5. import { ServiceManager } from '@jupyterlab/services';
  6. import {
  7. Context,
  8. DocumentRegistry,
  9. TextModelFactory
  10. } from '@jupyterlab/docregistry';
  11. import { SaveHandler } from '@jupyterlab/docmanager';
  12. import { acceptDialog, waitForDialog } from '../../utils';
  13. describe('docregistry/savehandler', () => {
  14. let manager: ServiceManager.IManager;
  15. let factory = new TextModelFactory();
  16. let context: Context<DocumentRegistry.IModel>;
  17. let handler: SaveHandler;
  18. before(() => {
  19. manager = new ServiceManager();
  20. return manager.ready;
  21. });
  22. beforeEach(() => {
  23. context = new Context({
  24. manager,
  25. factory,
  26. path: UUID.uuid4() + '.txt'
  27. });
  28. handler = new SaveHandler({ context });
  29. return context.initialize(true);
  30. });
  31. afterEach(() => {
  32. context.dispose();
  33. handler.dispose();
  34. });
  35. describe('SaveHandler', () => {
  36. describe('#constructor()', () => {
  37. it('should create a new save handler', () => {
  38. expect(handler).to.be.a(SaveHandler);
  39. });
  40. });
  41. describe('#saveInterval()', () => {
  42. it('should be the save interval of the handler', () => {
  43. expect(handler.saveInterval).to.be(120);
  44. });
  45. it('should be set-able', () => {
  46. handler.saveInterval = 200;
  47. expect(handler.saveInterval).to.be(200);
  48. });
  49. });
  50. describe('#isActive', () => {
  51. it('should test whether the handler is active', () => {
  52. expect(handler.isActive).to.be(false);
  53. handler.start();
  54. expect(handler.isActive).to.be(true);
  55. });
  56. });
  57. describe('#isDisposed', () => {
  58. it('should test whether the handler is disposed', () => {
  59. expect(handler.isDisposed).to.be(false);
  60. handler.dispose();
  61. expect(handler.isDisposed).to.be(true);
  62. });
  63. it('should be true after the context is disposed', () => {
  64. context.dispose();
  65. expect(handler.isDisposed).to.be(true);
  66. });
  67. });
  68. describe('#dispose()', () => {
  69. it('should dispose of the resources used by the handler', () => {
  70. expect(handler.isDisposed).to.be(false);
  71. handler.dispose();
  72. expect(handler.isDisposed).to.be(true);
  73. handler.dispose();
  74. expect(handler.isDisposed).to.be(true);
  75. });
  76. });
  77. describe('#start()', () => {
  78. it('should start the save handler', () => {
  79. handler.start();
  80. expect(handler.isActive).to.be(true);
  81. });
  82. it('should trigger a save', done => {
  83. context.fileChanged.connect(() => {
  84. done();
  85. });
  86. context.model.fromString('bar');
  87. expect(handler.isActive).to.be(false);
  88. handler.saveInterval = 1;
  89. handler.start();
  90. });
  91. it('should continue to save', done => {
  92. let called = 0;
  93. // Lower the duration multiplier.
  94. (handler as any)._multiplier = 1;
  95. context.fileChanged.connect(() => {
  96. if (called === 0) {
  97. context.model.fromString('bar');
  98. called++;
  99. } else {
  100. done();
  101. }
  102. });
  103. context.model.fromString('foo');
  104. expect(handler.isActive).to.be(false);
  105. handler.saveInterval = 1;
  106. handler.start();
  107. });
  108. it('should overwrite the file on disk', done => {
  109. // Lower the duration multiplier.
  110. (handler as any)._multiplier = 1;
  111. context.model.fromString('foo');
  112. context
  113. .initialize(true)
  114. .then(() => {
  115. setTimeout(() => {
  116. manager.contents
  117. .save(context.path, {
  118. type: factory.contentType,
  119. format: factory.fileFormat,
  120. content: 'bar'
  121. })
  122. .catch(done);
  123. handler.saveInterval = 1;
  124. handler.start();
  125. context.model.fromString('baz');
  126. context.fileChanged.connect(() => {
  127. expect(context.model.toString()).to.be('baz');
  128. done();
  129. });
  130. }, 1500); // The server has a one second resolution for saves.
  131. })
  132. .catch(done);
  133. acceptDialog().catch(done);
  134. });
  135. it('should revert to the file on disk', done => {
  136. // Lower the duration multiplier.
  137. (handler as any)._multiplier = 1;
  138. context.model.fromString('foo');
  139. context
  140. .initialize(true)
  141. .then(() => {
  142. context.fileChanged.connect(() => {
  143. expect(context.model.toString()).to.be('bar');
  144. done();
  145. });
  146. setTimeout(() => {
  147. manager.contents
  148. .save(context.path, {
  149. type: factory.contentType,
  150. format: factory.fileFormat,
  151. content: 'bar'
  152. })
  153. .catch(done);
  154. handler.saveInterval = 1;
  155. handler.start();
  156. context.model.fromString('baz');
  157. }, 1500); // The server has a one second resolution for saves.
  158. })
  159. .catch(done);
  160. waitForDialog().then(() => {
  161. let dialog = document.body.getElementsByClassName('jp-Dialog')[0];
  162. let buttons = dialog.getElementsByTagName('button');
  163. for (let i = 0; i < buttons.length; i++) {
  164. if (buttons[i].textContent === 'REVERT') {
  165. buttons[i].click();
  166. }
  167. }
  168. });
  169. });
  170. });
  171. describe('#stop()', () => {
  172. it('should stop the save timer', () => {
  173. handler.start();
  174. expect(handler.isActive).to.be(true);
  175. handler.stop();
  176. expect(handler.isActive).to.be(false);
  177. });
  178. });
  179. });
  180. });