import {TestBed, ComponentFixture} from '@angular/core/testing'; import {createGenericTestComponent} from '../test/common'; import {Component} from '@angular/core'; import {NgbHighlight} from './highlight'; import {NgbTypeaheadModule} from './typeahead.module'; const createTestComponent = (html: string) => createGenericTestComponent(html, TestComponent) as ComponentFixture; /** * Get generated innerHtml without HTML comments and Angular debug attributes */ function highlightHtml(fixture) { const elms = fixture.nativeElement.children[0].childNodes; let elm; let result = ''; let nodeName; for (let i = 0; i < elms.length; i++) { elm = elms[i]; if (elm.nodeType === Node.ELEMENT_NODE) { nodeName = elm.nodeName.toLowerCase(); result += `<${nodeName} class="${elm.className}">${elm.textContent}`; } else if (elm.nodeType === Node.TEXT_NODE) { result += elm.wholeText; } } return result; } describe('ngb-highlight', () => { beforeEach(() => { TestBed.overrideModule(NgbTypeaheadModule, {set: {exports: [NgbHighlight]}}); TestBed.configureTestingModule({declarations: [TestComponent], imports: [NgbTypeaheadModule.forRoot()]}); }); it('should render highlighted text when there is one match', () => { const fixture = createTestComponent(''); expect(highlightHtml(fixture)).toBe('foo bar baz'); }); it('should render highlighted text when there are multiple matches', () => { const fixture = createTestComponent(''); expect(highlightHtml(fixture)) .toBe('foo bar baz bar foo'); }); it('should render highlighted text when there is a match at the beginning', () => { const fixture = createTestComponent(''); expect(highlightHtml(fixture)).toBe('bar baz'); }); it('should render highlighted text when there is a match at the end', () => { const fixture = createTestComponent(''); expect(highlightHtml(fixture)).toBe('bar baz'); }); it('should render highlighted text when there is a case-insensitive match', () => { const fixture = createTestComponent(''); expect(highlightHtml(fixture)).toBe('foo bAR baz'); }); it('should render highlighted text with special characters', () => { const fixture = createTestComponent(''); expect(highlightHtml(fixture)).toBe('foo (bAR baz'); }); it('should render highlighted text for stringified non-string args', () => { const fixture = createTestComponent(''); fixture.detectChanges(); expect(highlightHtml(fixture)).toBe('123'); }); it('should behave reasonably for blank result', () => { const fixture = createTestComponent(''); expect(highlightHtml(fixture)).toBe(''); }); it('should not convert null result to string', () => { const fixture = createTestComponent(''); expect(highlightHtml(fixture)).toBe(''); }); it('should properly detect matches in 0 result', () => { const fixture = createTestComponent(''); expect(highlightHtml(fixture)).toBe(`0`); }); it('should not higlight anything for blank term', () => { const fixture = createTestComponent(''); expect(highlightHtml(fixture)).toBe('1null23'); }); it('should not higlight anything for blank term', () => { const fixture = createTestComponent(``); expect(highlightHtml(fixture)).toBe('123'); }); it('should properly higlight zeros', () => { const fixture = createTestComponent(``); expect(highlightHtml(fixture)).toBe('0123'); }); it('should support custom highlight class', () => { const fixture = createTestComponent(''); expect(highlightHtml(fixture)).toBe('123'); }); }); @Component({selector: 'test-cmp', template: ''}) class TestComponent { }