|
1 | 1 | // Copyright (c) Deepnote |
2 | 2 | // Distributed under the terms of the Modified BSD License. |
3 | 3 |
|
4 | | -import { NotebookPicker } from '../../src/components/NotebookPicker'; |
5 | | -import { framePromise } from '@jupyterlab/testing'; |
6 | | -import { NotebookPanel } from '@jupyterlab/notebook'; |
7 | | -import { INotebookModel } from '@jupyterlab/notebook'; |
8 | | -import { Widget } from '@lumino/widgets'; |
9 | | -import { Message } from '@lumino/messaging'; |
10 | | -import { simulate } from 'simulate-event'; |
11 | | - |
12 | | -describe('NotebookPicker', () => { |
13 | | - let panel: NotebookPanel; |
14 | | - let model: INotebookModel; |
15 | | - |
16 | | - beforeEach(async () => { |
17 | | - // Mock model + metadata |
18 | | - model = { |
19 | | - fromJSON: jest.fn(), |
20 | | - get cells() { |
21 | | - return []; |
22 | | - }, |
23 | | - dirty: true |
24 | | - } as any; |
25 | | - |
26 | | - panel = { |
27 | | - context: { |
28 | | - ready: Promise.resolve(), |
29 | | - model: { |
30 | | - getMetadata: jest.fn().mockReturnValue({ |
31 | | - notebooks: { |
32 | | - nb1: { id: 'nb1', name: 'nb1', cells: [{ source: 'code' }] }, |
33 | | - nb2: { id: 'nb2', name: 'nb2', cells: [] } |
34 | | - }, |
35 | | - notebook_names: ['nb1', 'nb2'] |
36 | | - }) |
37 | | - } |
38 | | - }, |
39 | | - model |
40 | | - } as any; |
41 | | - |
42 | | - // Attach to DOM |
43 | | - const widget = new NotebookPicker(panel); |
44 | | - // Override onAfterAttach to avoid errors from this.parent being null |
45 | | - (widget as any).onAfterAttach = jest.fn(); |
46 | | - Widget.attach(widget, document.body); |
47 | | - await framePromise(); |
48 | | - }); |
49 | | - |
50 | | - afterEach(() => { |
51 | | - document.body.innerHTML = ''; |
52 | | - jest.restoreAllMocks(); |
53 | | - }); |
54 | | - |
55 | | - it('should render a select element', async () => { |
56 | | - await framePromise(); // wait for rendering |
57 | | - const select = document.querySelector('select') as HTMLSelectElement; |
58 | | - expect(select).not.toBeNull(); |
59 | | - expect(select.options.length).toBe(2); |
60 | | - expect(select.options[0] && select.options[0].value).toBe('nb1'); |
61 | | - }); |
62 | | - |
63 | | - it('should call fromJSON when selecting a notebook', async () => { |
64 | | - const select = document.querySelector('select') as HTMLSelectElement; |
65 | | - simulate(select, 'change', { target: { value: 'nb2' } }); |
66 | | - await framePromise(); |
67 | | - expect(model.fromJSON).toHaveBeenCalledWith( |
68 | | - expect.objectContaining({ |
69 | | - cells: expect.any(Array), |
70 | | - metadata: expect.objectContaining({ |
71 | | - deepnote: expect.objectContaining({ |
72 | | - notebooks: expect.any(Object) |
73 | | - }) |
74 | | - }) |
75 | | - }) |
76 | | - ); |
77 | | - }); |
78 | | - |
79 | | - it('should not call fromJSON if selected notebook is invalid', async () => { |
80 | | - const getMetadata = panel.context.model.getMetadata as jest.Mock; |
81 | | - getMetadata.mockReturnValue({ notebooks: {}, notebook_names: [] }); |
82 | | - |
83 | | - const select = document.querySelector('select') as HTMLSelectElement; |
84 | | - simulate(select, 'change', { target: { value: 'nonexistent' } }); |
85 | | - await framePromise(); |
86 | | - expect(model.fromJSON).not.toHaveBeenCalled(); |
87 | | - }); |
88 | | - |
89 | | - it('should update UI after selection', async () => { |
90 | | - const select = document.querySelector('select') as HTMLSelectElement; |
91 | | - select.value = 'nb2'; |
92 | | - simulate(select, 'change'); |
93 | | - await framePromise(); |
94 | | - expect(select.value).toBe('nb2'); |
95 | | - }); |
96 | | - |
97 | | - it('should handle empty metadata gracefully', async () => { |
98 | | - const getMetadata = panel.context.model.getMetadata as jest.Mock; |
99 | | - getMetadata.mockReturnValue({ notebooks: {}, notebook_names: [] }); |
100 | | - |
101 | | - document.body.innerHTML = ''; |
102 | | - const widget = new NotebookPicker(panel); |
103 | | - // Override onAfterAttach to avoid errors from this.parent being null |
104 | | - (widget as any).onAfterAttach = jest.fn(); |
105 | | - Widget.attach(widget, document.body); |
106 | | - await framePromise(); |
107 | | - |
108 | | - const select = document.querySelector('select') as HTMLSelectElement; |
109 | | - expect(select.options.length).toBeGreaterThanOrEqual(1); |
110 | | - expect(select.options[0] && select.options[0].value).toBe('-'); |
111 | | - }); |
112 | | - |
113 | | - it('should handle context.ready rejection gracefully', async () => { |
114 | | - const consoleErrorSpy = jest |
115 | | - .spyOn(console, 'error') |
116 | | - .mockImplementation(() => {}); |
117 | | - const errorPanel = { |
118 | | - context: { |
119 | | - ready: Promise.reject(new Error('Failed to initialize')), |
120 | | - model: { |
121 | | - getMetadata: jest.fn() |
122 | | - } |
123 | | - }, |
124 | | - model |
125 | | - } as any; |
126 | | - |
127 | | - document.body.innerHTML = ''; |
128 | | - const widget = new NotebookPicker(errorPanel); |
129 | | - (widget as any).onAfterAttach = jest.fn(); |
130 | | - Widget.attach(widget, document.body); |
131 | | - await framePromise(); |
132 | | - |
133 | | - await new Promise(resolve => setTimeout(resolve, 10)); |
134 | | - |
135 | | - expect(consoleErrorSpy).toHaveBeenCalledWith( |
136 | | - 'Failed to initialize NotebookPicker:', |
137 | | - expect.any(Error) |
138 | | - ); |
139 | | - |
140 | | - consoleErrorSpy.mockRestore(); |
141 | | - }); |
142 | | - |
143 | | - it('should handle null model in handleChange', async () => { |
144 | | - const nullModelPanel = { |
145 | | - context: { |
146 | | - ready: Promise.resolve(), |
147 | | - model: { |
148 | | - getMetadata: jest.fn().mockReturnValue({ |
149 | | - notebooks: { |
150 | | - nb1: { id: 'nb1', name: 'nb1', cells: [] } |
151 | | - }, |
152 | | - notebook_names: ['nb1'] |
153 | | - }) |
154 | | - } |
155 | | - }, |
156 | | - model: null |
157 | | - } as any; |
158 | | - |
159 | | - document.body.innerHTML = ''; |
160 | | - const widget = new NotebookPicker(nullModelPanel); |
161 | | - (widget as any).onAfterAttach = jest.fn(); |
162 | | - Widget.attach(widget, document.body); |
163 | | - await framePromise(); |
164 | | - |
165 | | - const select = document.querySelector('select') as HTMLSelectElement; |
166 | | - simulate(select, 'change', { target: { value: 'nb1' } }); |
167 | | - await framePromise(); |
168 | | - }); |
169 | | - |
170 | | - it('should handle invalid metadata in handleChange', async () => { |
171 | | - const consoleErrorSpy = jest |
172 | | - .spyOn(console, 'error') |
173 | | - .mockImplementation(() => {}); |
174 | | - const getMetadata = panel.context.model.getMetadata as jest.Mock; |
175 | | - getMetadata.mockReturnValue({ invalid: 'metadata' }); |
176 | | - |
177 | | - const select = document.querySelector('select') as HTMLSelectElement; |
178 | | - simulate(select, 'change', { target: { value: 'nb1' } }); |
179 | | - await framePromise(); |
180 | | - |
181 | | - expect(consoleErrorSpy).toHaveBeenCalledWith( |
182 | | - 'Invalid deepnote metadata:', |
183 | | - expect.anything() |
184 | | - ); |
185 | | - expect(model.fromJSON).not.toHaveBeenCalled(); |
186 | | - |
187 | | - consoleErrorSpy.mockRestore(); |
188 | | - }); |
189 | | - |
190 | | - it('should handle onAfterAttach without parent', async () => { |
191 | | - document.body.innerHTML = ''; |
192 | | - const widget = new NotebookPicker(panel); |
193 | | - Widget.attach(widget, document.body); |
194 | | - await framePromise(); |
195 | | - |
196 | | - const onAfterAttachMethod = (widget as any).onAfterAttach.bind(widget); |
197 | | - onAfterAttachMethod({} as Message); |
198 | | - await new Promise(resolve => requestAnimationFrame(resolve)); |
199 | | - }); |
| 4 | +import { NotebookPicker } from "../../src/components/NotebookPicker"; |
| 5 | +import { framePromise } from "@jupyterlab/testing"; |
| 6 | +import { NotebookPanel } from "@jupyterlab/notebook"; |
| 7 | +import { INotebookModel } from "@jupyterlab/notebook"; |
| 8 | +import { Widget } from "@lumino/widgets"; |
| 9 | +import { simulate } from "simulate-event"; |
| 10 | + |
| 11 | +describe("NotebookPicker", () => { |
| 12 | + let panel: NotebookPanel; |
| 13 | + let model: INotebookModel; |
| 14 | + |
| 15 | + beforeEach(async () => { |
| 16 | + // Mock model + metadata |
| 17 | + model = { |
| 18 | + fromJSON: jest.fn(), |
| 19 | + get cells() { |
| 20 | + return []; |
| 21 | + }, |
| 22 | + dirty: true, |
| 23 | + } as any; |
| 24 | + |
| 25 | + panel = { |
| 26 | + context: { |
| 27 | + ready: Promise.resolve(), |
| 28 | + model: { |
| 29 | + getMetadata: jest.fn().mockReturnValue({ |
| 30 | + notebooks: { |
| 31 | + nb1: { id: "nb1", name: "nb1", cells: [{ source: "code" }] }, |
| 32 | + nb2: { id: "nb2", name: "nb2", cells: [] }, |
| 33 | + }, |
| 34 | + notebook_names: ["nb1", "nb2"], |
| 35 | + }), |
| 36 | + }, |
| 37 | + }, |
| 38 | + model, |
| 39 | + } as any; |
| 40 | + |
| 41 | + // Attach to DOM |
| 42 | + const widget = new NotebookPicker(panel); |
| 43 | + // Override onAfterAttach to avoid errors from this.parent being null |
| 44 | + (widget as any).onAfterAttach = jest.fn(); |
| 45 | + Widget.attach(widget, document.body); |
| 46 | + await framePromise(); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + document.body.innerHTML = ""; |
| 51 | + jest.restoreAllMocks(); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should render a select element", async () => { |
| 55 | + await framePromise(); // wait for rendering |
| 56 | + const select = document.querySelector("select") as HTMLSelectElement; |
| 57 | + expect(select).not.toBeNull(); |
| 58 | + expect(select.options.length).toBe(2); |
| 59 | + expect(select.options[0] && select.options[0].value).toBe("nb1"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should call fromJSON when selecting a notebook", async () => { |
| 63 | + const select = document.querySelector("select") as HTMLSelectElement; |
| 64 | + simulate(select, "change", { target: { value: "nb2" } }); |
| 65 | + await framePromise(); |
| 66 | + expect(model.fromJSON).toHaveBeenCalledWith( |
| 67 | + expect.objectContaining({ |
| 68 | + cells: expect.any(Array), |
| 69 | + metadata: expect.objectContaining({ |
| 70 | + deepnote: expect.objectContaining({ |
| 71 | + notebooks: expect.any(Object), |
| 72 | + }), |
| 73 | + }), |
| 74 | + }), |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should not call fromJSON if selected notebook is invalid", async () => { |
| 79 | + const getMetadata = panel.context.model.getMetadata as jest.Mock; |
| 80 | + getMetadata.mockReturnValue({ notebooks: {}, notebook_names: [] }); |
| 81 | + |
| 82 | + const select = document.querySelector("select") as HTMLSelectElement; |
| 83 | + simulate(select, "change", { target: { value: "nonexistent" } }); |
| 84 | + await framePromise(); |
| 85 | + expect(model.fromJSON).not.toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should update UI after selection", async () => { |
| 89 | + const select = document.querySelector("select") as HTMLSelectElement; |
| 90 | + select.value = "nb2"; |
| 91 | + simulate(select, "change"); |
| 92 | + await framePromise(); |
| 93 | + expect(select.value).toBe("nb2"); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should handle empty metadata gracefully", async () => { |
| 97 | + const getMetadata = panel.context.model.getMetadata as jest.Mock; |
| 98 | + getMetadata.mockReturnValue({ notebooks: {}, notebook_names: [] }); |
| 99 | + |
| 100 | + document.body.innerHTML = ""; |
| 101 | + const widget = new NotebookPicker(panel); |
| 102 | + // Override onAfterAttach to avoid errors from this.parent being null |
| 103 | + (widget as any).onAfterAttach = jest.fn(); |
| 104 | + Widget.attach(widget, document.body); |
| 105 | + await framePromise(); |
| 106 | + |
| 107 | + const select = document.querySelector("select") as HTMLSelectElement; |
| 108 | + expect(select.options.length).toBeGreaterThanOrEqual(1); |
| 109 | + expect(select.options[0] && select.options[0].value).toBe("-"); |
| 110 | + }); |
| 111 | + |
| 112 | + it("should handle null model in handleChange", async () => { |
| 113 | + const nullModelPanel = { |
| 114 | + context: { |
| 115 | + ready: Promise.resolve(), |
| 116 | + model: { |
| 117 | + getMetadata: jest.fn().mockReturnValue({ |
| 118 | + notebooks: { |
| 119 | + nb1: { id: "nb1", name: "nb1", cells: [] }, |
| 120 | + }, |
| 121 | + notebook_names: ["nb1"], |
| 122 | + }), |
| 123 | + }, |
| 124 | + }, |
| 125 | + model: null, |
| 126 | + } as any; |
| 127 | + |
| 128 | + document.body.innerHTML = ""; |
| 129 | + const widget = new NotebookPicker(nullModelPanel); |
| 130 | + (widget as any).onAfterAttach = jest.fn(); |
| 131 | + Widget.attach(widget, document.body); |
| 132 | + await framePromise(); |
| 133 | + |
| 134 | + const select = document.querySelector("select") as HTMLSelectElement; |
| 135 | + simulate(select, "change", { target: { value: "nb1" } }); |
| 136 | + await framePromise(); |
| 137 | + }); |
| 138 | + |
| 139 | + it("should handle invalid metadata in handleChange", async () => { |
| 140 | + const consoleErrorSpy = jest |
| 141 | + .spyOn(console, "error") |
| 142 | + .mockImplementation(() => {}); |
| 143 | + const getMetadata = panel.context.model.getMetadata as jest.Mock; |
| 144 | + getMetadata.mockReturnValue({ invalid: "metadata" }); |
| 145 | + |
| 146 | + const select = document.querySelector("select") as HTMLSelectElement; |
| 147 | + simulate(select, "change", { target: { value: "nb1" } }); |
| 148 | + await framePromise(); |
| 149 | + |
| 150 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 151 | + "Invalid deepnote metadata:", |
| 152 | + expect.anything(), |
| 153 | + ); |
| 154 | + expect(model.fromJSON).not.toHaveBeenCalled(); |
| 155 | + |
| 156 | + consoleErrorSpy.mockRestore(); |
| 157 | + }); |
200 | 158 | }); |
0 commit comments