@@ -2,6 +2,7 @@ import { ChangeDetectorRef, Component, DebugElement, inject } from '@angular/cor
22import { ComponentFixture , fakeAsync , flush , TestBed } from '@angular/core/testing' ;
33import { By } from '@angular/platform-browser' ;
44import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
5+ import { createKeyboardEvent , dispatchEvent , ENTER , SPACE } from '@koobiq/components/core' ;
56import {
67 KbqFilter ,
78 KbqFilterBar ,
@@ -413,6 +414,103 @@ describe('KbqPipeAdd', () => {
413414 } ) ) ;
414415 } ) ;
415416
417+ describe ( 'keyboard (Enter/Space)' , ( ) => {
418+ beforeEach ( ( ) => {
419+ fixture = TestBed . createComponent ( TestComponent ) ;
420+ filterBarDebugElement = fixture . debugElement . query ( By . directive ( KbqFilterBar ) ) ;
421+ fixture . detectChanges ( ) ;
422+ } ) ;
423+
424+ const pressEnterOnFirstOption = ( ) => {
425+ const option = document . querySelectorAll ( '.kbq-option' ) [ 0 ] as HTMLElement ;
426+
427+ dispatchEvent ( option , createKeyboardEvent ( 'keydown' , ENTER , undefined , 'Enter' ) ) ;
428+ } ;
429+
430+ const pressSpaceOnFirstOption = ( ) => {
431+ const option = document . querySelectorAll ( '.kbq-option' ) [ 0 ] as HTMLElement ;
432+
433+ dispatchEvent ( option , createKeyboardEvent ( 'keydown' , SPACE , undefined , ' ' ) ) ;
434+ } ;
435+
436+ it ( 'should add a pipe when Enter is pressed on a template option' , fakeAsync ( ( ) => {
437+ const filterBar = getFilterBar ( ) ;
438+ const pipeAdd = getPipeAdd ( ) ;
439+
440+ pipeAdd . select ( ) . open ( ) ;
441+ flush ( ) ;
442+ fixture . detectChanges ( ) ;
443+
444+ pressEnterOnFirstOption ( ) ;
445+ flush ( ) ;
446+ fixture . detectChanges ( ) ;
447+
448+ expect ( filterBar . filter ! . pipes . length ) . toBe ( 1 ) ;
449+ expect ( filterBar . filter ! . pipes [ 0 ] . id ) . toBe ( PIPE_TEMPLATE_ID_1 ) ;
450+ } ) ) ;
451+
452+ it ( 'should add a pipe when Space is pressed on a template option' , fakeAsync ( ( ) => {
453+ const filterBar = getFilterBar ( ) ;
454+ const pipeAdd = getPipeAdd ( ) ;
455+
456+ pipeAdd . select ( ) . open ( ) ;
457+ flush ( ) ;
458+ fixture . detectChanges ( ) ;
459+
460+ pressSpaceOnFirstOption ( ) ;
461+ flush ( ) ;
462+ fixture . detectChanges ( ) ;
463+
464+ expect ( filterBar . filter ! . pipes . length ) . toBe ( 1 ) ;
465+ expect ( filterBar . filter ! . pipes [ 0 ] . id ) . toBe ( PIPE_TEMPLATE_ID_1 ) ;
466+ } ) ) ;
467+
468+ it ( 'should emit onAddPipe when Enter is pressed on a template option' , fakeAsync ( ( ) => {
469+ const pipeAdd = getPipeAdd ( ) ;
470+ const spy = jest . fn ( ) ;
471+
472+ pipeAdd . onAddPipe . subscribe ( spy ) ;
473+
474+ pipeAdd . select ( ) . open ( ) ;
475+ flush ( ) ;
476+ fixture . detectChanges ( ) ;
477+
478+ pressEnterOnFirstOption ( ) ;
479+ flush ( ) ;
480+ fixture . detectChanges ( ) ;
481+
482+ expect ( spy ) . toHaveBeenCalledWith ( expect . objectContaining ( { id : PIPE_TEMPLATE_ID_1 } ) ) ;
483+ } ) ) ;
484+
485+ it ( 'should call filterBar.openPipe.next when Enter is pressed on an already-added option' , fakeAsync ( ( ) => {
486+ const filterBar = getFilterBar ( ) ;
487+ const pipeAdd = getPipeAdd ( ) ;
488+ const openPipeSpy = jest . spyOn ( filterBar . openPipe , 'next' ) ;
489+
490+ // First Enter — add the pipe
491+ pipeAdd . select ( ) . open ( ) ;
492+ flush ( ) ;
493+ fixture . detectChanges ( ) ;
494+
495+ pressEnterOnFirstOption ( ) ;
496+ flush ( ) ;
497+ fixture . detectChanges ( ) ;
498+
499+ // Second Enter — option is now selected, should trigger openPipe
500+ pipeAdd . select ( ) . open ( ) ;
501+ flush ( ) ;
502+ fixture . detectChanges ( ) ;
503+
504+ openPipeSpy . mockClear ( ) ;
505+
506+ pressEnterOnFirstOption ( ) ;
507+ flush ( ) ;
508+ fixture . detectChanges ( ) ;
509+
510+ expect ( openPipeSpy ) . toHaveBeenCalledWith ( PIPE_TEMPLATE_ID_1 ) ;
511+ } ) ) ;
512+ } ) ;
513+
416514 describe ( 'compareWith' , ( ) => {
417515 beforeEach ( ( ) => {
418516 fixture = TestBed . createComponent ( TestComponent ) ;
0 commit comments