Skip to content

Commit 4ef30ce

Browse files
committed
add comments, update tests
1 parent d86d9be commit 4ef30ce

File tree

10 files changed

+102
-133
lines changed

10 files changed

+102
-133
lines changed

e2e/config/reporter.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ class StkReporter implements Reporter {
4949
}
5050

5151
onTestEnd( test: TestCase, result: TestResult ) {
52+
// Console Reporter similar to Playwright's Dot Reporter:
53+
// '·' for passed test
54+
// '×' for attempt for retry
55+
// 'T' for timed out
56+
// 'F' for failed test
57+
5258
if ( test.outcome() === 'expected' ) {
5359
process.stdout.write( '·' )
5460
} else {
@@ -57,6 +63,7 @@ class StkReporter implements Reporter {
5763
const out = test.results.length <= test.retries ? '×' : ( result.status === 'timedOut' ? 'T' : 'F' )
5864
process.stdout.write( out )
5965

66+
// Compile error messages and snippets
6067
let testResult = `${ titlePath } ${ test.results.length > 1 ? `(retry #${ test.results.length - 1 }) ` : '' }[${ ms( result.duration ) }]` + '\n\n'
6168
if ( result.errors.length >= 1 ) {
6269
result.errors.forEach( error => {
@@ -103,6 +110,7 @@ class StkReporter implements Reporter {
103110
console.log( `---\n\nFailed Tests:` )
104111
console.log( this.failedTestErrors.join( '' ) )
105112

113+
// Generate md file for GitHub Job Summary Report
106114
const md = `
107115
| PASSED | FLAKY | SKIPPED | FAILED |
108116
| ------ | ----- | ------- | ------ |

e2e/playwright.config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ export default defineConfig( {
2323
/* Fail the build on CI if you accidentally left test.only in the source code. */
2424
// forbidOnly: !! process.env.CI,
2525
/* Retry on CI only */
26-
// retries: process.env.CI ? 1 : 0,
27-
retries: 1,
26+
retries: process.env.CI ? 1 : 0,
2827
// Locally, we could take advantage of parallelism due to multicore systems.
2928
// However, in the CI, we typically can use only one worker at a time.
3029
// It's more straightforward to align how we run tests in both systems.

e2e/test-utils/editor.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Editor as BaseEditor } from '@wordpress/e2e-test-utils-playwright'
2+
3+
class ExtendedEditor extends BaseEditor {
4+
blockErrors: Array<String> = []
5+
6+
getBlockAttributes = async function( clientId : String ) {
7+
await this.page.waitForFunction(
8+
() => window?.wp?.blocks && window?.wp?.data
9+
)
10+
11+
const attributes = await this.page.evaluate( async ( [ _clientId ] ) => {
12+
return await window.wp.data.select( 'core/block-editor' ).getBlockAttributes( _clientId )
13+
}, [ clientId ] )
14+
15+
return attributes
16+
}
17+
18+
getBlockErrors() :Array<String> {
19+
this.blockErrors = []
20+
this.page.on( 'console', msg => {
21+
if ( msg.type() === 'error' && msg.text().startsWith( 'Block validation' ) ) {
22+
this.blockErrors.push( msg.text() )
23+
}
24+
} )
25+
26+
return this.blockErrors
27+
}
28+
}
29+
30+
export { ExtendedEditor }

e2e/test-utils/stackable.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export class StackableFixture {
88
this.page = page
99
}
1010

11+
// When loading the Stackable settings in the Admin, there are 4 requests to wp/v2/settings
12+
// This function waits for all 4 requests to finish to ensure Stackable settings are loaded and reflected on the page
1113
async waitForSettings(): Promise<void> {
1214
return new Promise( ( resolve, reject ) => {
1315
let responseCount = 0

e2e/test-utils/test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66

77
import { ExtendedRequestUtils } from './requestUtils'
88
import { StackableFixture } from './stackable'
9+
import { ExtendedEditor } from './editor'
910

1011
// We could also import utils from other packages.
1112
// import { StoreApiUtils } from '@woocommerce/e2e-utils';
@@ -19,6 +20,7 @@ import { StackableFixture } from './stackable'
1920
const test = base.extend<{
2021
requestUtils: ExtendedRequestUtils;
2122
stackable: StackableFixture;
23+
editor: ExtendedEditor;
2224
}>( {
2325
requestUtils: async ( {}, use ) => {
2426
let requestUtils = null
@@ -38,6 +40,10 @@ const test = base.extend<{
3840
stackable: async ( { page }, use ) => {
3941
await use( new StackableFixture( page ) )
4042
},
43+
44+
editor: async ( { page }, use ) => {
45+
await use( new ExtendedEditor( { page } ) )
46+
},
4147
} )
4248

4349
export { test, expect }

e2e/tests/admin.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ test( 'Stackable settings should be saved', async ( {
3434
stackable,
3535
} ) => {
3636
// Start waiting for Stackable Settings JSON Response before visiting the page
37-
let settings = null
38-
settings = stackable.waitForSettings()
37+
let settings = stackable.waitForSettings()
3938

4039
await admin.visitAdminPage( 'options-general.php?page=stackable' )
4140
// Make sure all Stackable settings are loaded

e2e/tests/block-editor.spec.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Page } from '@playwright/test'
12
import { test, expect } from 'e2e/test-utils'
23

34
test.describe( 'Block Editor', () => {
@@ -21,8 +22,9 @@ test.describe( 'Block Editor', () => {
2122

2223
editor,
2324
} ) => {
25+
// Insert Stackable Text Block through block inserter
26+
// Also checks if Stackable Block is in the list of blocks in the Editor
2427
await page.getByLabel( 'Toggle block inserter' ).click()
25-
2628
await page.locator( '.editor-block-list-item-stackable-text' ).click()
2729

2830
await expect( editor.canvas.getByLabel( 'Block: Text' ) ).toBeVisible()
@@ -54,25 +56,22 @@ test.describe( 'Block Editor', () => {
5456
await editor.canvas.locator( '[data-type="stackable/text"] > .stk-block-text > p[role="textbox"]' ).fill( 'test' )
5557
await page.locator( '.stk-color-palette-control .stk-control-content > .components-dropdown > .components-button' ).first().click()
5658
await page.getByLabel( 'Hex color' ).fill( 'ff0000' )
59+
60+
// Click on the body to close the Color Picker popup
5761
await editor.canvas.locator( 'body' ).click()
5862

63+
// Verify if Text block contains correct content and color
5964
await expect( editor.canvas.locator( '[data-type="stackable/text"]' ) ).toContainText( 'test' )
6065
await expect( editor.canvas.locator( '[data-type="stackable/text"] > .stk-block-text > p[role="textbox"]' ) ).toHaveCSS( 'color', 'rgb(255, 0, 0)' )
6166

6267
await editor.saveDraft()
6368

64-
await page.waitForFunction(
65-
() => window?.wp?.blocks && window?.wp?.data
66-
)
67-
69+
// Verify block attributes
6870
const clientId = await editor.canvas.getByLabel( 'Block: Text' ).getAttribute( 'data-block' )
71+
const attributes = await editor.getBlockAttributes( clientId )
6972

70-
const attributes = await page.evaluate( async ( [ _clientId ] ) => {
71-
return await window.wp.data.select( 'core/block-editor' ).getBlockAttributes( _clientId )
72-
}, [ clientId ] )
73-
74-
expect( attributes ).toHaveAttribute( 'textColor1', '#ff0000' )
75-
expect( attributes ).toHaveAttribute( 'text', 'test' )
73+
expect( attributes ).toHaveProperty( 'textColor1', '#ff0000' )
74+
expect( attributes ).toHaveProperty( 'text', 'test' )
7675
} )
7776

7877
test( 'The Stackable block added in the editor should be visible in the frontend', async ( {
@@ -86,25 +85,27 @@ test.describe( 'Block Editor', () => {
8685
},
8786
} )
8887

89-
await page.waitForFunction(
90-
() => window?.wp?.blocks && window?.wp?.data
91-
)
92-
9388
const clientId = await editor.canvas.getByLabel( 'Block: Text' ).getAttribute( 'data-block' )
94-
95-
const attributes = await page.evaluate( async ( [ _clientId ] ) => {
96-
return await window.wp.data.select( 'core/block-editor' ).getBlockAttributes( _clientId )
97-
}, [ clientId ] )
98-
99-
const uniqueId = attributes.uniqueId
89+
const uniqueId = ( await editor.getBlockAttributes( clientId ) ).uniqueId
10090

10191
await editor.saveDraft()
10292

103-
const preview = await editor.openPreviewPage()
93+
let preview : Page
94+
95+
// openPreviewPage() from WordPress may fail as it relies on a button with a attribute name of 'view'
96+
// https://github.com/WordPress/gutenberg/blob/trunk/packages/e2e-test-utils-playwright/src/editor/preview.ts
97+
// Older versions of WordPress uses 'Preview' as the label for the Preview Button
98+
if ( await page.getByLabel( 'View', { exact: true } ).isVisible() ) {
99+
preview = await editor.openPreviewPage()
100+
} else {
101+
await page.getByLabel( 'Preview' ).click()
102+
const previewPromise = page.waitForEvent( 'popup' )
103+
await page.getByRole( 'menuitem', { name: 'Preview in new tab' } ).click()
104+
preview = await previewPromise
105+
}
104106

105107
await expect( preview.locator( `[data-block-id="${ uniqueId }"]` ) ).toBeVisible()
106108
await expect( preview.locator( `[data-block-id="${ uniqueId }"]` ) ).toContainText( 'test' )
107109
await expect( preview.locator( `[data-block-id="${ uniqueId }"] p` ) ).toHaveCSS( 'color', 'rgb(255, 0, 0)' )
108110
} )
109111
} )
110-

e2e/tests/existing-blocks.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* eslint-disable jest/no-disabled-tests */
2+
import { test, expect } from 'e2e/test-utils'
3+
4+
test.skip( process.env.WP_TEST_POSTID === undefined, 'For existing test page only' )
5+
6+
test( 'Existing Stackable blocks should have no errors', async ( {
7+
admin,
8+
editor,
9+
} ) => {
10+
// Start listening on console errors for block validation
11+
const blockErrors = editor.getBlockErrors()
12+
13+
await admin.editPost( process.env.WP_TEST_POSTID )
14+
15+
expect( blockErrors ).toHaveLength( 0 )
16+
} )

e2e/tests/global-settings.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,34 @@ test.describe( 'Global Settigs', () => {
3030
const globalColors = panel.locator( '.ugb-global-settings-color-picker__color-indicators > div' )
3131
const count = ( await globalColors.evaluate( node => Array.from( node.childNodes ) ) ).length
3232

33+
// Verify if a new color is added to the list
3334
const newColor = globalColors.getByRole( 'button', { name: `Custom Color ${ count } ` } )
3435
await expect( newColor ).toBeVisible()
36+
37+
// Get the value of the new global color
3538
await newColor.click()
3639
const hexValue = await page.getByLabel( 'Hex color' ).inputValue()
3740

38-
// Insert a Stackable Text Block and check if the added Global Colors is in the color picker
3941
await page.getByLabel( 'Settings', { exact: true } ).click()
4042
editor.insertBlock( {
4143
name: 'stackable/text',
4244
attributes: {
4345
text: 'test',
4446
},
4547
} )
48+
49+
// Open the color picker
4650
await page.locator( '.stk-color-palette-control .stk-control-content > .components-dropdown > .components-button' ).first().click()
4751

52+
// Verify the newly added global color is in the color picker
4853
await expect( page.getByRole( 'heading', { name: 'Global Colors' } ) ).toBeVisible()
4954
await expect( page.getByLabel( `Color: Custom Color ${ count }` ) ).toBeVisible()
55+
56+
// Verify the color value
5057
await page.getByLabel( `Color: Custom Color ${ count }` ).click()
5158
await expect( page.getByLabel( 'Hex color' ) ).toHaveValue( hexValue )
59+
60+
// Click on the color picker button to close the popup
5261
await page.locator( '.stk-color-palette-control .stk-control-content > .components-dropdown > .components-button' ).first().click()
5362

5463
// Delete added Global Color
@@ -67,7 +76,11 @@ test.describe( 'Global Settigs', () => {
6776
await page.locator( '.ugb-global-settings-typography-control' ).nth( 1 ).locator( '.components-base-control__field > .ugb-button-icon-control__wrapper > .components-button' ).click()
6877
await page.locator( '.stk-popover .components-base-control:nth-of-type(2)', { hasText: /Size/ } ).getByRole( 'textbox' ).fill( '32' )
6978
await page.locator( '.ugb-global-settings-typography-control' ).nth( 1 ).locator( '.components-base-control__field > .ugb-button-icon-control__wrapper > .components-button' ).click()
79+
80+
// Verify if the Heading 2 in Global Typography Styles has correct font size
7081
await expect( page.getByRole( 'heading', { name: 'Heading 2' } ) ).toHaveCSS( 'font-size', '32px' )
82+
83+
// Open Block Settings
7184
await page.getByLabel( 'Settings', { exact: true } ).click()
7285

7386
// Check if the added Stackable Heading Block has a font-size of 32

e2e/tests/site-editor.spec.ts

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)