Skip to content

Commit 617c15a

Browse files
committed
increase test coverage
1 parent d870e7f commit 617c15a

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

tests/e2e/code-snippets-evaluation.spec.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,65 @@ test.describe('Code Snippets Evaluation', () => {
102102
await helper.expectElementCount('text=Hello World HTML snippet in header!', 1);
103103
});
104104

105+
test('HTML snippet works with shortcode in editor', async ({ page }) => {
106+
const snippetId = await createHtmlSnippetForEditor();
107+
108+
const pageUrl = await createPageWithShortcode(snippetId);
109+
110+
await verifyShortcodeRendersCorrectly(page, pageUrl);
111+
112+
async function createHtmlSnippetForEditor(): Promise<string> {
113+
await helper.createAndActivateSnippet({
114+
name: TEST_SNIPPET_NAME,
115+
code: "<div class='custom-snippet-content'><h3>Custom HTML Content</h3><p>This content was inserted via shortcode!</p></div>",
116+
type: 'HTML',
117+
location: 'IN_EDITOR'
118+
});
119+
120+
const currentUrl = page.url();
121+
const urlMatch = currentUrl.match(/[?&]id=(\d+)/);
122+
expect(urlMatch).toBeTruthy();
123+
return urlMatch![1];
124+
}
125+
126+
async function createPageWithShortcode(snippetId: string): Promise<string> {
127+
const { exec } = require('child_process');
128+
const util = require('util');
129+
const execAsync = util.promisify(exec);
130+
131+
const shortcode = `[code_snippet id=${snippetId} format name="${TEST_SNIPPET_NAME}"]`;
132+
const pageContent = `<p>Page content before shortcode.</p>
133+
134+
${shortcode}
135+
136+
<p>Page content after shortcode.</p>`;
137+
138+
try {
139+
const createPageCmd = `npx wp-env run cli wp post create --post_type=page --post_title="Test Page for Snippet Shortcode" --post_content='${pageContent}' --post_status=publish --porcelain`;
140+
const { stdout } = await execAsync(createPageCmd);
141+
const pageId = stdout.trim();
142+
143+
const getUrlCmd = `npx wp-env run cli wp post url ${pageId}`;
144+
const { stdout: pageUrl } = await execAsync(getUrlCmd);
145+
return pageUrl.trim();
146+
} catch (error) {
147+
console.error('Failed to create page via WP-CLI:', error);
148+
throw error;
149+
}
150+
}
151+
152+
async function verifyShortcodeRendersCorrectly(page: any, pageUrl: string): Promise<void> {
153+
await page.goto(pageUrl);
154+
155+
await expect(page.locator('.custom-snippet-content')).toBeVisible();
156+
await expect(page.locator('.custom-snippet-content h3')).toContainText('Custom HTML Content');
157+
await expect(page.locator('.custom-snippet-content p')).toContainText('This content was inserted via shortcode!');
158+
159+
await expect(page.locator('text=Page content before shortcode.')).toBeVisible();
160+
await expect(page.locator('text=Page content after shortcode.')).toBeVisible();
161+
}
162+
});
163+
105164
test.afterEach(async ({ page }) => {
106165
await helper.cleanupSnippet(TEST_SNIPPET_NAME);
107166
});

tests/e2e/helpers/SnippetsTestHelper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class SnippetsTestHelper {
5353
await this.page.waitForSelector(SELECTORS.TITLE_INPUT);
5454
await this.page.fill(SELECTORS.TITLE_INPUT, options.name);
5555

56-
// Select snippet type if specified
56+
// Select snippet type
5757
if (options.type && options.type !== 'PHP') {
5858
await this.page.click(SELECTORS.SNIPPET_TYPE_SELECT);
5959
await this.page.click(`text=${SNIPPET_TYPES[options.type]}`);
@@ -63,7 +63,7 @@ export class SnippetsTestHelper {
6363
await this.page.waitForSelector(SELECTORS.CODE_MIRROR_TEXTAREA);
6464
await this.page.fill(SELECTORS.CODE_MIRROR_TEXTAREA, options.code);
6565

66-
// Select location if specified (mainly for HTML/CSS/JS snippets)
66+
// Select location
6767
if (options.location) {
6868
await this.page.waitForSelector(SELECTORS.LOCATION_SELECT, { timeout: TIMEOUTS.SHORT });
6969
await this.page.click(SELECTORS.LOCATION_SELECT);

0 commit comments

Comments
 (0)