Skip to content

Commit 186b470

Browse files
gedinakovaCopilot
andauthored
fix(*): Add null check to clearHighlight() and test for early destruction (#16672)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent bb39d94 commit 186b470

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

projects/igniteui-angular/src/lib/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/src/lib/directives/text-highlight/text-highlight.directive.ts

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

311-
this._lastSearchInfo.searchText = '';
312-
this._lastSearchInfo.matchCount = 0;
311+
if (this._lastSearchInfo) {
312+
this._lastSearchInfo.searchText = '';
313+
this._lastSearchInfo.matchCount = 0;
314+
}
313315
}
314316

315317
/**

0 commit comments

Comments
 (0)