/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Component, ViewChildren } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { HighlightTransformService } from '../services/highlight-transform.service';
import { HighlightDirective } from './highlight.directive';
import { setupTestBed } from '../testing/setup-test-bed';
import { CoreTestingModule } from '../testing/core.testing.module';
import { TranslateModule } from '@ngx-translate/core';
/* spellchecker: disable */
const template: string = `
Lorem ipsum salana-eyong-aysis dolor sit amet
Lorem ipsum salana-eyong-aysis dolor sit amet
consectetur adipiscing elit
sed do eiusmod salana-eyong-aysis tempor incididunt
Lorem ipsum salana-eyong-aysis dolor sit amet
`;
@Component({ selector: 'adf-test-component', template })
class TestComponent {
@ViewChildren(HighlightDirective) public highlightDirectives;
}
describe('HighlightDirective', () => {
let fixture: ComponentFixture;
let component: TestComponent;
setupTestBed({
imports: [
TranslateModule.forRoot(),
CoreTestingModule
],
declarations: [
TestComponent
]
});
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should replace the searched text with the default highlight class in the proper element (adf-highlight-selector)', () => {
component.highlightDirectives.last.highlight('salana-eyong-aysis');
fixture.detectChanges();
const containerElement = fixture.debugElement.query(By.css('#innerDiv21'));
expect(containerElement).not.toBeNull();
expect(containerElement.nativeElement.innerHTML).toBe('Lorem ipsum salana-eyong-aysis dolor sit amet');
});
it('should replace the searched text with the default highlight class in every proper element (highlight-for-free-willy)', () => {
component.highlightDirectives.first.highlight('salana-eyong-aysis');
fixture.detectChanges();
const containerElement1 = fixture.debugElement.query(By.css('#innerDiv11'));
const containerElement2 = fixture.debugElement.query(By.css('#innerDiv14'));
expect(containerElement1).not.toBeNull();
expect(containerElement2).not.toBeNull();
expect(containerElement1.nativeElement.innerHTML).toBe('Lorem ipsum salana-eyong-aysis dolor sit amet');
expect(containerElement2.nativeElement.innerHTML).toBe('sed do eiusmod salana-eyong-aysis tempor incididunt');
});
it('should NOT replace the searched text in an element without the proper selector class', () => {
component.highlightDirectives.first.highlight('salana-eyong-aysis');
fixture.detectChanges();
const containerElement1 = fixture.debugElement.query(By.css('#innerDiv12'));
expect(containerElement1).not.toBeNull();
expect(containerElement1.nativeElement.innerHTML).toBe('Lorem ipsum salana-eyong-aysis dolor sit amet');
});
it('should NOT reinsert the same text to the innerText if there was no change at all (search string is not found)', () => {
const highlighter = TestBed.inject(HighlightTransformService);
spyOn(highlighter, 'highlight').and.returnValue({ changed: false, text: 'Modified text' });
component.highlightDirectives.first.highlight('salana-eyong-aysis');
fixture.detectChanges();
const containerElement = fixture.debugElement.query(By.css('#innerDiv11'));
expect(containerElement).not.toBeNull();
expect(containerElement.nativeElement.innerHTML).not.toContain('Modified text');
});
it('should do the search only if there is a search string presented', () => {
const highlighter = TestBed.inject(HighlightTransformService);
spyOn(highlighter, 'highlight').and.callThrough();
component.highlightDirectives.first.highlight('');
fixture.detectChanges();
expect(highlighter.highlight).not.toHaveBeenCalled();
});
it('should do the search only if there is a node selector string presented', () => {
const highlighter = TestBed.inject(HighlightTransformService);
spyOn(highlighter, 'highlight').and.callThrough();
const callback = function() {
component.highlightDirectives.first.highlight('raddish', '');
fixture.detectChanges();
};
expect(callback).not.toThrowError();
expect(highlighter.highlight).not.toHaveBeenCalled();
});
});