Skip to content

Commit d543e12

Browse files
committed
add tests for settings page and editor page
1 parent aa58c5a commit d543e12

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

e2e/playwright.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ export default defineConfig( {
6161
// testDir,
6262
testMatch: 'setup/auth.js',
6363
},
64+
{
65+
name: 'tests',
66+
use: {
67+
storageState: process.env.WP_AUTH_STORAGE,
68+
},
69+
testMatch: 'tests/*.ts',
70+
},
6471
{
6572
name: 'chromium',
6673
use: { ...devices[ 'Desktop Chrome' ] },

e2e/tests/admin.spec.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { test, expect } from '@wordpress/e2e-test-utils-playwright'
2+
3+
test( 'Activating Stackable should redirect to the Getting Started Page', async ( {
4+
page,
5+
admin,
6+
} ) => {
7+
await admin.visitAdminPage( 'plugins.php' )
8+
9+
// Deactivate Stackable
10+
const deactivate = page.locator( '#deactivate-stackable-ultimate-gutenberg-blocks' )
11+
await expect( deactivate ).toBeVisible()
12+
await deactivate.click()
13+
14+
// Activate Stackable
15+
const activate = page.locator( '#activate-stackable-ultimate-gutenberg-blocks' )
16+
await expect( activate ).toBeVisible()
17+
await activate.click()
18+
19+
await expect( page ).toHaveURL( /stackable-getting-started/ )
20+
} )
21+
22+
test( 'Stackable settings should be saved', async ( {
23+
page,
24+
admin,
25+
baseURL,
26+
} ) => {
27+
// Start waiting for Stackable Settings JSON Response before visiting the page
28+
let settings = page.waitForResponse( response =>
29+
response.url() === `${ baseURL }wp-json/wp/v2/settings/` && response.request().method() === 'POST'
30+
)
31+
await admin.visitAdminPage( 'options-general.php?page=stackable' )
32+
// Make sure all Stackable settings are loaded
33+
let response = await settings
34+
await response.finished()
35+
36+
// Retrieves the value of the first option, toggles it and check if the value changed
37+
const option = page.locator( '.ugb-admin-toggle-setting__button' ).first()
38+
const oldVal = await option.evaluate( node => node.getAttribute( 'aria-checked' ) )
39+
40+
await option.click()
41+
const newVal = await option.evaluate( node => node.getAttribute( 'aria-checked' ) )
42+
43+
expect( newVal ).not.toBe( oldVal )
44+
45+
// Check if the value is correct after reloading
46+
settings = page.waitForResponse( response =>
47+
response.url() === `${ baseURL }wp-json/wp/v2/settings/` && response.request().method() === 'POST'
48+
)
49+
await page.reload()
50+
response = await settings
51+
await response.finished()
52+
53+
const _option = page.locator( '.ugb-admin-toggle-setting__button' ).first()
54+
expect( await _option.evaluate( node => node.getAttribute( 'aria-checked' ) ) ).toBe( newVal )
55+
} )

e2e/tests/block-editor.spec.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { test, expect } from '@wordpress/e2e-test-utils-playwright'
2+
3+
test( 'Stackable blocks can be added in the editor', async ( {
4+
page,
5+
admin,
6+
editor,
7+
} ) => {
8+
await admin.createNewPost()
9+
await page.getByLabel( 'Toggle block inserter' ).click()
10+
11+
await page.locator( '.editor-block-list-item-stackable-text' ).click()
12+
13+
const blocks = await editor.getBlocks()
14+
15+
expect( blocks[ 0 ].name ).toContain( 'stackable/text' )
16+
} )
17+
18+
test( 'Stackable Inspector Controls should show up upon clicking a Stackable block', async ( {
19+
page,
20+
admin,
21+
editor,
22+
} ) => {
23+
await admin.createNewPost()
24+
25+
await editor.insertBlock( {
26+
name: 'stackable/text',
27+
} )
28+
29+
await editor.selectBlocks( page.locator( 'iframe[name="editor-canvas"]' ).contentFrame().getByLabel( 'Block: Text' ) )
30+
await expect( page.getByLabel( 'Layout Tab' ) ).toBeVisible()
31+
await expect( page.getByLabel( 'Style Tab' ) ).toBeVisible()
32+
await expect( page.getByLabel( 'Advanced Tab' ) ).toBeVisible()
33+
} )
34+
35+
test( 'A Stackable block\'s attributes should update when settings are changed in the Inspector Controls.', async ( {
36+
page,
37+
admin,
38+
editor,
39+
} ) => {
40+
await admin.createNewPost()
41+
42+
await editor.insertBlock( {
43+
name: 'stackable/text',
44+
} )
45+
46+
const editorCanvas = page.locator( 'iframe[name="editor-canvas"]' ).contentFrame()
47+
await editorCanvas.locator( '[data-type="stackable/text"] > .stk-block-text > p[role="textbox"]' ).fill( 'test' )
48+
await page.locator( '.stk-color-palette-control .stk-control-content > .components-dropdown > .components-button' ).first().click()
49+
await page.getByLabel( 'Hex color' ).fill( 'ff0000' )
50+
await editorCanvas.locator( 'body' ).click()
51+
52+
await expect( editorCanvas.locator( '[data-type="stackable/text"]' ) ).toContainText( 'test' )
53+
await expect( editorCanvas.locator( '[data-type="stackable/text"] > .stk-block-text > p[role="textbox"]' ) ).toHaveCSS( 'color', 'rgb(255, 0, 0)' )
54+
55+
await editor.saveDraft()
56+
57+
const blocks = await editor.getBlocks()
58+
const attributes = blocks[ 0 ].attributes
59+
60+
expect( attributes.textColor1 ).toBe( '#ff0000' )
61+
expect( attributes.text ).toBe( 'test' )
62+
} )
63+
64+
test( 'The Stackable block added in the editor should be visible in the frontend', async ( {
65+
admin,
66+
editor,
67+
} ) => {
68+
await admin.createNewPost()
69+
70+
await editor.insertBlock( {
71+
name: 'stackable/text',
72+
attributes: {
73+
text: 'test',
74+
textColor1: '#ff0000',
75+
},
76+
} )
77+
78+
const blocks = await editor.getBlocks()
79+
const uniqueId = blocks[ 0 ].attributes.uniqueId
80+
81+
await editor.saveDraft()
82+
83+
const preview = await editor.openPreviewPage()
84+
85+
await expect( preview.locator( `[data-block-id="${ uniqueId }"]` ) ).toBeVisible()
86+
await expect( preview.locator( `[data-block-id="${ uniqueId }"]` ) ).toContainText( 'test' )
87+
await expect( preview.locator( `[data-block-id="${ uniqueId }"] p` ) ).toHaveCSS( 'color', 'rgb(255, 0, 0)' )
88+
} )

0 commit comments

Comments
 (0)