@@ -10,6 +10,8 @@ import {
10
10
triggerEvent ,
11
11
} from ' @ember/test-helpers' ;
12
12
13
+ import { tracked } from ' @glimmer/tracking' ;
14
+
13
15
import percySnapshot from ' @percy/ember' ;
14
16
import format from ' date-fns/format' ;
15
17
import parseISO from ' date-fns/parseISO' ;
@@ -2283,6 +2285,157 @@ module('Integration | card-basics', function (hooks) {
2283
2285
await waitUntil (() => cleanWhiteSpace (root .textContent ! ) === ' Quint' );
2284
2286
});
2285
2287
2288
+ test (' Re-ordering items in a linksToMany field will preserve the template and box component state' , async function (assert ) {
2289
+ class Fitted extends Component <typeof Pet1 > {
2290
+ @tracked counter = 0 ;
2291
+
2292
+ incrementCounter = () => {
2293
+ this .counter ++ ;
2294
+ };
2295
+ <template >
2296
+ {{@ model.name }}
2297
+ <button
2298
+ {{on ' click' this . incrementCounter}}
2299
+ data-test-increment-counter
2300
+ >Increment</button >
2301
+ <div data-test-counter >
2302
+ {{this .counter }}
2303
+ </div >
2304
+ </template >
2305
+ }
2306
+
2307
+ class FittedPrime extends Component <typeof Pet1Prime > {
2308
+ @tracked counter = 0 ;
2309
+
2310
+ incrementCounter = () => {
2311
+ this .counter ++ ;
2312
+ };
2313
+ <template >
2314
+ <div data-test-different-template >Different Template</div >
2315
+ {{@ model.name }}
2316
+ <button
2317
+ {{on ' click' this . incrementCounter}}
2318
+ data-test-increment-counter
2319
+ >Increment</button >
2320
+ <div data-test-counter >
2321
+ {{this .counter }}
2322
+ </div >
2323
+ </template >
2324
+ }
2325
+ class Pet1 extends CardDef {
2326
+ @field name = contains (StringField );
2327
+ static fitted = Fitted ;
2328
+ }
2329
+
2330
+ class Pet1Prime extends Pet1 {
2331
+ static fitted = FittedPrime ;
2332
+ }
2333
+
2334
+ class Person1 extends CardDef {
2335
+ @field pets = linksToMany (Pet1 );
2336
+ static isolated = class Embedded extends Component <typeof this > {
2337
+ reorder = () => {
2338
+ if (
2339
+ this .args .model .pets &&
2340
+ this .args .model .pets [0 ] &&
2341
+ this .args .model .pets [1 ]
2342
+ ) {
2343
+ this .args .model .pets = [
2344
+ this .args .model .pets [1 ],
2345
+ this .args .model .pets [0 ],
2346
+ ];
2347
+ }
2348
+ };
2349
+ <template >
2350
+ <button
2351
+ {{on ' click' this . reorder}}
2352
+ data-test-reorder
2353
+ >Reorder</button >
2354
+ <@ fields.pets @ format =' fitted' />
2355
+ </template >
2356
+ };
2357
+ }
2358
+
2359
+ loader .shimModule (` ${testRealmURL }test-cards ` , {
2360
+ Pet1 ,
2361
+ Pet1Prime ,
2362
+ Person1 ,
2363
+ });
2364
+
2365
+ let pet1 = new Pet1 ({ name: ' jersey' });
2366
+ let pet2 = new Pet1Prime ({ name: ' boboy' });
2367
+ await saveCard (pet1 , ` ${testRealmURL }Pet/pet1 ` , loader );
2368
+ await saveCard (pet2 , ` ${testRealmURL }Pet/pet2 ` , loader );
2369
+ let person = new Person1 ({
2370
+ name: ' Mango' ,
2371
+ pets: [pet1 , pet2 ],
2372
+ });
2373
+ await renderCard (loader , person , ' isolated' );
2374
+ assert
2375
+ .dom (` [data-test-plural-view-item="0"] [data-test-counter] ` )
2376
+ .hasText (' 0' );
2377
+ assert
2378
+ .dom (
2379
+ ` [data-test-plural-view-item="0"][data-test-card="${testRealmURL }Pet/pet1"] ` ,
2380
+ )
2381
+ .containsText (' jersey' );
2382
+
2383
+ await click (
2384
+ ` [data-test-plural-view-item="0"] [data-test-increment-counter] ` ,
2385
+ );
2386
+ await click (
2387
+ ` [data-test-plural-view-item="0"] [data-test-increment-counter] ` ,
2388
+ );
2389
+ assert
2390
+ .dom (` [data-test-plural-view-item="0"] [data-test-counter] ` )
2391
+ .hasText (' 2' );
2392
+ assert
2393
+ .dom (
2394
+ ` [data-test-plural-view-item="0"][data-test-card="${testRealmURL }Pet/pet1"] ` ,
2395
+ )
2396
+ .containsText (' jersey' );
2397
+ assert
2398
+ .dom (` [data-test-plural-view-item="0"] [data-test-different-template] ` )
2399
+ .doesNotExist ();
2400
+ assert
2401
+ .dom (` [data-test-plural-view-item="1"] [data-test-counter] ` )
2402
+ .hasText (' 0' );
2403
+ assert
2404
+ .dom (
2405
+ ` [data-test-plural-view-item="1"][data-test-card="${testRealmURL }Pet/pet2"] ` ,
2406
+ )
2407
+ .containsText (' boboy' );
2408
+ assert
2409
+ .dom (` [data-test-plural-view-item="1"] [data-test-different-template] ` )
2410
+ .exists ();
2411
+ await click (' [data-test-reorder]' ); // Reorder
2412
+ assert
2413
+ .dom (` [data-test-plural-view-item="0"] [data-test-counter] ` )
2414
+ .hasText (' 0' );
2415
+ assert
2416
+ .dom (
2417
+ ` [data-test-plural-view-item="0"][data-test-card="${testRealmURL }Pet/pet2"] ` ,
2418
+ )
2419
+ .containsText (' boboy' );
2420
+ assert
2421
+ .dom (` [data-test-plural-view-item="0"] [data-test-counter] ` )
2422
+ .hasText (' 0' );
2423
+ assert
2424
+ .dom (` [data-test-plural-view-item="0"] [data-test-different-template] ` )
2425
+ .exists ();
2426
+ assert
2427
+ .dom (
2428
+ ` [data-test-plural-view-item="1"][data-test-card="${testRealmURL }Pet/pet1"] ` ,
2429
+ )
2430
+ .containsText (' jersey' );
2431
+ assert
2432
+ .dom (` [data-test-plural-view-item="1"] [data-test-counter] ` )
2433
+ .hasText (' 2' );
2434
+ assert
2435
+ .dom (` [data-test-plural-view-item="1"] [data-test-different-template] ` )
2436
+ .doesNotExist ();
2437
+ });
2438
+
2286
2439
test (' rerender when a containsMany field is fully replaced' , async function (assert ) {
2287
2440
class Person extends CardDef {
2288
2441
@field pets = containsMany (StringField );
0 commit comments