|
| 1 | +/** |
| 2 | + * Unit tests for block-editor.js - filter registration and module behavior |
| 3 | + * |
| 4 | + * Note: Testing React HOCs that use hooks requires careful mocking. |
| 5 | + * These tests focus on verifying module setup and filter registration. |
| 6 | + */ |
| 7 | + |
| 8 | +import { addFilter } from '@wordpress/hooks'; |
| 9 | + |
| 10 | +// Mock WordPress dependencies |
| 11 | +jest.mock( '@wordpress/hooks', () => ( { |
| 12 | + addFilter: jest.fn(), |
| 13 | +} ) ); |
| 14 | + |
| 15 | +jest.mock( '@wordpress/element', () => ( { |
| 16 | + useCallback: jest.fn( ( fn ) => fn ), |
| 17 | + useMemo: jest.fn( ( fn ) => fn() ), |
| 18 | +} ) ); |
| 19 | + |
| 20 | +jest.mock( '@wordpress/compose', () => ( { |
| 21 | + createHigherOrderComponent: jest.fn( ( fn, name ) => { |
| 22 | + const hoc = fn; |
| 23 | + hoc.displayName = name; |
| 24 | + return hoc; |
| 25 | + } ), |
| 26 | +} ) ); |
| 27 | + |
| 28 | +jest.mock( '@wordpress/block-editor', () => ( { |
| 29 | + InspectorControls: 'InspectorControls', |
| 30 | + useBlockBindingsUtils: jest.fn( () => ( { |
| 31 | + updateBlockBindings: jest.fn(), |
| 32 | + removeAllBlockBindings: jest.fn(), |
| 33 | + } ) ), |
| 34 | +} ) ); |
| 35 | + |
| 36 | +jest.mock( '@wordpress/components', () => ( { |
| 37 | + ComboboxControl: 'ComboboxControl', |
| 38 | + __experimentalToolsPanel: 'ToolsPanel', |
| 39 | + __experimentalToolsPanelItem: 'ToolsPanelItem', |
| 40 | +} ) ); |
| 41 | + |
| 42 | +jest.mock( '@wordpress/i18n', () => ( { |
| 43 | + __: ( str ) => str, |
| 44 | +} ) ); |
| 45 | + |
| 46 | +jest.mock( '../../../assets/src/js/bindings/constants', () => ( { |
| 47 | + BINDING_SOURCE: 'acf/field', |
| 48 | +} ) ); |
| 49 | + |
| 50 | +jest.mock( '../../../assets/src/js/bindings/utils', () => ( { |
| 51 | + getBindableAttributes: jest.fn( () => [] ), |
| 52 | + getFilteredFieldOptions: jest.fn( () => [] ), |
| 53 | + canUseUnifiedBinding: jest.fn( () => false ), |
| 54 | + fieldsToOptions: jest.fn( () => [] ), |
| 55 | +} ) ); |
| 56 | + |
| 57 | +jest.mock( '../../../assets/src/js/bindings/hooks', () => ( { |
| 58 | + useSiteEditorContext: jest.fn( () => ( { |
| 59 | + isSiteEditor: false, |
| 60 | + templatePostType: null, |
| 61 | + } ) ), |
| 62 | + usePostEditorFields: jest.fn( () => ( {} ) ), |
| 63 | + useSiteEditorFields: jest.fn( () => ( { fields: {}, isLoading: false } ) ), |
| 64 | + useBoundFields: jest.fn( () => ( { |
| 65 | + boundFields: {}, |
| 66 | + setBoundFields: jest.fn(), |
| 67 | + } ) ), |
| 68 | +} ) ); |
| 69 | + |
| 70 | +describe( 'Block Editor Module', () => { |
| 71 | + beforeEach( () => { |
| 72 | + jest.clearAllMocks(); |
| 73 | + } ); |
| 74 | + |
| 75 | + describe( 'Filter Registration', () => { |
| 76 | + it( 'should register the editor.BlockEdit filter on module load', () => { |
| 77 | + jest.isolateModules( () => { |
| 78 | + require( '../../../assets/src/js/bindings/block-editor' ); |
| 79 | + } ); |
| 80 | + |
| 81 | + expect( addFilter ).toHaveBeenCalledWith( |
| 82 | + 'editor.BlockEdit', |
| 83 | + 'secure-custom-fields/with-custom-controls', |
| 84 | + expect.any( Function ) |
| 85 | + ); |
| 86 | + } ); |
| 87 | + |
| 88 | + it( 'should register filter with correct hook name', () => { |
| 89 | + jest.isolateModules( () => { |
| 90 | + require( '../../../assets/src/js/bindings/block-editor' ); |
| 91 | + } ); |
| 92 | + |
| 93 | + const [ hookName ] = addFilter.mock.calls[ 0 ]; |
| 94 | + expect( hookName ).toBe( 'editor.BlockEdit' ); |
| 95 | + } ); |
| 96 | + |
| 97 | + it( 'should register filter with correct namespace', () => { |
| 98 | + jest.isolateModules( () => { |
| 99 | + require( '../../../assets/src/js/bindings/block-editor' ); |
| 100 | + } ); |
| 101 | + |
| 102 | + const [ , namespace ] = addFilter.mock.calls[ 0 ]; |
| 103 | + expect( namespace ).toBe( |
| 104 | + 'secure-custom-fields/with-custom-controls' |
| 105 | + ); |
| 106 | + } ); |
| 107 | + |
| 108 | + it( 'should register filter with a function callback', () => { |
| 109 | + jest.isolateModules( () => { |
| 110 | + require( '../../../assets/src/js/bindings/block-editor' ); |
| 111 | + } ); |
| 112 | + |
| 113 | + const [ , , callback ] = addFilter.mock.calls[ 0 ]; |
| 114 | + expect( typeof callback ).toBe( 'function' ); |
| 115 | + } ); |
| 116 | + |
| 117 | + it( 'should only register the filter once', () => { |
| 118 | + jest.isolateModules( () => { |
| 119 | + require( '../../../assets/src/js/bindings/block-editor' ); |
| 120 | + } ); |
| 121 | + |
| 122 | + expect( addFilter ).toHaveBeenCalledTimes( 1 ); |
| 123 | + } ); |
| 124 | + } ); |
| 125 | + |
| 126 | + describe( 'HOC Creation', () => { |
| 127 | + it( 'should create a higher order component', () => { |
| 128 | + const { |
| 129 | + createHigherOrderComponent, |
| 130 | + } = require( '@wordpress/compose' ); |
| 131 | + |
| 132 | + jest.isolateModules( () => { |
| 133 | + require( '../../../assets/src/js/bindings/block-editor' ); |
| 134 | + } ); |
| 135 | + |
| 136 | + expect( createHigherOrderComponent ).toHaveBeenCalledWith( |
| 137 | + expect.any( Function ), |
| 138 | + 'withCustomControls' |
| 139 | + ); |
| 140 | + } ); |
| 141 | + |
| 142 | + it( 'should name the HOC "withCustomControls"', () => { |
| 143 | + const { |
| 144 | + createHigherOrderComponent, |
| 145 | + } = require( '@wordpress/compose' ); |
| 146 | + |
| 147 | + jest.isolateModules( () => { |
| 148 | + require( '../../../assets/src/js/bindings/block-editor' ); |
| 149 | + } ); |
| 150 | + |
| 151 | + const [ , hocName ] = createHigherOrderComponent.mock.calls[ 0 ]; |
| 152 | + expect( hocName ).toBe( 'withCustomControls' ); |
| 153 | + } ); |
| 154 | + } ); |
| 155 | + |
| 156 | + describe( 'Filter Callback', () => { |
| 157 | + let filterCallback; |
| 158 | + |
| 159 | + beforeEach( () => { |
| 160 | + jest.isolateModules( () => { |
| 161 | + require( '../../../assets/src/js/bindings/block-editor' ); |
| 162 | + } ); |
| 163 | + filterCallback = addFilter.mock.calls[ 0 ][ 2 ]; |
| 164 | + } ); |
| 165 | + |
| 166 | + it( 'should return a function when passed a BlockEdit component', () => { |
| 167 | + const MockBlockEdit = () => null; |
| 168 | + const result = filterCallback( MockBlockEdit ); |
| 169 | + |
| 170 | + expect( typeof result ).toBe( 'function' ); |
| 171 | + } ); |
| 172 | + |
| 173 | + it( 'should wrap different BlockEdit components independently', () => { |
| 174 | + const BlockEdit1 = () => 'edit1'; |
| 175 | + const BlockEdit2 = () => 'edit2'; |
| 176 | + |
| 177 | + const wrapped1 = filterCallback( BlockEdit1 ); |
| 178 | + const wrapped2 = filterCallback( BlockEdit2 ); |
| 179 | + |
| 180 | + expect( wrapped1 ).not.toBe( wrapped2 ); |
| 181 | + } ); |
| 182 | + } ); |
| 183 | +} ); |
0 commit comments