11// Copyright (c) Deepnote
22// Distributed under the terms of the Modified BSD License.
33
4+ import type { DeepnoteBlock } from '@deepnote/blocks' ;
45import { convertDeepnoteBlockToJupyterCell } from '../convert-deepnote-block-to-jupyter-cell' ;
5- import { DeepnoteBlock } from '@deepnote/blocks' ;
6-
7- jest . mock ( '@deepnote/blocks' , ( ) => ( {
8- createPythonCode : jest . fn ( ( block : any ) => block . source || 'print("test")' ) ,
9- createMarkdown : jest . fn ( ( block : any ) => block . source || '# Test' )
10- } ) ) ;
11-
12- jest . mock ( '../convert-deepnote-block-type-to-jupyter' , ( ) => ( {
13- convertDeepnoteBlockTypeToJupyter : jest . fn ( ( type : string ) => {
14- if (
15- [
16- 'code' ,
17- 'sql' ,
18- 'notebook-function' ,
19- 'big-number' ,
20- 'visualization' ,
21- 'input-text' ,
22- 'input-checkbox' ,
23- 'input-textarea' ,
24- 'input-file' ,
25- 'input-select' ,
26- 'input-date-range' ,
27- 'input-date' ,
28- 'input-slider'
29- ] . includes ( type )
30- ) {
31- return 'code' ;
32- }
33- return 'markdown' ;
34- } )
35- } ) ) ;
366
377describe ( 'convertDeepnoteBlockToJupyterCell' , ( ) => {
388 describe ( 'code cells' , ( ) => {
399 it ( 'should convert a basic code block to a Jupyter code cell' , ( ) => {
4010 const block : DeepnoteBlock = {
4111 id : 'block-1' ,
4212 type : 'code' ,
43- source : 'print("hello")' ,
44- metadata : { foo : 'bar' }
45- } as any ;
13+ content : 'print("hello")' ,
14+ metadata : { foo : 'bar' } ,
15+ sortingKey : '1'
16+ } ;
4617
4718 const result = convertDeepnoteBlockToJupyterCell ( block ) ;
4819
@@ -51,22 +22,17 @@ describe('convertDeepnoteBlockToJupyterCell', () => {
5122 expect ( result . source ) . toBe ( 'print("hello")' ) ;
5223 expect ( result . execution_count ) . toBeNull ( ) ;
5324 expect ( result . outputs ) . toEqual ( [ ] ) ;
54-
55- const { createPythonCode } = jest . requireMock ( '@deepnote/blocks' ) ;
56- expect ( createPythonCode ) . toHaveBeenCalledTimes ( 1 ) ;
57- expect ( createPythonCode ) . toHaveBeenCalledWith (
58- expect . objectContaining ( { id : 'block-1' } )
59- ) ;
6025 } ) ;
6126
6227 it ( 'should include execution count if present' , ( ) => {
6328 const block : DeepnoteBlock = {
6429 id : 'block-2' ,
6530 type : 'code' ,
66- source : 'x = 1' ,
31+ content : 'x = 1' ,
6732 metadata : { } ,
68- executionCount : 5
69- } as any ;
33+ executionCount : 5 ,
34+ sortingKey : '1'
35+ } ;
7036
7137 const result = convertDeepnoteBlockToJupyterCell ( block ) ;
7238
@@ -86,10 +52,11 @@ describe('convertDeepnoteBlockToJupyterCell', () => {
8652 const block : DeepnoteBlock = {
8753 id : 'block-3' ,
8854 type : 'code' ,
89- source : 'print("hello")' ,
55+ content : 'print("hello")' ,
9056 metadata : { } ,
91- outputs : blockOutputs
92- } as any ;
57+ outputs : blockOutputs ,
58+ sortingKey : '1'
59+ } ;
9360
9461 const result = convertDeepnoteBlockToJupyterCell ( block ) ;
9562
@@ -110,16 +77,17 @@ describe('convertDeepnoteBlockToJupyterCell', () => {
11077 const block : DeepnoteBlock = {
11178 id : 'block-4' ,
11279 type : 'code' ,
113- source : 'print("hello")' ,
80+ content : 'print("hello")' ,
11481 metadata : { } ,
115- outputs : blockOutputs
116- } as any ;
82+ outputs : blockOutputs ,
83+ sortingKey : '1'
84+ } ;
11785
11886 const result = convertDeepnoteBlockToJupyterCell ( block ) ;
11987
12088 expect ( result . cell_type ) . toBe ( 'code' ) ;
12189 expect ( result . outputs ) . toHaveLength ( 1 ) ;
122- const resultOutputs = result . outputs as any [ ] ;
90+ const resultOutputs = result . outputs as Array < { output_type : string ; name : string ; text : string } > ;
12391 expect ( resultOutputs [ 0 ] ) . not . toHaveProperty ( 'truncated' ) ;
12492 expect ( resultOutputs [ 0 ] ) . toEqual ( {
12593 output_type : 'stream' ,
@@ -147,16 +115,17 @@ describe('convertDeepnoteBlockToJupyterCell', () => {
147115 const block : DeepnoteBlock = {
148116 id : 'block-5' ,
149117 type : 'code' ,
150- source : 'print("test")' ,
118+ content : 'print("test")' ,
151119 metadata : { } ,
152- outputs : blockOutputs
153- } as any ;
120+ outputs : blockOutputs ,
121+ sortingKey : '1'
122+ } ;
154123
155124 const result = convertDeepnoteBlockToJupyterCell ( block ) ;
156125
157126 expect ( result . cell_type ) . toBe ( 'code' ) ;
158127 expect ( result . outputs ) . toHaveLength ( 2 ) ;
159- const resultOutputs = result . outputs as any [ ] ;
128+ const resultOutputs = result . outputs as Array < { output_type : string ; name : string ; text : string } > ;
160129 expect ( resultOutputs [ 0 ] ) . not . toHaveProperty ( 'truncated' ) ;
161130 expect ( resultOutputs [ 1 ] ) . not . toHaveProperty ( 'truncated' ) ;
162131 } ) ;
@@ -174,14 +143,15 @@ describe('convertDeepnoteBlockToJupyterCell', () => {
174143 const block : DeepnoteBlock = {
175144 id : 'block-6' ,
176145 type : 'code' ,
177- source : 'print("hello")' ,
146+ content : 'print("hello")' ,
178147 metadata : { test : 'value' } ,
179- outputs : blockOutputs
180- } as any ;
148+ outputs : blockOutputs ,
149+ sortingKey : '1'
150+ } ;
181151
182152 convertDeepnoteBlockToJupyterCell ( block ) ;
183153
184- expect ( block . outputs ! [ 0 ] ) . toHaveProperty ( 'truncated' ) ;
154+ expect ( block . outputs ?. [ 0 ] ) . toHaveProperty ( 'truncated' ) ;
185155 expect ( block . metadata ) . toEqual ( { test : 'value' } ) ;
186156 } ) ;
187157 } ) ;
@@ -191,30 +161,26 @@ describe('convertDeepnoteBlockToJupyterCell', () => {
191161 const block : DeepnoteBlock = {
192162 id : 'block-7' ,
193163 type : 'markdown' ,
194- source : '# Hello' ,
195- metadata : { foo : 'bar' }
196- } as any ;
164+ content : '# Hello' ,
165+ metadata : { foo : 'bar' } ,
166+ sortingKey : '1'
167+ } ;
197168
198169 const result = convertDeepnoteBlockToJupyterCell ( block ) ;
199170
200171 expect ( result . cell_type ) . toBe ( 'markdown' ) ;
201172 expect ( result . metadata ) . toEqual ( { } ) ;
202173 expect ( result . source ) . toBe ( '# Hello' ) ;
203-
204- const { createMarkdown } = jest . requireMock ( '@deepnote/blocks' ) ;
205- expect ( createMarkdown ) . toHaveBeenCalledTimes ( 1 ) ;
206- expect ( createMarkdown ) . toHaveBeenCalledWith (
207- expect . objectContaining ( { id : 'block-7' } )
208- ) ;
209174 } ) ;
210175
211176 it ( 'should convert text-cell-h1 to markdown cell' , ( ) => {
212177 const block : DeepnoteBlock = {
213178 id : 'block-8' ,
214179 type : 'text-cell-h1' ,
215- source : 'Heading 1' ,
216- metadata : { }
217- } as any ;
180+ content : 'Heading 1' ,
181+ metadata : { } ,
182+ sortingKey : '1'
183+ } ;
218184
219185 const result = convertDeepnoteBlockToJupyterCell ( block ) ;
220186
@@ -225,9 +191,10 @@ describe('convertDeepnoteBlockToJupyterCell', () => {
225191 const block : DeepnoteBlock = {
226192 id : 'block-9' ,
227193 type : 'image' ,
228- source : '' ,
229- metadata : { }
230- } as any ;
194+ content : '' ,
195+ metadata : { } ,
196+ sortingKey : '1'
197+ } ;
231198
232199 const result = convertDeepnoteBlockToJupyterCell ( block ) ;
233200
@@ -238,9 +205,10 @@ describe('convertDeepnoteBlockToJupyterCell', () => {
238205 const block : DeepnoteBlock = {
239206 id : 'block-10' ,
240207 type : 'markdown' ,
241- source : 'Text' ,
242- metadata : { deepnoteMetadata : 'should not appear' }
243- } as any ;
208+ content : 'Text' ,
209+ metadata : { deepnoteMetadata : 'should not appear' } ,
210+ sortingKey : '1'
211+ } ;
244212
245213 const result = convertDeepnoteBlockToJupyterCell ( block ) ;
246214
@@ -254,9 +222,10 @@ describe('convertDeepnoteBlockToJupyterCell', () => {
254222 const block : DeepnoteBlock = {
255223 id : 'block-11' ,
256224 type : 'sql' ,
257- source : 'SELECT * FROM table' ,
258- metadata : { }
259- } as any ;
225+ content : 'SELECT * FROM table' ,
226+ metadata : { } ,
227+ sortingKey : '1'
228+ } ;
260229
261230 const result = convertDeepnoteBlockToJupyterCell ( block ) ;
262231
@@ -267,39 +236,14 @@ describe('convertDeepnoteBlockToJupyterCell', () => {
267236 const block : DeepnoteBlock = {
268237 id : 'block-12' ,
269238 type : 'visualization' ,
270- source : 'chart_data' ,
271- metadata : { }
272- } as any ;
239+ content : 'chart_data' ,
240+ metadata : { } ,
241+ sortingKey : '1'
242+ } ;
273243
274244 const result = convertDeepnoteBlockToJupyterCell ( block ) ;
275245
276246 expect ( result . cell_type ) . toBe ( 'code' ) ;
277247 } ) ;
278-
279- it ( 'should convert input blocks to code cells' , ( ) => {
280- const inputTypes = [
281- 'input-text' ,
282- 'input-checkbox' ,
283- 'input-textarea' ,
284- 'input-file' ,
285- 'input-select' ,
286- 'input-date-range' ,
287- 'input-date' ,
288- 'input-slider'
289- ] ;
290-
291- inputTypes . forEach ( type => {
292- const block : DeepnoteBlock = {
293- id : `block-${ type } ` ,
294- type,
295- source : 'input_value' ,
296- metadata : { }
297- } as any ;
298-
299- const result = convertDeepnoteBlockToJupyterCell ( block ) ;
300-
301- expect ( result . cell_type ) . toBe ( 'code' ) ;
302- } ) ;
303- } ) ;
304248 } ) ;
305249} ) ;
0 commit comments