1515 * limitations under the License.
1616 */
1717
18- import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core' ;
18+ import { CUSTOM_ELEMENTS_SCHEMA , DebugElement } from '@angular/core' ;
1919import { ComponentFixture , fakeAsync , flush , TestBed , tick } from '@angular/core/testing' ;
2020import { By } from '@angular/platform-browser' ;
2121import { Node , NodeEntry , NodePaging , RequestScope , ResultSetPaging , SiteEntry , SitePaging , SitePagingList } from '@alfresco/js-api' ;
@@ -30,6 +30,7 @@ import { SearchQueryBuilderService } from '../../search';
3030import { mockSearchRequest } from '../../mock/search-query.mock' ;
3131import { SitesService } from '../../common/services/sites.service' ;
3232import { NodesApiService } from '../../common/services/nodes-api.service' ;
33+ import { UnitTestingUtils } from '../../../../../core/src/lib/testing/unit-testing-utils' ;
3334
3435const fakeResultSetPaging : ResultSetPaging = {
3536 list : {
@@ -60,15 +61,18 @@ describe('ContentNodeSelectorPanelComponent', () => {
6061 const fakeNodeEntry = new Node ( { id : 'fakeId' } ) ;
6162 const nodeEntryEvent = new NodeEntryEvent ( fakeNodeEntry ) ;
6263 let searchQueryBuilderService : SearchQueryBuilderService ;
64+ let testingUtils : UnitTestingUtils ;
6365
64- const typeToSearchBox = ( searchTerm = 'string-to-search' ) => {
65- const searchInput = fixture . debugElement . query ( By . css ( '[data-automation-id="content-node-selector-search-input"]' ) ) ;
66+ const typeToSearchBox = ( searchTerm = 'string-to-search' ) : void => {
67+ const searchInput = testingUtils . getByCSS ( '[data-automation-id="content-node-selector-search-input"]' ) ;
6668 searchInput . nativeElement . value = searchTerm ;
6769 component . searchInput . setValue ( searchTerm ) ;
6870 fixture . detectChanges ( ) ;
6971 } ;
7072
71- const triggerSearchResults = ( searchResults : ResultSetPaging ) => {
73+ const getSearchIcon = ( type : string ) : DebugElement => testingUtils . getByCSS ( `[data-automation-id="content-node-selector-search-${ type } "]` ) ;
74+
75+ const triggerSearchResults = ( searchResults : ResultSetPaging ) : void => {
7276 const service = fixture . debugElement . injector . get ( SearchQueryBuilderService ) ;
7377 service . executed . next ( searchResults ) ;
7478 } ;
@@ -92,6 +96,8 @@ describe('ContentNodeSelectorPanelComponent', () => {
9296 searchQueryBuilderService = fixture . debugElement . injector . get ( SearchQueryBuilderService ) ;
9397 searchQueryBuilderService . resetToDefaults ( ) ;
9498
99+ testingUtils = new UnitTestingUtils ( fixture . debugElement ) ;
100+
95101 spyOn ( nodeService , 'getNode' ) . and . returnValue (
96102 of (
97103 new Node ( {
@@ -196,7 +202,7 @@ describe('ContentNodeSelectorPanelComponent', () => {
196202 spyOn ( component , 'clearSearch' ) ;
197203
198204 fixture . detectChanges ( ) ;
199- const clearIcon = fixture . debugElement . query ( By . css ( '[data-automation-id="content-node-selector-search- clear"]' ) ) ;
205+ const clearIcon = getSearchIcon ( ' clear' ) ;
200206 clearIcon . nativeElement . click ( ) ;
201207
202208 fixture . detectChanges ( ) ;
@@ -357,8 +363,8 @@ describe('ContentNodeSelectorPanelComponent', () => {
357363 fixture . detectChanges ( ) ;
358364 tick ( debounceSearch ) ;
359365
360- const searchIcon = fixture . debugElement . query ( By . css ( '[data-automation-id="content-node-selector-search- icon"]' ) ) ;
361- const clearIcon = fixture . debugElement . query ( By . css ( '[data-automation-id="content-node-selector-search- clear"]' ) ) ;
366+ const searchIcon = getSearchIcon ( ' icon' ) ;
367+ const clearIcon = getSearchIcon ( ' clear' ) ;
362368
363369 expect ( searchIcon ) . not . toBeNull ( ) ;
364370 expect ( clearIcon ) . toBeNull ( ) ;
@@ -371,13 +377,26 @@ describe('ContentNodeSelectorPanelComponent', () => {
371377
372378 fixture . detectChanges ( ) ;
373379
374- const searchIcon = fixture . debugElement . query ( By . css ( '[data-automation-id="content-node-selector-search- icon"]' ) ) ;
375- const clearIcon = fixture . debugElement . query ( By . css ( '[data-automation-id="content-node-selector-search- clear"]' ) ) ;
380+ const searchIcon = getSearchIcon ( ' icon' ) ;
381+ const clearIcon = getSearchIcon ( ' clear' ) ;
376382
377383 expect ( searchIcon ) . toBeNull ( ) ;
378384 expect ( clearIcon ) . not . toBeNull ( ) ;
379385 } ) ) ;
380386
387+ it ( 'should call clear method on the X (clear) button click' , fakeAsync ( ( ) => {
388+ fixture . detectChanges ( ) ;
389+ typeToSearchBox ( '123' ) ;
390+ tick ( debounceSearch ) ;
391+
392+ fixture . detectChanges ( ) ;
393+
394+ spyOn ( component , 'clear' ) ;
395+ const clearButton = getSearchIcon ( 'clear' ) ;
396+ clearButton . nativeElement . click ( ) ;
397+ expect ( component . clear ) . toHaveBeenCalled ( ) ;
398+ } ) ) ;
399+
381400 it ( 'should clear the search field, nodes and chosenNode when clicking on the X (clear) icon' , async ( ) => {
382401 component . chosenNode = [ entry ] ;
383402
@@ -553,7 +572,7 @@ describe('ContentNodeSelectorPanelComponent', () => {
553572 } ) ) ;
554573
555574 it ( 'should show the current folder content instead of search results if search was not performed' , async ( ) => {
556- const documentList = fixture . debugElement . query ( By . directive ( DocumentListComponent ) ) ;
575+ const documentList = testingUtils . getByDirective ( DocumentListComponent ) ;
557576 expect ( documentList ) . not . toBeNull ( ) ;
558577 expect ( documentList . componentInstance . currentFolderId ) . toBe ( 'cat-girl-nuku-nuku' ) ;
559578 } ) ;
@@ -565,7 +584,7 @@ describe('ContentNodeSelectorPanelComponent', () => {
565584
566585 fixture . detectChanges ( ) ;
567586
568- const documentList = fixture . debugElement . query ( By . directive ( DocumentListComponent ) ) ;
587+ const documentList = testingUtils . getByDirective ( DocumentListComponent ) ;
569588 expect ( documentList ) . not . toBeNull ( ) ;
570589 expect (
571590 documentList . componentInstance . rowFilter ( {
@@ -593,7 +612,7 @@ describe('ContentNodeSelectorPanelComponent', () => {
593612
594613 fixture . detectChanges ( ) ;
595614
596- const documentList = fixture . debugElement . query ( By . directive ( DocumentListComponent ) ) ;
615+ const documentList = testingUtils . getByDirective ( DocumentListComponent ) ;
597616 expect ( documentList ) . not . toBeNull ( ) ;
598617 expect ( documentList . componentInstance . rowFilter ) . toBeTruthy ( ) ;
599618
@@ -607,7 +626,7 @@ describe('ContentNodeSelectorPanelComponent', () => {
607626
608627 fixture . detectChanges ( ) ;
609628
610- const documentList = fixture . debugElement . query ( By . directive ( DocumentListComponent ) ) ;
629+ const documentList = testingUtils . getByDirective ( DocumentListComponent ) ;
611630 expect ( documentList ) . not . toBeNull ( ) ;
612631 expect ( documentList . componentInstance . imageResolver ) . toBe ( resolver ) ;
613632 } ) ;
@@ -619,7 +638,7 @@ describe('ContentNodeSelectorPanelComponent', () => {
619638 triggerSearchResults ( fakeResultSetPaging ) ;
620639
621640 fixture . detectChanges ( ) ;
622- const documentList = fixture . debugElement . query ( By . css ( '[data-automation-id="content-node-selector-document-list"]' ) ) ;
641+ const documentList = testingUtils . getByCSS ( '[data-automation-id="content-node-selector-document-list"]' ) ;
623642 expect ( documentList ) . not . toBeNull ( ) ;
624643 expect ( component . hasValidQuery ) . toEqual ( true ) ;
625644 expect ( documentList . componentInstance . currentFolderId ) . toBeNull ( ) ;
@@ -664,12 +683,12 @@ describe('ContentNodeSelectorPanelComponent', () => {
664683 fixture . detectChanges ( ) ;
665684
666685 fixture . whenStable ( ) . then ( ( ) => {
667- const clearButton = fixture . debugElement . query ( By . css ( '[data-automation-id="content-node-selector-search- clear"]' ) ) ;
686+ const clearButton = getSearchIcon ( ' clear' ) ;
668687 expect ( clearButton ) . not . toBeNull ( ) ;
669688 clearButton . triggerEventHandler ( 'click' , { } ) ;
670689 fixture . detectChanges ( ) ;
671690
672- const documentList = fixture . debugElement . query ( By . css ( '[data-automation-id="content-node-selector-document-list"]' ) ) ;
691+ const documentList = testingUtils . getByCSS ( '[data-automation-id="content-node-selector-document-list"]' ) ;
673692 expect ( documentList ) . not . toBeNull ( ) ;
674693 expect ( documentList . componentInstance . currentFolderId ) . toBe ( 'cat-girl-nuku-nuku' ) ;
675694 done ( ) ;
@@ -696,13 +715,13 @@ describe('ContentNodeSelectorPanelComponent', () => {
696715 component . siteChanged ( { entry : { guid : 'Kame-Sennin Muten Roshi' } } as SiteEntry ) ;
697716 fixture . detectChanges ( ) ;
698717
699- let documentList = fixture . debugElement . query ( By . css ( '[data-automation-id="content-node-selector-document-list"]' ) ) ;
718+ let documentList = testingUtils . getByCSS ( '[data-automation-id="content-node-selector-document-list"]' ) ;
700719 expect ( documentList . componentInstance . currentFolderId ) . toBe ( 'Kame-Sennin Muten Roshi' ) ;
701720
702721 component . siteChanged ( { entry : { guid : undefined } } as SiteEntry ) ;
703722 fixture . detectChanges ( ) ;
704723
705- documentList = fixture . debugElement . query ( By . css ( '[data-automation-id="content-node-selector-document-list"]' ) ) ;
724+ documentList = testingUtils . getByCSS ( '[data-automation-id="content-node-selector-document-list"]' ) ;
706725 expect ( documentList . componentInstance . currentFolderId ) . toBe ( 'cat-girl-nuku-nuku' ) ;
707726
708727 done ( ) ;
@@ -711,7 +730,7 @@ describe('ContentNodeSelectorPanelComponent', () => {
711730 describe ( 'Pagination "Load more" button' , ( ) => {
712731 it ( 'should NOT be shown by default' , ( ) => {
713732 fixture . detectChanges ( ) ;
714- const pagination = fixture . debugElement . query ( By . css ( '[data-automation-id="adf-infinite-pagination-button"]' ) ) ;
733+ const pagination = testingUtils . getByCSS ( '[data-automation-id="adf-infinite-pagination-button"]' ) ;
715734 expect ( pagination ) . toBeNull ( ) ;
716735 } ) ;
717736
0 commit comments