Debian Codegraph @

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

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