forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-edit-category.test.js
226 lines (172 loc) · 7.59 KB
/
add-edit-category.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
import {
visitAdminPage,
pressKeyTimes,
} from "@wordpress/e2e-test-utils";
import { addQueryArgs } from "@wordpress/url";
async function deleteAllCategories() {
const categoriesPageQuery = addQueryArgs( '', {
taxonomy: 'category',
} );
await visitAdminPage( 'edit-tags.php', categoriesPageQuery );
// Wait for the categories rows to appear
await page.waitForSelector( '#the-list tr' );
const allCategoriesRows = await page.$$( '#the-list tr' );
// If there is more than one category row, delete all the categories
if( allCategoriesRows.length > 1 ) {
await page.click( '[id^=cb-select-all-]' );
await page.select( '#bulk-action-selector-top', 'delete' );
await page.focus( '#doaction' );
await page.keyboard.press( 'Enter' );
}
// Make sure that all the categories have been deleted
await page.waitForSelector( '#the-list tr:only-child' );
}
async function createNewCategory() {
const categoriesPageQuery = addQueryArgs( '', {
taxonomy: 'category',
} );
await visitAdminPage( 'edit-tags.php', categoriesPageQuery );
// Wait for the input for new category name to appear and focus it
const newCategoryNameInput = await page.waitForSelector( 'input#tag-name' );
await newCategoryNameInput.focus();
// Type the new category name
await page.keyboard.type( 'New category' );
// Save the new category
await page.keyboard.press( 'Enter' );
// Wait for the two categories rows to be present
await page.waitForSelector( '#the-list tr + tr' );
}
async function searchForCategory( categoryToSearch ) {
// Wait for the search field to appear and focus it
const categorySearchInput = await page.waitForSelector( '#tag-search-input' );
categorySearchInput.focus();
// Type the new category title in the search input
await page.keyboard.type( categoryToSearch );
// Click on the search button
await page.waitForSelector( '#search-submit' );
await page.click( '#search-submit' );
// Wait for the search indication text to appears
await page.waitForSelector( 'span.subtitle' );
}
describe( 'Core Categories', () => {
beforeEach( async () => {
await deleteAllCategories();
} );
it( 'Shows correctly the Uncategorized category', async () => {
// Wait for the categories rows to appear
await page.waitForSelector( '#the-list tr' );
// Check that there is only one category row
const allCategoriesRows = await page.$$( '#the-list tr' );
expect( allCategoriesRows.length ).toBe( 1 );
// Check that the "Uncategorized" category is present
const categoryTitle = await page.waitForSelector( '#the-list .row-title' );
expect(
await categoryTitle.evaluate( ( element ) => element.innerText )
).toContain( 'Uncategorized' );
} );
it( 'Creates a new category and shows it correctly', async () => {
await createNewCategory();
// Wait for the new category row to appear
await page.waitForSelector( '#the-list tr:first-child .row-title' );
// Check that the new category is added and shows correctly
const newCategoryLink = await page.$x(
`//a[contains( @class, "row-title" )][contains( text(), "New category" )]`
);
expect( newCategoryLink.length ).toBe( 1 );
} );
it( 'Allows an existing category to be deleted using the delete link', async () => {
await createNewCategory();
// Check that the new category is added and shows correctly
const newCategoryLink = await page.waitForSelector( '#the-list tr:first-child .row-title' )
// Focus on the new category link and move to the delete link
newCategoryLink.focus();
await pressKeyTimes( 'Tab', 3 );
// Click on the delete link
await page.keyboard.press( 'Enter' );
await page.reload();
// Check that there is only one remaining row
const allCategoriesRows = await page.$$( '#the-list tr' );
expect( allCategoriesRows.length ).toBe( 1 );
// Check that the remaining category is "Uncategorized"
const categoryTitle = await page.waitForSelector( '#the-list .row-title' );
expect(
await categoryTitle.evaluate( ( element ) => element.innerText )
).toContain( 'Uncategorized' );
} );
it( 'Returns the appropriate result when searching for an existing category', async () => {
await createNewCategory();
await searchForCategory( 'New category' );
// Wait for the categories rows to appear
await page.waitForSelector( '#the-list tr' );
// Check that there is only one category row
const allCategoriesRows = await page.$$( '#the-list tr' );
expect( allCategoriesRows.length ).toBe( 1 );
// Check that the remaining category is "New Category"
const categoryTitle = await page.waitForSelector( '#the-list .row-title' );
expect(
await categoryTitle.evaluate( ( element ) => element.innerText )
).toContain( 'New category' );
} );
it( 'Should return No categories found. when searching for a category that does not exist', async () => {
await searchForCategory( 'Non existing category' );
// Wait for the not found categories rows to appear
await page.waitForSelector( '#the-list tr.no-items' );
// Check that there is only one category row
const allCategoriesRows = await page.$$( '#the-list tr.no-items' );
expect( allCategoriesRows.length ).toBe( 1 );
// Check that the remaining category contains "No categories found."
const categoryTitle = await page.waitForSelector( '#the-list tr.no-items' );
expect(
await categoryTitle.evaluate( ( element ) => element.innerText )
).toContain( 'No categories found.' );
} );
it( 'Allows an existing category to be edited with the Quick Edit link', async () => {
await createNewCategory();
// Check that the new category is added and shows correctly
const newCategoryLink = await page.waitForSelector( '#the-list tr:first-child .row-title' )
// Focus on the new category link and move to the quick edit link
newCategoryLink.focus();
await pressKeyTimes( 'Tab', 2 );
// Click on the quick edit link
await page.keyboard.press( 'Enter' );
// Add some content at the end of the category name
await page.keyboard.type( ' edited' );
// Save the edited category
await page.keyboard.press( 'Enter' );
// Wait for the quick edit button to reappear.
await page.waitForSelector( 'button.editinline', { visible: true } );
// Check that the edited category title is "New category edited"
const editedCategoryLink = await page.$x(
`//a[contains( @class, "row-title" )][contains( text(), "New category edited" )]`
);
expect( editedCategoryLink.length ).toBe( 1 );
} );
it( 'Allows an existing category to be edited using the Edit link', async () => {
await createNewCategory();
// Check that the new category is added and shows correctly
const newCategoryLink = await page.waitForSelector( '#the-list tr:first-child .row-title' )
// Focus on the new category link and move to the edit link
newCategoryLink.focus();
await page.keyboard.press( 'Tab' );
// Click on the edit link
await page.keyboard.press( 'Enter' );
// Wait for the category name input to appears
await page.waitForSelector( 'input#name' );
// Add some content at the beginning of the category name
await page.keyboard.type( 'Edited ' );
// Save the edited category
await page.keyboard.press( 'Enter' );
// Wait for the success notice message to show
await page.waitForSelector( '.notice-success' );
// Go back to the categories list page
const categoriesPageQuery = addQueryArgs( '', {
taxonomy: 'category',
} );
await visitAdminPage( 'edit-tags.php', categoriesPageQuery );
// Check that the edited category title is "New category edited"
const editedCategoryLink = await page.$x(
`//a[contains( @class, "row-title" )][contains( text(), "Edited New category" )]`
);
expect( editedCategoryLink.length ).toBe( 1 );
} );
} );