Skip to content

Commit 85fcc9d

Browse files
committed
test: add faust integration tests, better constants
1 parent 49f989f commit 85fcc9d

File tree

6 files changed

+147
-27
lines changed

6 files changed

+147
-27
lines changed

plugins/hwp-previews/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,6 @@ tests/_support/
5252
tests/_output/
5353
tests/_generated/
5454
tests/_data/
55+
56+
# Playwright outputs
57+
artifacts
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const HWP_SLUG = "hwp-previews";
2+
export const RESET_HELPER_PLUGIN_SLUG = "reset-hwp-previews-settings";
3+
export const TEST_PREVIEW_URL = "https://example.com/testPreview?preview=true";
4+
export const FAUST_FRONTEND_URL = "http://localhost:3001";
5+
export const DEFAULT_FAUST_PREVIEW_URL =
6+
FAUST_FRONTEND_URL +
7+
"/preview?p={ID}&preview=true&previewPathname=p{ID}&typeName={type}";
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { expect, test } from "@wordpress/e2e-test-utils-playwright";
2+
import {
3+
defaultPostTypes,
4+
getPostObject,
5+
getSettingsField,
6+
goToPluginPage,
7+
installFaust,
8+
resetPluginSettings,
9+
switchToTab,
10+
uninstallFaust,
11+
} from "../utils";
12+
import {
13+
DEFAULT_FAUST_PREVIEW_URL,
14+
FAUST_FRONTEND_URL,
15+
HWP_SLUG,
16+
} from "../constants";
17+
18+
test.describe("HWP Previews Faust Integration Test", () => {
19+
test.beforeEach(async ({ admin, page, requestUtils }) => {
20+
await requestUtils.resetPreferences();
21+
await installFaust(admin, page);
22+
23+
// Set Faust frontend url
24+
await admin.visitAdminPage("/options-general.php?page=faustwp-settings");
25+
await page.locator("input#frontend_uri").fill(FAUST_FRONTEND_URL);
26+
await page.keyboard.press("Enter");
27+
});
28+
29+
test.afterEach(async ({ admin, page }) => {
30+
await uninstallFaust(admin, page);
31+
});
32+
33+
test("Faust settings applied properly", async ({
34+
page,
35+
admin,
36+
requestUtils,
37+
}) => {
38+
await requestUtils.activatePlugin(HWP_SLUG);
39+
await resetPluginSettings(admin);
40+
41+
await goToPluginPage(admin);
42+
43+
// Check Faust integration notification
44+
await expect(
45+
page.locator("#hwp_previews_faust_notice.notice"),
46+
).toBeVisible();
47+
48+
// Settings should be enabled for all types with Faust preview URL
49+
for (const postType of defaultPostTypes) {
50+
await switchToTab(page, postType.label);
51+
52+
const enabledCheckbox = getSettingsField(postType.key).enabledCheckbox;
53+
const previewUrlInput = getSettingsField(postType.key).previewUrlInput;
54+
55+
await expect(page.locator(enabledCheckbox)).toBeChecked();
56+
await expect(page.locator(previewUrlInput)).toHaveValue(
57+
DEFAULT_FAUST_PREVIEW_URL,
58+
);
59+
}
60+
});
61+
62+
test("Faust preview link continuity assured", async ({
63+
page,
64+
admin,
65+
requestUtils,
66+
}) => {
67+
// Create new page
68+
const post = await requestUtils.createPost(getPostObject("post"));
69+
70+
// Check preview link on the table
71+
await admin.visitAdminPage(`/edit.php?post_type=post`);
72+
73+
const faustPreviewLink = page
74+
.locator(`#post-${post.id} .view a`, {
75+
hasText: "Preview",
76+
exact: true,
77+
})
78+
.getAttribute("href");
79+
80+
await requestUtils.activatePlugin(HWP_SLUG);
81+
await resetPluginSettings(admin);
82+
await admin.visitAdminPage(`/edit.php?post_type=post`);
83+
84+
await expect(
85+
page.locator(`#post-${post.id} .view a`, {
86+
hasText: "Preview",
87+
exact: true,
88+
}),
89+
).toHaveAttribute("href", faustPreviewLink);
90+
});
91+
});

plugins/hwp-previews/tests/e2e/specs/previews.spec.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ import {
33
getPostObject,
44
getSettingsField,
55
goToPluginPage,
6-
hwpSlug,
7-
resetHelperPluginSlug,
86
saveChanges,
97
switchToTab,
10-
testPreviewUrl,
118
resetPluginSettings,
129
} from "../utils";
10+
import {
11+
HWP_SLUG,
12+
RESET_HELPER_PLUGIN_SLUG,
13+
TEST_PREVIEW_URL,
14+
} from "../constants";
1315

1416
test.describe("HWP Previews Preview Link", () => {
15-
const typesToTest = ["post", "page"]; // TODO add CPT
17+
const typesToTest = ["post", "page"];
1618
let contentIds = {};
1719

1820
test.beforeAll(async ({ requestUtils }) => {
@@ -24,8 +26,8 @@ test.describe("HWP Previews Preview Link", () => {
2426
const newPage = await requestUtils.createPage(getPostObject("page"));
2527
contentIds.page = newPage.id;
2628

27-
await requestUtils.activatePlugin(hwpSlug);
28-
await requestUtils.activatePlugin(resetHelperPluginSlug);
29+
await requestUtils.activatePlugin(HWP_SLUG);
30+
await requestUtils.activatePlugin(RESET_HELPER_PLUGIN_SLUG);
2931
});
3032

3133
test.beforeEach(async ({ admin }) => {
@@ -44,7 +46,7 @@ test.describe("HWP Previews Preview Link", () => {
4446
await page.locator(getSettingsField(postKey).enabledCheckbox).check();
4547
await page
4648
.locator(getSettingsField(postKey).previewUrlInput)
47-
.fill(testPreviewUrl);
49+
.fill(TEST_PREVIEW_URL);
4850
await saveChanges(page);
4951

5052
// Check preview link on the table
@@ -54,15 +56,15 @@ test.describe("HWP Previews Preview Link", () => {
5456
hasText: "Preview",
5557
exact: true,
5658
}),
57-
).toHaveAttribute("href", testPreviewUrl);
59+
).toHaveAttribute("href", TEST_PREVIEW_URL);
5860

5961
// Check preview link on edit page
6062
await admin.editPost(contentIds[postKey]);
6163
await page.getByRole("button", { name: "View", exact: true }).click();
6264
await page.waitForSelector(".components-popover");
6365
await expect(
6466
page.getByRole("menuitem", { name: "Preview in new tab" }),
65-
).toHaveAttribute("href", testPreviewUrl);
67+
).toHaveAttribute("href", TEST_PREVIEW_URL);
6668
});
6769
});
6870

@@ -84,15 +86,15 @@ test.describe("HWP Previews Preview Link", () => {
8486
hasText: "Preview",
8587
exact: true,
8688
}),
87-
).not.toHaveAttribute("href", testPreviewUrl);
89+
).not.toHaveAttribute("href", TEST_PREVIEW_URL);
8890

8991
// Check preview link on edit page
9092
await admin.editPost(contentIds[postKey]);
9193
await page.getByRole("button", { name: "View", exact: true }).click();
9294
await page.waitForSelector(".components-popover");
9395
await expect(
9496
page.getByRole("menuitem", { name: "Preview in new tab" }),
95-
).not.toHaveAttribute("href", testPreviewUrl);
97+
).not.toHaveAttribute("href", TEST_PREVIEW_URL);
9698
});
9799
});
98100

@@ -108,7 +110,7 @@ test.describe("HWP Previews Preview Link", () => {
108110
await page.locator(getSettingsField(postKey).iframeCheckbox).check();
109111
await page
110112
.locator(getSettingsField(postKey).previewUrlInput)
111-
.fill(testPreviewUrl);
113+
.fill(TEST_PREVIEW_URL);
112114
await saveChanges(page);
113115

114116
// Visit the preview page
@@ -123,10 +125,7 @@ test.describe("HWP Previews Preview Link", () => {
123125

124126
// Check if iframe included with the correct URL
125127
const iframe = page.locator("iframe.headless-preview-frame");
126-
await expect(iframe).toHaveAttribute("src", testPreviewUrl);
128+
await expect(iframe).toHaveAttribute("src", TEST_PREVIEW_URL);
127129
});
128130
});
129131
});
130-
131-
// TODO test if parameters being applied
132-
// TODO test post_statuses_as_parent

plugins/hwp-previews/tests/e2e/specs/settings.spec.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ import { expect, test } from "@wordpress/e2e-test-utils-playwright";
22
import {
33
getDefaultPreviewUrl,
44
goToPluginPage,
5-
hwpSlug,
65
defaultPostTypes,
76
switchToTab,
87
saveChanges,
9-
testPreviewUrl,
108
resetPluginSettings,
119
getSettingsField,
1210
} from "../utils";
11+
import { HWP_SLUG, TEST_PREVIEW_URL } from "../constants";
1312

1413
test.describe("HWP Previews Admin Settings", () => {
1514
test.beforeAll(async ({ requestUtils }) => {
16-
await requestUtils.activatePlugin(hwpSlug);
15+
await requestUtils.activatePlugin(HWP_SLUG);
1716
});
1817

1918
test.beforeEach(async ({ admin }) => {
@@ -81,7 +80,7 @@ test.describe("HWP Previews Admin Settings", () => {
8180
// Update settings
8281
await page.locator(enabledCheckbox).check();
8382
await page.locator(iframeCheckbox).check();
84-
await page.locator(previewUrlInput).fill(testPreviewUrl);
83+
await page.locator(previewUrlInput).fill(TEST_PREVIEW_URL);
8584

8685
// Save settings
8786
await saveChanges(page);
@@ -95,7 +94,7 @@ test.describe("HWP Previews Admin Settings", () => {
9594
// Verify settings were saved
9695
await expect(page.locator(enabledCheckbox)).toBeChecked();
9796
await expect(page.locator(iframeCheckbox)).toBeChecked();
98-
await expect(page.locator(previewUrlInput)).toHaveValue(testPreviewUrl);
97+
await expect(page.locator(previewUrlInput)).toHaveValue(TEST_PREVIEW_URL);
9998
});
10099
});
101100

plugins/hwp-previews/tests/e2e/utils.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
export const hwpSlug = "hwp-previews";
2-
export const resetHelperPluginSlug = "reset-hwp-previews-settings";
3-
export const testPreviewUrl = "https://example.com/testPreview?preview=true";
4-
51
export const defaultPostTypes = [
62
{ label: "Pages", key: "page" },
73
{ label: "Media", key: "attachment" },
@@ -33,8 +29,6 @@ export async function switchToTab(page, tabName) {
3329
.locator("#wpbody-content")
3430
.getByRole("link", { name: tabName })
3531
.click();
36-
37-
// TODO add waitfor
3832
}
3933

4034
export async function saveChanges(page) {
@@ -51,3 +45,30 @@ export async function resetPluginSettings(admin) {
5145
"/options-general.php?page=hwp-previews&reset=true",
5246
);
5347
}
48+
49+
export async function installFaust(admin, page) {
50+
const installSelector = '.install-now[data-slug="faustwp"]';
51+
const activateSelector = '.activate-now[data-slug="faustwp"]';
52+
53+
await admin.visitAdminPage(
54+
"/plugin-install.php?s=faust&tab=search&type=term",
55+
);
56+
57+
const installButton = page.locator(installSelector);
58+
59+
if (await installButton.isVisible()) {
60+
await installButton.click();
61+
await page.waitForSelector(activateSelector, { timeout: 1000 * 90 });
62+
await page.locator(activateSelector).click();
63+
} else {
64+
await page.locator(activateSelector).click();
65+
}
66+
}
67+
68+
export async function uninstallFaust(admin, page) {
69+
page.on("dialog", (dialog) => dialog.accept());
70+
71+
await admin.visitAdminPage("/plugins.php");
72+
await page.locator("a#deactivate-faustwp").click();
73+
await page.locator("a#delete-faustwp").click();
74+
}

0 commit comments

Comments
 (0)