-
Notifications
You must be signed in to change notification settings - Fork 797
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Image Compare: Add image compare block tests and fixtures (#19064)
Co-authored-by: Jeremy Herve <jeremy@jeremy.hu>
- Loading branch information
1 parent
488952f
commit 5de2fec
Showing
13 changed files
with
320 additions
and
19 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/plugins/jetpack/changelog/add-image-compare-block-tests
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,4 @@ | ||
Significance: minor | ||
Type: enhancement | ||
|
||
Image Compare: Add block tests and fixtures. |
21 changes: 21 additions & 0 deletions
21
projects/plugins/jetpack/extensions/blocks/image-compare/controls.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,21 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { PanelBody, RadioControl } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
export default function ImageCompareControls( { attributes, setAttributes } ) { | ||
const { orientation } = attributes; | ||
return ( | ||
<PanelBody title={ __( 'Orientation', 'jetpack' ) }> | ||
<RadioControl | ||
selected={ orientation || 'horizontal' } | ||
options={ [ | ||
{ label: __( 'Side by side', 'jetpack' ), value: 'horizontal' }, | ||
{ label: __( 'Above and below', 'jetpack' ), value: 'vertical' }, | ||
] } | ||
onChange={ value => setAttributes( { orientation: value } ) } | ||
/> | ||
</PanelBody> | ||
); | ||
} |
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
55 changes: 55 additions & 0 deletions
55
projects/plugins/jetpack/extensions/blocks/image-compare/test/controls.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,55 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ImageCompareControls from '../controls'; | ||
|
||
describe( 'ImageCompareControls', () => { | ||
const setAttributes = jest.fn(); | ||
const defaultProps = { | ||
attributes: { orientation: undefined }, | ||
setAttributes, | ||
}; | ||
|
||
beforeEach( () => { | ||
setAttributes.mockClear(); | ||
} ); | ||
|
||
test( 'loads and displays orientation controls', () => { | ||
render( <ImageCompareControls { ...defaultProps } /> ); | ||
|
||
expect( screen.getByText( 'Orientation' ) ).toBeInTheDocument(); | ||
expect( screen.getByLabelText( 'Side by side' ) ).toBeInTheDocument(); | ||
expect( screen.getByLabelText( 'Above and below' ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'defaults orientation selection to horizontal', () => { | ||
render( <ImageCompareControls { ...defaultProps } /> ); | ||
|
||
expect( screen.getByLabelText( 'Side by side' ) ).toHaveAttribute( 'checked' ); | ||
} ); | ||
|
||
test( 'selects option according to orientation attribute', () => { | ||
const attributes = { orientation: 'vertical' }; | ||
render( <ImageCompareControls { ...{ ...defaultProps, attributes } } /> ); | ||
|
||
expect( screen.getByLabelText( 'Above and below' ) ).toHaveAttribute( 'checked' ); | ||
} ); | ||
|
||
test( 'sets the orientation attribute ', () => { | ||
render( <ImageCompareControls { ...defaultProps } /> ) | ||
userEvent.click( screen.getByLabelText( 'Above and below' ) ); | ||
|
||
expect( setAttributes ).toHaveBeenCalledWith( { orientation: 'vertical' } ); | ||
} ); | ||
} ); |
112 changes: 112 additions & 0 deletions
112
projects/plugins/jetpack/extensions/blocks/image-compare/test/edit.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,112 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ImageCompareEdit from '../edit'; | ||
|
||
function renderImageCompare( props ) { | ||
const { container } = render( <ImageCompareEdit { ...props } /> ); | ||
return container.querySelector( `.${ props.className } > div` ); | ||
} | ||
|
||
describe( 'ImageCompareEdit', () => { | ||
const emptyImages = { imageBefore: {}, imageAfter: {} }; | ||
const defaultAttributes = { | ||
orientation: undefined, | ||
imageBefore: { | ||
id: 1, | ||
url: 'http://test.com/1.jpg', | ||
alt: 'Test image one', | ||
}, | ||
imageAfter: { | ||
id: 2, | ||
url: 'http://test.com/2.jpg', | ||
alt: 'Test image two', | ||
}, | ||
caption: 'Default caption', | ||
}; | ||
|
||
const defaultProps = { | ||
attributes: defaultAttributes, | ||
isSelected: true, | ||
className: 'custom-image-compare-class', | ||
clientId: '1', | ||
}; | ||
|
||
test( 'applies correct attributes to block wrapper', () => { | ||
render( <ImageCompareEdit { ...defaultProps } /> ); | ||
const wrapper = screen.getByRole( 'figure' ); | ||
|
||
expect( wrapper ).toHaveClass( defaultProps.className ); | ||
expect( wrapper ).toHaveAttribute( 'id', defaultProps.clientId ); | ||
} ) | ||
|
||
test( 'applies juxtapose classes when images present', () => { | ||
const element = renderImageCompare( defaultProps ); | ||
|
||
expect( element ).toHaveClass( 'image-compare__comparison' ); | ||
expect( element ).toHaveClass( 'juxtapose' ); | ||
expect( element ).not.toHaveClass( 'image-compare__placeholder' ); | ||
} ); | ||
|
||
test( 'applies placeholder classes when without images', () => { | ||
const attributes = { ...defaultAttributes, ...emptyImages }; | ||
const element = renderImageCompare( { ...defaultProps, attributes } ); | ||
|
||
expect( element ).not.toHaveClass( 'image-compare__comparison' ); | ||
expect( element ).not.toHaveClass( 'juxtapose' ); | ||
expect( element ).toHaveClass( 'image-compare__placeholder' ); | ||
} ); | ||
|
||
test( 'applies fallback horizontal orientation in data-mode attribute', () => { | ||
const element = renderImageCompare( defaultProps ); | ||
|
||
expect( element ).toHaveAttribute( 'data-mode', 'horizontal' ); | ||
} ); | ||
|
||
test( 'applies selected orientation in data-mode attribute', () => { | ||
const attributes = { ...defaultAttributes, orientation: 'vertical' }; | ||
const element = renderImageCompare( { ...defaultProps, attributes } ); | ||
|
||
expect( element ).toHaveAttribute( 'data-mode', 'vertical' ); | ||
} ); | ||
|
||
test( 'displays Placeholder without selected images', () => { | ||
const attributes = { ...defaultAttributes, ...emptyImages }; | ||
render( <ImageCompareEdit { ...{ ...defaultProps, attributes } } /> ); | ||
|
||
expect( screen.getByText( 'Image before' ) ).toBeInTheDocument(); | ||
expect( screen.getByText( 'Image after' ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'displays caption component when selected and images present', () => { | ||
const attributes = { ...defaultAttributes, caption: undefined }; | ||
render( <ImageCompareEdit { ...{ ...defaultProps, attributes } } /> ); | ||
|
||
expect( screen.getByLabelText( 'Write caption' ) ).toBeInTheDocument(); | ||
} ); | ||
|
||
test( 'displays caption component when attribute set and block not selected', () => { | ||
render( <ImageCompareEdit { ...{ ...defaultProps, isSelected: false } } /> ); | ||
const caption = screen.getByLabelText( 'Write caption' ); | ||
|
||
expect( caption ).toBeInTheDocument(); | ||
expect( caption ).toHaveTextContent( defaultAttributes.caption ); | ||
} ); | ||
|
||
test( 'caption hidden when no value set and block not selected', () => { | ||
const attributes = { ...defaultAttributes, caption: undefined }; | ||
render( <ImageCompareEdit { ...{ ...defaultProps, attributes, isSelected: false } } /> ); | ||
|
||
expect( screen.queryByLabelText( 'Write caption' ) ).not.toBeInTheDocument(); | ||
} ); | ||
} ); |
4 changes: 4 additions & 0 deletions
4
...plugins/jetpack/extensions/blocks/image-compare/test/fixtures/jetpack__image-compare.html
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,4 @@ | ||
<!-- wp:jetpack/image-compare {"imageBefore":{"id":181,"url":"http://localhost:4759/wp-content/uploads/2021/03/jorge-gardner-hsU_aix2rVk-unsplash.jpg","alt":"","width":1024,"height":872},"imageAfter":{"id":182,"url":"http://localhost:4759/wp-content/uploads/2021/03/jorge-gardner-hsU_aix2rVk-unsplash2.jpg","alt":"","width":1024,"height":872}} --> | ||
<figure class="wp-block-jetpack-image-compare"><div class="juxtapose" data-mode="horizontal"><img id="181" src="http://localhost:4759/wp-content/uploads/2021/03/jorge-gardner-hsU_aix2rVk-unsplash.jpg" alt="" width="1024" height="872" class="image-compare__image-before"/><img id="182" src="http://localhost:4759/wp-content/uploads/2021/03/jorge-gardner-hsU_aix2rVk-unsplash2.jpg" alt="" width="1024" height="872" class="image-compare__image-after"/></div></figure> | ||
<!-- /wp:jetpack/image-compare --> | ||
|
26 changes: 26 additions & 0 deletions
26
...plugins/jetpack/extensions/blocks/image-compare/test/fixtures/jetpack__image-compare.json
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,26 @@ | ||
[ | ||
{ | ||
"clientId": "_clientId_0", | ||
"name": "jetpack/image-compare", | ||
"isValid": true, | ||
"attributes": { | ||
"imageBefore": { | ||
"id": 181, | ||
"url": "http://localhost:4759/wp-content/uploads/2021/03/jorge-gardner-hsU_aix2rVk-unsplash.jpg", | ||
"alt": "", | ||
"width": 1024, | ||
"height": 872 | ||
}, | ||
"imageAfter": { | ||
"id": 182, | ||
"url": "http://localhost:4759/wp-content/uploads/2021/03/jorge-gardner-hsU_aix2rVk-unsplash2.jpg", | ||
"alt": "", | ||
"width": 1024, | ||
"height": 872 | ||
}, | ||
"orientation": "horizontal" | ||
}, | ||
"innerBlocks": [], | ||
"originalContent": "<figure class=\"wp-block-jetpack-image-compare\"><div class=\"juxtapose\" data-mode=\"horizontal\"><img id=\"181\" src=\"http://localhost:4759/wp-content/uploads/2021/03/jorge-gardner-hsU_aix2rVk-unsplash.jpg\" alt=\"\" width=\"1024\" height=\"872\" class=\"image-compare__image-before\"/><img id=\"182\" src=\"http://localhost:4759/wp-content/uploads/2021/03/jorge-gardner-hsU_aix2rVk-unsplash2.jpg\" alt=\"\" width=\"1024\" height=\"872\" class=\"image-compare__image-after\"/></div></figure>" | ||
} | ||
] |
35 changes: 35 additions & 0 deletions
35
.../jetpack/extensions/blocks/image-compare/test/fixtures/jetpack__image-compare.parsed.json
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,35 @@ | ||
[ | ||
{ | ||
"blockName": "jetpack/image-compare", | ||
"attrs": { | ||
"imageBefore": { | ||
"id": 181, | ||
"url": "http://localhost:4759/wp-content/uploads/2021/03/jorge-gardner-hsU_aix2rVk-unsplash.jpg", | ||
"alt": "", | ||
"width": 1024, | ||
"height": 872 | ||
}, | ||
"imageAfter": { | ||
"id": 182, | ||
"url": "http://localhost:4759/wp-content/uploads/2021/03/jorge-gardner-hsU_aix2rVk-unsplash2.jpg", | ||
"alt": "", | ||
"width": 1024, | ||
"height": 872 | ||
} | ||
}, | ||
"innerBlocks": [], | ||
"innerHTML": "\n<figure class=\"wp-block-jetpack-image-compare\"><div class=\"juxtapose\" data-mode=\"horizontal\"><img id=\"181\" src=\"http://localhost:4759/wp-content/uploads/2021/03/jorge-gardner-hsU_aix2rVk-unsplash.jpg\" alt=\"\" width=\"1024\" height=\"872\" class=\"image-compare__image-before\"/><img id=\"182\" src=\"http://localhost:4759/wp-content/uploads/2021/03/jorge-gardner-hsU_aix2rVk-unsplash2.jpg\" alt=\"\" width=\"1024\" height=\"872\" class=\"image-compare__image-after\"/></div></figure>\n", | ||
"innerContent": [ | ||
"\n<figure class=\"wp-block-jetpack-image-compare\"><div class=\"juxtapose\" data-mode=\"horizontal\"><img id=\"181\" src=\"http://localhost:4759/wp-content/uploads/2021/03/jorge-gardner-hsU_aix2rVk-unsplash.jpg\" alt=\"\" width=\"1024\" height=\"872\" class=\"image-compare__image-before\"/><img id=\"182\" src=\"http://localhost:4759/wp-content/uploads/2021/03/jorge-gardner-hsU_aix2rVk-unsplash2.jpg\" alt=\"\" width=\"1024\" height=\"872\" class=\"image-compare__image-after\"/></div></figure>\n" | ||
] | ||
}, | ||
{ | ||
"blockName": null, | ||
"attrs": {}, | ||
"innerBlocks": [], | ||
"innerHTML": "\n\n", | ||
"innerContent": [ | ||
"\n\n" | ||
] | ||
} | ||
] |
3 changes: 3 additions & 0 deletions
3
...pack/extensions/blocks/image-compare/test/fixtures/jetpack__image-compare.serialized.html
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,3 @@ | ||
<!-- wp:jetpack/image-compare {"imageBefore":{"id":181,"url":"http://localhost:4759/wp-content/uploads/2021/03/jorge-gardner-hsU_aix2rVk-unsplash.jpg","alt":"","width":1024,"height":872},"imageAfter":{"id":182,"url":"http://localhost:4759/wp-content/uploads/2021/03/jorge-gardner-hsU_aix2rVk-unsplash2.jpg","alt":"","width":1024,"height":872}} --> | ||
<figure class="wp-block-jetpack-image-compare"><div class="juxtapose" data-mode="horizontal"><img id="181" src="http://localhost:4759/wp-content/uploads/2021/03/jorge-gardner-hsU_aix2rVk-unsplash.jpg" alt="" width="1024" height="872" class="image-compare__image-before"/><img id="182" src="http://localhost:4759/wp-content/uploads/2021/03/jorge-gardner-hsU_aix2rVk-unsplash2.jpg" alt="" width="1024" height="872" class="image-compare__image-after"/></div></figure> | ||
<!-- /wp:jetpack/image-compare --> |
46 changes: 46 additions & 0 deletions
46
projects/plugins/jetpack/extensions/blocks/image-compare/test/img-upload.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,46 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ImgUpload from '../img-upload'; | ||
|
||
describe( 'ImgUpload', () => { | ||
const onChange = jest.fn(); | ||
const image = { | ||
id: 1, | ||
url: 'https://test.com/test.jpg', | ||
alt: 'Image description', | ||
}; | ||
const defaultProps = { | ||
image, | ||
placeHolderLabel: 'Image Placeholder Label', | ||
onChange, | ||
}; | ||
|
||
test( 'display placeholder when no image present', () => { | ||
render( <ImgUpload { ...{ ...defaultProps, image: {} } } /> ); | ||
|
||
expect( screen.getByText( defaultProps.placeHolderLabel ) ).toBeInTheDocument(); | ||
expect( screen.queryByRole( 'img' ) ).not.toBeInTheDocument(); | ||
} ) | ||
|
||
test( 'displays image when available', () => { | ||
render( <ImgUpload { ...defaultProps } /> ); | ||
const element = screen.getByRole( 'img' ); | ||
|
||
expect( screen.queryByText( defaultProps.placeHolderLabel ) ).not.toBeInTheDocument(); | ||
expect( element ).toBeInTheDocument(); | ||
expect( element ).toHaveAttribute( 'id', `${ image.id }` ); | ||
expect( element ).toHaveAttribute( 'src', image.url ); | ||
expect( element ).toHaveAttribute( 'alt', image.alt ); | ||
} ); | ||
} ); |
8 changes: 8 additions & 0 deletions
8
projects/plugins/jetpack/extensions/blocks/image-compare/test/validate.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,8 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { name, settings } from '../'; | ||
import runBlockFixtureTests from '../../../shared/test/block-fixtures'; | ||
|
||
const blocks = [ { name: `jetpack/${ name }`, settings } ]; | ||
runBlockFixtureTests( `jetpack/${ name }`, blocks, __dirname ); |
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