|
| 1 | +import { expect, IJupyterLabPageFixture, test } from '@jupyterlab/galata'; |
| 2 | +import { NotebookPanel } from '@jupyterlab/notebook'; |
| 3 | + |
| 4 | +/** |
| 5 | + * The command to open the cell unified diff view. |
| 6 | + */ |
| 7 | +const UNIFIED_CELL_DIFF_COMMAND = 'jupyterlab-cell-diff:unified-cell-diff'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Setup a notebook cell with unified diff view. |
| 11 | + */ |
| 12 | +async function setupCellWithUnifiedDiff( |
| 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 | + }); |
| 27 | + }, |
| 28 | + { originalSource, newSource, command: UNIFIED_CELL_DIFF_COMMAND } |
| 29 | + ); |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Get the content of the first cell in the active notebook. |
| 34 | + */ |
| 35 | +async function getCellContent(page: IJupyterLabPageFixture): Promise<string> { |
| 36 | + return await page.evaluate(() => { |
| 37 | + const nbPanel = window.jupyterapp.shell.currentWidget as NotebookPanel; |
| 38 | + return nbPanel.content.widgets[0].model.sharedModel.getSource(); |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +test.describe('Unified Cell Diff Extension', () => { |
| 43 | + test.beforeEach(async ({ page }) => { |
| 44 | + await page.sidebar.close(); |
| 45 | + }); |
| 46 | + |
| 47 | + test('should show unified diff with action buttons', async ({ page }) => { |
| 48 | + const originalSource = 'print("Hello, World!")'; |
| 49 | + const newSource = 'print("Hello, JupyterLab!")\nprint("Testing diffs")'; |
| 50 | + |
| 51 | + await setupCellWithUnifiedDiff(page, originalSource, newSource); |
| 52 | + |
| 53 | + const acceptButton = page.getByRole('button', { name: 'Accept All' }); |
| 54 | + await expect(acceptButton).toBeVisible(); |
| 55 | + |
| 56 | + const rejectButton = page.getByRole('button', { name: 'Reject All' }); |
| 57 | + await expect(rejectButton).toBeVisible(); |
| 58 | + }); |
| 59 | + |
| 60 | + test('should accept all changes when the accept button is clicked', async ({ |
| 61 | + page |
| 62 | + }) => { |
| 63 | + const originalSource = 'x = 1'; |
| 64 | + const newSource = 'x = 2'; |
| 65 | + |
| 66 | + await setupCellWithUnifiedDiff(page, originalSource, newSource); |
| 67 | + |
| 68 | + const acceptButton = page.getByText('Accept All'); |
| 69 | + await acceptButton.click(); |
| 70 | + |
| 71 | + await expect(acceptButton).not.toBeVisible(); |
| 72 | + |
| 73 | + const cellContent = await getCellContent(page); |
| 74 | + expect(cellContent).toBe(newSource); |
| 75 | + }); |
| 76 | + |
| 77 | + test('should reject all changes when the reject button is clicked', async ({ |
| 78 | + page |
| 79 | + }) => { |
| 80 | + const originalSource = 'y = 10'; |
| 81 | + const newSource = 'y = 20'; |
| 82 | + |
| 83 | + await setupCellWithUnifiedDiff(page, originalSource, newSource); |
| 84 | + |
| 85 | + const rejectButton = page.getByText('Reject All'); |
| 86 | + await rejectButton.click(); |
| 87 | + |
| 88 | + await expect(rejectButton).not.toBeVisible(); |
| 89 | + |
| 90 | + const cellContent = await getCellContent(page); |
| 91 | + expect(cellContent).toBe(originalSource); |
| 92 | + }); |
| 93 | +}); |
0 commit comments