-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathtaxonomies.test.js
250 lines (189 loc) · 6.84 KB
/
taxonomies.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* WordPress dependencies
*/
import {
createNewPost,
findSidebarPanelWithTitle,
openDocumentSettingsSidebar,
publishPost,
} from '@wordpress/e2e-test-utils';
/**
* Module constants
*/
const TAG_TOKEN_SELECTOR =
'.components-form-token-field__token-text span:not(.components-visually-hidden)';
function generateRandomNumber() {
// Using `Math.random()` directly is fine in this testing context.
// eslint-disable-next-line no-restricted-syntax
return Math.round( 1 + Math.random() * ( Number.MAX_SAFE_INTEGER - 1 ) );
}
describe( 'Taxonomies', () => {
const canCreatTermInTaxonomy = ( taxonomy ) => {
return page.evaluate( ( _taxonomy ) => {
const post = wp.data.select( 'core/editor' ).getCurrentPost();
if ( ! post._links ) {
return false;
}
return !! post._links[ `wp:action-create-${ _taxonomy }` ];
}, taxonomy );
};
const getSelectCategories = () => {
return page.evaluate( () => {
return Array.from(
document.querySelectorAll(
'.editor-post-taxonomies__hierarchical-terms-choice .components-checkbox-control__input:checked'
)
).map( ( node ) => {
return node.parentElement.nextSibling.innerText;
} );
} );
};
const getCurrentTags = async () => {
const tagsPanel = await findSidebarPanelWithTitle( 'Tags' );
return page.evaluate(
( node, selector ) => {
return Array.from( node.querySelectorAll( selector ) ).map(
( field ) => {
return field.innerText;
}
);
},
tagsPanel,
TAG_TOKEN_SELECTOR
);
};
const openSidebarPanelWithTitle = async ( title ) => {
const panel = await page.waitForXPath(
`//div[contains(@class,"edit-post-sidebar")]//button[@class="components-button components-panel__body-toggle"][contains(text(),"${ title }")]`
);
await panel.click();
};
it( 'should be able to open the categories panel and create a new main category if the user has the right capabilities', async () => {
await createNewPost();
await openDocumentSettingsSidebar();
await openSidebarPanelWithTitle( 'Categories' );
await page.waitForSelector(
'.editor-post-taxonomies__hierarchical-terms-list'
);
// If the user has no permission to add a new category finish the test.
if ( ! ( await canCreatTermInTaxonomy( 'categories' ) ) ) {
return;
}
await page.waitForSelector(
'button.editor-post-taxonomies__hierarchical-terms-add'
);
// Click add new category button.
await page.click(
'button.editor-post-taxonomies__hierarchical-terms-add'
);
// Type the category name in the field.
await page.type(
'.editor-post-taxonomies__hierarchical-terms-input input[type=text]',
'z rand category 1'
);
// Click the submit button.
await page.click(
'.editor-post-taxonomies__hierarchical-terms-submit'
);
// Wait for the categories to load.
await page.waitForSelector(
'.editor-post-taxonomies__hierarchical-terms-choice .components-checkbox-control__input:checked'
);
let selectedCategories = await getSelectCategories();
// The new category is selected.
expect( selectedCategories ).toHaveLength( 1 );
expect( selectedCategories[ 0 ] ).toEqual( 'z rand category 1' );
// Type something in the title so we can publish the post.
await page.type( '.editor-post-title__input', 'Hello World' );
// Publish the post.
await publishPost();
// Reload the editor.
await page.reload();
// Wait for the categories to load.
await page.waitForSelector(
'.editor-post-taxonomies__hierarchical-terms-choice .components-checkbox-control__input:checked'
);
selectedCategories = await getSelectCategories();
// The category selection was persisted after the publish process.
expect( selectedCategories ).toHaveLength( 1 );
expect( selectedCategories[ 0 ] ).toEqual( 'z rand category 1' );
} );
it( "should be able to create a new tag with ' on the name", async () => {
await createNewPost();
await openDocumentSettingsSidebar();
await openSidebarPanelWithTitle( 'Tags' );
// If the user has no permission to add a new tag finish the test.
if ( ! ( await canCreatTermInTaxonomy( 'tags' ) ) ) {
return;
}
const tagsPanel = await findSidebarPanelWithTitle( 'Tags' );
const tagInput = await tagsPanel.$(
'.components-form-token-field__input'
);
// Click the tag input field.
await tagInput.click();
const tagName = "tag'-" + generateRandomNumber();
// Type the category name in the field.
await tagInput.type( tagName );
// Press enter to create a new tag.
await tagInput.press( 'Enter' );
await page.waitForSelector( TAG_TOKEN_SELECTOR );
// Get an array with the tags of the post.
let tags = await getCurrentTags();
// The post should only contain the tag we added.
expect( tags ).toHaveLength( 1 );
expect( tags[ 0 ] ).toEqual( tagName );
// Type something in the title so we can publish the post.
await page.type( '.editor-post-title__input', 'Hello World' );
// Publish the post.
await publishPost();
// Reload the editor.
await page.reload();
// Wait for the tags to load.
await page.waitForSelector( '.components-form-token-field__token' );
tags = await getCurrentTags();
// The tag selection was persisted after the publish process.
expect( tags ).toHaveLength( 1 );
expect( tags[ 0 ] ).toEqual( tagName );
} );
it( 'should be able to open the tags panel and create a new tag if the user has the right capabilities', async () => {
await createNewPost();
await openDocumentSettingsSidebar();
await openSidebarPanelWithTitle( 'Tags' );
// If the user has no permission to add a new tag finish the test.
if ( ! ( await canCreatTermInTaxonomy( 'tags' ) ) ) {
return;
}
// At the start there are no tag tokens.
expect( await page.$$( TAG_TOKEN_SELECTOR ) ).toHaveLength( 0 );
const tagsPanel = await findSidebarPanelWithTitle( 'Tags' );
const tagInput = await tagsPanel.$(
'.components-form-token-field__input'
);
// Click the tag input field.
await tagInput.click();
const tagName = 'tag-' + generateRandomNumber();
// Type the category name in the field.
await tagInput.type( tagName );
// Press enter to create a new tag.
await tagInput.press( 'Enter' );
await page.waitForSelector( TAG_TOKEN_SELECTOR );
// Get an array with the tags of the post.
let tags = await getCurrentTags();
// The post should only contain the tag we added.
expect( tags ).toHaveLength( 1 );
expect( tags[ 0 ] ).toEqual( tagName );
// Type something in the title so we can publish the post.
await page.type( '.editor-post-title__input', 'Hello World' );
// Publish the post.
await publishPost();
// Reload the editor.
await page.reload();
// Wait for the tags to load.
await page.waitForSelector( '.components-form-token-field__token' );
tags = await getCurrentTags();
// The tag selection was persisted after the publish process.
expect( tags ).toHaveLength( 1 );
expect( tags[ 0 ] ).toEqual( tagName );
} );
} );