ymodels.spec.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { YCodeCell, YNotebook } from '../src';
  4. describe('@jupyterlab/shared-models', () => {
  5. describe('ynotebook', () => {
  6. it('should create a notebook', () => {
  7. const notebook = YNotebook.create();
  8. expect(notebook.cells.length).toBe(0);
  9. });
  10. });
  11. describe('ynotebook metadata', () => {
  12. it('should update metadata', () => {
  13. const notebook = YNotebook.create();
  14. const metadata = notebook.getMetadata();
  15. expect(metadata).toBeTruthy();
  16. metadata.orig_nbformat = 1;
  17. metadata.kernelspec = {
  18. display_name: 'python',
  19. name: 'python'
  20. };
  21. notebook.setMetadata(metadata);
  22. {
  23. const metadata = notebook.getMetadata();
  24. expect(metadata.kernelspec!.name).toBe('python');
  25. expect(metadata.orig_nbformat).toBe(1);
  26. }
  27. notebook.updateMetadata({
  28. orig_nbformat: 2
  29. });
  30. {
  31. const metadata = notebook.getMetadata();
  32. expect(metadata.kernelspec!.name).toBe('python');
  33. expect(metadata.orig_nbformat).toBe(2);
  34. }
  35. });
  36. });
  37. describe('ycell shared', () => {
  38. it('should insert a cell', () => {
  39. const notebook = YNotebook.create();
  40. const codeCell = YCodeCell.create();
  41. notebook.insertCell(0, codeCell);
  42. expect(notebook.cells.length).toBe(1);
  43. });
  44. it('should set cell source', () => {
  45. const notebook = YNotebook.create();
  46. const codeCell = YCodeCell.create();
  47. notebook.insertCell(0, codeCell);
  48. codeCell.setSource('test');
  49. expect(notebook.cells[0].getSource()).toBe('test');
  50. });
  51. it('should update source', () => {
  52. const notebook = YNotebook.create();
  53. const codeCell = YCodeCell.create();
  54. notebook.insertCell(0, codeCell);
  55. codeCell.setSource('test');
  56. codeCell.updateSource(0, 0, 'hello');
  57. expect(codeCell.getSource()).toBe('hellotest');
  58. });
  59. });
  60. describe('ycell standalone', () => {
  61. it('should not insert a standalone cell', () => {
  62. const notebook = YNotebook.create();
  63. const codeCell = YCodeCell.createStandalone();
  64. let failed = false;
  65. try {
  66. notebook.insertCell(0, codeCell);
  67. } catch (error) {
  68. failed = true;
  69. }
  70. expect(failed).toBe(true);
  71. });
  72. it('should set source', () => {
  73. const codeCell = YCodeCell.createStandalone();
  74. codeCell.setSource('test');
  75. expect(codeCell.getSource()).toBe('test');
  76. });
  77. it('should update source', () => {
  78. const codeCell = YCodeCell.createStandalone();
  79. codeCell.setSource('test');
  80. codeCell.updateSource(0, 0, 'hello');
  81. expect(codeCell.getSource()).toBe('hellotest');
  82. });
  83. });
  84. });