|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +import { randomUUID } from 'node:crypto'; |
| 5 | + |
1 | 6 | /** |
2 | 7 | * WordPress dependencies |
3 | 8 | */ |
@@ -165,3 +170,233 @@ test.describe( 'Quick Draft', () => { |
165 | 170 | ).toContainText( '(no title)' ); |
166 | 171 | } ); |
167 | 172 | } ); |
| 173 | + |
| 174 | +// Wait for the matching save before reloading, rather than racing the Ajax request. |
| 175 | +async function saveDashboardPreference( page, action, change ) { |
| 176 | + const [ response ] = await Promise.all( [ |
| 177 | + page.waitForResponse( |
| 178 | + ( result ) => |
| 179 | + new URL( result.url() ).pathname.endsWith( |
| 180 | + '/admin-ajax.php' |
| 181 | + ) && |
| 182 | + new URLSearchParams( result.request().postData() || '' ).get( |
| 183 | + 'action' |
| 184 | + ) === action |
| 185 | + ), |
| 186 | + change(), |
| 187 | + ] ); |
| 188 | + expect( response.ok() ).toBe( true ); |
| 189 | +} |
| 190 | + |
| 191 | +test.describe( 'Dashboard preferences', () => { |
| 192 | + test.use( { storageState: { cookies: [], origins: [] } } ); |
| 193 | + let dashboard; |
| 194 | + let user; |
| 195 | + |
| 196 | + test.beforeEach( async ( { page, requestUtils, baseURL } ) => { |
| 197 | + user = undefined; |
| 198 | + const username = `e2e-dashboard-${ randomUUID() }`; |
| 199 | + const password = randomUUID(); |
| 200 | + user = await requestUtils.createUser( { |
| 201 | + username, |
| 202 | + password, |
| 203 | + email: `${ username }@example.org`, |
| 204 | + roles: [ 'administrator' ], |
| 205 | + } ); |
| 206 | + dashboard = page; |
| 207 | + await dashboard.goto( new URL( 'wp-login.php', baseURL ).href ); |
| 208 | + const usernameField = dashboard.getByLabel( |
| 209 | + 'Username or Email Address', |
| 210 | + { exact: true } |
| 211 | + ); |
| 212 | + await expect( usernameField ).toBeFocused(); |
| 213 | + await usernameField.fill( username ); |
| 214 | + await dashboard |
| 215 | + .getByLabel( 'Password', { exact: true } ) |
| 216 | + .fill( password ); |
| 217 | + await dashboard |
| 218 | + .getByRole( 'button', { name: 'Log In', exact: true } ) |
| 219 | + .click(); |
| 220 | + await expect( |
| 221 | + dashboard.getByRole( 'heading', { name: 'Dashboard', exact: true } ) |
| 222 | + ).toBeVisible(); |
| 223 | + // Set a known Welcome state for this user's first visit without altering shared preferences. |
| 224 | + const response = await dashboard.request.get( |
| 225 | + new URL( 'wp-admin/index.php?welcome=1', baseURL ).href |
| 226 | + ); |
| 227 | + expect( response.ok() ).toBe( true ); |
| 228 | + await response.dispose(); |
| 229 | + await dashboard.goto( new URL( 'wp-admin/index.php', baseURL ).href ); |
| 230 | + } ); |
| 231 | + |
| 232 | + test.afterEach( async ( { requestUtils } ) => { |
| 233 | + if ( user ) { |
| 234 | + // User deletion only trashes posts; permanently delete this user's Quick Draft auto-drafts first. |
| 235 | + const drafts = await requestUtils.rest( { |
| 236 | + path: '/wp/v2/posts', |
| 237 | + params: { author: user.id, status: 'auto-draft', per_page: 100 }, |
| 238 | + } ); |
| 239 | + for ( const draft of drafts ) { |
| 240 | + await requestUtils.rest( { |
| 241 | + method: 'DELETE', |
| 242 | + path: `/wp/v2/posts/${ draft.id }`, |
| 243 | + params: { force: true }, |
| 244 | + } ); |
| 245 | + } |
| 246 | + await requestUtils.rest( { |
| 247 | + method: 'DELETE', |
| 248 | + path: `/wp/v2/users/${ user.id }`, |
| 249 | + params: { force: true, reassign: false }, |
| 250 | + } ); |
| 251 | + } |
| 252 | + } ); |
| 253 | + |
| 254 | + test( 'keeps the Welcome panel dismissed after reload', async () => { |
| 255 | + const panel = dashboard.locator( '#welcome-panel' ); |
| 256 | + await expect( panel ).toBeVisible(); |
| 257 | + await saveDashboardPreference( dashboard, 'update-welcome-panel', () => |
| 258 | + dashboard |
| 259 | + .getByRole( 'link', { |
| 260 | + name: 'Dismiss the welcome panel', |
| 261 | + exact: true, |
| 262 | + } ) |
| 263 | + .click() |
| 264 | + ); |
| 265 | + await expect( panel ).toBeHidden(); |
| 266 | + await dashboard.reload(); |
| 267 | + await expect( panel ).toBeHidden(); |
| 268 | + await dashboard |
| 269 | + .getByRole( 'button', { name: 'Screen Options', exact: true } ) |
| 270 | + .click(); |
| 271 | + await expect( |
| 272 | + dashboard.getByLabel( 'Welcome', { exact: true } ) |
| 273 | + ).not.toBeChecked(); |
| 274 | + } ); |
| 275 | + |
| 276 | + test( 'saves Welcome visibility from Screen Options', async () => { |
| 277 | + const panel = dashboard.locator( '#welcome-panel' ); |
| 278 | + await expect( panel ).toBeVisible(); |
| 279 | + await dashboard |
| 280 | + .getByRole( 'button', { name: 'Screen Options', exact: true } ) |
| 281 | + .click(); |
| 282 | + const checkbox = dashboard.getByLabel( 'Welcome', { exact: true } ); |
| 283 | + await saveDashboardPreference( dashboard, 'update-welcome-panel', () => |
| 284 | + checkbox.uncheck() |
| 285 | + ); |
| 286 | + await expect( panel ).toBeHidden(); |
| 287 | + await dashboard.reload(); |
| 288 | + await expect( panel ).toBeHidden(); |
| 289 | + await dashboard |
| 290 | + .getByRole( 'button', { name: 'Screen Options', exact: true } ) |
| 291 | + .click(); |
| 292 | + await expect( checkbox ).not.toBeChecked(); |
| 293 | + await saveDashboardPreference( dashboard, 'update-welcome-panel', () => |
| 294 | + checkbox.check() |
| 295 | + ); |
| 296 | + await expect( panel ).toBeVisible(); |
| 297 | + await dashboard.reload(); |
| 298 | + await expect( panel ).toBeVisible(); |
| 299 | + } ); |
| 300 | + |
| 301 | + test( 'saves widget visibility from Screen Options', async () => { |
| 302 | + const widget = dashboard.locator( '#dashboard_right_now' ); |
| 303 | + await expect( widget ).toBeVisible(); |
| 304 | + await dashboard |
| 305 | + .getByRole( 'button', { name: 'Screen Options', exact: true } ) |
| 306 | + .click(); |
| 307 | + const checkbox = dashboard.getByRole( 'checkbox', { |
| 308 | + name: 'At a Glance', |
| 309 | + exact: true, |
| 310 | + } ); |
| 311 | + await saveDashboardPreference( dashboard, 'closed-postboxes', () => |
| 312 | + checkbox.uncheck() |
| 313 | + ); |
| 314 | + await expect( widget ).toBeHidden(); |
| 315 | + await dashboard.reload(); |
| 316 | + await expect( widget ).toBeHidden(); |
| 317 | + await dashboard |
| 318 | + .getByRole( 'button', { name: 'Screen Options', exact: true } ) |
| 319 | + .click(); |
| 320 | + await expect( checkbox ).not.toBeChecked(); |
| 321 | + await saveDashboardPreference( dashboard, 'closed-postboxes', () => |
| 322 | + checkbox.check() |
| 323 | + ); |
| 324 | + await expect( widget ).toBeVisible(); |
| 325 | + await dashboard.reload(); |
| 326 | + await expect( widget ).toBeVisible(); |
| 327 | + } ); |
| 328 | + |
| 329 | + test( 'saves a widget collapse and expansion', async () => { |
| 330 | + const widget = dashboard.locator( '#dashboard_right_now' ); |
| 331 | + const content = widget.locator( '.inside' ); |
| 332 | + const toggle = widget.getByRole( 'button', { |
| 333 | + name: 'Show or hide panel', |
| 334 | + exact: true, |
| 335 | + } ); |
| 336 | + await expect( content ).toBeVisible(); |
| 337 | + await saveDashboardPreference( dashboard, 'closed-postboxes', () => |
| 338 | + toggle.click() |
| 339 | + ); |
| 340 | + await expect( content ).toBeHidden(); |
| 341 | + await dashboard.reload(); |
| 342 | + await expect( content ).toBeHidden(); |
| 343 | + await saveDashboardPreference( dashboard, 'closed-postboxes', () => |
| 344 | + toggle.click() |
| 345 | + ); |
| 346 | + await expect( content ).toBeVisible(); |
| 347 | + await dashboard.reload(); |
| 348 | + await expect( content ).toBeVisible(); |
| 349 | + } ); |
| 350 | + |
| 351 | + test( 'saves widget order after moving down and up', async () => { |
| 352 | + await dashboard |
| 353 | + .getByRole( 'button', { name: 'Screen Options', exact: true } ) |
| 354 | + .click(); |
| 355 | + const reordering = dashboard.locator( '#meta-box-reordering' ); |
| 356 | + if ( ! ( await reordering.isChecked() ) ) { |
| 357 | + await saveDashboardPreference( |
| 358 | + dashboard, |
| 359 | + 'meta-box-reordering', |
| 360 | + () => reordering.check() |
| 361 | + ); |
| 362 | + } |
| 363 | + await dashboard |
| 364 | + .getByRole( 'button', { name: 'Screen Options', exact: true } ) |
| 365 | + .click(); |
| 366 | + const widget = dashboard.locator( '#dashboard_right_now' ); |
| 367 | + const normal = dashboard.locator( '#normal-sortables > .postbox' ); |
| 368 | + const originalOrder = await normal.evaluateAll( ( boxes ) => |
| 369 | + boxes.map( ( box ) => box.id ) |
| 370 | + ); |
| 371 | + const index = originalOrder.indexOf( 'dashboard_right_now' ); |
| 372 | + expect( index ).toBeGreaterThanOrEqual( 0 ); |
| 373 | + expect( index ).toBeLessThan( originalOrder.length - 1 ); |
| 374 | + const movedOrder = [ ...originalOrder ]; |
| 375 | + [ movedOrder[ index ], movedOrder[ index + 1 ] ] = [ |
| 376 | + movedOrder[ index + 1 ], |
| 377 | + movedOrder[ index ], |
| 378 | + ]; |
| 379 | + await saveDashboardPreference( dashboard, 'meta-box-order', () => |
| 380 | + widget |
| 381 | + .getByRole( 'button', { name: 'Move down', exact: true } ) |
| 382 | + .click() |
| 383 | + ); |
| 384 | + await dashboard.reload(); |
| 385 | + expect( |
| 386 | + await normal.evaluateAll( ( boxes ) => |
| 387 | + boxes.map( ( box ) => box.id ) |
| 388 | + ) |
| 389 | + ).toEqual( movedOrder ); |
| 390 | + await saveDashboardPreference( dashboard, 'meta-box-order', () => |
| 391 | + widget |
| 392 | + .getByRole( 'button', { name: 'Move up', exact: true } ) |
| 393 | + .click() |
| 394 | + ); |
| 395 | + await dashboard.reload(); |
| 396 | + expect( |
| 397 | + await normal.evaluateAll( ( boxes ) => |
| 398 | + boxes.map( ( box ) => box.id ) |
| 399 | + ) |
| 400 | + ).toEqual( originalOrder ); |
| 401 | + } ); |
| 402 | +} ); |
0 commit comments