Skip to content

Commit bdab09a

Browse files
committed
chore: improve test, avoid unnecessary mocks
1 parent 63c316f commit bdab09a

File tree

1 file changed

+60
-108
lines changed

1 file changed

+60
-108
lines changed
Lines changed: 60 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,19 @@
11
// Copyright (c) Deepnote
22
// Distributed under the terms of the Modified BSD License.
33

4+
import type { DeepnoteBlock } from '@deepnote/blocks';
45
import { 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

377
describe('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,21 @@ 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<{
91+
output_type: string;
92+
name: string;
93+
text: string;
94+
}>;
12395
expect(resultOutputs[0]).not.toHaveProperty('truncated');
12496
expect(resultOutputs[0]).toEqual({
12597
output_type: 'stream',
@@ -147,16 +119,21 @@ describe('convertDeepnoteBlockToJupyterCell', () => {
147119
const block: DeepnoteBlock = {
148120
id: 'block-5',
149121
type: 'code',
150-
source: 'print("test")',
122+
content: 'print("test")',
151123
metadata: {},
152-
outputs: blockOutputs
153-
} as any;
124+
outputs: blockOutputs,
125+
sortingKey: '1'
126+
};
154127

155128
const result = convertDeepnoteBlockToJupyterCell(block);
156129

157130
expect(result.cell_type).toBe('code');
158131
expect(result.outputs).toHaveLength(2);
159-
const resultOutputs = result.outputs as any[];
132+
const resultOutputs = result.outputs as Array<{
133+
output_type: string;
134+
name: string;
135+
text: string;
136+
}>;
160137
expect(resultOutputs[0]).not.toHaveProperty('truncated');
161138
expect(resultOutputs[1]).not.toHaveProperty('truncated');
162139
});
@@ -174,14 +151,15 @@ describe('convertDeepnoteBlockToJupyterCell', () => {
174151
const block: DeepnoteBlock = {
175152
id: 'block-6',
176153
type: 'code',
177-
source: 'print("hello")',
154+
content: 'print("hello")',
178155
metadata: { test: 'value' },
179-
outputs: blockOutputs
180-
} as any;
156+
outputs: blockOutputs,
157+
sortingKey: '1'
158+
};
181159

182160
convertDeepnoteBlockToJupyterCell(block);
183161

184-
expect(block.outputs![0]).toHaveProperty('truncated');
162+
expect(block.outputs?.[0]).toHaveProperty('truncated');
185163
expect(block.metadata).toEqual({ test: 'value' });
186164
});
187165
});
@@ -191,30 +169,26 @@ describe('convertDeepnoteBlockToJupyterCell', () => {
191169
const block: DeepnoteBlock = {
192170
id: 'block-7',
193171
type: 'markdown',
194-
source: '# Hello',
195-
metadata: { foo: 'bar' }
196-
} as any;
172+
content: '# Hello',
173+
metadata: { foo: 'bar' },
174+
sortingKey: '1'
175+
};
197176

198177
const result = convertDeepnoteBlockToJupyterCell(block);
199178

200179
expect(result.cell_type).toBe('markdown');
201180
expect(result.metadata).toEqual({});
202181
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-
);
209182
});
210183

211184
it('should convert text-cell-h1 to markdown cell', () => {
212185
const block: DeepnoteBlock = {
213186
id: 'block-8',
214187
type: 'text-cell-h1',
215-
source: 'Heading 1',
216-
metadata: {}
217-
} as any;
188+
content: 'Heading 1',
189+
metadata: {},
190+
sortingKey: '1'
191+
};
218192

219193
const result = convertDeepnoteBlockToJupyterCell(block);
220194

@@ -225,9 +199,10 @@ describe('convertDeepnoteBlockToJupyterCell', () => {
225199
const block: DeepnoteBlock = {
226200
id: 'block-9',
227201
type: 'image',
228-
source: '![alt](url)',
229-
metadata: {}
230-
} as any;
202+
content: '![alt](url)',
203+
metadata: {},
204+
sortingKey: '1'
205+
};
231206

232207
const result = convertDeepnoteBlockToJupyterCell(block);
233208

@@ -238,9 +213,10 @@ describe('convertDeepnoteBlockToJupyterCell', () => {
238213
const block: DeepnoteBlock = {
239214
id: 'block-10',
240215
type: 'markdown',
241-
source: 'Text',
242-
metadata: { deepnoteMetadata: 'should not appear' }
243-
} as any;
216+
content: 'Text',
217+
metadata: { deepnoteMetadata: 'should not appear' },
218+
sortingKey: '1'
219+
};
244220

245221
const result = convertDeepnoteBlockToJupyterCell(block);
246222

@@ -254,9 +230,10 @@ describe('convertDeepnoteBlockToJupyterCell', () => {
254230
const block: DeepnoteBlock = {
255231
id: 'block-11',
256232
type: 'sql',
257-
source: 'SELECT * FROM table',
258-
metadata: {}
259-
} as any;
233+
content: 'SELECT * FROM table',
234+
metadata: {},
235+
sortingKey: '1'
236+
};
260237

261238
const result = convertDeepnoteBlockToJupyterCell(block);
262239

@@ -267,39 +244,14 @@ describe('convertDeepnoteBlockToJupyterCell', () => {
267244
const block: DeepnoteBlock = {
268245
id: 'block-12',
269246
type: 'visualization',
270-
source: 'chart_data',
271-
metadata: {}
272-
} as any;
247+
content: 'chart_data',
248+
metadata: {},
249+
sortingKey: '1'
250+
};
273251

274252
const result = convertDeepnoteBlockToJupyterCell(block);
275253

276254
expect(result.cell_type).toBe('code');
277255
});
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-
});
304256
});
305257
});

0 commit comments

Comments
 (0)