@@ -2391,6 +2391,212 @@ describe('MDC-based MatAutocomplete', () => {
2391
2391
2392
2392
} ) ;
2393
2393
2394
+ describe ( 'automatically selecting the active option' , ( ) => {
2395
+ let fixture : ComponentFixture < SimpleAutocomplete > ;
2396
+
2397
+ beforeEach ( ( ) => {
2398
+ fixture = createComponent ( SimpleAutocomplete ) ;
2399
+ fixture . detectChanges ( ) ;
2400
+ fixture . componentInstance . trigger . autocomplete . autoSelectActiveOption = true ;
2401
+ } ) ;
2402
+
2403
+ it ( 'should update the input value as the user is navigating, without changing the model ' +
2404
+ 'value or closing the panel' , fakeAsync ( ( ) => {
2405
+ const { trigger, stateCtrl, closedSpy} = fixture . componentInstance ;
2406
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2407
+
2408
+ trigger . openPanel ( ) ;
2409
+ fixture . detectChanges ( ) ;
2410
+ zone . simulateZoneExit ( ) ;
2411
+ fixture . detectChanges ( ) ;
2412
+
2413
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2414
+ expect ( input . value ) . toBeFalsy ( ) ;
2415
+ expect ( trigger . panelOpen ) . toBe ( true ) ;
2416
+ expect ( closedSpy ) . not . toHaveBeenCalled ( ) ;
2417
+
2418
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2419
+ fixture . detectChanges ( ) ;
2420
+
2421
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2422
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2423
+ expect ( trigger . panelOpen ) . toBe ( true ) ;
2424
+ expect ( closedSpy ) . not . toHaveBeenCalled ( ) ;
2425
+
2426
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2427
+ fixture . detectChanges ( ) ;
2428
+
2429
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2430
+ expect ( input . value ) . toBe ( 'California' ) ;
2431
+ expect ( trigger . panelOpen ) . toBe ( true ) ;
2432
+ expect ( closedSpy ) . not . toHaveBeenCalled ( ) ;
2433
+ } ) ) ;
2434
+
2435
+ it ( 'should revert back to the last typed value if the user presses escape' , fakeAsync ( ( ) => {
2436
+ const { trigger, stateCtrl, closedSpy} = fixture . componentInstance ;
2437
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2438
+
2439
+ trigger . openPanel ( ) ;
2440
+ fixture . detectChanges ( ) ;
2441
+ zone . simulateZoneExit ( ) ;
2442
+ fixture . detectChanges ( ) ;
2443
+ typeInElement ( input , 'al' ) ;
2444
+ fixture . detectChanges ( ) ;
2445
+ tick ( ) ;
2446
+
2447
+ expect ( stateCtrl . value ) . toBe ( 'al' ) ;
2448
+ expect ( input . value ) . toBe ( 'al' ) ;
2449
+ expect ( trigger . panelOpen ) . toBe ( true ) ;
2450
+ expect ( closedSpy ) . not . toHaveBeenCalled ( ) ;
2451
+
2452
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2453
+ fixture . detectChanges ( ) ;
2454
+
2455
+ expect ( stateCtrl . value ) . toBe ( 'al' ) ;
2456
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2457
+ expect ( trigger . panelOpen ) . toBe ( true ) ;
2458
+ expect ( closedSpy ) . not . toHaveBeenCalled ( ) ;
2459
+
2460
+ dispatchKeyboardEvent ( document . body , 'keydown' , ESCAPE ) ;
2461
+ fixture . detectChanges ( ) ;
2462
+
2463
+ expect ( stateCtrl . value ) . toBe ( 'al' ) ;
2464
+ expect ( input . value ) . toBe ( 'al' ) ;
2465
+ expect ( trigger . panelOpen ) . toBe ( false ) ;
2466
+ expect ( closedSpy ) . toHaveBeenCalledTimes ( 1 ) ;
2467
+ } ) ) ;
2468
+
2469
+ it ( 'should clear the input if the user presses escape while there was a pending ' +
2470
+ 'auto selection and there is no previous value' , fakeAsync ( ( ) => {
2471
+ const { trigger, stateCtrl} = fixture . componentInstance ;
2472
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2473
+
2474
+ trigger . openPanel ( ) ;
2475
+ fixture . detectChanges ( ) ;
2476
+ zone . simulateZoneExit ( ) ;
2477
+ fixture . detectChanges ( ) ;
2478
+
2479
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2480
+ expect ( input . value ) . toBeFalsy ( ) ;
2481
+
2482
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2483
+ fixture . detectChanges ( ) ;
2484
+
2485
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2486
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2487
+
2488
+ dispatchKeyboardEvent ( document . body , 'keydown' , ESCAPE ) ;
2489
+ fixture . detectChanges ( ) ;
2490
+
2491
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2492
+ expect ( input . value ) . toBeFalsy ( ) ;
2493
+ } ) ) ;
2494
+
2495
+ it ( 'should propagate the auto-selected value if the user clicks away' , fakeAsync ( ( ) => {
2496
+ const { trigger, stateCtrl} = fixture . componentInstance ;
2497
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2498
+
2499
+ trigger . openPanel ( ) ;
2500
+ fixture . detectChanges ( ) ;
2501
+ zone . simulateZoneExit ( ) ;
2502
+ fixture . detectChanges ( ) ;
2503
+
2504
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2505
+ expect ( input . value ) . toBeFalsy ( ) ;
2506
+
2507
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2508
+ fixture . detectChanges ( ) ;
2509
+
2510
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2511
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2512
+
2513
+ dispatchFakeEvent ( document , 'click' ) ;
2514
+ fixture . detectChanges ( ) ;
2515
+
2516
+ expect ( stateCtrl . value ) . toEqual ( { code : 'AL' , name : 'Alabama' } ) ;
2517
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2518
+ } ) ) ;
2519
+
2520
+ it ( 'should propagate the auto-selected value if the user tabs away' , fakeAsync ( ( ) => {
2521
+ const { trigger, stateCtrl} = fixture . componentInstance ;
2522
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2523
+
2524
+ trigger . openPanel ( ) ;
2525
+ fixture . detectChanges ( ) ;
2526
+ zone . simulateZoneExit ( ) ;
2527
+ fixture . detectChanges ( ) ;
2528
+
2529
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2530
+ expect ( input . value ) . toBeFalsy ( ) ;
2531
+
2532
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2533
+ fixture . detectChanges ( ) ;
2534
+
2535
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2536
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2537
+
2538
+ dispatchKeyboardEvent ( input , 'keydown' , TAB ) ;
2539
+ fixture . detectChanges ( ) ;
2540
+
2541
+ expect ( stateCtrl . value ) . toEqual ( { code : 'AL' , name : 'Alabama' } ) ;
2542
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2543
+ } ) ) ;
2544
+
2545
+ it ( 'should propagate the auto-selected value if the user presses enter on it' , fakeAsync ( ( ) => {
2546
+ const { trigger, stateCtrl} = fixture . componentInstance ;
2547
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2548
+
2549
+ trigger . openPanel ( ) ;
2550
+ fixture . detectChanges ( ) ;
2551
+ zone . simulateZoneExit ( ) ;
2552
+ fixture . detectChanges ( ) ;
2553
+
2554
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2555
+ expect ( input . value ) . toBeFalsy ( ) ;
2556
+
2557
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2558
+ fixture . detectChanges ( ) ;
2559
+
2560
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2561
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2562
+
2563
+ dispatchKeyboardEvent ( input , 'keydown' , ENTER ) ;
2564
+ fixture . detectChanges ( ) ;
2565
+
2566
+ expect ( stateCtrl . value ) . toEqual ( { code : 'AL' , name : 'Alabama' } ) ;
2567
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2568
+ } ) ) ;
2569
+
2570
+ it ( 'should allow the user to click on an option different from the auto-selected one' ,
2571
+ fakeAsync ( ( ) => {
2572
+ const { trigger, stateCtrl} = fixture . componentInstance ;
2573
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2574
+
2575
+ trigger . openPanel ( ) ;
2576
+ fixture . detectChanges ( ) ;
2577
+ zone . simulateZoneExit ( ) ;
2578
+ fixture . detectChanges ( ) ;
2579
+
2580
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2581
+ expect ( input . value ) . toBeFalsy ( ) ;
2582
+
2583
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2584
+ fixture . detectChanges ( ) ;
2585
+
2586
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2587
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2588
+
2589
+ const options =
2590
+ overlayContainerElement . querySelectorAll ( 'mat-option' ) as NodeListOf < HTMLElement > ;
2591
+ options [ 2 ] . click ( ) ;
2592
+ fixture . detectChanges ( ) ;
2593
+
2594
+ expect ( stateCtrl . value ) . toEqual ( { code : 'FL' , name : 'Florida' } ) ;
2595
+ expect ( input . value ) . toBe ( 'Florida' ) ;
2596
+ } ) ) ;
2597
+
2598
+ } ) ;
2599
+
2394
2600
it ( 'should have correct width when opened' , ( ) => {
2395
2601
const widthFixture = createComponent ( SimpleAutocomplete ) ;
2396
2602
widthFixture . componentInstance . width = 300 ;
0 commit comments