@wordpress/edit-widgets 7.1.0 → 7.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/components/error-boundary/index.cjs +1 -1
- package/build/components/error-boundary/index.cjs.map +1 -1
- package/build/components/header/index.cjs +7 -2
- package/build/components/header/index.cjs.map +1 -1
- package/build/components/keyboard-shortcut-help-modal/shortcut.cjs +1 -0
- package/build/components/keyboard-shortcut-help-modal/shortcut.cjs.map +1 -1
- package/build/components/layout/unsaved-changes-warning.cjs +3 -6
- package/build/components/layout/unsaved-changes-warning.cjs.map +1 -1
- package/build/components/more-menu/index.cjs +0 -1
- package/build/components/more-menu/index.cjs.map +1 -1
- package/build/store/actions.cjs +9 -2
- package/build/store/actions.cjs.map +1 -1
- package/build-module/components/error-boundary/index.mjs +2 -2
- package/build-module/components/error-boundary/index.mjs.map +1 -1
- package/build-module/components/header/index.mjs +6 -2
- package/build-module/components/header/index.mjs.map +1 -1
- package/build-module/components/keyboard-shortcut-help-modal/shortcut.mjs +1 -0
- package/build-module/components/keyboard-shortcut-help-modal/shortcut.mjs.map +1 -1
- package/build-module/components/layout/unsaved-changes-warning.mjs +3 -6
- package/build-module/components/layout/unsaved-changes-warning.mjs.map +1 -1
- package/build-module/components/more-menu/index.mjs +0 -1
- package/build-module/components/more-menu/index.mjs.map +1 -1
- package/build-module/store/actions.mjs +9 -2
- package/build-module/store/actions.mjs.map +1 -1
- package/build-style/style-rtl.css +32 -11
- package/build-style/style.css +32 -11
- package/package.json +33 -33
- package/src/components/error-boundary/index.jsx +2 -2
- package/src/components/header/index.jsx +11 -2
- package/src/components/header/style.scss +30 -3
- package/src/components/keyboard-shortcut-help-modal/shortcut.jsx +1 -0
- package/src/components/keyboard-shortcut-help-modal/style.scss +1 -1
- package/src/components/layout/style.scss +5 -9
- package/src/components/layout/unsaved-changes-warning.js +4 -9
- package/src/components/more-menu/index.jsx +0 -1
- package/src/store/actions.js +25 -2
- package/src/store/test/actions.jsdom.test.js +230 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { saveWidgetArea } from '../actions';
|
|
3
|
+
|
|
4
|
+
// Only the store descriptors are imported from these packages; stub them so
|
|
5
|
+
// their browser-only side effects do not run.
|
|
6
|
+
vi.mock( import( '@wordpress/interface' ), () => ( {
|
|
7
|
+
store: 'core/interface',
|
|
8
|
+
} ) );
|
|
9
|
+
vi.mock( import( '@wordpress/block-editor' ), () => ( {
|
|
10
|
+
store: 'core/block-editor',
|
|
11
|
+
} ) );
|
|
12
|
+
vi.mock( import( '@wordpress/notices' ), () => ( { store: 'core/notices' } ) );
|
|
13
|
+
|
|
14
|
+
vi.mock( import( '../transformers' ), () => ( {
|
|
15
|
+
transformBlockToWidget: ( block, relatedWidget = {} ) => ( {
|
|
16
|
+
...relatedWidget,
|
|
17
|
+
id_base: 'block',
|
|
18
|
+
instance: { raw: { content: block.attributes.content } },
|
|
19
|
+
} ),
|
|
20
|
+
} ) );
|
|
21
|
+
|
|
22
|
+
function setup(
|
|
23
|
+
blocks,
|
|
24
|
+
widgets,
|
|
25
|
+
{ area = 'sidebar-1', areaHoldingId = 'sidebar-1' } = {}
|
|
26
|
+
) {
|
|
27
|
+
let nextWidgetNumber = 10;
|
|
28
|
+
const saveEntityRecord = vi.fn( () => ( {
|
|
29
|
+
id: `block-${ nextWidgetNumber++ }`,
|
|
30
|
+
} ) );
|
|
31
|
+
const saveEditedEntityRecord = vi.fn( ( kind, name, id ) => ( { id } ) );
|
|
32
|
+
const coreDispatch = {
|
|
33
|
+
editEntityRecord: vi.fn(),
|
|
34
|
+
receiveEntityRecords: vi.fn(),
|
|
35
|
+
saveEditedEntityRecord: vi.fn(),
|
|
36
|
+
__experimentalBatch: vi.fn( async ( tasks ) =>
|
|
37
|
+
Promise.all(
|
|
38
|
+
tasks.map( ( task ) =>
|
|
39
|
+
task( { saveEntityRecord, saveEditedEntityRecord } )
|
|
40
|
+
)
|
|
41
|
+
)
|
|
42
|
+
),
|
|
43
|
+
};
|
|
44
|
+
const coreSelect = {
|
|
45
|
+
getEditedEntityRecord: () => ( { blocks } ),
|
|
46
|
+
hasEditsForEntityRecord: () => true,
|
|
47
|
+
getLastEntitySaveError: () => undefined,
|
|
48
|
+
};
|
|
49
|
+
const registry = {
|
|
50
|
+
select: () => coreSelect,
|
|
51
|
+
dispatch: () => coreDispatch,
|
|
52
|
+
};
|
|
53
|
+
const select = {
|
|
54
|
+
getWidgets: () => widgets,
|
|
55
|
+
getWidgetAreaForWidgetId: () => ( { id: areaHoldingId } ),
|
|
56
|
+
};
|
|
57
|
+
return {
|
|
58
|
+
run: () =>
|
|
59
|
+
saveWidgetArea( area )( {
|
|
60
|
+
dispatch: vi.fn(),
|
|
61
|
+
select,
|
|
62
|
+
registry,
|
|
63
|
+
} ),
|
|
64
|
+
coreDispatch,
|
|
65
|
+
saveEntityRecord,
|
|
66
|
+
saveEditedEntityRecord,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
describe( 'saveWidgetArea', () => {
|
|
71
|
+
it( 'saves a block that shares an existing widget ID with an earlier block as a new widget', async () => {
|
|
72
|
+
// Splitting a paragraph widget copies the block's attributes,
|
|
73
|
+
// including `__internalWidgetId`, to the new block.
|
|
74
|
+
const blocks = [
|
|
75
|
+
{
|
|
76
|
+
name: 'core/paragraph',
|
|
77
|
+
clientId: 'head',
|
|
78
|
+
attributes: { content: 'Hello', __internalWidgetId: 'block-2' },
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
name: 'core/paragraph',
|
|
82
|
+
clientId: 'tail',
|
|
83
|
+
attributes: { content: 'world', __internalWidgetId: 'block-2' },
|
|
84
|
+
},
|
|
85
|
+
];
|
|
86
|
+
const widgets = {
|
|
87
|
+
'block-2': {
|
|
88
|
+
id: 'block-2',
|
|
89
|
+
id_base: 'block',
|
|
90
|
+
sidebar: 'sidebar-1',
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
const { run, coreDispatch, saveEntityRecord, saveEditedEntityRecord } =
|
|
94
|
+
setup( blocks, widgets );
|
|
95
|
+
|
|
96
|
+
await run();
|
|
97
|
+
|
|
98
|
+
const widgetEdits = coreDispatch.editEntityRecord.mock.calls.filter(
|
|
99
|
+
( [ kind, name ] ) => kind === 'root' && name === 'widget'
|
|
100
|
+
);
|
|
101
|
+
expect( widgetEdits ).toHaveLength( 1 );
|
|
102
|
+
expect( widgetEdits[ 0 ][ 3 ].instance.raw.content ).toBe( 'Hello' );
|
|
103
|
+
expect( saveEditedEntityRecord ).toHaveBeenCalledTimes( 1 );
|
|
104
|
+
expect( saveEntityRecord ).toHaveBeenCalledTimes( 1 );
|
|
105
|
+
expect( saveEntityRecord.mock.calls[ 0 ][ 2 ] ).toMatchObject( {
|
|
106
|
+
sidebar: 'sidebar-1',
|
|
107
|
+
instance: { raw: { content: 'world' } },
|
|
108
|
+
} );
|
|
109
|
+
|
|
110
|
+
expect( coreDispatch.editEntityRecord ).toHaveBeenCalledWith(
|
|
111
|
+
'root',
|
|
112
|
+
'sidebar',
|
|
113
|
+
'sidebar-1',
|
|
114
|
+
{ widgets: [ 'block-2', 'block-10' ] },
|
|
115
|
+
{ undoIgnore: true }
|
|
116
|
+
);
|
|
117
|
+
expect( blocks[ 1 ].attributes.__internalWidgetId ).toBe( 'block-10' );
|
|
118
|
+
} );
|
|
119
|
+
|
|
120
|
+
it( 'keeps updating existing widgets when every block has its own widget ID', async () => {
|
|
121
|
+
const blocks = [
|
|
122
|
+
{
|
|
123
|
+
name: 'core/paragraph',
|
|
124
|
+
clientId: 'a',
|
|
125
|
+
attributes: { content: 'A', __internalWidgetId: 'block-2' },
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
name: 'core/paragraph',
|
|
129
|
+
clientId: 'b',
|
|
130
|
+
attributes: { content: 'B', __internalWidgetId: 'block-3' },
|
|
131
|
+
},
|
|
132
|
+
];
|
|
133
|
+
const widgets = {
|
|
134
|
+
'block-2': {
|
|
135
|
+
id: 'block-2',
|
|
136
|
+
id_base: 'block',
|
|
137
|
+
sidebar: 'sidebar-1',
|
|
138
|
+
},
|
|
139
|
+
'block-3': {
|
|
140
|
+
id: 'block-3',
|
|
141
|
+
id_base: 'block',
|
|
142
|
+
sidebar: 'sidebar-1',
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
const { run, coreDispatch, saveEntityRecord, saveEditedEntityRecord } =
|
|
146
|
+
setup( blocks, widgets );
|
|
147
|
+
|
|
148
|
+
await run();
|
|
149
|
+
|
|
150
|
+
expect( saveEntityRecord ).not.toHaveBeenCalled();
|
|
151
|
+
expect( saveEditedEntityRecord ).toHaveBeenCalledTimes( 2 );
|
|
152
|
+
expect( coreDispatch.editEntityRecord ).toHaveBeenCalledWith(
|
|
153
|
+
'root',
|
|
154
|
+
'sidebar',
|
|
155
|
+
'sidebar-1',
|
|
156
|
+
{ widgets: [ 'block-2', 'block-3' ] },
|
|
157
|
+
{ undoIgnore: true }
|
|
158
|
+
);
|
|
159
|
+
} );
|
|
160
|
+
|
|
161
|
+
it( 'saves a block as a new widget when an earlier widget area holds the same widget ID', async () => {
|
|
162
|
+
// The new block from a split was moved to another widget area, so both
|
|
163
|
+
// areas hold a block with `block-2`. The first area keeps the widget.
|
|
164
|
+
const blocks = [
|
|
165
|
+
{
|
|
166
|
+
name: 'core/paragraph',
|
|
167
|
+
clientId: 'tail',
|
|
168
|
+
attributes: { content: 'world', __internalWidgetId: 'block-2' },
|
|
169
|
+
},
|
|
170
|
+
];
|
|
171
|
+
const widgets = {
|
|
172
|
+
'block-2': {
|
|
173
|
+
id: 'block-2',
|
|
174
|
+
id_base: 'block',
|
|
175
|
+
sidebar: 'sidebar-1',
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
const { run, coreDispatch, saveEntityRecord, saveEditedEntityRecord } =
|
|
179
|
+
setup( blocks, widgets, {
|
|
180
|
+
area: 'sidebar-2',
|
|
181
|
+
areaHoldingId: 'sidebar-1',
|
|
182
|
+
} );
|
|
183
|
+
|
|
184
|
+
await run();
|
|
185
|
+
|
|
186
|
+
expect( saveEditedEntityRecord ).not.toHaveBeenCalled();
|
|
187
|
+
expect( saveEntityRecord ).toHaveBeenCalledTimes( 1 );
|
|
188
|
+
expect( saveEntityRecord.mock.calls[ 0 ][ 2 ] ).toMatchObject( {
|
|
189
|
+
sidebar: 'sidebar-2',
|
|
190
|
+
} );
|
|
191
|
+
expect( coreDispatch.editEntityRecord ).toHaveBeenCalledWith(
|
|
192
|
+
'root',
|
|
193
|
+
'sidebar',
|
|
194
|
+
'sidebar-2',
|
|
195
|
+
{ widgets: [ 'block-10' ] },
|
|
196
|
+
{ undoIgnore: true }
|
|
197
|
+
);
|
|
198
|
+
} );
|
|
199
|
+
|
|
200
|
+
it( 'keeps updating a widget that was moved to another widget area', async () => {
|
|
201
|
+
const blocks = [
|
|
202
|
+
{
|
|
203
|
+
name: 'core/paragraph',
|
|
204
|
+
clientId: 'moved',
|
|
205
|
+
attributes: { content: 'Moved', __internalWidgetId: 'block-2' },
|
|
206
|
+
},
|
|
207
|
+
];
|
|
208
|
+
const widgets = {
|
|
209
|
+
'block-2': {
|
|
210
|
+
id: 'block-2',
|
|
211
|
+
id_base: 'block',
|
|
212
|
+
sidebar: 'sidebar-1',
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
const { run, saveEntityRecord, saveEditedEntityRecord } = setup(
|
|
216
|
+
blocks,
|
|
217
|
+
widgets,
|
|
218
|
+
{ area: 'sidebar-2', areaHoldingId: 'sidebar-2' }
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
await run();
|
|
222
|
+
|
|
223
|
+
expect( saveEntityRecord ).not.toHaveBeenCalled();
|
|
224
|
+
expect( saveEditedEntityRecord ).toHaveBeenCalledWith(
|
|
225
|
+
'root',
|
|
226
|
+
'widget',
|
|
227
|
+
'block-2'
|
|
228
|
+
);
|
|
229
|
+
} );
|
|
230
|
+
} );
|