@@ -2540,6 +2540,212 @@ expect(scrollContainer.scrollTop)
2540
2540
2541
2541
} ) ;
2542
2542
2543
+ describe ( 'automatically selecting the active option' , ( ) => {
2544
+ let fixture : ComponentFixture < SimpleAutocomplete > ;
2545
+
2546
+ beforeEach ( ( ) => {
2547
+ fixture = createComponent ( SimpleAutocomplete ) ;
2548
+ fixture . detectChanges ( ) ;
2549
+ fixture . componentInstance . trigger . autocomplete . autoSelectActiveOption = true ;
2550
+ } ) ;
2551
+
2552
+ it ( 'should update the input value as the user is navigating, without changing the model ' +
2553
+ 'value or closing the panel' , fakeAsync ( ( ) => {
2554
+ const { trigger, stateCtrl, closedSpy} = fixture . componentInstance ;
2555
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2556
+
2557
+ trigger . openPanel ( ) ;
2558
+ fixture . detectChanges ( ) ;
2559
+ zone . simulateZoneExit ( ) ;
2560
+ fixture . detectChanges ( ) ;
2561
+
2562
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2563
+ expect ( input . value ) . toBeFalsy ( ) ;
2564
+ expect ( trigger . panelOpen ) . toBe ( true ) ;
2565
+ expect ( closedSpy ) . not . toHaveBeenCalled ( ) ;
2566
+
2567
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2568
+ fixture . detectChanges ( ) ;
2569
+
2570
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2571
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2572
+ expect ( trigger . panelOpen ) . toBe ( true ) ;
2573
+ expect ( closedSpy ) . not . toHaveBeenCalled ( ) ;
2574
+
2575
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2576
+ fixture . detectChanges ( ) ;
2577
+
2578
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2579
+ expect ( input . value ) . toBe ( 'California' ) ;
2580
+ expect ( trigger . panelOpen ) . toBe ( true ) ;
2581
+ expect ( closedSpy ) . not . toHaveBeenCalled ( ) ;
2582
+ } ) ) ;
2583
+
2584
+ it ( 'should revert back to the last typed value if the user presses escape' , fakeAsync ( ( ) => {
2585
+ const { trigger, stateCtrl, closedSpy} = fixture . componentInstance ;
2586
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2587
+
2588
+ trigger . openPanel ( ) ;
2589
+ fixture . detectChanges ( ) ;
2590
+ zone . simulateZoneExit ( ) ;
2591
+ fixture . detectChanges ( ) ;
2592
+ typeInElement ( input , 'al' ) ;
2593
+ fixture . detectChanges ( ) ;
2594
+ tick ( ) ;
2595
+
2596
+ expect ( stateCtrl . value ) . toBe ( 'al' ) ;
2597
+ expect ( input . value ) . toBe ( 'al' ) ;
2598
+ expect ( trigger . panelOpen ) . toBe ( true ) ;
2599
+ expect ( closedSpy ) . not . toHaveBeenCalled ( ) ;
2600
+
2601
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2602
+ fixture . detectChanges ( ) ;
2603
+
2604
+ expect ( stateCtrl . value ) . toBe ( 'al' ) ;
2605
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2606
+ expect ( trigger . panelOpen ) . toBe ( true ) ;
2607
+ expect ( closedSpy ) . not . toHaveBeenCalled ( ) ;
2608
+
2609
+ dispatchKeyboardEvent ( document . body , 'keydown' , ESCAPE ) ;
2610
+ fixture . detectChanges ( ) ;
2611
+
2612
+ expect ( stateCtrl . value ) . toBe ( 'al' ) ;
2613
+ expect ( input . value ) . toBe ( 'al' ) ;
2614
+ expect ( trigger . panelOpen ) . toBe ( false ) ;
2615
+ expect ( closedSpy ) . toHaveBeenCalledTimes ( 1 ) ;
2616
+ } ) ) ;
2617
+
2618
+ it ( 'should clear the input if the user presses escape while there was a pending ' +
2619
+ 'auto selection and there is no previous value' , fakeAsync ( ( ) => {
2620
+ const { trigger, stateCtrl} = fixture . componentInstance ;
2621
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2622
+
2623
+ trigger . openPanel ( ) ;
2624
+ fixture . detectChanges ( ) ;
2625
+ zone . simulateZoneExit ( ) ;
2626
+ fixture . detectChanges ( ) ;
2627
+
2628
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2629
+ expect ( input . value ) . toBeFalsy ( ) ;
2630
+
2631
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2632
+ fixture . detectChanges ( ) ;
2633
+
2634
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2635
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2636
+
2637
+ dispatchKeyboardEvent ( document . body , 'keydown' , ESCAPE ) ;
2638
+ fixture . detectChanges ( ) ;
2639
+
2640
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2641
+ expect ( input . value ) . toBeFalsy ( ) ;
2642
+ } ) ) ;
2643
+
2644
+ it ( 'should propagate the auto-selected value if the user clicks away' , fakeAsync ( ( ) => {
2645
+ const { trigger, stateCtrl} = fixture . componentInstance ;
2646
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2647
+
2648
+ trigger . openPanel ( ) ;
2649
+ fixture . detectChanges ( ) ;
2650
+ zone . simulateZoneExit ( ) ;
2651
+ fixture . detectChanges ( ) ;
2652
+
2653
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2654
+ expect ( input . value ) . toBeFalsy ( ) ;
2655
+
2656
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2657
+ fixture . detectChanges ( ) ;
2658
+
2659
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2660
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2661
+
2662
+ dispatchFakeEvent ( document , 'click' ) ;
2663
+ fixture . detectChanges ( ) ;
2664
+
2665
+ expect ( stateCtrl . value ) . toEqual ( { code : 'AL' , name : 'Alabama' } ) ;
2666
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2667
+ } ) ) ;
2668
+
2669
+ it ( 'should propagate the auto-selected value if the user tabs away' , fakeAsync ( ( ) => {
2670
+ const { trigger, stateCtrl} = fixture . componentInstance ;
2671
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2672
+
2673
+ trigger . openPanel ( ) ;
2674
+ fixture . detectChanges ( ) ;
2675
+ zone . simulateZoneExit ( ) ;
2676
+ fixture . detectChanges ( ) ;
2677
+
2678
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2679
+ expect ( input . value ) . toBeFalsy ( ) ;
2680
+
2681
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2682
+ fixture . detectChanges ( ) ;
2683
+
2684
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2685
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2686
+
2687
+ dispatchKeyboardEvent ( input , 'keydown' , TAB ) ;
2688
+ fixture . detectChanges ( ) ;
2689
+
2690
+ expect ( stateCtrl . value ) . toEqual ( { code : 'AL' , name : 'Alabama' } ) ;
2691
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2692
+ } ) ) ;
2693
+
2694
+ it ( 'should propagate the auto-selected value if the user presses enter on it' , fakeAsync ( ( ) => {
2695
+ const { trigger, stateCtrl} = fixture . componentInstance ;
2696
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2697
+
2698
+ trigger . openPanel ( ) ;
2699
+ fixture . detectChanges ( ) ;
2700
+ zone . simulateZoneExit ( ) ;
2701
+ fixture . detectChanges ( ) ;
2702
+
2703
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2704
+ expect ( input . value ) . toBeFalsy ( ) ;
2705
+
2706
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2707
+ fixture . detectChanges ( ) ;
2708
+
2709
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2710
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2711
+
2712
+ dispatchKeyboardEvent ( input , 'keydown' , ENTER ) ;
2713
+ fixture . detectChanges ( ) ;
2714
+
2715
+ expect ( stateCtrl . value ) . toEqual ( { code : 'AL' , name : 'Alabama' } ) ;
2716
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2717
+ } ) ) ;
2718
+
2719
+ it ( 'should allow the user to click on an option different from the auto-selected one' ,
2720
+ fakeAsync ( ( ) => {
2721
+ const { trigger, stateCtrl} = fixture . componentInstance ;
2722
+ const input : HTMLInputElement = fixture . nativeElement . querySelector ( 'input' ) ;
2723
+
2724
+ trigger . openPanel ( ) ;
2725
+ fixture . detectChanges ( ) ;
2726
+ zone . simulateZoneExit ( ) ;
2727
+ fixture . detectChanges ( ) ;
2728
+
2729
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2730
+ expect ( input . value ) . toBeFalsy ( ) ;
2731
+
2732
+ dispatchKeyboardEvent ( input , 'keydown' , DOWN_ARROW ) ;
2733
+ fixture . detectChanges ( ) ;
2734
+
2735
+ expect ( stateCtrl . value ) . toBeFalsy ( ) ;
2736
+ expect ( input . value ) . toBe ( 'Alabama' ) ;
2737
+
2738
+ const options =
2739
+ overlayContainerElement . querySelectorAll ( 'mat-option' ) as NodeListOf < HTMLElement > ;
2740
+ options [ 2 ] . click ( ) ;
2741
+ fixture . detectChanges ( ) ;
2742
+
2743
+ expect ( stateCtrl . value ) . toEqual ( { code : 'FL' , name : 'Florida' } ) ;
2744
+ expect ( input . value ) . toBe ( 'Florida' ) ;
2745
+ } ) ) ;
2746
+
2747
+ } ) ;
2748
+
2543
2749
it ( 'should have correct width when opened' , ( ) => {
2544
2750
const widthFixture = createComponent ( SimpleAutocomplete ) ;
2545
2751
widthFixture . componentInstance . width = 300 ;
0 commit comments