-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Expand file tree
/
Copy pathuse-dashboard-layout.test.ts
More file actions
107 lines (86 loc) · 2.59 KB
/
Copy pathuse-dashboard-layout.test.ts
File metadata and controls
107 lines (86 loc) · 2.59 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
/**
* @jest-environment jsdom
*/
/**
* External dependencies
*/
import { act, renderHook } from '@testing-library/react';
/**
* WordPress dependencies
*/
import apiFetch from '@wordpress/api-fetch';
import { dispatch } from '@wordpress/data';
import { store as preferencesStore } from '@wordpress/preferences';
/**
* Internal dependencies
*/
import { useDashboardLayout } from '../';
import type { DashboardWidget } from '../../../widget-dashboard';
jest.mock( '@wordpress/api-fetch' );
const mockedApiFetch = apiFetch as unknown as jest.Mock;
const SCOPE = 'core/dashboard';
const KEY = 'dashboardLayout';
const DASHBOARD_NAME = 'gutenberg_dashboard';
const SAMPLE_LAYOUT: DashboardWidget[] = [
{ uuid: 'a', type: 'core/quick-draft' },
{ uuid: 'b', type: 'core/at-a-glance' },
];
const DEFAULT_LAYOUT: DashboardWidget[] = [
{
uuid: 'default-on-this-day-widget-instance',
type: 'core/on-this-day',
attributes: { windowDays: 1 },
placement: { width: 2, height: 2 },
},
];
describe( 'useDashboardLayout', () => {
beforeEach( () => {
dispatch( preferencesStore ).set( SCOPE, KEY, undefined );
mockedApiFetch.mockReset();
} );
it( 'returns an empty array when nothing is persisted', () => {
const { result } = renderHook( () =>
useDashboardLayout( DASHBOARD_NAME )
);
const [ layout ] = result.current;
expect( layout ).toEqual( [] );
} );
it( 'persists updates written through setLayout', () => {
const { result } = renderHook( () =>
useDashboardLayout( DASHBOARD_NAME )
);
act( () => {
const [ , setLayout ] = result.current;
setLayout( SAMPLE_LAYOUT );
} );
const [ layout ] = result.current;
expect( layout ).toEqual( SAMPLE_LAYOUT );
} );
it( 'reads the layout written from outside the hook', () => {
dispatch( preferencesStore ).set( SCOPE, KEY, SAMPLE_LAYOUT );
const { result } = renderHook( () =>
useDashboardLayout( DASHBOARD_NAME )
);
const [ layout ] = result.current;
expect( layout ).toEqual( SAMPLE_LAYOUT );
} );
it( 'restores the registered default via resetLayout', async () => {
mockedApiFetch.mockResolvedValueOnce( DEFAULT_LAYOUT );
const { result } = renderHook( () =>
useDashboardLayout( DASHBOARD_NAME )
);
act( () => {
const [ , setLayout ] = result.current;
setLayout( SAMPLE_LAYOUT );
} );
await act( async () => {
const [ , , resetLayout ] = result.current;
await resetLayout();
} );
expect( mockedApiFetch ).toHaveBeenCalledWith( {
path: `/wp/v2/dashboards/${ DASHBOARD_NAME }/default-layout`,
} );
const [ layout ] = result.current;
expect( layout ).toEqual( DEFAULT_LAYOUT );
} );
} );