Skip to content

Commit 259f74b

Browse files
Copilotgedinakova
andcommitted
Add null check to clearHighlight() and test for early destruction
Co-authored-by: gedinakova <[email protected]>
1 parent 0f345af commit 259f74b

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

projects/igniteui-angular/directives/src/directives/text-highlight/text-highlight.directive.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,15 @@ describe('IgxHighlight', () => {
308308

309309
expect(() => component.highlight.activateIfNecessary()).not.toThrowError();
310310
});
311+
312+
it('Should not throw error when destroyed before ngAfterViewInit completes', () => {
313+
// Create the component but do NOT call detectChanges()
314+
// This simulates the directive being destroyed before ngAfterViewInit is called
315+
const fix = TestBed.createComponent(HighlightLoremIpsumComponent);
316+
317+
// Destroy the component without initializing it
318+
expect(() => fix.destroy()).not.toThrowError();
319+
});
311320
});
312321

313322
@Component({

projects/igniteui-angular/directives/src/directives/text-highlight/text-highlight.directive.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,10 @@ export class IgxTextHighlightDirective implements AfterViewInit, AfterViewChecke
302302
public clearHighlight(): void {
303303
this.clearChildElements(false);
304304

305-
this._lastSearchInfo.searchText = '';
306-
this._lastSearchInfo.matchCount = 0;
305+
if (this._lastSearchInfo) {
306+
this._lastSearchInfo.searchText = '';
307+
this._lastSearchInfo.matchCount = 0;
308+
}
307309
}
308310

309311
/**

0 commit comments

Comments
 (0)