observablejson.spec.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. Message
  6. } from '@phosphor/messaging';
  7. import {
  8. Widget
  9. } from '@phosphor/widgets';
  10. import {
  11. simulate
  12. } from 'simulate-event';
  13. import {
  14. CodeMirrorEditorFactory
  15. } from '../../../lib/codemirror';
  16. import {
  17. IObservableJSON, ObservableJSON, ObservableJSONWidget
  18. } from '../../../lib/common/observablejson';
  19. class LogEditor extends ObservableJSONWidget {
  20. methods: string[] = [];
  21. events: string[] = [];
  22. handleEvent(event: Event): void {
  23. super.handleEvent(event);
  24. this.events.push(event.type);
  25. }
  26. protected onAfterAttach(msg: Message): void {
  27. super.onAfterAttach(msg);
  28. this.methods.push('onAfterAttach');
  29. }
  30. protected onBeforeDetach(msg: Message): void {
  31. super.onBeforeDetach(msg);
  32. this.methods.push('onBeforeDetach');
  33. }
  34. }
  35. describe('common/observablejson', () => {
  36. describe('ObservableJSON', () => {
  37. describe('#constructor()', () => {
  38. it('should create an observable JSON object', () => {
  39. let item = new ObservableJSON();
  40. expect(item).to.be.an(ObservableJSON);
  41. });
  42. it('should accept initial values', () => {
  43. let item = new ObservableJSON({
  44. values: { 'foo': 1, 'bar': 'baz'}
  45. });
  46. expect(item).to.be.an(ObservableJSON);
  47. });
  48. });
  49. describe('#toJSON()', () => {
  50. it('should serialize the model to JSON', () => {
  51. let item = new ObservableJSON();
  52. item.set('foo', 1);
  53. expect(item.toJSON()['foo']).to.be(1);
  54. });
  55. it('should return a copy of the data', () => {
  56. let item = new ObservableJSON();
  57. item.set('foo', { 'bar': 1 });
  58. let value = item.toJSON();
  59. value['bar'] = 2;
  60. expect((item.get('foo') as any)['bar']).to.be(1);
  61. });
  62. });
  63. });
  64. describe('ObservableJSON.ChangeMessage', () => {
  65. describe('#constructor()', () => {
  66. it('should create a new message', () => {
  67. let message = new ObservableJSON.ChangeMessage({
  68. key: 'foo',
  69. type: 'add',
  70. oldValue: 1,
  71. newValue: 2
  72. });
  73. expect(message).to.be.a(ObservableJSON.ChangeMessage);
  74. });
  75. });
  76. describe('#args', () => {
  77. it('should be the args of the message', () => {
  78. let args: IObservableJSON.IChangedArgs = {
  79. key: 'foo',
  80. type: 'add',
  81. oldValue: 'ho',
  82. newValue: 'hi'
  83. };
  84. let message = new ObservableJSON.ChangeMessage(args);
  85. expect(message.args).to.be(args);
  86. });
  87. });
  88. });
  89. describe('ObservableJSONWidget', () => {
  90. let editor: LogEditor;
  91. const editorFactory = new CodeMirrorEditorFactory().newInlineEditor;
  92. beforeEach(() => {
  93. editor = new LogEditor({ editorFactory });
  94. });
  95. afterEach(() => {
  96. editor.dispose();
  97. });
  98. describe('#constructor', () => {
  99. it('should create a new metadata editor', () => {
  100. let newEditor = new ObservableJSONWidget({ editorFactory });
  101. expect(newEditor).to.be.a(ObservableJSONWidget);
  102. });
  103. });
  104. describe('#editorHostNode', () => {
  105. it('should be the editor host node used by the editor', () => {
  106. expect(editor.editorHostNode.classList).to.contain('jp-ObservableJSONWidget-host');
  107. });
  108. });
  109. describe('#revertButtonNode', () => {
  110. it('should be the revert button node used by the editor', () => {
  111. expect(editor.revertButtonNode.classList).to.contain('jp-ObservableJSONWidget-revertButton');
  112. });
  113. });
  114. describe('#commitButtonNode', () => {
  115. it('should be the commit button node used by the editor', () => {
  116. expect(editor.commitButtonNode.classList).to.contain('jp-ObservableJSONWidget-commitButton');
  117. });
  118. });
  119. describe('#source', () => {
  120. it('should be the source of the metadata', () => {
  121. expect(editor.source).to.be(null);
  122. });
  123. it('should be settable', () => {
  124. let source = new ObservableJSON();
  125. editor.source = source;
  126. expect(editor.source).to.be(source);
  127. });
  128. it('should update the text area value', () => {
  129. let model = editor.model;
  130. expect(model.value.text).to.be('No data!');
  131. editor.source = new ObservableJSON();
  132. expect(model.value.text).to.be('{}');
  133. });
  134. });
  135. describe('#isDirty', () => {
  136. it('should test whether the editor value is dirty', () => {
  137. expect(editor.isDirty).to.be(false);
  138. Widget.attach(editor, document.body);
  139. editor.model.value.text = 'a';
  140. expect(editor.isDirty).to.be(true);
  141. });
  142. it('should be dirty if the value changes while focused', () => {
  143. editor.source = new ObservableJSON();
  144. Widget.attach(editor, document.body);
  145. editor.editor.focus();
  146. expect(editor.isDirty).to.be(false);
  147. editor.source.set('foo', 1);
  148. expect(editor.isDirty).to.be(true);
  149. });
  150. it('should not be set if not focused', () => {
  151. editor.source = new ObservableJSON();
  152. Widget.attach(editor, document.body);
  153. expect(editor.isDirty).to.be(false);
  154. editor.source.set('foo', 1);
  155. expect(editor.isDirty).to.be(false);
  156. });
  157. });
  158. context('model.value.changed', () => {
  159. it('should add the error flag if invalid JSON', () => {
  160. editor.model.value.text = 'foo';
  161. expect(editor.hasClass('jp-mod-error')).to.be(true);
  162. });
  163. it('should show the commit button if the value has changed', () => {
  164. editor.model.value.text = '{"foo": 2}';
  165. editor.model.value.text = '{"foo": 1}';
  166. expect(editor.commitButtonNode.hidden).to.be(false);
  167. });
  168. it('should not show the commit button if the value is invalid', () => {
  169. editor.model.value.text = 'foo';
  170. expect(editor.commitButtonNode.hidden).to.be(true);
  171. });
  172. it('should show the revert button if the value has changed', () => {
  173. editor.model.value.text = 'foo';
  174. expect(editor.revertButtonNode.hidden).to.be(false);
  175. });
  176. });
  177. describe('#handleEvent()', () => {
  178. beforeEach(() => {
  179. Widget.attach(editor, document.body);
  180. });
  181. context('blur', () => {
  182. it('should handle blur events on the host node', () => {
  183. editor.editor.focus();
  184. simulate(editor.editorHostNode, 'blur');
  185. expect(editor.events).to.contain('blur');
  186. });
  187. it('should revert to current data if there was no change', () => {
  188. editor.source = new ObservableJSON();
  189. editor.editor.focus();
  190. editor.source.set('foo', 1);
  191. let model = editor.model;
  192. expect(model.value.text).to.be('{}');
  193. simulate(editor.editorHostNode, 'blur');
  194. expect(model.value.text).to.be('{\n "foo": 1\n}');
  195. });
  196. it('should not revert to current data if there was a change', () => {
  197. editor.source = new ObservableJSON();
  198. editor.model.value.text = 'foo';
  199. editor.source.set('foo', 1);
  200. let model = editor.model;
  201. expect(model.value.text).to.be('foo');
  202. simulate(editor.editorHostNode, 'blur');
  203. expect(model.value.text).to.be('foo');
  204. expect(editor.commitButtonNode.hidden).to.be(true);
  205. expect(editor.revertButtonNode.hidden).to.be(false);
  206. });
  207. });
  208. context('click', () => {
  209. it('should handle click events on the revert button', () => {
  210. simulate(editor.revertButtonNode, 'click');
  211. expect(editor.events).to.contain('click');
  212. });
  213. it('should revert the current data', () => {
  214. editor.source = new ObservableJSON();
  215. editor.model.value.text = 'foo';
  216. simulate(editor.revertButtonNode, 'click');
  217. expect(editor.model.value.text).to.be('{}');
  218. });
  219. it('should handle programmatic changes', () => {
  220. editor.source = new ObservableJSON();
  221. editor.model.value.text = 'foo';
  222. editor.source.set('foo', 1);
  223. simulate(editor.revertButtonNode, 'click');
  224. expect(editor.model.value.text).to.be('{\n "foo": 1\n}');
  225. });
  226. it('should handle click events on the commit button', () => {
  227. simulate(editor.commitButtonNode, 'click');
  228. expect(editor.events).to.contain('click');
  229. });
  230. it('should bail if it is not valid JSON', () => {
  231. editor.source = new ObservableJSON();
  232. editor.model.value.text = 'foo';
  233. editor.source.set('foo', 1);
  234. simulate(editor.commitButtonNode, 'click');
  235. expect(editor.model.value.text).to.be('foo');
  236. });
  237. it('should override a key that was set programmatically', () => {
  238. editor.source = new ObservableJSON();
  239. editor.model.value.text = '{"foo": 2}';
  240. editor.source.set('foo', 1);
  241. simulate(editor.commitButtonNode, 'click');
  242. expect(editor.model.value.text).to.be('{\n "foo": 2\n}');
  243. });
  244. it('should allow a programmatic key to update', () => {
  245. editor.source = new ObservableJSON();
  246. editor.source.set('foo', 1);
  247. editor.source.set('bar', 1);
  248. editor.model.value.text = '{"foo":1, "bar": 2}';
  249. editor.source.set('foo', 2);
  250. simulate(editor.commitButtonNode, 'click');
  251. let expected = '{\n "foo": 2,\n "bar": 2\n}';
  252. expect(editor.model.value.text).to.be(expected);
  253. });
  254. it('should allow a key to be added by the user', () => {
  255. editor.source = new ObservableJSON();
  256. editor.source.set('foo', 1);
  257. editor.source.set('bar', 1);
  258. editor.model.value.text = '{"foo":1, "bar": 2, "baz": 3}';
  259. editor.source.set('foo', 2);
  260. simulate(editor.commitButtonNode, 'click');
  261. let value = '{\n "foo": 2,\n "bar": 2,\n "baz": 3\n}';
  262. expect(editor.model.value.text).to.be(value);
  263. });
  264. it('should allow a key to be removed by the user', () => {
  265. editor.source = new ObservableJSON();
  266. editor.source.set('foo', 1);
  267. editor.source.set('bar', 1);
  268. editor.model.value.text = '{"foo": 1}';
  269. simulate(editor.commitButtonNode, 'click');
  270. expect(editor.model.value.text).to.be('{\n "foo": 1\n}');
  271. });
  272. it('should allow a key to be removed programmatically that was not set by the user', () => {
  273. editor.source = new ObservableJSON();
  274. editor.source.set('foo', 1);
  275. editor.source.set('bar', 1);
  276. editor.model.value.text = '{"foo": 1, "bar": 3}';
  277. editor.source.set('foo', void 0);
  278. simulate(editor.commitButtonNode, 'click');
  279. expect(editor.model.value.text).to.be('{\n "bar": 3\n}');
  280. });
  281. it('should keep a key that was removed programmatically that was changed by the user', () => {
  282. editor.source = new ObservableJSON();
  283. editor.source.set('foo', 1);
  284. editor.source.set('bar', 1);
  285. editor.model.value.text = '{"foo": 2, "bar": 3}';
  286. editor.source.set('foo', void 0);
  287. simulate(editor.commitButtonNode, 'click');
  288. let expected = '{\n "foo": 2,\n "bar": 3\n}';
  289. expect(editor.model.value.text).to.be(expected);
  290. });
  291. });
  292. });
  293. describe('#onAfterAttach()', () => {
  294. it('should add event listeners', () => {
  295. Widget.attach(editor, document.body);
  296. expect(editor.methods).to.contain('onAfterAttach');
  297. editor.editor.focus();
  298. simulate(editor.editorHostNode, 'blur');
  299. simulate(editor.revertButtonNode, 'click');
  300. simulate(editor.commitButtonNode, 'click');
  301. expect(editor.events).to.eql(['blur', 'click', 'click']);
  302. });
  303. });
  304. describe('#onBeforeDetach()', () => {
  305. it('should remove event listeners', () => {
  306. Widget.attach(editor, document.body);
  307. Widget.detach(editor);
  308. expect(editor.methods).to.contain('onBeforeDetach');
  309. editor.editor.focus();
  310. simulate(editor.editorHostNode, 'blur');
  311. simulate(editor.revertButtonNode, 'click');
  312. simulate(editor.commitButtonNode, 'click');
  313. expect(editor.events).to.eql([]);
  314. });
  315. });
  316. context('#source.changed', () => {
  317. it('should update the value', () => {
  318. editor.source = new ObservableJSON();
  319. editor.source.set('foo', 1);
  320. expect(editor.model.value.text).to.be('{\n "foo": 1\n}');
  321. });
  322. it('should bail if the input is dirty', () => {
  323. Widget.attach(editor, document.body);
  324. editor.source = new ObservableJSON();
  325. editor.model.value.text = 'ha';
  326. editor.source.set('foo', 2);
  327. expect(editor.model.value.text).to.be('ha');
  328. });
  329. it('should bail if the input is focused', () => {
  330. Widget.attach(editor, document.body);
  331. editor.model.value.text = '{}';
  332. editor.source = new ObservableJSON();
  333. editor.editor.focus();
  334. editor.source.set('foo', 2);
  335. expect(editor.model.value.text).to.be('{}');
  336. });
  337. });
  338. });
  339. });