|
1 | | -import { expect, test } from '@jupyterlab/galata'; |
| 1 | +import { expect, IJupyterLabPageFixture, test } from '@jupyterlab/galata'; |
| 2 | +import { NotebookPanel } from '@jupyterlab/notebook'; |
2 | 3 |
|
3 | 4 | /** |
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. |
6 | 6 | */ |
7 | | -test.use({ autoGoto: false }); |
| 7 | +const SPLIT_CELL_DIFF_COMMAND = 'jupyterlab-cell-diff:show-codemirror'; |
8 | 8 |
|
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 | +} |
11 | 44 |
|
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(); |
14 | 48 | }); |
15 | 49 |
|
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(); |
17 | 75 |
|
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 | + }); |
20 | 117 | }); |
0 commit comments