|
| 1 | +import {test, expect} from '@playwright/test'; |
| 2 | +import {openApp} from '../../tests/shared/utils'; |
| 3 | +import {SessionMode} from '../src/store/sessionMode'; |
| 4 | +import {checkCodeEditorForText, forceCodeEditorText} from '../../tests/shared/utilsCodeEditor'; |
| 5 | + |
| 6 | +test.use({ |
| 7 | + permissions: ['clipboard-read', 'clipboard-write'], |
| 8 | +}); |
| 9 | + |
| 10 | +async function readClipboard(page: import('@playwright/test').Page): Promise<string> { |
| 11 | + return await page.evaluate(() => navigator.clipboard.readText()); |
| 12 | +} |
| 13 | + |
| 14 | +test('Copy to clipboard works when data in editor is valid', async ({page}) => { |
| 15 | + await openApp(page, 'settings_testpanel.json', 'data_minimal.json', 'schema_minimal.schema.json'); |
| 16 | + |
| 17 | + // wait for the data to load |
| 18 | + await checkCodeEditorForText(page, '"name": "Alex"', SessionMode.DataEditor); |
| 19 | + |
| 20 | + // pre-clear the clipboard |
| 21 | + await page.evaluate(() => navigator.clipboard.writeText('')); |
| 22 | + |
| 23 | + await page.getByTestId(`copy-to-clipboard-${SessionMode.DataEditor}`).click(); |
| 24 | + |
| 25 | + const clipboardText = await readClipboard(page); |
| 26 | + expect(JSON.parse(clipboardText)).toEqual({name: 'Alex', age: 25}); |
| 27 | +}); |
| 28 | + |
| 29 | +test('Copy to clipboard works when data in editor is unparsable', async ({page}) => { |
| 30 | + await openApp(page, 'settings_testpanel.json', 'data_minimal.json', 'schema_minimal.schema.json'); |
| 31 | + |
| 32 | + // wait for the data to load |
| 33 | + await checkCodeEditorForText(page, '"name": "Alex"', SessionMode.DataEditor); |
| 34 | + |
| 35 | + // write invalid JSON (missing closing brace) into the text editor |
| 36 | + const invalidJson = '{ "name": "Alex", "age": 25'; |
| 37 | + await forceCodeEditorText(page, invalidJson, SessionMode.DataEditor); |
| 38 | + |
| 39 | + // pre-clear the clipboard |
| 40 | + await page.evaluate(() => navigator.clipboard.writeText('')); |
| 41 | + |
| 42 | + await page.getByTestId(`copy-to-clipboard-${SessionMode.DataEditor}`).click(); |
| 43 | + |
| 44 | + const clipboardText = await readClipboard(page); |
| 45 | + // the invalid string should be on the clipboard (verbatim what's in the editor) |
| 46 | + expect(clipboardText).toContain('"name": "Alex"'); |
| 47 | + expect(clipboardText).toContain('"age": 25'); |
| 48 | + // and it should be unparsable |
| 49 | + expect(() => JSON.parse(clipboardText)).toThrow(); |
| 50 | +}); |
| 51 | + |
| 52 | +test('Copy to clipboard works when data in editor is empty (unparsable empty string)', async ({page}) => { |
| 53 | + await openApp(page, 'settings_testpanel.json', 'data_minimal.json', 'schema_minimal.schema.json'); |
| 54 | + |
| 55 | + // wait for the data to load |
| 56 | + await checkCodeEditorForText(page, '"name": "Alex"', SessionMode.DataEditor); |
| 57 | + |
| 58 | + // clear the editor (becomes empty string, which is unparsable as JSON) |
| 59 | + await forceCodeEditorText(page, '', SessionMode.DataEditor); |
| 60 | + |
| 61 | + // pre-clear the clipboard |
| 62 | + await page.evaluate(() => navigator.clipboard.writeText('PRECLEAR')); |
| 63 | + |
| 64 | + await page.getByTestId(`copy-to-clipboard-${SessionMode.DataEditor}`).click(); |
| 65 | + |
| 66 | + const clipboardText = await readClipboard(page); |
| 67 | + // the editor has empty/near-empty content - clipboard should reflect that, not the old data |
| 68 | + expect(clipboardText).not.toContain('Alex'); |
| 69 | + expect(clipboardText).not.toBe('PRECLEAR'); |
| 70 | +}); |
0 commit comments