From c055fa3ed85bde4403c12d5d7b8fbe3a85d7724f Mon Sep 17 00:00:00 2001 From: David Szabo Date: Mon, 26 Apr 2021 16:11:13 +0200 Subject: [PATCH 1/3] Use JSON serialization --- packages/e2e-test-utils/src/wp-data-select.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/e2e-test-utils/src/wp-data-select.js b/packages/e2e-test-utils/src/wp-data-select.js index 294a242de9a480..a45dbe7471712e 100644 --- a/packages/e2e-test-utils/src/wp-data-select.js +++ b/packages/e2e-test-utils/src/wp-data-select.js @@ -10,9 +10,10 @@ export async function wpDataSelect( store, selector, ...parameters ) { return page.evaluate( ( _store, _selector, ..._parameters ) => { - return window.wp.data + const data = window.wp.data .select( _store ) [ _selector ]( ..._parameters ); + return JSON.parse( JSON.stringify( data ) ); }, store, selector, From c27f8c28b83bcfb4d7c7d22279e2df86489a0d6a Mon Sep 17 00:00:00 2001 From: David Szabo Date: Mon, 26 Apr 2021 17:43:09 +0200 Subject: [PATCH 2/3] Move JSON to getAllBlocks --- packages/e2e-test-utils/src/get-all-blocks.js | 10 ++++------ packages/e2e-test-utils/src/wp-data-select.js | 3 +-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/packages/e2e-test-utils/src/get-all-blocks.js b/packages/e2e-test-utils/src/get-all-blocks.js index 714dee2ef0ea5d..33826ca24587c8 100644 --- a/packages/e2e-test-utils/src/get-all-blocks.js +++ b/packages/e2e-test-utils/src/get-all-blocks.js @@ -1,13 +1,11 @@ -/** - * Internal dependencies - */ -import { wpDataSelect } from './wp-data-select'; - /** * Returns an array with all blocks; Equivalent to calling wp.data.select( 'core/block-editor' ).getBlocks(); * * @return {Promise} Promise resolving with an array containing all blocks in the document. */ export async function getAllBlocks() { - return wpDataSelect( 'core/block-editor', 'getBlocks' ); + return page.evaluate( () => { + const blocks = window.wp.data.select( 'core/block-editor' ).getBlocks(); + return JSON.parse( JSON.stringify( blocks ) ); + } ); } diff --git a/packages/e2e-test-utils/src/wp-data-select.js b/packages/e2e-test-utils/src/wp-data-select.js index a45dbe7471712e..294a242de9a480 100644 --- a/packages/e2e-test-utils/src/wp-data-select.js +++ b/packages/e2e-test-utils/src/wp-data-select.js @@ -10,10 +10,9 @@ export async function wpDataSelect( store, selector, ...parameters ) { return page.evaluate( ( _store, _selector, ..._parameters ) => { - const data = window.wp.data + return window.wp.data .select( _store ) [ _selector ]( ..._parameters ); - return JSON.parse( JSON.stringify( data ) ); }, store, selector, From 05a7e844450d1e33383fc6b11db7b4dfb6adee40 Mon Sep 17 00:00:00 2001 From: David Szabo Date: Mon, 26 Apr 2021 18:07:55 +0200 Subject: [PATCH 3/3] Add comment --- packages/e2e-test-utils/src/get-all-blocks.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/e2e-test-utils/src/get-all-blocks.js b/packages/e2e-test-utils/src/get-all-blocks.js index 33826ca24587c8..70d7458e86aa46 100644 --- a/packages/e2e-test-utils/src/get-all-blocks.js +++ b/packages/e2e-test-utils/src/get-all-blocks.js @@ -6,6 +6,16 @@ export async function getAllBlocks() { return page.evaluate( () => { const blocks = window.wp.data.select( 'core/block-editor' ).getBlocks(); + /** + * We are turning the result into a JSON object and back, to turn all the non-serializable + * values into `null`. + * + * `page.evaluate` returns `undefined` if it encounters a non-serializable value. + * To avoid returning `undefined`, we turn the result into a JSON string and back, + * which results in a serializable value every time. + * + * For more information see: https://github.com/WordPress/gutenberg/pull/31199 + */ return JSON.parse( JSON.stringify( blocks ) ); } ); }