Skip to content

Commit be5e8ce

Browse files
committed
Add tests for the GalleryImageSave functionality
1 parent 5e90cc7 commit be5e8ce

File tree

2 files changed

+102
-1
lines changed
  • projects/plugins/jetpack/extensions/blocks/tiled-gallery/gallery-image

2 files changed

+102
-1
lines changed

projects/plugins/jetpack/extensions/blocks/tiled-gallery/gallery-image/save.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export default function GalleryImageSave( props ) {
2525
href = '';
2626
}
2727

28+
const ampLayout = ! isSimpleSite() ? { 'data-amp-layout': 'responsive' } : {};
29+
2830
const img = (
2931
<img
3032
alt={ alt }
@@ -34,7 +36,7 @@ export default function GalleryImageSave( props ) {
3436
data-url={ origUrl }
3537
data-width={ width }
3638
src={ url }
37-
{ ...( ! isSimpleSite && { 'data-amp-layout': 'responsive' } ) } // This is stripped on Simple sites causing block validation issues (and is also not needed there) - see _wpcom_remove_data_wildcard_attribute in wp-content/mu-plugins/wpcom-kses-config.php on WPCom.
39+
{ ...ampLayout } // This is stripped on Simple sites causing block validation issues (and is also not needed there) - see _wpcom_remove_data_wildcard_attribute in wp-content/mu-plugins/wpcom-kses-config.php on WPCom.
3840
/>
3941
);
4042

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { render, screen } from '@testing-library/react';
2+
import GalleryImageSave from '../save';
3+
4+
jest.mock( '@wordpress/blob', () => ( {
5+
isBlobURL: jest.fn( () => false ),
6+
} ) );
7+
8+
jest.mock( '@wordpress/block-editor', () => ( {
9+
...jest.requireActual( '@wordpress/block-editor' ),
10+
useBlockProps: jest.fn(),
11+
} ) );
12+
13+
const mockIsSimpleSite = jest.fn();
14+
15+
jest.mock( '@automattic/jetpack-script-data', () => ( {
16+
isSimpleSite: () => mockIsSimpleSite(),
17+
} ) );
18+
19+
const mockRender = jest.fn();
20+
21+
jest.mock( '@wordpress/element', () => {
22+
const actualElement = jest.requireActual( '@wordpress/element' );
23+
return {
24+
...actualElement,
25+
createRoot: () => ( {
26+
render: mockRender,
27+
} ),
28+
memo: component => component,
29+
};
30+
} );
31+
32+
const defaultProps = {
33+
alt: 'Test image',
34+
imageFilter: 'grayscale',
35+
height: 300,
36+
id: 123,
37+
link: 'http://example.com/attachment',
38+
linkTo: 'attachment',
39+
customLink: '',
40+
origUrl: 'http://example.com/image.jpg',
41+
url: 'http://example.com/image.jpg',
42+
width: 400,
43+
};
44+
45+
describe( 'Gallery Image Save', () => {
46+
beforeEach( () => {
47+
document.body.innerHTML = '';
48+
jest.clearAllMocks();
49+
} );
50+
51+
it( 'renders image with data-amp-layout when not a simple site', () => {
52+
mockIsSimpleSite.mockReturnValue( false );
53+
const { container } = render( <GalleryImageSave { ...defaultProps } /> );
54+
expect( container.innerHTML ).toContain( 'data-amp-layout="responsive"' );
55+
} );
56+
57+
it( 'renders image without data-amp-layout when a simple site', () => {
58+
mockIsSimpleSite.mockReturnValue( true );
59+
const { container } = render( <GalleryImageSave { ...defaultProps } /> );
60+
expect( container.innerHTML ).not.toContain( 'data-amp-layout' );
61+
} );
62+
63+
it( 'handles blob URLs by returning null', () => {
64+
jest.requireMock( '@wordpress/blob' ).isBlobURL.mockReturnValueOnce( true );
65+
const { container } = render( <GalleryImageSave { ...defaultProps } /> );
66+
expect( container ).toBeEmptyDOMElement();
67+
} );
68+
69+
it( 'renders image with custom link', () => {
70+
const props = {
71+
...defaultProps,
72+
linkTo: 'custom',
73+
customLink: 'http://example.com/custom',
74+
};
75+
render( <GalleryImageSave { ...props } /> );
76+
const link = screen.getByRole( 'link' );
77+
expect( link ).toHaveAttribute( 'href', 'http://example.com/custom' );
78+
} );
79+
80+
it( 'renders image with media link', () => {
81+
const props = {
82+
...defaultProps,
83+
linkTo: 'media',
84+
};
85+
render( <GalleryImageSave { ...props } /> );
86+
const link = screen.getByRole( 'link' );
87+
expect( link ).toHaveAttribute( 'href', 'http://example.com/image.jpg' );
88+
} );
89+
90+
it( 'renders image without link', () => {
91+
const props = {
92+
...defaultProps,
93+
linkTo: 'none',
94+
};
95+
render( <GalleryImageSave { ...props } /> );
96+
expect( screen.getByRole( 'img' ) ).toBeInTheDocument();
97+
expect( screen.queryByRole( 'link' ) ).not.toBeInTheDocument();
98+
} );
99+
} );

0 commit comments

Comments
 (0)