Skip to content

Commit 024da6b

Browse files
authored
test: added script for generating extension test templates (#731)
1 parent 8bf8502 commit 024da6b

File tree

3 files changed

+259
-0
lines changed

3 files changed

+259
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"prepublishOnly": "npm run lint && npm run build",
3434
"playwright:install": "playwright install chromium webkit --with-deps",
3535
"playwright": "playwright test --config=tests/playwright/playwright.config.ts",
36+
"playwright:generate": "node scripts/generate-playwright-test.js",
3637
"playwright:watch": "npm run playwright -- --ui",
3738
"playwright:update": "npm run playwright -- -u",
3839
"playwright:clear": "rm -rf ./tests/playwright/.cache",

scripts/generate-playwright-test.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/* eslint-disable no-console, import/no-extraneous-dependencies, no-undef */
2+
const fs = require('fs');
3+
const path = require('path');
4+
const process = require('process');
5+
6+
const ALIASES = {
7+
r: 'rewrite',
8+
};
9+
const TEMPLATE_DIR = 'tests/playwright/templates';
10+
const DEFAULT_OUTPUT_DIR = 'tests/visual-tests/playground';
11+
const templatePath = path.join(TEMPLATE_DIR, 'Extension.template.test.tsx');
12+
13+
if (!fs.existsSync(templatePath)) {
14+
console.error(`❌ Template file not found at ${templatePath}`);
15+
process.exit(1);
16+
}
17+
const template = fs.readFileSync(templatePath, 'utf-8');
18+
19+
/**
20+
* CLI arguments: <TestName> [--out <outputDir>] [--rewrite|-r]
21+
*/
22+
const args = parseAnyArgs(process.argv.slice(2), ALIASES);
23+
const name = args._[0];
24+
const outputDir = args.out || args['out'] || DEFAULT_OUTPUT_DIR;
25+
const shouldRewrite = Boolean(args.rewrite || args.r);
26+
27+
if (!name) {
28+
console.error(
29+
'❌ Please specify a test name. Usage: npm run playwright:generate <TestName> [--out <outputDir>] [--rewrite|-r]',
30+
);
31+
process.exit(1);
32+
}
33+
34+
const outputDirPath = path.join(process.cwd(), outputDir);
35+
if (!fs.existsSync(outputDirPath)) {
36+
fs.mkdirSync(outputDirPath, {recursive: true});
37+
}
38+
39+
const fileName = `${name}.test.tsx`;
40+
const filePath = path.join(outputDirPath, fileName);
41+
const content = template.replace(/%%name%%/g, name);
42+
43+
if (fs.existsSync(filePath) && !shouldRewrite) {
44+
console.error(`❌ File ${fileName} already exists. Use --rewrite to overwrite.`);
45+
process.exit(1);
46+
}
47+
48+
fs.writeFileSync(filePath, content);
49+
console.log(`✅ Successfully created file ${fileName}`);
50+
process.exit(0);
51+
52+
/**
53+
* Parses CLI arguments into an options object.
54+
*/
55+
function parseAnyArgs(args, aliases = {}) {
56+
const result = {_: []};
57+
for (let i = 0; i < args.length; i++) {
58+
const arg = args[i];
59+
60+
if (arg.startsWith('--') && arg.includes('=')) {
61+
const [key, value] = arg.slice(2).split('=');
62+
result[key] = value;
63+
} else if (arg.startsWith('--')) {
64+
const key = arg.slice(2);
65+
const next = args[i + 1];
66+
if (next && !next.startsWith('-')) {
67+
result[key] = next;
68+
i++;
69+
} else {
70+
result[key] = true;
71+
}
72+
} else if (arg.startsWith('-') && arg.length > 1) {
73+
const flags = arg.slice(1);
74+
if (flags.length > 1) {
75+
for (const c of flags) {
76+
const key = aliases[c] || c;
77+
result[key] = true;
78+
}
79+
} else {
80+
const c = flags;
81+
const key = aliases[c] || c;
82+
const next = args[i + 1];
83+
if (next && !next.startsWith('-')) {
84+
result[key] = next;
85+
i++;
86+
} else {
87+
result[key] = true;
88+
}
89+
}
90+
} else {
91+
result._.push(arg);
92+
}
93+
}
94+
return result;
95+
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/* eslint-disable */
2+
// @ts-nocheck
3+
/* TODO: delete eslint-disable and @ts-nocheck */
4+
import dd from 'ts-dedent';
5+
6+
import {expect, test} from 'playwright/core';
7+
8+
import {Playground} from './Playground.helpers';
9+
10+
test.describe('%%name%%', () => {
11+
test.beforeEach(async ({mount}) => {
12+
const initialMarkup = dd`
13+
/* TODO: add initialMarkup */
14+
`;
15+
16+
await mount(<Playground initial={initialMarkup} />);
17+
});
18+
19+
test.describe('insert', () => {
20+
test.skip(' should insert via toolbar @wysiwyg', async ({editor, wait}) => {
21+
/* TODO: unskip */
22+
await editor.switchMode('wysiwyg');
23+
await editor.clearContent();
24+
25+
await editor.clickToolbarButton('/* TODO: extension name */');
26+
await wait.timeout();
27+
28+
/* TODO: write test */
29+
});
30+
31+
test.skip('should insert via command menu @wysiwyg', async ({page, editor, wait}) => {
32+
/* TODO: unskip */
33+
await editor.switchPreview('hidden');
34+
await editor.switchMode('wysiwyg');
35+
await editor.clearContent();
36+
37+
await editor.pressSequentially('/* TODO: write sequentially */');
38+
await expect(page.getByTestId('g-md-command-menu')).toBeVisible();
39+
40+
const menuItem = editor.getByTextInCommandMenu('/* TODO: write name */').first();
41+
await wait.visible(menuItem);
42+
43+
await menuItem.click();
44+
45+
await expect(
46+
editor.getBySelectorInContenteditable('/* TODO: write selector */'),
47+
).toBeVisible();
48+
49+
/* TODO: write test */
50+
});
51+
52+
test.skip('should insert via input rule @wysiwyg', async ({editor, wait}) => {
53+
/* TODO: unskip */
54+
await editor.switchMode('wysiwyg');
55+
await editor.inputRule('/* TODO: input rule */');
56+
await wait.timeout();
57+
58+
/* TODO: write test */
59+
});
60+
61+
test.skip('should insert via keyboard shortcut @wysiwyg', async ({editor, wait}) => {
62+
/* TODO: unskip */
63+
await editor.switchMode('wysiwyg');
64+
await editor.clearContent();
65+
await editor.press('/* TODO: add keyboard shortcut */');
66+
await wait.timeout();
67+
68+
/* TODO: write test */
69+
});
70+
71+
test.skip('should insert via pasted HTML @wysiwyg', async ({editor, wait}) => {
72+
/* TODO: unskip */
73+
await editor.switchMode('wysiwyg');
74+
await editor.clearContent();
75+
76+
const html = '/* TODO: add html content */'; // TODO
77+
await editor.paste(html);
78+
await wait.timeout();
79+
80+
/* TODO: write test */
81+
});
82+
83+
test.skip('should insert via toolbar @markup', async ({editor, wait}) => {
84+
/* TODO: unskip */
85+
await editor.switchMode('markup');
86+
await editor.clickToolbarButton('/* TODO: extension name */');
87+
await wait.timeout();
88+
89+
/* TODO: write test */
90+
});
91+
92+
test.skip('should insert via command menu @markup', async ({
93+
page,
94+
editor,
95+
actions,
96+
wait,
97+
}) => {
98+
/* TODO: unskip */
99+
await editor.switchMode('markup');
100+
await editor.clearContent();
101+
102+
await editor.pressSequentially('{%');
103+
await expect(page.getByText('/* TODO: extension name */')).toBeVisible();
104+
await wait.timeout(300);
105+
106+
await actions.pressFocused('Enter');
107+
await wait.timeout(300);
108+
109+
await expect(
110+
editor.getByTextInContenteditable('/* TODO: extension markup */'),
111+
).toBeVisible();
112+
});
113+
});
114+
115+
test.describe('mode switch', () => {
116+
test.skip('should remain after mode switch @wysiwyg @markup', async ({editor, wait}) => {
117+
/* TODO: unskip */
118+
const markup = '/* TODO: add markup\n */';
119+
120+
await editor.switchMode('markup');
121+
await editor.fill(markup);
122+
await wait.timeout();
123+
124+
await editor.switchMode('wysiwyg');
125+
126+
/* TODO: write test */
127+
128+
await editor.switchMode('markup');
129+
130+
/* TODO: write test */
131+
});
132+
});
133+
134+
test.describe('interaction', () => {
135+
test.skip('should edit block via context menu @wysiwyg', async ({editor}) => {
136+
/* TODO: unskip */
137+
await editor.switchMode('wysiwyg');
138+
await editor.clearContent();
139+
140+
/* TODO: write test */
141+
});
142+
143+
test.skip('should delete block via context menu @wysiwyg', async ({editor}) => {
144+
/* TODO: unskip */
145+
await editor.switchMode('wysiwyg');
146+
await editor.clearContent();
147+
148+
/* TODO: write test */
149+
});
150+
151+
test.skip('should delete block via remove button @wysiwyg', async ({editor}) => {
152+
/* TODO: unskip */
153+
await editor.switchMode('wysiwyg');
154+
await editor.clearContent();
155+
156+
/* TODO: write test */
157+
});
158+
});
159+
160+
test.describe('specific', () => {
161+
/* TODO: implement extension-specific tests */
162+
});
163+
});

0 commit comments

Comments
 (0)