@elementor/editor-site-navigation 0.22.8 → 0.23.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/CHANGELOG.md +11 -0
- package/package.json +17 -10
- package/src/__tests__/icons-map.test.tsx +0 -33
- package/src/components/panel/actions-menu/actions/__tests__/delete.test.tsx +0 -90
- package/src/components/panel/actions-menu/actions/__tests__/set-home.test.tsx +0 -122
- package/src/components/panel/actions-menu/actions/__tests__/view.test.tsx +0 -40
- package/src/components/panel/posts-list/__tests__/post-list-item.test.tsx +0 -211
- package/src/components/panel/posts-list/__tests__/posts-collapsible-list.test.tsx +0 -248
- package/src/components/top-bar/__tests__/add-new-page.test.tsx +0 -129
- package/src/components/top-bar/__tests__/recently-edited.test.tsx +0 -380
- package/src/hooks/__tests__/use-create-page.test.ts +0 -39
- package/src/hooks/__tests__/use-homepage-actions.test.ts +0 -43
- package/src/hooks/__tests__/use-homepage.test.ts +0 -42
- package/src/hooks/__tests__/use-post-actions.test.ts +0 -89
- package/src/hooks/__tests__/use-posts.test.ts +0 -93
- package/src/hooks/__tests__/use-recent-posts.test.ts +0 -40
|
@@ -1,380 +0,0 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
import { fireEvent, render, screen } from '@testing-library/react';
|
|
3
|
-
import {
|
|
4
|
-
__useHostDocument as useHostDocument,
|
|
5
|
-
__useActiveDocument as useActiveDocument,
|
|
6
|
-
__useNavigateToDocument as useNavigateToDocument,
|
|
7
|
-
} from '@elementor/editor-documents';
|
|
8
|
-
import RecentlyEdited from '../recently-edited';
|
|
9
|
-
import { createMockDocument } from 'test-utils';
|
|
10
|
-
import useRecentPosts from '../../../hooks/use-recent-posts';
|
|
11
|
-
import { RecentPost } from '../../../types';
|
|
12
|
-
|
|
13
|
-
jest.mock( '@elementor/editor-documents', () => ( {
|
|
14
|
-
__useActiveDocument: jest.fn(),
|
|
15
|
-
__useHostDocument: jest.fn(),
|
|
16
|
-
__useNavigateToDocument: jest.fn(),
|
|
17
|
-
} ) );
|
|
18
|
-
|
|
19
|
-
jest.mock( '../../../hooks/use-recent-posts', () => ( {
|
|
20
|
-
default: jest.fn( () => ( { isLoading: false, data: [] } ) ),
|
|
21
|
-
__esModule: true,
|
|
22
|
-
} ) );
|
|
23
|
-
|
|
24
|
-
jest.mock( '../../../hooks/use-user', () => ( {
|
|
25
|
-
default: jest.fn( () => ( {
|
|
26
|
-
isLoading: false,
|
|
27
|
-
data: {
|
|
28
|
-
capabilities: {
|
|
29
|
-
edit_posts: true,
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
} ) ),
|
|
33
|
-
__esModule: true,
|
|
34
|
-
} ) );
|
|
35
|
-
|
|
36
|
-
describe( '@elementor/recently-edited - Top bar Recently Edited', () => {
|
|
37
|
-
it( 'should show the title of the active document without its status when the document is published', async () => {
|
|
38
|
-
// Arrange.
|
|
39
|
-
jest.mocked( useActiveDocument ).mockReturnValue(
|
|
40
|
-
createMockDocument( {
|
|
41
|
-
id: 1,
|
|
42
|
-
title: 'Active Document',
|
|
43
|
-
} )
|
|
44
|
-
);
|
|
45
|
-
|
|
46
|
-
// Act.
|
|
47
|
-
render( <RecentlyEdited /> );
|
|
48
|
-
|
|
49
|
-
// Assert.
|
|
50
|
-
expect( screen.getByText( 'Active Document' ) ).toBeInTheDocument();
|
|
51
|
-
expect( screen.queryByText( '(publish)' ) ).not.toBeInTheDocument();
|
|
52
|
-
} );
|
|
53
|
-
|
|
54
|
-
it( 'should show the title of the active document with its status when the document is not published', async () => {
|
|
55
|
-
// Arrange.
|
|
56
|
-
jest.mocked( useActiveDocument ).mockReturnValue(
|
|
57
|
-
createMockDocument( {
|
|
58
|
-
id: 1,
|
|
59
|
-
title: 'Active Document',
|
|
60
|
-
status: {
|
|
61
|
-
value: 'draft',
|
|
62
|
-
label: 'Draft',
|
|
63
|
-
},
|
|
64
|
-
} )
|
|
65
|
-
);
|
|
66
|
-
|
|
67
|
-
// Act.
|
|
68
|
-
render( <RecentlyEdited /> );
|
|
69
|
-
|
|
70
|
-
// Assert.
|
|
71
|
-
expect( screen.getByText( 'Active Document' ) ).toBeInTheDocument();
|
|
72
|
-
expect( screen.getByText( '(Draft)' ) ).toBeInTheDocument();
|
|
73
|
-
} );
|
|
74
|
-
|
|
75
|
-
it( 'should show the title of the host document when there is no active document', () => {
|
|
76
|
-
// Arrange.
|
|
77
|
-
jest.mocked( useActiveDocument ).mockReturnValue( null );
|
|
78
|
-
|
|
79
|
-
jest.mocked( useHostDocument ).mockReturnValue(
|
|
80
|
-
createMockDocument( {
|
|
81
|
-
id: 1,
|
|
82
|
-
title: 'Host Document',
|
|
83
|
-
} )
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
// Act.
|
|
87
|
-
render( <RecentlyEdited /> );
|
|
88
|
-
|
|
89
|
-
// Assert.
|
|
90
|
-
expect( screen.getByText( 'Host Document' ) ).toBeInTheDocument();
|
|
91
|
-
} );
|
|
92
|
-
|
|
93
|
-
it( 'should show the title of the host document when the active document is kit', () => {
|
|
94
|
-
// Arrange.
|
|
95
|
-
jest.mocked( useActiveDocument ).mockReturnValue(
|
|
96
|
-
createMockDocument( {
|
|
97
|
-
id: 1,
|
|
98
|
-
title: 'Active Document',
|
|
99
|
-
type: {
|
|
100
|
-
value: 'kit',
|
|
101
|
-
label: 'Kit',
|
|
102
|
-
},
|
|
103
|
-
} )
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
jest.mocked( useHostDocument ).mockReturnValue(
|
|
107
|
-
createMockDocument( {
|
|
108
|
-
id: 2,
|
|
109
|
-
title: 'Host Document',
|
|
110
|
-
} )
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
// Act.
|
|
114
|
-
render( <RecentlyEdited /> );
|
|
115
|
-
|
|
116
|
-
// Assert.
|
|
117
|
-
expect( screen.getByText( 'Host Document' ) ).toBeInTheDocument();
|
|
118
|
-
} );
|
|
119
|
-
|
|
120
|
-
it( 'should show nothing if there are no documents', () => {
|
|
121
|
-
// Arrange.
|
|
122
|
-
jest.mocked( useActiveDocument ).mockReturnValue( null );
|
|
123
|
-
jest.mocked( useHostDocument ).mockReturnValue( null );
|
|
124
|
-
|
|
125
|
-
// Act.
|
|
126
|
-
render( <RecentlyEdited /> );
|
|
127
|
-
|
|
128
|
-
// Assert.
|
|
129
|
-
expect( screen.queryByText( 'Host Document' ) ).not.toBeInTheDocument();
|
|
130
|
-
expect( screen.queryByText( 'Active Document' ) ).not.toBeInTheDocument();
|
|
131
|
-
} );
|
|
132
|
-
|
|
133
|
-
it( 'should show empty state', () => {
|
|
134
|
-
// Arrange.
|
|
135
|
-
jest.mocked( useActiveDocument ).mockReturnValue(
|
|
136
|
-
createMockDocument( {
|
|
137
|
-
id: 1,
|
|
138
|
-
title: 'Header',
|
|
139
|
-
type: {
|
|
140
|
-
value: 'header',
|
|
141
|
-
label: 'Header',
|
|
142
|
-
},
|
|
143
|
-
} )
|
|
144
|
-
);
|
|
145
|
-
|
|
146
|
-
const isLoading = false;
|
|
147
|
-
const recentPosts: RecentPost[] = [];
|
|
148
|
-
|
|
149
|
-
jest.mocked( useRecentPosts ).mockReturnValue( { isLoading, data: recentPosts } as ReturnType<
|
|
150
|
-
typeof useRecentPosts
|
|
151
|
-
> );
|
|
152
|
-
|
|
153
|
-
render( <RecentlyEdited /> );
|
|
154
|
-
|
|
155
|
-
// Act.
|
|
156
|
-
const buttons = screen.getAllByRole( 'button' );
|
|
157
|
-
fireEvent.click( buttons[ 0 ] ); // Opens the recently edited menu
|
|
158
|
-
|
|
159
|
-
// Assert.
|
|
160
|
-
const label = screen.getByText( 'There are no other pages or templates on this site yet', { exact: false } );
|
|
161
|
-
expect( label ).toBeInTheDocument();
|
|
162
|
-
} );
|
|
163
|
-
|
|
164
|
-
it( 'should open the recently edited menu on click', () => {
|
|
165
|
-
// Arrange.
|
|
166
|
-
jest.mocked( useActiveDocument ).mockReturnValue(
|
|
167
|
-
createMockDocument( {
|
|
168
|
-
id: 1,
|
|
169
|
-
title: 'Header',
|
|
170
|
-
type: {
|
|
171
|
-
value: 'header',
|
|
172
|
-
label: 'Header',
|
|
173
|
-
},
|
|
174
|
-
} )
|
|
175
|
-
);
|
|
176
|
-
|
|
177
|
-
const isLoading = false;
|
|
178
|
-
const recentPosts: RecentPost[] = [
|
|
179
|
-
{
|
|
180
|
-
id: 2,
|
|
181
|
-
title: 'Test post',
|
|
182
|
-
edit_url: 'some_url',
|
|
183
|
-
type: {
|
|
184
|
-
post_type: 'post',
|
|
185
|
-
doc_type: 'wp-post',
|
|
186
|
-
label: 'Post',
|
|
187
|
-
},
|
|
188
|
-
date_modified: 123,
|
|
189
|
-
user_can: {
|
|
190
|
-
edit: true,
|
|
191
|
-
},
|
|
192
|
-
},
|
|
193
|
-
];
|
|
194
|
-
|
|
195
|
-
jest.mocked( useRecentPosts ).mockReturnValue( { isLoading, data: recentPosts } as ReturnType<
|
|
196
|
-
typeof useRecentPosts
|
|
197
|
-
> );
|
|
198
|
-
|
|
199
|
-
render( <RecentlyEdited /> );
|
|
200
|
-
|
|
201
|
-
// Act.
|
|
202
|
-
const buttons = screen.getAllByRole( 'button' );
|
|
203
|
-
fireEvent.click( buttons[ 0 ] ); // Opens the recently edited menu
|
|
204
|
-
|
|
205
|
-
// Assert.
|
|
206
|
-
const menu = screen.getByRole( 'menu' );
|
|
207
|
-
expect( menu ).toBeInTheDocument();
|
|
208
|
-
|
|
209
|
-
const label = screen.getByText( 'Recent' );
|
|
210
|
-
expect( label ).toBeInTheDocument();
|
|
211
|
-
|
|
212
|
-
expect( screen.getByText( 'Test post' ) ).toBeInTheDocument();
|
|
213
|
-
} );
|
|
214
|
-
|
|
215
|
-
it( 'should render titles with HTML entities', () => {
|
|
216
|
-
// Arrange.
|
|
217
|
-
jest.mocked( useActiveDocument ).mockReturnValue(
|
|
218
|
-
createMockDocument( {
|
|
219
|
-
id: 1,
|
|
220
|
-
title: 'Header title with special char ¥',
|
|
221
|
-
type: {
|
|
222
|
-
value: 'header',
|
|
223
|
-
label: 'Header',
|
|
224
|
-
},
|
|
225
|
-
} )
|
|
226
|
-
);
|
|
227
|
-
|
|
228
|
-
const isLoading = false;
|
|
229
|
-
const recentPosts: RecentPost[] = [
|
|
230
|
-
{
|
|
231
|
-
id: 1,
|
|
232
|
-
title: 'Header title with special char ¥',
|
|
233
|
-
edit_url: 'some_url',
|
|
234
|
-
type: {
|
|
235
|
-
post_type: 'post',
|
|
236
|
-
doc_type: 'wp-post',
|
|
237
|
-
label: 'Post',
|
|
238
|
-
},
|
|
239
|
-
date_modified: 123,
|
|
240
|
-
user_can: {
|
|
241
|
-
edit: true,
|
|
242
|
-
},
|
|
243
|
-
},
|
|
244
|
-
{
|
|
245
|
-
id: 3,
|
|
246
|
-
title: 'Post title with <h1>HTML</h1>',
|
|
247
|
-
edit_url: 'some_url',
|
|
248
|
-
type: {
|
|
249
|
-
post_type: 'post',
|
|
250
|
-
doc_type: 'wp-post',
|
|
251
|
-
label: 'Post',
|
|
252
|
-
},
|
|
253
|
-
date_modified: 123,
|
|
254
|
-
user_can: {
|
|
255
|
-
edit: true,
|
|
256
|
-
},
|
|
257
|
-
},
|
|
258
|
-
{
|
|
259
|
-
id: 2,
|
|
260
|
-
title: 'Post title with <HTML entities>',
|
|
261
|
-
edit_url: 'some_url_2',
|
|
262
|
-
type: {
|
|
263
|
-
post_type: 'post',
|
|
264
|
-
doc_type: 'wp-post',
|
|
265
|
-
label: 'Post 2',
|
|
266
|
-
},
|
|
267
|
-
date_modified: 1234,
|
|
268
|
-
user_can: {
|
|
269
|
-
edit: true,
|
|
270
|
-
},
|
|
271
|
-
},
|
|
272
|
-
];
|
|
273
|
-
|
|
274
|
-
jest.mocked( useRecentPosts ).mockReturnValue( { isLoading, data: recentPosts } as ReturnType<
|
|
275
|
-
typeof useRecentPosts
|
|
276
|
-
> );
|
|
277
|
-
|
|
278
|
-
// Act.
|
|
279
|
-
render( <RecentlyEdited /> );
|
|
280
|
-
|
|
281
|
-
// Assert - the document title should be rendered with the HTML entity.
|
|
282
|
-
expect( screen.getByText( 'Header title with special char ¥' ) ).toBeInTheDocument();
|
|
283
|
-
|
|
284
|
-
// Open the posts list.
|
|
285
|
-
fireEvent.click( screen.getByRole( 'button' ) );
|
|
286
|
-
|
|
287
|
-
// Assert - the post title should be rendered with the HTML entity.
|
|
288
|
-
expect( screen.getByText( 'Post title with <h1>HTML</h1>' ) ).toBeInTheDocument();
|
|
289
|
-
expect( screen.getByText( 'Post title with <HTML entities>' ) ).toBeInTheDocument();
|
|
290
|
-
} );
|
|
291
|
-
|
|
292
|
-
it( 'should navigate to document on click', () => {
|
|
293
|
-
// Arrange.
|
|
294
|
-
jest.mocked( useActiveDocument ).mockReturnValue(
|
|
295
|
-
createMockDocument( {
|
|
296
|
-
id: 1,
|
|
297
|
-
title: 'Test',
|
|
298
|
-
} )
|
|
299
|
-
);
|
|
300
|
-
|
|
301
|
-
const navigateToDocument = jest.fn();
|
|
302
|
-
|
|
303
|
-
jest.mocked( useNavigateToDocument ).mockReturnValue( navigateToDocument );
|
|
304
|
-
|
|
305
|
-
jest.mocked( useRecentPosts ).mockReturnValue( {
|
|
306
|
-
isLoading: false,
|
|
307
|
-
data: [
|
|
308
|
-
{
|
|
309
|
-
id: 123,
|
|
310
|
-
title: 'Test post',
|
|
311
|
-
edit_url: 'some_url',
|
|
312
|
-
type: {
|
|
313
|
-
post_type: 'post',
|
|
314
|
-
doc_type: 'wp-post',
|
|
315
|
-
label: 'Post',
|
|
316
|
-
},
|
|
317
|
-
date_modified: 123,
|
|
318
|
-
user_can: {
|
|
319
|
-
edit: true,
|
|
320
|
-
},
|
|
321
|
-
},
|
|
322
|
-
],
|
|
323
|
-
} as ReturnType< typeof useRecentPosts > );
|
|
324
|
-
|
|
325
|
-
render( <RecentlyEdited /> );
|
|
326
|
-
|
|
327
|
-
// Open the posts list.
|
|
328
|
-
fireEvent.click( screen.getByRole( 'button' ) );
|
|
329
|
-
|
|
330
|
-
// Act.
|
|
331
|
-
fireEvent.click( screen.getByText( 'Test post' ) );
|
|
332
|
-
|
|
333
|
-
// Assert.
|
|
334
|
-
expect( navigateToDocument ).toHaveBeenCalledTimes( 1 );
|
|
335
|
-
expect( navigateToDocument ).toHaveBeenCalledWith( 123 );
|
|
336
|
-
} );
|
|
337
|
-
|
|
338
|
-
it( 'should be disabled when user cant edit post', () => {
|
|
339
|
-
// Arrange.
|
|
340
|
-
jest.mocked( useActiveDocument ).mockReturnValue(
|
|
341
|
-
createMockDocument( {
|
|
342
|
-
id: 1,
|
|
343
|
-
title: 'Test',
|
|
344
|
-
} )
|
|
345
|
-
);
|
|
346
|
-
|
|
347
|
-
const navigateToDocument = jest.fn();
|
|
348
|
-
|
|
349
|
-
jest.mocked( useNavigateToDocument ).mockReturnValue( navigateToDocument );
|
|
350
|
-
|
|
351
|
-
jest.mocked( useRecentPosts ).mockReturnValue( {
|
|
352
|
-
isLoading: false,
|
|
353
|
-
data: [
|
|
354
|
-
{
|
|
355
|
-
id: 123,
|
|
356
|
-
title: 'Test post',
|
|
357
|
-
edit_url: 'some_url',
|
|
358
|
-
type: {
|
|
359
|
-
post_type: 'post',
|
|
360
|
-
doc_type: 'wp-post',
|
|
361
|
-
label: 'Post',
|
|
362
|
-
},
|
|
363
|
-
date_modified: 123,
|
|
364
|
-
user_can: {
|
|
365
|
-
edit: false,
|
|
366
|
-
},
|
|
367
|
-
},
|
|
368
|
-
],
|
|
369
|
-
} as ReturnType< typeof useRecentPosts > );
|
|
370
|
-
|
|
371
|
-
render( <RecentlyEdited /> );
|
|
372
|
-
|
|
373
|
-
// Open the posts list.
|
|
374
|
-
fireEvent.click( screen.getByRole( 'button' ) );
|
|
375
|
-
|
|
376
|
-
// Assert.
|
|
377
|
-
const listItem = screen.getAllByRole( 'menuitem' )[ 0 ];
|
|
378
|
-
expect( listItem ).toHaveAttribute( 'aria-disabled', 'true' );
|
|
379
|
-
} );
|
|
380
|
-
} );
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { renderHook } from '@testing-library/react';
|
|
2
|
-
import apiFetch from '@wordpress/api-fetch';
|
|
3
|
-
import useCreatePage, { endpointPath } from '../use-create-page';
|
|
4
|
-
|
|
5
|
-
// Mock apiFetch to return a promise that resolves to an empty array.
|
|
6
|
-
jest.mock( '@wordpress/api-fetch' );
|
|
7
|
-
|
|
8
|
-
describe( '@elementor/recently-edited/use-page', () => {
|
|
9
|
-
beforeEach( () => {
|
|
10
|
-
jest.mocked( apiFetch ).mockImplementation( () => Promise.resolve( [] ) );
|
|
11
|
-
} );
|
|
12
|
-
|
|
13
|
-
afterEach( () => {
|
|
14
|
-
jest.clearAllMocks();
|
|
15
|
-
} );
|
|
16
|
-
|
|
17
|
-
it( 'should run useCreatePage hook', async () => {
|
|
18
|
-
// Arrange.
|
|
19
|
-
const { result } = renderHook( useCreatePage );
|
|
20
|
-
const newPost = {
|
|
21
|
-
id: 1,
|
|
22
|
-
edit_url: 'editurl.com',
|
|
23
|
-
};
|
|
24
|
-
jest.mocked( apiFetch ).mockImplementation( () => Promise.resolve( newPost ) );
|
|
25
|
-
|
|
26
|
-
const { create } = result.current;
|
|
27
|
-
|
|
28
|
-
// Act.
|
|
29
|
-
create();
|
|
30
|
-
|
|
31
|
-
// Assert.
|
|
32
|
-
expect( apiFetch ).toHaveBeenCalledTimes( 1 );
|
|
33
|
-
expect( apiFetch ).toHaveBeenCalledWith( {
|
|
34
|
-
data: { post_type: 'page' },
|
|
35
|
-
method: 'POST',
|
|
36
|
-
path: endpointPath,
|
|
37
|
-
} );
|
|
38
|
-
} );
|
|
39
|
-
} );
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import apiFetch from '@wordpress/api-fetch';
|
|
2
|
-
import { renderHookWithQuery } from 'test-utils';
|
|
3
|
-
import { useHomepageActions } from '../use-homepage-actions';
|
|
4
|
-
import { settingsQueryKey } from '../use-homepage';
|
|
5
|
-
|
|
6
|
-
jest.mock( '@wordpress/api-fetch' );
|
|
7
|
-
|
|
8
|
-
describe( '@elementor/site-settings/use-homepage-actions', () => {
|
|
9
|
-
beforeEach( () => {
|
|
10
|
-
jest.mocked( apiFetch ).mockImplementation( () => Promise.resolve( {} ) );
|
|
11
|
-
} );
|
|
12
|
-
|
|
13
|
-
afterEach( () => {
|
|
14
|
-
jest.clearAllMocks();
|
|
15
|
-
} );
|
|
16
|
-
|
|
17
|
-
it( 'should run updateSettings from useHomepageActions hook', async () => {
|
|
18
|
-
// Arrange.
|
|
19
|
-
const { component, queryClient } = renderHookWithQuery( () => useHomepageActions() );
|
|
20
|
-
const { updateSettingsMutation } = component.result.current;
|
|
21
|
-
|
|
22
|
-
const queryKey = settingsQueryKey();
|
|
23
|
-
await queryClient.setQueryData( queryKey, {
|
|
24
|
-
show_on_front: '',
|
|
25
|
-
page_on_front: 0,
|
|
26
|
-
} );
|
|
27
|
-
|
|
28
|
-
// Act.
|
|
29
|
-
await updateSettingsMutation.mutateAsync( { show_on_front: 'page', page_on_front: 1 } );
|
|
30
|
-
|
|
31
|
-
expect( apiFetch ).toHaveBeenCalledTimes( 1 );
|
|
32
|
-
expect( apiFetch ).toHaveBeenCalledWith( {
|
|
33
|
-
path: '/wp/v2/settings',
|
|
34
|
-
method: 'POST',
|
|
35
|
-
data: {
|
|
36
|
-
show_on_front: 'page',
|
|
37
|
-
page_on_front: 1,
|
|
38
|
-
},
|
|
39
|
-
} );
|
|
40
|
-
|
|
41
|
-
expect( queryClient.getQueryState( queryKey )?.isInvalidated ).toBe( true );
|
|
42
|
-
} );
|
|
43
|
-
} );
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { waitFor } from '@testing-library/react';
|
|
2
|
-
import apiFetch from '@wordpress/api-fetch';
|
|
3
|
-
import { useHomepage } from '../use-homepage';
|
|
4
|
-
import { renderHookWithQuery } from 'test-utils';
|
|
5
|
-
|
|
6
|
-
jest.mock( '@wordpress/api-fetch' );
|
|
7
|
-
|
|
8
|
-
describe( '@elementor/site-settings/use-homepage', () => {
|
|
9
|
-
beforeEach( () => {
|
|
10
|
-
jest.mocked( apiFetch ).mockImplementation( () => Promise.resolve( [] ) );
|
|
11
|
-
} );
|
|
12
|
-
|
|
13
|
-
afterEach( () => {
|
|
14
|
-
jest.clearAllMocks();
|
|
15
|
-
} );
|
|
16
|
-
|
|
17
|
-
it( 'useHomepage hook should return homepage settings', async () => {
|
|
18
|
-
// Arrange.
|
|
19
|
-
const settings = {
|
|
20
|
-
show_on_front: 'page',
|
|
21
|
-
page_on_front: 1,
|
|
22
|
-
};
|
|
23
|
-
jest.mocked( apiFetch ).mockImplementation( () => Promise.resolve( settings ) );
|
|
24
|
-
|
|
25
|
-
// Act.
|
|
26
|
-
const { component } = renderHookWithQuery( () => useHomepage() );
|
|
27
|
-
|
|
28
|
-
// Assert.
|
|
29
|
-
const expectedPath = '/elementor/v1/site-navigation/homepage';
|
|
30
|
-
|
|
31
|
-
expect( apiFetch ).toHaveBeenCalledWith( {
|
|
32
|
-
path: expectedPath,
|
|
33
|
-
} );
|
|
34
|
-
expect( apiFetch ).toHaveBeenCalledTimes( 1 );
|
|
35
|
-
|
|
36
|
-
await waitFor( () => {
|
|
37
|
-
return component.result.current.isSuccess;
|
|
38
|
-
} );
|
|
39
|
-
|
|
40
|
-
expect( component.result.current.data ).toBe( settings );
|
|
41
|
-
} );
|
|
42
|
-
} );
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import apiFetch from '@wordpress/api-fetch';
|
|
2
|
-
import { renderHookWithQuery } from 'test-utils';
|
|
3
|
-
import { usePostActions } from '../use-posts-actions';
|
|
4
|
-
import { postsQueryKey } from '../use-posts';
|
|
5
|
-
|
|
6
|
-
jest.mock( '@wordpress/api-fetch' );
|
|
7
|
-
|
|
8
|
-
describe( '@elementor/site-settings/use-post-actions', () => {
|
|
9
|
-
beforeEach( () => {
|
|
10
|
-
jest.mocked( apiFetch ).mockImplementation( () => Promise.resolve( {} ) );
|
|
11
|
-
} );
|
|
12
|
-
|
|
13
|
-
afterEach( () => {
|
|
14
|
-
jest.clearAllMocks();
|
|
15
|
-
} );
|
|
16
|
-
|
|
17
|
-
it( 'should run createPost from usePostActions hook', async () => {
|
|
18
|
-
// Arrange.
|
|
19
|
-
const { component, queryClient } = renderHookWithQuery( () => usePostActions( 'page' ) );
|
|
20
|
-
const { createPost } = component.result.current;
|
|
21
|
-
|
|
22
|
-
const queryKey = postsQueryKey( 'page' );
|
|
23
|
-
await queryClient.setQueryData( queryKey, {
|
|
24
|
-
posts: [],
|
|
25
|
-
} );
|
|
26
|
-
|
|
27
|
-
// Act.
|
|
28
|
-
await createPost.mutateAsync( { title: 'Page', status: 'publish' } );
|
|
29
|
-
|
|
30
|
-
expect( apiFetch ).toHaveBeenCalledTimes( 1 );
|
|
31
|
-
expect( apiFetch ).toHaveBeenCalledWith( {
|
|
32
|
-
path: '/wp/v2/pages',
|
|
33
|
-
method: 'POST',
|
|
34
|
-
data: {
|
|
35
|
-
title: 'Page',
|
|
36
|
-
status: 'publish',
|
|
37
|
-
},
|
|
38
|
-
} );
|
|
39
|
-
|
|
40
|
-
expect( queryClient.getQueryState( queryKey )?.isInvalidated ).toBe( true );
|
|
41
|
-
} );
|
|
42
|
-
|
|
43
|
-
it( 'should run updatePost from usePostActions hook', async () => {
|
|
44
|
-
// Arrange.
|
|
45
|
-
const { component, queryClient } = renderHookWithQuery( () => usePostActions( 'page' ) );
|
|
46
|
-
const { updatePost } = component.result.current;
|
|
47
|
-
|
|
48
|
-
const queryKey = postsQueryKey( 'page' );
|
|
49
|
-
await queryClient.setQueryData( queryKey, {
|
|
50
|
-
posts: [],
|
|
51
|
-
} );
|
|
52
|
-
|
|
53
|
-
// Act.
|
|
54
|
-
await updatePost.mutateAsync( { id: 1, title: 'Page' } );
|
|
55
|
-
|
|
56
|
-
expect( apiFetch ).toHaveBeenCalledTimes( 1 );
|
|
57
|
-
expect( apiFetch ).toHaveBeenCalledWith( {
|
|
58
|
-
path: '/wp/v2/pages/1',
|
|
59
|
-
method: 'POST',
|
|
60
|
-
data: {
|
|
61
|
-
title: 'Page',
|
|
62
|
-
},
|
|
63
|
-
} );
|
|
64
|
-
|
|
65
|
-
expect( queryClient.getQueryState( queryKey )?.isInvalidated ).toBe( true );
|
|
66
|
-
} );
|
|
67
|
-
|
|
68
|
-
it( 'should run deletePost from usePostActions hook', async () => {
|
|
69
|
-
// Arrange.
|
|
70
|
-
const { component, queryClient } = renderHookWithQuery( () => usePostActions( 'page' ) );
|
|
71
|
-
const { deletePost } = component.result.current;
|
|
72
|
-
|
|
73
|
-
const queryKey = postsQueryKey( 'page' );
|
|
74
|
-
await queryClient.setQueryData( queryKey, {
|
|
75
|
-
posts: [],
|
|
76
|
-
} );
|
|
77
|
-
|
|
78
|
-
// Act.
|
|
79
|
-
await deletePost.mutateAsync( 1 );
|
|
80
|
-
|
|
81
|
-
expect( apiFetch ).toHaveBeenCalledTimes( 1 );
|
|
82
|
-
expect( apiFetch ).toHaveBeenCalledWith( {
|
|
83
|
-
path: '/wp/v2/pages/1',
|
|
84
|
-
method: 'DELETE',
|
|
85
|
-
} );
|
|
86
|
-
|
|
87
|
-
expect( queryClient.getQueryState( queryKey )?.isInvalidated ).toBe( true );
|
|
88
|
-
} );
|
|
89
|
-
} );
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { waitFor } from '@testing-library/react';
|
|
2
|
-
import apiFetch from '@wordpress/api-fetch';
|
|
3
|
-
import { renderHookWithQuery } from 'test-utils';
|
|
4
|
-
import { usePosts } from '../use-posts';
|
|
5
|
-
import { POST_PER_PAGE } from '../../api/post';
|
|
6
|
-
|
|
7
|
-
const MOCK_POST_PER_PAGE = 2;
|
|
8
|
-
|
|
9
|
-
jest.mock( '@wordpress/api-fetch', () => ( {
|
|
10
|
-
default: jest.fn( ( param ) => {
|
|
11
|
-
const pages = [
|
|
12
|
-
{ id: 1, type: 'page', title: { rendered: 'Home' }, status: 'draft', link: 'www.test.demo' },
|
|
13
|
-
{ id: 2, type: 'page', title: { rendered: 'About' }, status: 'publish', link: 'www.test.demo' },
|
|
14
|
-
{
|
|
15
|
-
id: 3,
|
|
16
|
-
type: 'page',
|
|
17
|
-
title: { rendered: 'Services' },
|
|
18
|
-
status: 'publish',
|
|
19
|
-
link: 'www.test.demo',
|
|
20
|
-
isHome: true,
|
|
21
|
-
},
|
|
22
|
-
];
|
|
23
|
-
|
|
24
|
-
const url = new URL( 'http:/example.com' + param.path );
|
|
25
|
-
|
|
26
|
-
const page = Number( url.searchParams.get( 'page' ) );
|
|
27
|
-
|
|
28
|
-
const startIndex = ( page - 1 ) * MOCK_POST_PER_PAGE;
|
|
29
|
-
const endIndex = startIndex + MOCK_POST_PER_PAGE;
|
|
30
|
-
const paginatedPages = pages.slice( startIndex, endIndex );
|
|
31
|
-
|
|
32
|
-
return {
|
|
33
|
-
json: () => Promise.resolve( paginatedPages ),
|
|
34
|
-
headers: {
|
|
35
|
-
get: ( header: string ) => {
|
|
36
|
-
switch ( header ) {
|
|
37
|
-
case 'x-wp-totalpages':
|
|
38
|
-
return Math.ceil( pages.length / MOCK_POST_PER_PAGE );
|
|
39
|
-
case 'x-wp-total':
|
|
40
|
-
return pages.length;
|
|
41
|
-
default:
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
};
|
|
47
|
-
} ),
|
|
48
|
-
__esModule: true,
|
|
49
|
-
} ) );
|
|
50
|
-
|
|
51
|
-
describe( '@elementor/site-settings/use-posts', () => {
|
|
52
|
-
afterEach( () => {
|
|
53
|
-
jest.clearAllMocks();
|
|
54
|
-
} );
|
|
55
|
-
|
|
56
|
-
it( 'usePosts hook should return posts list by type', async () => {
|
|
57
|
-
//Arrange
|
|
58
|
-
const pages = [
|
|
59
|
-
{ id: 1, type: 'page', title: { rendered: 'Home' }, status: 'draft', link: 'www.test.demo' },
|
|
60
|
-
{ id: 2, type: 'page', title: { rendered: 'About' }, status: 'publish', link: 'www.test.demo' },
|
|
61
|
-
{
|
|
62
|
-
id: 3,
|
|
63
|
-
type: 'page',
|
|
64
|
-
title: { rendered: 'Services' },
|
|
65
|
-
status: 'publish',
|
|
66
|
-
link: 'www.test.demo',
|
|
67
|
-
isHome: true,
|
|
68
|
-
},
|
|
69
|
-
];
|
|
70
|
-
|
|
71
|
-
// Act.
|
|
72
|
-
const { component } = renderHookWithQuery( () => usePosts( 'page' ) );
|
|
73
|
-
|
|
74
|
-
// Assert.
|
|
75
|
-
const expectedPath = `/wp/v2/pages?status=any&order=asc&page=1&per_page=${ POST_PER_PAGE }&_fields=${ encodeURIComponent(
|
|
76
|
-
'id,type,title,link,status,user_can'
|
|
77
|
-
) }`;
|
|
78
|
-
|
|
79
|
-
expect( apiFetch ).toHaveBeenCalledWith( {
|
|
80
|
-
parse: false,
|
|
81
|
-
path: expectedPath,
|
|
82
|
-
} );
|
|
83
|
-
expect( apiFetch ).toHaveBeenCalledTimes( 1 );
|
|
84
|
-
|
|
85
|
-
await waitFor( () => {
|
|
86
|
-
expect( component.result.current.isLoading ).toBeFalsy();
|
|
87
|
-
} );
|
|
88
|
-
|
|
89
|
-
expect( component.result.current.data.posts ).toContainEqual( pages[ 0 ] );
|
|
90
|
-
expect( component.result.current.data.posts ).toContainEqual( pages[ 1 ] );
|
|
91
|
-
expect( component.result.current.data.posts ).not.toContainEqual( pages[ 2 ] );
|
|
92
|
-
} );
|
|
93
|
-
} );
|