savehandler.spec.ts 5.2 KB

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