Debian Codegraph @

ui-tests-ipw8/tests/ipydatagrid.test.ts

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3
4 import { expect, IJupyterLabPageFixture, test } from '@jupyterlab/galata';
5 import * as path from 'path';
6 const klaw = require('klaw-sync');
7
8
9 const filterUpdateNotebooks = item => {
10 const basename = path.basename(item.path);
11 return basename.includes('_update');
12 }
13
14 const testCellOutputs = async (page: IJupyterLabPageFixture, tmpPath: string, theme: 'JupyterLab Light' | 'JupyterLab Dark') => {
15 const paths = klaw(path.resolve(__dirname, './notebooks'), { filter: item => !filterUpdateNotebooks(item), nodir: true });
16 const notebooks = paths.map(item => path.basename(item.path));
17
18 const contextPrefix = theme == 'JupyterLab Light' ? 'light' : 'dark';
19 page.theme.setTheme(theme);
20
21 for (const notebook of notebooks) {
22 let results = [];
23
24 await page.notebook.openByPath(`${tmpPath}/${notebook}`);
25 await page.notebook.activate(notebook);
26
27 let numCellImages = 0;
28
29 const getCaptureImageName = (contextPrefix: string, notebook: string, id: number): string => {
30 return `${contextPrefix}-${notebook}-cell-${id}.png`;
31 };
32
33 await page.notebook.runCellByCell({
34 onAfterCellRun: async (cellIndex: number) => {
35 const cell = await page.notebook.getCellOutput(cellIndex);
36 if (cell) {
37 results.push(await cell.screenshot());
38 numCellImages++;
39 }
40 }
41 });
42
43 await page.notebook.save();
44
45 for (let c = 0; c < numCellImages; ++c) {
46 expect(results[c]).toMatchSnapshot(getCaptureImageName(contextPrefix, notebook, c));
47 }
48
49 await page.notebook.close(true);
50 }
51 }
52
53 const testPlotUpdates = async (page: IJupyterLabPageFixture, tmpPath: string, theme: 'JupyterLab Light' | 'JupyterLab Dark') => {
54 const paths = klaw(path.resolve(__dirname, './notebooks'), { filter: item => filterUpdateNotebooks(item), nodir: true });
55 const notebooks = paths.map(item => path.basename(item.path));
56
57 const contextPrefix = theme == 'JupyterLab Light' ? 'light' : 'dark';
58 page.theme.setTheme(theme);
59
60 for (const notebook of notebooks) {
61 let results = [];
62
63 await page.notebook.openByPath(`${tmpPath}/${notebook}`);
64 await page.notebook.activate(notebook);
65
66 const getCaptureImageName = (contextPrefix: string, notebook: string, id: number): string => {
67 return `${contextPrefix}-${notebook}-cell-${id}.png`;
68 };
69
70 let cellCount = 0;
71 await page.notebook.runCellByCell({
72 onAfterCellRun: async (cellIndex: number) => {
73 // Always get first cell output which must contain the plot
74 const cell = await page.notebook.getCellOutput(0);
75 if (cell) {
76 results.push(await cell.screenshot());
77 cellCount++;
78 }
79 }
80 });
81
82 await page.notebook.save();
83
84 for (let i = 0; i < cellCount; i++) {
85 expect(results[i]).toMatchSnapshot(getCaptureImageName(contextPrefix, notebook, i));
86 }
87
88 await page.notebook.close(true);
89 }
90 };
91
92 test.describe('ipydatagrid Visual Regression', () => {
93 test.beforeEach(async ({ page, tmpPath }) => {
94 page.on("console", (message) => {
95 console.log('CONSOLE MSG ---', message.text());
96 });
97
98 await page.contents.uploadDirectory(
99 path.resolve(__dirname, './notebooks'),
100 tmpPath
101 );
102 await page.filebrowser.openDirectory(tmpPath);
103 });
104
105 test('Light theme: Check ipydatagrid first renders', async ({
106 page,
107 tmpPath,
108 }) => {
109 await testCellOutputs(page, tmpPath, 'JupyterLab Light');
110 });
111
112 test('Dark theme: Check ipydatagrid first renders', async ({
113 page,
114 tmpPath,
115 }) => {
116 await testCellOutputs(page, tmpPath, 'JupyterLab Dark');
117 });
118
119 test('Light theme: Check ipydatagrid update plot properties', async ({
120 page,
121 tmpPath,
122 }) => {
123 await testPlotUpdates(page, tmpPath, 'JupyterLab Light');
124 });
125
126 test('Dark theme: Check ipydatagrid update plot properties', async ({
127 page,
128 tmpPath,
129 }) => {
130 await testPlotUpdates(page, tmpPath, 'JupyterLab Dark');
131 });
132 });