Skip to content

Commit 977a463

Browse files
committed
add some tests
1 parent 9d34580 commit 977a463

File tree

3 files changed

+111
-13
lines changed

3 files changed

+111
-13
lines changed

src/widget.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export abstract class BaseDiffWidget extends Widget {
131131
private _createButtons(footer: any): void {
132132
this._toggleButton = new ToolbarButton({
133133
label: this._trans.__('Compare changes'),
134+
tooltip: this._trans.__('Compare changes'),
134135
enabled: true,
135136
className: 'jp-DiffView-toggle',
136137
onClick: () => {
Lines changed: 108 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,117 @@
1-
import { expect, test } from '@jupyterlab/galata';
1+
import { expect, IJupyterLabPageFixture, test } from '@jupyterlab/galata';
2+
import { NotebookPanel } from '@jupyterlab/notebook';
23

34
/**
4-
* Don't load JupyterLab webpage before running the tests.
5-
* This is required to ensure we capture all log messages.
5+
* The command to open the cell split diff view.
66
*/
7-
test.use({ autoGoto: false });
7+
const SPLIT_CELL_DIFF_COMMAND = 'jupyterlab-cell-diff:show-codemirror';
88

9-
test('should emit an activation console message', async ({ page }) => {
10-
const logs: string[] = [];
9+
/**
10+
* Setup a notebook cell with diff view.
11+
*/
12+
async function setupCellWithDiff(
13+
page: IJupyterLabPageFixture,
14+
originalSource: string,
15+
newSource: string
16+
) {
17+
await page.notebook.createNew();
18+
await page.notebook.setCell(0, 'code', originalSource);
19+
20+
await page.evaluate(
21+
async ({ originalSource, newSource, command }) => {
22+
await window.jupyterapp.commands.execute(command, {
23+
originalSource,
24+
newSource,
25+
showActionButtons: true,
26+
openDiff: true
27+
});
28+
},
29+
{ originalSource, newSource, command: SPLIT_CELL_DIFF_COMMAND }
30+
);
31+
32+
return page.locator('.cm-mergeView');
33+
}
34+
35+
/**
36+
* Get the content of the first cell in the active notebook.
37+
*/
38+
async function getCellContent(page: IJupyterLabPageFixture): Promise<string> {
39+
return await page.evaluate(() => {
40+
const nbPanel = window.jupyterapp.shell.currentWidget as NotebookPanel;
41+
return nbPanel.content.widgets[0].model.sharedModel.getSource();
42+
});
43+
}
1144

12-
page.on('console', message => {
13-
logs.push(message.text());
45+
test.describe('Cell Diff Extension', () => {
46+
test.beforeEach(async ({ page }) => {
47+
await page.sidebar.close();
1448
});
1549

16-
await page.goto();
50+
test('should show diff with action buttons', async ({ page }) => {
51+
const originalSource = 'print("Hello, World!")';
52+
const newSource = 'print("Hello, JupyterLab!")\nprint("Testing diffs")';
53+
54+
const diffWidget = await setupCellWithDiff(page, originalSource, newSource);
55+
await expect(diffWidget).toBeVisible();
56+
57+
const toggleButton = page.getByRole('button', { name: 'Compare changes' });
58+
await expect(toggleButton).toBeVisible();
59+
60+
const acceptButton = page.getByRole('button', { name: 'Accept Changes' });
61+
await expect(acceptButton).toBeVisible();
62+
63+
const rejectButton = page.getByRole('button', { name: 'Reject Changes' });
64+
await expect(rejectButton).toBeVisible();
65+
});
66+
67+
test('should accept changes when accept button is clicked', async ({
68+
page
69+
}) => {
70+
const originalSource = 'x = 1';
71+
const newSource = 'x = 2';
72+
73+
const diffWidget = await setupCellWithDiff(page, originalSource, newSource);
74+
await expect(diffWidget).toBeVisible();
1775

18-
// placeholder for now
19-
expect(true).toBe(true);
76+
const acceptButton = page.getByRole('button', { name: 'Accept Changes' });
77+
await acceptButton.click();
78+
79+
const cellContent = await getCellContent(page);
80+
expect(cellContent).toBe(newSource);
81+
});
82+
83+
test('should reject changes when reject button is clicked', async ({
84+
page
85+
}) => {
86+
const originalSource = 'y = 10';
87+
const newSource = 'y = 20';
88+
89+
const diffWidget = await setupCellWithDiff(page, originalSource, newSource);
90+
await expect(diffWidget).toBeVisible();
91+
92+
const rejectButton = page.getByRole('button', { name: 'Reject Changes' });
93+
await rejectButton.click();
94+
95+
const cellContent = await getCellContent(page);
96+
expect(cellContent).toBe(originalSource);
97+
});
98+
99+
test('should toggle diff visibility', async ({ page }) => {
100+
const originalSource = 'z = 100';
101+
const newSource = 'z = 200';
102+
103+
const diffWidget = await setupCellWithDiff(page, originalSource, newSource);
104+
await expect(diffWidget).toBeVisible();
105+
106+
const toggleButton = page
107+
.getByRole('button', { name: 'Compare changes' })
108+
.first();
109+
await toggleButton.click({ force: true });
110+
111+
await expect(diffWidget).toBeHidden();
112+
113+
await toggleButton.click({ force: true });
114+
115+
await expect(diffWidget).toBeVisible();
116+
});
20117
});

ui-tests/yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3438,9 +3438,9 @@ __metadata:
34383438
languageName: node
34393439
linkType: hard
34403440

3441-
"jupyterlite-ai-ui-tests@workspace:.":
3441+
"jupyterlab-diff-ui-tests@workspace:.":
34423442
version: 0.0.0-use.local
3443-
resolution: "jupyterlite-ai-ui-tests@workspace:."
3443+
resolution: "jupyterlab-diff-ui-tests@workspace:."
34443444
dependencies:
34453445
"@jupyterlab/galata": ^5.4.0
34463446
"@playwright/test": ^1.55.1

0 commit comments

Comments
 (0)