|
| 1 | +/** |
| 2 | + * Unit tests for BlockPlaceholder component |
| 3 | + * Tests the placeholder UI shown when a block has no preview HTML |
| 4 | + */ |
| 5 | + |
| 6 | +import React from 'react'; |
| 7 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 8 | +import '@testing-library/jest-dom'; |
| 9 | + |
| 10 | +import { BlockPlaceholder } from '../../../assets/src/js/pro/blocks-v3/components/block-placeholder'; |
| 11 | + |
| 12 | +// Mock the acf global object |
| 13 | +global.acf = { |
| 14 | + __: jest.fn( ( key ) => { |
| 15 | + const translations = { |
| 16 | + 'Edit Block': 'Edit Block', |
| 17 | + }; |
| 18 | + return translations[ key ] || key; |
| 19 | + } ), |
| 20 | +}; |
| 21 | + |
| 22 | +describe( 'BlockPlaceholder Component', () => { |
| 23 | + beforeEach( () => { |
| 24 | + jest.clearAllMocks(); |
| 25 | + } ); |
| 26 | + |
| 27 | + test( 'renders with block label', () => { |
| 28 | + const mockSetModalOpen = jest.fn(); |
| 29 | + |
| 30 | + render( |
| 31 | + <BlockPlaceholder |
| 32 | + blockLabel="Test Block" |
| 33 | + setBlockFormModalOpen={ mockSetModalOpen } |
| 34 | + /> |
| 35 | + ); |
| 36 | + |
| 37 | + expect( screen.getByTestId( 'placeholder' ) ).toBeInTheDocument(); |
| 38 | + expect( screen.getByTestId( 'placeholder-label' ) ).toHaveTextContent( |
| 39 | + 'Test Block' |
| 40 | + ); |
| 41 | + } ); |
| 42 | + |
| 43 | + test( 'renders with instructions when provided', () => { |
| 44 | + const mockSetModalOpen = jest.fn(); |
| 45 | + |
| 46 | + render( |
| 47 | + <BlockPlaceholder |
| 48 | + blockLabel="Test Block" |
| 49 | + setBlockFormModalOpen={ mockSetModalOpen } |
| 50 | + instructions="Please fill in the block fields" |
| 51 | + /> |
| 52 | + ); |
| 53 | + |
| 54 | + expect( |
| 55 | + screen.getByTestId( 'placeholder-instructions' ) |
| 56 | + ).toHaveTextContent( 'Please fill in the block fields' ); |
| 57 | + } ); |
| 58 | + |
| 59 | + test( 'does not render instructions when not provided', () => { |
| 60 | + const mockSetModalOpen = jest.fn(); |
| 61 | + |
| 62 | + render( |
| 63 | + <BlockPlaceholder |
| 64 | + blockLabel="Test Block" |
| 65 | + setBlockFormModalOpen={ mockSetModalOpen } |
| 66 | + /> |
| 67 | + ); |
| 68 | + |
| 69 | + expect( |
| 70 | + screen.queryByTestId( 'placeholder-instructions' ) |
| 71 | + ).not.toBeInTheDocument(); |
| 72 | + } ); |
| 73 | + |
| 74 | + test( 'renders Edit Block button', () => { |
| 75 | + const mockSetModalOpen = jest.fn(); |
| 76 | + |
| 77 | + render( |
| 78 | + <BlockPlaceholder |
| 79 | + blockLabel="Test Block" |
| 80 | + setBlockFormModalOpen={ mockSetModalOpen } |
| 81 | + /> |
| 82 | + ); |
| 83 | + |
| 84 | + const button = screen.getByRole( 'button' ); |
| 85 | + expect( button ).toHaveTextContent( 'Edit Block' ); |
| 86 | + } ); |
| 87 | + |
| 88 | + test( 'calls setBlockFormModalOpen with true when Edit Block button is clicked', () => { |
| 89 | + const mockSetModalOpen = jest.fn(); |
| 90 | + |
| 91 | + render( |
| 92 | + <BlockPlaceholder |
| 93 | + blockLabel="Test Block" |
| 94 | + setBlockFormModalOpen={ mockSetModalOpen } |
| 95 | + /> |
| 96 | + ); |
| 97 | + |
| 98 | + const button = screen.getByRole( 'button' ); |
| 99 | + fireEvent.click( button ); |
| 100 | + |
| 101 | + expect( mockSetModalOpen ).toHaveBeenCalledTimes( 1 ); |
| 102 | + expect( mockSetModalOpen ).toHaveBeenCalledWith( true ); |
| 103 | + } ); |
| 104 | + |
| 105 | + test( 'renders icon in placeholder', () => { |
| 106 | + const mockSetModalOpen = jest.fn(); |
| 107 | + |
| 108 | + render( |
| 109 | + <BlockPlaceholder |
| 110 | + blockLabel="Test Block" |
| 111 | + setBlockFormModalOpen={ mockSetModalOpen } |
| 112 | + /> |
| 113 | + ); |
| 114 | + |
| 115 | + expect( screen.getByTestId( 'icon' ) ).toBeInTheDocument(); |
| 116 | + } ); |
| 117 | + |
| 118 | + test( 'renders button with primary variant', () => { |
| 119 | + const mockSetModalOpen = jest.fn(); |
| 120 | + |
| 121 | + render( |
| 122 | + <BlockPlaceholder |
| 123 | + blockLabel="Test Block" |
| 124 | + setBlockFormModalOpen={ mockSetModalOpen } |
| 125 | + /> |
| 126 | + ); |
| 127 | + |
| 128 | + const button = screen.getByRole( 'button' ); |
| 129 | + expect( button ).toHaveAttribute( 'data-variant', 'primary' ); |
| 130 | + } ); |
| 131 | + |
| 132 | + test( 'uses acf.__ for translations', () => { |
| 133 | + const mockSetModalOpen = jest.fn(); |
| 134 | + |
| 135 | + render( |
| 136 | + <BlockPlaceholder |
| 137 | + blockLabel="Test Block" |
| 138 | + setBlockFormModalOpen={ mockSetModalOpen } |
| 139 | + /> |
| 140 | + ); |
| 141 | + |
| 142 | + expect( global.acf.__ ).toHaveBeenCalledWith( 'Edit Block' ); |
| 143 | + } ); |
| 144 | + |
| 145 | + test( 'renders with different block labels', () => { |
| 146 | + const mockSetModalOpen = jest.fn(); |
| 147 | + const testCases = [ |
| 148 | + 'Testimonial Block', |
| 149 | + 'Image Gallery', |
| 150 | + 'Contact Form', |
| 151 | + 'Hero Section', |
| 152 | + ]; |
| 153 | + |
| 154 | + testCases.forEach( ( label ) => { |
| 155 | + const { unmount } = render( |
| 156 | + <BlockPlaceholder |
| 157 | + blockLabel={ label } |
| 158 | + setBlockFormModalOpen={ mockSetModalOpen } |
| 159 | + /> |
| 160 | + ); |
| 161 | + |
| 162 | + expect( |
| 163 | + screen.getByTestId( 'placeholder-label' ) |
| 164 | + ).toHaveTextContent( label ); |
| 165 | + unmount(); |
| 166 | + } ); |
| 167 | + } ); |
| 168 | +} ); |
0 commit comments