/
sidebar.test.js
171 lines (144 loc) · 5.4 KB
/
sidebar.test.js
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
/**
* WordPress dependencies
*/
import {
clearLocalStorage,
createNewPost,
findSidebarPanelWithTitle,
enableFocusLossObservation,
disableFocusLossObservation,
openDocumentSettingsSidebar,
pressKeyWithModifier,
setBrowserViewport,
} from '@wordpress/e2e-test-utils';
const SIDEBAR_SELECTOR = '.edit-post-sidebar';
const ACTIVE_SIDEBAR_TAB_SELECTOR =
'div[aria-label="Editor settings"] [role="tab"][aria-selected="true"]';
const ACTIVE_SIDEBAR_BUTTON_TEXT = 'Post';
describe( 'Sidebar', () => {
afterEach( () => {
disableFocusLossObservation();
} );
it( 'should have sidebar visible at the start with document sidebar active on desktop', async () => {
await setBrowserViewport( 'large' );
await clearLocalStorage();
await createNewPost();
await enableFocusLossObservation();
const { nodesCount, content, height, width } = await page.$$eval(
ACTIVE_SIDEBAR_TAB_SELECTOR,
( nodes ) => {
const firstNode = nodes[ 0 ];
return {
nodesCount: nodes.length,
content: firstNode.innerText,
height: firstNode.offsetHeight,
width: firstNode.offsetWidth,
};
}
);
// should have only one active sidebar tab.
expect( nodesCount ).toBe( 1 );
// the active sidebar tab should be document.
expect( content ).toBe( ACTIVE_SIDEBAR_BUTTON_TEXT );
// the active sidebar tab should be visible
expect( width ).toBeGreaterThan( 10 );
expect( height ).toBeGreaterThan( 10 );
} );
it( 'should have the sidebar closed by default on mobile', async () => {
await setBrowserViewport( 'small' );
await clearLocalStorage();
await createNewPost();
await enableFocusLossObservation();
const sidebar = await page.$( SIDEBAR_SELECTOR );
expect( sidebar ).toBeNull();
} );
it( 'should close the sidebar when resizing from desktop to mobile', async () => {
await setBrowserViewport( 'large' );
await clearLocalStorage();
await createNewPost();
await enableFocusLossObservation();
const sidebars = await page.$$( SIDEBAR_SELECTOR );
expect( sidebars ).toHaveLength( 1 );
await setBrowserViewport( 'small' );
const sidebarsMobile = await page.$$( SIDEBAR_SELECTOR );
// sidebar should be closed when resizing to mobile.
expect( sidebarsMobile ).toHaveLength( 0 );
} );
it( 'should reopen sidebar the sidebar when resizing from mobile to desktop if the sidebar was closed automatically', async () => {
await setBrowserViewport( 'large' );
await clearLocalStorage();
await createNewPost();
await enableFocusLossObservation();
await setBrowserViewport( 'small' );
const sidebarsMobile = await page.$$( SIDEBAR_SELECTOR );
expect( sidebarsMobile ).toHaveLength( 0 );
await setBrowserViewport( 'large' );
await page.waitForSelector( SIDEBAR_SELECTOR );
} );
it( 'should preserve tab order while changing active tab', async () => {
await createNewPost();
await enableFocusLossObservation();
// Region navigate to Sidebar.
await pressKeyWithModifier( 'ctrl', '`' );
// Tab lands at first (presumed selected) option "Post".
await page.keyboard.press( 'Tab' );
// The Post tab should be focused and selected.
const [ documentInspectorTab ] = await page.$x(
'//button[@role="tab"][@aria-selected="true"][contains(text(), "Post")]'
);
expect( documentInspectorTab ).toBeDefined();
expect( documentInspectorTab ).toHaveFocus();
// Arrow key into and activate "Block".
await page.keyboard.press( 'ArrowRight' );
await page.keyboard.press( 'Space' );
// The Block tab should be focused and selected.
const [ blockInspectorTab ] = await page.$x(
'//button[@role="tab"][@aria-selected="true"][contains(text(), "Block")]'
);
expect( blockInspectorTab ).toBeDefined();
expect( blockInspectorTab ).toHaveFocus();
} );
it( 'should be possible to programmatically remove Document Settings panels', async () => {
await createNewPost();
await enableFocusLossObservation();
await openDocumentSettingsSidebar();
expect( await findSidebarPanelWithTitle( 'Categories' ) ).toBeDefined();
expect( await findSidebarPanelWithTitle( 'Tags' ) ).toBeDefined();
expect(
await findSidebarPanelWithTitle( 'Featured image' )
).toBeDefined();
expect( await findSidebarPanelWithTitle( 'Excerpt' ) ).toBeDefined();
expect( await findSidebarPanelWithTitle( 'Discussion' ) ).toBeDefined();
expect( await findSidebarPanelWithTitle( 'Summary' ) ).toBeDefined();
await page.evaluate( () => {
const { removeEditorPanel } = wp.data.dispatch( 'core/editor' );
removeEditorPanel( 'taxonomy-panel-category' );
removeEditorPanel( 'taxonomy-panel-post_tag' );
removeEditorPanel( 'featured-image' );
removeEditorPanel( 'post-excerpt' );
removeEditorPanel( 'discussion-panel' );
removeEditorPanel( 'post-status' );
} );
const getPanelToggleSelector = ( panelTitle ) => {
return `//div[contains(@class, "edit-post-sidebar")]//button[contains(@class, "components-panel__body-toggle") and contains(text(),"${ panelTitle }")]`;
};
expect(
await page.$x( getPanelToggleSelector( 'Categories' ) )
).toEqual( [] );
expect( await page.$x( getPanelToggleSelector( 'Tags' ) ) ).toEqual(
[]
);
expect(
await page.$x( getPanelToggleSelector( 'Featured image' ) )
).toEqual( [] );
expect( await page.$x( getPanelToggleSelector( 'Excerpt' ) ) ).toEqual(
[]
);
expect(
await page.$x( getPanelToggleSelector( 'Discussion' ) )
).toEqual( [] );
expect( await page.$x( getPanelToggleSelector( 'Summary' ) ) ).toEqual(
[]
);
} );
} );