|
157 | 157 | '</li>'; |
158 | 158 | } |
159 | 159 |
|
| 160 | + /** |
| 161 | + * Builds the synthetic Home row. |
| 162 | + * |
| 163 | + * When no static front page is set, nav-menu.php gives Home the |
| 164 | + * placeholder as its object ID, so the ID changes on every render. |
| 165 | + * |
| 166 | + * @return {string} The `li` markup. |
| 167 | + */ |
| 168 | + function homeItem() { |
| 169 | + return checklistItem( placeholder - 1, { |
| 170 | + type: 'custom', |
| 171 | + object: 'custom', |
| 172 | + title: 'Home', |
| 173 | + url: 'https://example.org/' |
| 174 | + } ); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Builds a post type archive row, which also uses a placeholder ID. |
| 179 | + * |
| 180 | + * @return {string} The `li` markup. |
| 181 | + */ |
| 182 | + function archiveItem() { |
| 183 | + return checklistItem( placeholder - 1, { |
| 184 | + type: 'post_type_archive', |
| 185 | + object: 'post', |
| 186 | + title: 'Post Archives', |
| 187 | + url: 'https://example.org/archive/' |
| 188 | + } ); |
| 189 | + } |
| 190 | + |
160 | 191 | /** |
161 | 192 | * Wraps items in a meta box with an active and an inactive tab panel. |
162 | 193 | * |
|
325 | 356 | ); |
326 | 357 | } ); |
327 | 358 |
|
| 359 | + QUnit.test( 'getSelectedMenuItemKey() identifies Home by URL, not by its placeholder.', function( assert ) { |
| 360 | + var firstRender = { |
| 361 | + 'menu-item-type': 'custom', |
| 362 | + 'menu-item-object': 'custom', |
| 363 | + 'menu-item-object-id': '-1', |
| 364 | + 'menu-item-url': 'https://example.org/', |
| 365 | + 'menu-item-title': 'Home' |
| 366 | + }, |
| 367 | + secondRender = $.extend( {}, firstRender, { 'menu-item-object-id': '-4' } ); |
| 368 | + |
| 369 | + assert.strictEqual( |
| 370 | + api.getSelectedMenuItemKey( firstRender, -1 ), |
| 371 | + api.getSelectedMenuItemKey( secondRender, -4 ), |
| 372 | + 'Home keeps one key across renders even though its object ID is a placeholder.' |
| 373 | + ); |
| 374 | + } ); |
| 375 | + |
| 376 | + QUnit.test( 'getSelectedMenuItemKey() identifies a post type archive across renders.', function( assert ) { |
| 377 | + var firstRender = { |
| 378 | + 'menu-item-type': 'post_type_archive', |
| 379 | + 'menu-item-object': 'post', |
| 380 | + 'menu-item-object-id': '-2', |
| 381 | + 'menu-item-url': 'https://example.org/archive/', |
| 382 | + 'menu-item-title': 'Post Archives' |
| 383 | + }, |
| 384 | + secondRender = $.extend( {}, firstRender, { 'menu-item-object-id': '-9' } ); |
| 385 | + |
| 386 | + assert.strictEqual( |
| 387 | + api.getSelectedMenuItemKey( firstRender, -2 ), |
| 388 | + api.getSelectedMenuItemKey( secondRender, -9 ), |
| 389 | + 'A post type archive keeps one key across renders.' |
| 390 | + ); |
| 391 | + } ); |
| 392 | + |
328 | 393 | QUnit.test( 'getSelectedMenuItemKey() keeps two custom links apart.', function( assert ) { |
329 | 394 | var first = { |
330 | 395 | 'menu-item-type': 'custom', |
|
467 | 532 | assert.strictEqual( checkboxFor( metaboxEl, 13 ).prop( 'checked' ), true, 'Item 13 is ticked again.' ); |
468 | 533 | } ); |
469 | 534 |
|
| 535 | + QUnit.test( 'A ticked Home row survives pagination.', function( assert ) { |
| 536 | + renderScreen( metabox( 'posttype-page', 'posttypediv', [ homeItem(), checklistItem( 12 ) ] ) ); |
| 537 | + |
| 538 | + var metaboxEl = fixture.find( '#posttype-page' ), |
| 539 | + homeId = metaboxEl.find( 'input.menu-item-checkbox' ).first().val(), |
| 540 | + pageOneAgain; |
| 541 | + |
| 542 | + metaboxEl.find( 'input.menu-item-checkbox' ).first().trigger( 'click' ); |
| 543 | + assert.strictEqual( countSelected( metaboxEl ), 1, 'Home was cached.' ); |
| 544 | + |
| 545 | + // A later request renders Home under a different placeholder. |
| 546 | + newRequest(); |
| 547 | + nextPlaceholder(); |
| 548 | + nextPlaceholder(); |
| 549 | + pageOneAgain = metabox( 'posttype-page', 'posttypediv', [ homeItem(), checklistItem( 12 ) ] ); |
| 550 | + |
| 551 | + $.post = function( url, data, success ) { |
| 552 | + success( JSON.stringify( { 'replace-id': 'posttype-page', markup: pageOneAgain } ) ); |
| 553 | + }; |
| 554 | + fixture.find( 'a.page-numbers' ).trigger( 'click' ); |
| 555 | + |
| 556 | + metaboxEl = fixture.find( '#posttype-page' ); |
| 557 | + assert.notStrictEqual( |
| 558 | + metaboxEl.find( 'input.menu-item-checkbox' ).first().val(), |
| 559 | + homeId, |
| 560 | + 'Home really did come back under a different object ID.' |
| 561 | + ); |
| 562 | + assert.strictEqual( |
| 563 | + metaboxEl.find( 'input.menu-item-checkbox' ).first().prop( 'checked' ), |
| 564 | + true, |
| 565 | + 'Home is still ticked.' |
| 566 | + ); |
| 567 | + } ); |
| 568 | + |
| 569 | + QUnit.test( 'A ticked post type archive row survives pagination.', function( assert ) { |
| 570 | + renderScreen( metabox( 'posttype-post', 'posttypediv', [ archiveItem(), checklistItem( 31 ) ] ) ); |
| 571 | + |
| 572 | + var metaboxEl = fixture.find( '#posttype-post' ), |
| 573 | + archiveId = metaboxEl.find( 'input.menu-item-checkbox' ).first().val(), |
| 574 | + pageOneAgain; |
| 575 | + |
| 576 | + metaboxEl.find( 'input.menu-item-checkbox' ).first().trigger( 'click' ); |
| 577 | + assert.strictEqual( countSelected( metaboxEl ), 1, 'The archive row was cached.' ); |
| 578 | + |
| 579 | + // A later request renders the archive under a different placeholder. |
| 580 | + newRequest(); |
| 581 | + nextPlaceholder(); |
| 582 | + nextPlaceholder(); |
| 583 | + pageOneAgain = metabox( 'posttype-post', 'posttypediv', [ archiveItem(), checklistItem( 31 ) ] ); |
| 584 | + |
| 585 | + $.post = function( url, data, success ) { |
| 586 | + success( JSON.stringify( { 'replace-id': 'posttype-post', markup: pageOneAgain } ) ); |
| 587 | + }; |
| 588 | + fixture.find( 'a.page-numbers' ).trigger( 'click' ); |
| 589 | + |
| 590 | + metaboxEl = fixture.find( '#posttype-post' ); |
| 591 | + assert.notStrictEqual( |
| 592 | + metaboxEl.find( 'input.menu-item-checkbox' ).first().val(), |
| 593 | + archiveId, |
| 594 | + 'The archive row really did come back under a different object ID.' |
| 595 | + ); |
| 596 | + assert.strictEqual( |
| 597 | + metaboxEl.find( 'input.menu-item-checkbox' ).first().prop( 'checked' ), |
| 598 | + true, |
| 599 | + 'The archive row is still ticked.' |
| 600 | + ); |
| 601 | + } ); |
| 602 | + |
470 | 603 | QUnit.test( 'Taxonomy meta boxes keep their selections across pagination.', function( assert ) { |
471 | 604 | renderScreen( metabox( 'taxonomy-category', 'taxonomydiv', [ |
472 | 605 | checklistItem( 5, { type: 'taxonomy', object: 'category', title: 'News' } ), |
|
0 commit comments