|
| 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