Skip to content

Commit f72d834

Browse files
authored
fix(Highlight): Check if the highlight group exists before activation. (#15762)
* test(Highlight): Adding a test for activating non-existent group. * fix(Highlight): Adding a check for undefined before activation.
1 parent a801fbe commit f72d834

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,17 @@ describe('IgxHighlight', () => {
299299
expect(() => component.highlight.activateIfNecessary()).not.toThrowError();
300300
});
301301

302+
it('Should not throw when attempting to activate a non-existing group.', () => {
303+
const fix = TestBed.createComponent(HighlightLoremIpsumComponent);
304+
fix.detectChanges();
305+
306+
const component: HighlightLoremIpsumComponent = fix.debugElement.componentInstance;
307+
component.highlightText('a');
308+
component.groupName = 'test1';
309+
fix.detectChanges();
310+
311+
expect(() => component.highlight.activateIfNecessary()).not.toThrowError();
312+
});
302313
});
303314

304315
@Component({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ export class IgxTextHighlightDirective implements AfterViewInit, AfterViewChecke
318318
public activateIfNecessary(): void {
319319
const group = this.service.highlightGroupsMap.get(this.groupName);
320320

321-
if (group.index >= 0 && group.column === this.column && group.row === this.row && compareMaps(this.metadata, group.metadata)) {
321+
if (group && group.index >= 0 && group.column === this.column && group.row === this.row && compareMaps(this.metadata, group.metadata)) {
322322
this.activate(group.index);
323323
}
324324
}

0 commit comments

Comments
 (0)