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