11import { HttpClientModule } from '@angular/common/http' ;
22import { HttpClientTestingModule } from '@angular/common/http/testing' ;
3- import { ComponentFixture , TestBed } from '@angular/core/testing' ;
3+ import { ComponentFixture , TestBed , fakeAsync , tick } from '@angular/core/testing' ;
44import { RouterTestingModule } from '@angular/router/testing' ;
55import { MatrixComponent , MatrixRow } from './matrix.component' ;
6- import { MatChip } from '@angular/material/chips' ;
6+ import { MatChipSelectionChange } from '@angular/material/chips' ;
77import { ModalMessageComponent } from '../../component/modal-message/modal-message.component' ;
88import { MatDialogModule , MatDialogRef } from '@angular/material/dialog' ;
99import { LoaderService } from 'src/app/service/loader/data-loader.service' ;
@@ -25,14 +25,22 @@ const MOCK_DATA: any = {
2525} ;
2626let mockLoaderService : MockLoaderService ;
2727
28+ function createChipSelectionEvent ( value : string , selected : boolean ) : MatChipSelectionChange {
29+ return {
30+ source : { value } as any ,
31+ selected,
32+ isUserInput : true ,
33+ } ;
34+ }
35+
2836describe ( 'MatrixComponent' , ( ) => {
2937 let component : MatrixComponent ;
3038 let fixture : ComponentFixture < MatrixComponent > ;
3139
3240 beforeEach ( async ( ) => {
3341 mockLoaderService = new MockLoaderService ( MOCK_DATA ) ;
3442 await TestBed . configureTestingModule ( {
35- declarations : [ MatrixComponent , MatChip ] ,
43+ declarations : [ MatrixComponent ] ,
3644 imports : [ RouterTestingModule , HttpClientModule , MatDialogModule ] ,
3745 providers : [
3846 HttpClientTestingModule ,
@@ -68,36 +76,24 @@ describe('MatrixComponent', () => {
6876 expect ( Object . keys ( component . filtersDim ) ) . toContain ( 'Test Dimension' ) ;
6977 } ) ;
7078
71- it ( 'should filter data when tag filter is selected' , ( ) => {
79+ it ( 'should filter data when tag filter is selected' , fakeAsync ( ( ) => {
7280 expect ( component . dataSource . data . length ) . toBe ( 2 ) ;
7381 expect ( component . dataSource . data [ 0 ] . level1 . length ) . toBe ( 2 ) ;
7482
75- // Create a mock MatChip with proper state tracking
76- const mockChip = {
77- value : 'tag1' ,
78- selected : false ,
79- toggleSelected : function ( ) {
80- this . selected = ! this . selected ;
81- } ,
82- } as MatChip ;
83-
84- // Ensure initial state
85- mockChip . selected = false ;
86-
8783 // Toggle tag filter on
8884 console . log ( 'Turn chip filter on' ) ;
89- component . toggleTagFilters ( mockChip ) ;
90- // console.log('data after "on":', component.dataSource.data);
85+ component . toggleTagFilters ( createChipSelectionEvent ( 'tag1' , true ) ) ;
86+ tick ( ) ; // flush the setTimeout inside toggleTagFilters
9187 expect ( component . filtersTag [ 'tag1' ] ) . toBeTrue ( ) ;
9288 expect ( component . dataSource . data . length ) . toBe ( 1 ) ;
9389 expect ( component . dataSource . data [ 0 ] . level1 . length ) . toBe ( 1 ) ;
9490
9591 // Toggle tag filter off again
9692 console . log ( 'Turn chip filter off' ) ;
97- component . toggleTagFilters ( mockChip ) ;
98- // console.log('data after "off": ', component.dataSource.data);
93+ component . toggleTagFilters ( createChipSelectionEvent ( 'tag1' , false ) ) ;
94+ tick ( ) ; // flush the setTimeout inside toggleTagFilters
9995 expect ( component . filtersTag [ 'tag1' ] ) . toBeFalse ( ) ;
10096 expect ( component . dataSource . data . length ) . toBe ( 2 ) ;
10197 expect ( component . dataSource . data [ 0 ] . level1 . length ) . toBe ( 2 ) ;
102- } ) ;
98+ } ) ) ;
10399} ) ;
0 commit comments