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