-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(5P) SPT add JS Functional tests to the plugin (#35379)
* Adds unit test framework via @wordpress/wp-scripts * Adds tests for TemplateSelectorControl * Adds and update tests * Fix tests following rebase * Refactor components and tests to pass following rebase * Include Jest EsLint Plugin We will enable this when we get lint-staged running. Configuring now. * Adds static preview and event handler tests * Removes redunant aria prop * Adds tests for Dynamic Preview mode * Refactor to DRY up test fixture data into helper file * Update snapshot * Move dev deps to correct location in package json * Adds tests for TemplateSelectorPreview component * Add tests and improve fixture data * Updates to remove @wordpress/scripts dep and utilise npx for tests instead This is required because of the Calypso monorepo relying on root dependencies. We don’t want to install @wordpress/scripts in Calypso root so npx is the best interim soluton. See #35379 (comment) * Restore @wordpress/scripts to devDeps as npx doesn’t cover all dependencies and causes test runner errors * Refactor to React Testing Library as Enzyme is not being prefered by Gutenberg Core Rewrote tests using RTL and it is much nicer and avoids testing implementation details. I have much more confident in my tests. * Rewrite Template Preview tests using RTL * Update to local usage of queryByText to avoid unecessary ref to container * Remove unecessary test * Update to use custom Jest config to enable one line test setup imports * Update to remove need to manually import test setup * Include only necessary deps @wordpress/jest-preset-default is required in order to extend the @wordpress/scripts jest config * Resolve errors introduced due to rebase * Fix broken tests by creating proxy for BlockPreview component Exists as a pass through component to simplying testing components which consume `BlockPreview` from `@wordpress/block-editor`. This is because jest cannot mock node modules that are not part of the root node modules. Due to the way this projects dependencies are defined `@wordpress/block-editor` does not exist within `node_modules` and it is there impossible to mock it without providing a wrapping component to act as a pass though. See https://jestjs.io/docs/en/manual-mocks * Fix to ensure Blank mode is preserved Previously adding conditionals in tests had removed the ability to provide a “blank” value in the Templates. Now add this as an explicit assertion across multiple tests to ensure it isn’t lost again. * Reinstate missing default value Whilst it is important to test for the presence of `blocks` it does not hurt to provide a default value as this ensures the “blank” state can be provided when in dynamic mode.
- Loading branch information
Showing
15 changed files
with
23,289 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...ite-editing-plugin/starter-page-templates/page-template-modal/components/block-preview.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import { BlockPreview } from '@wordpress/block-editor'; | ||
/* eslint-enable import/no-extraneous-dependencies */ | ||
|
||
// Exists as a pass through component to simplying testing | ||
// components which consume `BlockPreview` from | ||
// `@wordpress/block-editor`. This is because jest cannot mock | ||
// node modules that are not part of the root node modules. | ||
// Due to the way this projects dependencies are defined | ||
// `@wordpress/block-editor` does not exist within `node_modules` | ||
// and it is there impossible to mock it without providing a wrapping | ||
// component to act as a pass though. | ||
// See https://jestjs.io/docs/en/manual-mocks | ||
export default function( props ) { | ||
return <BlockPreview { ...props } />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
.../page-template-modal/components/test/__snapshots__/template-selector-control-test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`TemplateSelectorControl Basic rendering renders with required props 1`] = ` | ||
<div> | ||
<div | ||
class="components-base-control template-selector-control" | ||
> | ||
<div | ||
class="components-base-control__field" | ||
> | ||
<label | ||
class="components-base-control__label" | ||
for="template-selector-control-17" | ||
> | ||
Select a Template... | ||
</label> | ||
<ul | ||
class="template-selector-control__options" | ||
data-testid="template-selector-control-options" | ||
> | ||
<li | ||
class="template-selector-control__template" | ||
> | ||
<button | ||
aria-labelledby="template-selector-control-17 label-template-selector-control-17-blank" | ||
class="template-selector-item__label" | ||
type="button" | ||
value="blank" | ||
> | ||
<div | ||
class="template-selector-item__preview-wrap" | ||
> | ||
<img | ||
alt="" | ||
class="template-selector-item__media" | ||
/> | ||
</div> | ||
<span | ||
id="label-template-selector-control-17-blank" | ||
> | ||
Blank | ||
</span> | ||
</button> | ||
</li> | ||
<li | ||
class="template-selector-control__template" | ||
> | ||
<button | ||
aria-labelledby="template-selector-control-17 label-template-selector-control-17-template-1" | ||
class="template-selector-item__label" | ||
type="button" | ||
value="template-1" | ||
> | ||
<div | ||
class="template-selector-item__preview-wrap" | ||
> | ||
<img | ||
alt="Testing alt" | ||
class="template-selector-item__media" | ||
src="https://via.placeholder.com/350x150" | ||
/> | ||
</div> | ||
<span | ||
id="label-template-selector-control-17-template-1" | ||
> | ||
Template 1 | ||
</span> | ||
</button> | ||
</li> | ||
<li | ||
class="template-selector-control__template" | ||
> | ||
<button | ||
aria-labelledby="template-selector-control-17 label-template-selector-control-17-template-2" | ||
class="template-selector-item__label" | ||
type="button" | ||
value="template-2" | ||
> | ||
<div | ||
class="template-selector-item__preview-wrap" | ||
> | ||
<img | ||
alt="Testing alt 2" | ||
class="template-selector-item__media" | ||
src="https://via.placeholder.com/300x250" | ||
/> | ||
</div> | ||
<span | ||
id="label-template-selector-control-17-template-2" | ||
> | ||
Template 2 | ||
</span> | ||
</button> | ||
</li> | ||
<li | ||
class="template-selector-control__template" | ||
> | ||
<button | ||
aria-labelledby="template-selector-control-17 label-template-selector-control-17-template-3" | ||
class="template-selector-item__label" | ||
type="button" | ||
value="template-3" | ||
> | ||
<div | ||
class="template-selector-item__preview-wrap" | ||
> | ||
<img | ||
alt="Testing alt 3" | ||
class="template-selector-item__media" | ||
src="https://via.placeholder.com/500x200" | ||
/> | ||
</div> | ||
<span | ||
id="label-template-selector-control-17-template-3" | ||
> | ||
Template 3 | ||
</span> | ||
</button> | ||
</li> | ||
</ul> | ||
</div> | ||
</div> | ||
</div> | ||
`; |
44 changes: 44 additions & 0 deletions
44
.../page-template-modal/components/test/__snapshots__/template-selector-preview-test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`TemplateSelectorPreview Basic rendering renders the preview when blocks are provided 1`] = ` | ||
<div> | ||
<div | ||
class="template-selector-preview editor-styles-wrapper" | ||
> | ||
<div | ||
class="components-disabled" | ||
> | ||
<div | ||
class="edit-post-visual-editor" | ||
> | ||
<div | ||
class="editor-styles-wrapper" | ||
style="visibility: hidden;" | ||
> | ||
<div | ||
class="editor-writing-flow" | ||
> | ||
<div | ||
class="editor-post-title" | ||
style="transform: none;" | ||
> | ||
<div | ||
class="editor-post-title__block" | ||
> | ||
<textarea | ||
class="editor-post-title__input" | ||
/> | ||
</div> | ||
</div> | ||
<div | ||
data-testid="block-template-preview" | ||
> | ||
MockedBlockPreview | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
`; |
Oops, something went wrong.