|
| 1 | +/* globals wp */ |
| 2 | +/* jshint qunit: true */ |
| 3 | +/* eslint-env qunit */ |
| 4 | + |
| 5 | +( function () { |
| 6 | + 'use strict'; |
| 7 | + |
| 8 | + QUnit.module( 'wp.media.model.Attachments' ); |
| 9 | + |
| 10 | + /* |
| 11 | + * Trac #65053: opening the media modal via "Set featured image" or |
| 12 | + * an Image block reported an incorrect "Showing X of Y" total because |
| 13 | + * Attachments#observe() bound the totalAttachments trackers to every |
| 14 | + * observed collection, including the Selection. Selection mutations |
| 15 | + * therefore mutated the mirrored Query's totalAttachments. |
| 16 | + * |
| 17 | + * The library mirrors a Query (which owns totalAttachments) AND |
| 18 | + * additionally observes a Selection (so a pre-existing featured image |
| 19 | + * stays visible in the grid). Only the Query should drive the count. |
| 20 | + */ |
| 21 | + QUnit.test( |
| 22 | + 'observing a selection does not corrupt totalAttachments (Trac #65053)', |
| 23 | + function ( assert ) { |
| 24 | + var Attachments = wp.media.model.Attachments, |
| 25 | + Selection = wp.media.model.Selection, |
| 26 | + Attachment = wp.media.model.Attachment, |
| 27 | + query, |
| 28 | + library, |
| 29 | + selection; |
| 30 | + |
| 31 | + // Stand-in for a Query collection: the source of truth for totalAttachments. |
| 32 | + query = new Attachments(); |
| 33 | + query.totalAttachments = 8; |
| 34 | + |
| 35 | + // The library is what AttachmentsBrowser renders. It mirrors the query. |
| 36 | + library = new Attachments(); |
| 37 | + library.mirror( query ); |
| 38 | + |
| 39 | + assert.strictEqual( |
| 40 | + library.getTotalAttachments(), |
| 41 | + 8, |
| 42 | + 'precondition: getTotalAttachments() reflects the mirrored query total' |
| 43 | + ); |
| 44 | + |
| 45 | + // FeaturedImage / ReplaceImage controllers additionally observe the selection. |
| 46 | + selection = new Selection( [], { multiple: false } ); |
| 47 | + library.observe( selection ); |
| 48 | + |
| 49 | + // Adding to the selection must not mutate the mirrored query's total. |
| 50 | + selection.add( new Attachment( { id: 42 } ) ); |
| 51 | + assert.strictEqual( |
| 52 | + library.getTotalAttachments(), |
| 53 | + 8, |
| 54 | + 'add to observed selection leaves totalAttachments untouched' |
| 55 | + ); |
| 56 | + |
| 57 | + // Single-select swap: Selection#add internally remove()s prior models |
| 58 | + // before adding the new one. Both events must be ignored by the count. |
| 59 | + selection.add( new Attachment( { id: 99 } ) ); |
| 60 | + assert.strictEqual( |
| 61 | + library.getTotalAttachments(), |
| 62 | + 8, |
| 63 | + 'single-select swap on observed selection leaves totalAttachments untouched' |
| 64 | + ); |
| 65 | + |
| 66 | + // Removing from the selection must also leave the total alone. |
| 67 | + selection.remove( selection.models ); |
| 68 | + assert.strictEqual( |
| 69 | + library.getTotalAttachments(), |
| 70 | + 8, |
| 71 | + 'remove from observed selection leaves totalAttachments untouched' |
| 72 | + ); |
| 73 | + } |
| 74 | + ); |
| 75 | + |
| 76 | + QUnit.test( |
| 77 | + 'mirrored query add/remove still drives totalAttachments', |
| 78 | + function ( assert ) { |
| 79 | + var Attachments = wp.media.model.Attachments, |
| 80 | + Attachment = wp.media.model.Attachment, |
| 81 | + query, |
| 82 | + library, |
| 83 | + attachment; |
| 84 | + |
| 85 | + query = new Attachments(); |
| 86 | + query.totalAttachments = 5; |
| 87 | + |
| 88 | + library = new Attachments(); |
| 89 | + library.mirror( query ); |
| 90 | + |
| 91 | + attachment = new Attachment( { id: 1 } ); |
| 92 | + query.add( attachment ); |
| 93 | + assert.strictEqual( |
| 94 | + library.getTotalAttachments(), |
| 95 | + 6, |
| 96 | + 'adding to the mirrored query increments totalAttachments' |
| 97 | + ); |
| 98 | + |
| 99 | + query.remove( attachment ); |
| 100 | + assert.strictEqual( |
| 101 | + library.getTotalAttachments(), |
| 102 | + 5, |
| 103 | + 'removing from the mirrored query decrements totalAttachments' |
| 104 | + ); |
| 105 | + } |
| 106 | + ); |
| 107 | +} )(); |
0 commit comments