@@ -2345,6 +2345,212 @@ describe('MDC-based MatAutocomplete', () => {
2345
2345
2346
2346
} ) ;
2347
2347
2348
+ describe ( 'automatically selecting the active option' , ( ) => {
2349
+ let fixture : ComponentFixture < SimpleAutocomplete > ;
2350
+
2351
+ beforeEach ( ( ) => {
2352
+ fixture = createComponent ( SimpleAutocomplete ) ;
2353
+ fixture . detectChanges ( ) ;
2354
+ fixture . componentInstance . trigger . autocomplete . autoSelectActiveOption = true ;
2355
+ } ) ;
2356
+
2357
+ it ( 'should update the input value as the user is navigating, without changing the model ' +
2358
+ 'value or closing the panel' , fakeAsync ( ( ) => {
2359
+ const { trigger, stateCtrl, closedSpy} = fixture . componentInstance ;
2360
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2361
+
2362
+ trigger . openPanel ( ) ;
2363
+ fixture . detectChanges ( ) ;
2364
+ zone . simulateZoneExit ( ) ;
2365
+ fixture . detectChanges ( ) ;
2366
+
2367
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2368
+ expect ( input . value ) . toBeFalsy ( ) ;
2369
+ expect ( trigger . panelOpen ) . toBe ( true ) ;
2370
+ expect ( closedSpy ) . not . toHaveBeenCalled ( ) ;
2371
+
2372
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2373
+ fixture . detectChanges ( ) ;
2374
+
2375
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2376
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2377
+ expect ( trigger . panelOpen ) . toBe ( true ) ;
2378
+ expect ( closedSpy ) . not . toHaveBeenCalled ( ) ;
2379
+
2380
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2381
+ fixture . detectChanges ( ) ;
2382
+
2383
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2384
+ expect ( input . value ) . toBe ( 'California' ) ;
2385
+ expect ( trigger . panelOpen ) . toBe ( true ) ;
2386
+ expect ( closedSpy ) . not . toHaveBeenCalled ( ) ;
2387
+ } ) ) ;
2388
+
2389
+ it ( 'should revert back to the last typed value if the user presses escape' , fakeAsync ( ( ) => {
2390
+ const { trigger, stateCtrl, closedSpy} = fixture . componentInstance ;
2391
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2392
+
2393
+ trigger . openPanel ( ) ;
2394
+ fixture . detectChanges ( ) ;
2395
+ zone . simulateZoneExit ( ) ;
2396
+ fixture . detectChanges ( ) ;
2397
+ typeInElement ( input , 'al' ) ;
2398
+ fixture . detectChanges ( ) ;
2399
+ tick ( ) ;
2400
+
2401
+ expect ( stateCtrl . value ) . toBe ( 'al' ) ;
2402
+ expect ( input . value ) . toBe ( 'al' ) ;
2403
+ expect ( trigger . panelOpen ) . toBe ( true ) ;
2404
+ expect ( closedSpy ) . not . toHaveBeenCalled ( ) ;
2405
+
2406
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2407
+ fixture . detectChanges ( ) ;
2408
+
2409
+ expect ( stateCtrl . value ) . toBe ( 'al' ) ;
2410
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2411
+ expect ( trigger . panelOpen ) . toBe ( true ) ;
2412
+ expect ( closedSpy ) . not . toHaveBeenCalled ( ) ;
2413
+
2414
+ dispatchKeyboardEvent ( document . body , 'keydown' , ESCAPE ) ;
2415
+ fixture . detectChanges ( ) ;
2416
+
2417
+ expect ( stateCtrl . value ) . toBe ( 'al' ) ;
2418
+ expect ( input . value ) . toBe ( 'al' ) ;
2419
+ expect ( trigger . panelOpen ) . toBe ( false ) ;
2420
+ expect ( closedSpy ) . toHaveBeenCalledTimes ( 1 ) ;
2421
+ } ) ) ;
2422
+
2423
+ it ( 'should clear the input if the user presses escape while there was a pending ' +
2424
+ 'auto selection and there is no previous value' , fakeAsync ( ( ) => {
2425
+ const { trigger, stateCtrl} = fixture . componentInstance ;
2426
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2427
+
2428
+ trigger . openPanel ( ) ;
2429
+ fixture . detectChanges ( ) ;
2430
+ zone . simulateZoneExit ( ) ;
2431
+ fixture . detectChanges ( ) ;
2432
+
2433
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2434
+ expect ( input . value ) . toBeFalsy ( ) ;
2435
+
2436
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2437
+ fixture . detectChanges ( ) ;
2438
+
2439
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2440
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2441
+
2442
+ dispatchKeyboardEvent ( document . body , 'keydown' , ESCAPE ) ;
2443
+ fixture . detectChanges ( ) ;
2444
+
2445
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2446
+ expect ( input . value ) . toBeFalsy ( ) ;
2447
+ } ) ) ;
2448
+
2449
+ it ( 'should propagate the auto-selected value if the user clicks away' , fakeAsync ( ( ) => {
2450
+ const { trigger, stateCtrl} = fixture . componentInstance ;
2451
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2452
+
2453
+ trigger . openPanel ( ) ;
2454
+ fixture . detectChanges ( ) ;
2455
+ zone . simulateZoneExit ( ) ;
2456
+ fixture . detectChanges ( ) ;
2457
+
2458
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2459
+ expect ( input . value ) . toBeFalsy ( ) ;
2460
+
2461
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2462
+ fixture . detectChanges ( ) ;
2463
+
2464
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2465
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2466
+
2467
+ dispatchFakeEvent ( document , 'click' ) ;
2468
+ fixture . detectChanges ( ) ;
2469
+
2470
+ expect ( stateCtrl . value ) . toEqual ( { code : 'AL' , name : 'Alabama' } ) ;
2471
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2472
+ } ) ) ;
2473
+
2474
+ it ( 'should propagate the auto-selected value if the user tabs away' , fakeAsync ( ( ) => {
2475
+ const { trigger, stateCtrl} = fixture . componentInstance ;
2476
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2477
+
2478
+ trigger . openPanel ( ) ;
2479
+ fixture . detectChanges ( ) ;
2480
+ zone . simulateZoneExit ( ) ;
2481
+ fixture . detectChanges ( ) ;
2482
+
2483
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2484
+ expect ( input . value ) . toBeFalsy ( ) ;
2485
+
2486
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2487
+ fixture . detectChanges ( ) ;
2488
+
2489
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2490
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2491
+
2492
+ dispatchKeyboardEvent ( input , 'keydown' , TAB ) ;
2493
+ fixture . detectChanges ( ) ;
2494
+
2495
+ expect ( stateCtrl . value ) . toEqual ( { code : 'AL' , name : 'Alabama' } ) ;
2496
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2497
+ } ) ) ;
2498
+
2499
+ it ( 'should propagate the auto-selected value if the user presses enter on it' , fakeAsync ( ( ) => {
2500
+ const { trigger, stateCtrl} = fixture . componentInstance ;
2501
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2502
+
2503
+ trigger . openPanel ( ) ;
2504
+ fixture . detectChanges ( ) ;
2505
+ zone . simulateZoneExit ( ) ;
2506
+ fixture . detectChanges ( ) ;
2507
+
2508
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2509
+ expect ( input . value ) . toBeFalsy ( ) ;
2510
+
2511
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2512
+ fixture . detectChanges ( ) ;
2513
+
2514
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2515
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2516
+
2517
+ dispatchKeyboardEvent ( input , 'keydown' , ENTER ) ;
2518
+ fixture . detectChanges ( ) ;
2519
+
2520
+ expect ( stateCtrl . value ) . toEqual ( { code : 'AL' , name : 'Alabama' } ) ;
2521
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2522
+ } ) ) ;
2523
+
2524
+ it ( 'should allow the user to click on an option different from the auto-selected one' ,
2525
+ fakeAsync ( ( ) => {
2526
+ const { trigger, stateCtrl} = fixture . componentInstance ;
2527
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2528
+
2529
+ trigger . openPanel ( ) ;
2530
+ fixture . detectChanges ( ) ;
2531
+ zone . simulateZoneExit ( ) ;
2532
+ fixture . detectChanges ( ) ;
2533
+
2534
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2535
+ expect ( input . value ) . toBeFalsy ( ) ;
2536
+
2537
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2538
+ fixture . detectChanges ( ) ;
2539
+
2540
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2541
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2542
+
2543
+ const options =
2544
+ overlayContainerElement . querySelectorAll ( 'mat-option' ) as NodeListOf < HTMLElement > ;
2545
+ options [ 2 ] . click ( ) ;
2546
+ fixture . detectChanges ( ) ;
2547
+
2548
+ expect ( stateCtrl . value ) . toEqual ( { code : 'FL' , name : 'Florida' } ) ;
2549
+ expect ( input . value ) . toBe ( 'Florida' ) ;
2550
+ } ) ) ;
2551
+
2552
+ } ) ;
2553
+
2348
2554
it ( 'should have correct width when opened' , ( ) => {
2349
2555
const widthFixture = createComponent ( SimpleAutocomplete ) ;
2350
2556
widthFixture . componentInstance . width = 300 ;
0 commit comments