Skip to content

Commit b3b86ba

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

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
@@ -297,6 +297,17 @@ describe('IgxHighlight', () => {
297297
expect(() => component.highlight.activateIfNecessary()).not.toThrowError();
298298
});
299299

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

302313
@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)