|
| 1 | +// Copyright (c) Jupyter Development Team. |
| 2 | +// Distributed under the terms of the Modified BSD License. |
| 3 | + |
| 4 | +import { IMapChange, YCodeCell, YNotebook } from '../src'; |
| 5 | + |
| 6 | +describe('@jupyter/ydoc', () => { |
| 7 | + describe('YCell standalone', () => { |
| 8 | + test('should set source', () => { |
| 9 | + const codeCell = YCodeCell.create(); |
| 10 | + codeCell.setSource('test'); |
| 11 | + expect(codeCell.getSource()).toBe('test'); |
| 12 | + codeCell.dispose(); |
| 13 | + }); |
| 14 | + |
| 15 | + test('should update source', () => { |
| 16 | + const codeCell = YCodeCell.create(); |
| 17 | + codeCell.setSource('test'); |
| 18 | + codeCell.updateSource(0, 0, 'hello'); |
| 19 | + expect(codeCell.getSource()).toBe('hellotest'); |
| 20 | + codeCell.dispose(); |
| 21 | + }); |
| 22 | + |
| 23 | + test('should get metadata', () => { |
| 24 | + const cell = YCodeCell.create(); |
| 25 | + const metadata = { |
| 26 | + collapsed: true, |
| 27 | + editable: false, |
| 28 | + name: 'cell-name' |
| 29 | + }; |
| 30 | + |
| 31 | + cell.setMetadata(metadata); |
| 32 | + |
| 33 | + expect(cell.metadata).toEqual({ |
| 34 | + ...metadata, |
| 35 | + jupyter: { outputs_hidden: true } |
| 36 | + }); |
| 37 | + cell.dispose(); |
| 38 | + }); |
| 39 | + |
| 40 | + test('should get all metadata', () => { |
| 41 | + const cell = YCodeCell.create(); |
| 42 | + const metadata = { |
| 43 | + jupyter: { outputs_hidden: true }, |
| 44 | + editable: false, |
| 45 | + name: 'cell-name' |
| 46 | + }; |
| 47 | + |
| 48 | + cell.setMetadata(metadata); |
| 49 | + |
| 50 | + expect(cell.getMetadata()).toEqual({ ...metadata, collapsed: true }); |
| 51 | + cell.dispose(); |
| 52 | + }); |
| 53 | + |
| 54 | + test('should get one metadata', () => { |
| 55 | + const cell = YCodeCell.create(); |
| 56 | + const metadata = { |
| 57 | + collapsed: true, |
| 58 | + editable: false, |
| 59 | + name: 'cell-name' |
| 60 | + }; |
| 61 | + |
| 62 | + cell.setMetadata(metadata); |
| 63 | + |
| 64 | + expect(cell.getMetadata('editable')).toEqual(metadata.editable); |
| 65 | + cell.dispose(); |
| 66 | + }); |
| 67 | + |
| 68 | + it.each([null, undefined, 1, true, 'string', { a: 1 }, [1, 2]])( |
| 69 | + 'should get single metadata %s', |
| 70 | + value => { |
| 71 | + const cell = YCodeCell.create(); |
| 72 | + const metadata = { |
| 73 | + collapsed: true, |
| 74 | + editable: false, |
| 75 | + name: 'cell-name', |
| 76 | + test: value |
| 77 | + }; |
| 78 | + |
| 79 | + cell.setMetadata(metadata); |
| 80 | + |
| 81 | + expect(cell.getMetadata('test')).toEqual(value); |
| 82 | + cell.dispose(); |
| 83 | + } |
| 84 | + ); |
| 85 | + |
| 86 | + test('should set one metadata', () => { |
| 87 | + const cell = YCodeCell.create(); |
| 88 | + const metadata = { |
| 89 | + collapsed: true, |
| 90 | + editable: false, |
| 91 | + name: 'cell-name' |
| 92 | + }; |
| 93 | + |
| 94 | + cell.setMetadata(metadata); |
| 95 | + cell.setMetadata('test', 'banana'); |
| 96 | + |
| 97 | + expect(cell.getMetadata('test')).toEqual('banana'); |
| 98 | + cell.dispose(); |
| 99 | + }); |
| 100 | + |
| 101 | + test('should emit all metadata changes', () => { |
| 102 | + const notebook = YNotebook.create(); |
| 103 | + |
| 104 | + const metadata = { |
| 105 | + collapsed: true, |
| 106 | + editable: false, |
| 107 | + name: 'cell-name' |
| 108 | + }; |
| 109 | + |
| 110 | + const changes: IMapChange[] = []; |
| 111 | + notebook.metadataChanged.connect((_, c) => { |
| 112 | + changes.push(c); |
| 113 | + }); |
| 114 | + notebook.metadata = metadata; |
| 115 | + |
| 116 | + expect(changes).toHaveLength(3); |
| 117 | + expect(changes).toEqual([ |
| 118 | + { |
| 119 | + type: 'add', |
| 120 | + key: 'collapsed', |
| 121 | + newValue: metadata.collapsed, |
| 122 | + oldValue: undefined |
| 123 | + }, |
| 124 | + { |
| 125 | + type: 'add', |
| 126 | + key: 'editable', |
| 127 | + newValue: metadata.editable, |
| 128 | + oldValue: undefined |
| 129 | + }, |
| 130 | + { |
| 131 | + type: 'add', |
| 132 | + key: 'name', |
| 133 | + newValue: metadata.name, |
| 134 | + oldValue: undefined |
| 135 | + } |
| 136 | + ]); |
| 137 | + |
| 138 | + notebook.dispose(); |
| 139 | + }); |
| 140 | + |
| 141 | + test('should emit a add metadata change', () => { |
| 142 | + const cell = YCodeCell.create(); |
| 143 | + const metadata = { |
| 144 | + collapsed: true, |
| 145 | + editable: false, |
| 146 | + name: 'cell-name' |
| 147 | + }; |
| 148 | + cell.metadata = metadata; |
| 149 | + |
| 150 | + const changes: IMapChange[] = []; |
| 151 | + cell.metadataChanged.connect((_, c) => { |
| 152 | + changes.push(c); |
| 153 | + }); |
| 154 | + cell.setMetadata('test', 'banana'); |
| 155 | + |
| 156 | + try { |
| 157 | + expect(changes).toHaveLength(1); |
| 158 | + expect(changes).toEqual([ |
| 159 | + { type: 'add', key: 'test', newValue: 'banana', oldValue: undefined } |
| 160 | + ]); |
| 161 | + } finally { |
| 162 | + cell.dispose(); |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + test('should emit a delete metadata change', () => { |
| 167 | + const cell = YCodeCell.create(); |
| 168 | + const metadata = { |
| 169 | + collapsed: true, |
| 170 | + editable: false, |
| 171 | + name: 'cell-name' |
| 172 | + }; |
| 173 | + cell.metadata = metadata; |
| 174 | + |
| 175 | + const changes: IMapChange[] = []; |
| 176 | + cell.setMetadata('test', 'banana'); |
| 177 | + |
| 178 | + cell.metadataChanged.connect((_, c) => { |
| 179 | + changes.push(c); |
| 180 | + }); |
| 181 | + cell.deleteMetadata('test'); |
| 182 | + |
| 183 | + try { |
| 184 | + expect(changes).toHaveLength(1); |
| 185 | + expect(changes).toEqual([ |
| 186 | + { |
| 187 | + type: 'remove', |
| 188 | + key: 'test', |
| 189 | + newValue: undefined, |
| 190 | + oldValue: 'banana' |
| 191 | + } |
| 192 | + ]); |
| 193 | + } finally { |
| 194 | + cell.dispose(); |
| 195 | + } |
| 196 | + }); |
| 197 | + |
| 198 | + test('should emit an update metadata change', () => { |
| 199 | + const cell = YCodeCell.create(); |
| 200 | + const metadata = { |
| 201 | + collapsed: true, |
| 202 | + editable: false, |
| 203 | + name: 'cell-name' |
| 204 | + }; |
| 205 | + cell.metadata = metadata; |
| 206 | + |
| 207 | + const changes: IMapChange[] = []; |
| 208 | + cell.setMetadata('test', 'banana'); |
| 209 | + |
| 210 | + cell.metadataChanged.connect((_, c) => { |
| 211 | + changes.push(c); |
| 212 | + }); |
| 213 | + cell.setMetadata('test', 'orange'); |
| 214 | + |
| 215 | + try { |
| 216 | + expect(changes).toHaveLength(1); |
| 217 | + expect(changes).toEqual([ |
| 218 | + { |
| 219 | + type: 'change', |
| 220 | + key: 'test', |
| 221 | + newValue: 'orange', |
| 222 | + oldValue: 'banana' |
| 223 | + } |
| 224 | + ]); |
| 225 | + } finally { |
| 226 | + cell.dispose(); |
| 227 | + } |
| 228 | + }); |
| 229 | + }); |
| 230 | +}); |
0 commit comments