/*!
* @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, ViewChild } from '@angular/core';
import { ComponentFixture, TestBed, tick, fakeAsync } from '@angular/core/testing';
import { setupTestBed } from '../testing/setup-test-bed';
import { ClipboardService } from './clipboard.service';
import { ClipboardDirective } from './clipboard.directive';
import { CoreTestingModule } from '../testing/core.testing.module';
import { TranslateModule } from '@ngx-translate/core';
@Component({
selector: 'adf-test-component',
template: `
`
})
class TestTargetClipboardComponent {}
describe('ClipboardDirective', () => {
let fixture: ComponentFixture;
let clipboardService: ClipboardService;
setupTestBed({
imports: [
TranslateModule.forRoot(),
CoreTestingModule
],
declarations: [
TestTargetClipboardComponent
]
});
beforeEach(() => {
fixture = TestBed.createComponent(TestTargetClipboardComponent);
clipboardService = TestBed.inject(ClipboardService);
fixture.detectChanges();
});
it('should notify copy target value on button click event', () => {
spyOn(clipboardService, 'copyToClipboard');
fixture.nativeElement.querySelector('input').value = 'some value';
fixture.nativeElement.querySelector('button').dispatchEvent(new MouseEvent('click'));
expect(clipboardService.copyToClipboard).toHaveBeenCalled();
});
});
describe('CopyClipboardDirective', () => {
@Component({
selector: 'adf-copy-conent-test-component',
template: `{{ mockText }}`
})
class TestCopyClipboardComponent {
mockText = 'text to copy';
placeholder = 'copy text';
@ViewChild(ClipboardDirective)
clipboardDirective: ClipboardDirective;
}
let fixture: ComponentFixture;
let element: HTMLElement;
setupTestBed({
imports: [
TranslateModule.forRoot(),
CoreTestingModule
],
declarations: [
TestCopyClipboardComponent
]
});
beforeEach(() => {
fixture = TestBed.createComponent(TestCopyClipboardComponent);
element = fixture.debugElement.nativeElement;
fixture.detectChanges();
});
it('should show tooltip when hover element', (() => {
const spanHTMLElement: HTMLInputElement = element.querySelector('span');
spanHTMLElement.dispatchEvent(new Event('mouseenter'));
fixture.detectChanges();
expect(fixture.debugElement.nativeElement.querySelector('.adf-copy-tooltip')).not.toBeNull();
}));
it('should not show tooltip when element it is not hovered', (() => {
const spanHTMLElement: HTMLInputElement = element.querySelector('span');
spanHTMLElement.dispatchEvent(new Event('mouseenter'));
fixture.detectChanges();
expect(fixture.debugElement.nativeElement.querySelector('.adf-copy-tooltip')).not.toBeNull();
spanHTMLElement.dispatchEvent(new Event('mouseleave'));
fixture.detectChanges();
expect(fixture.debugElement.nativeElement.querySelector('.adf-copy-tooltip')).toBeNull();
}));
it('should copy the content of element when click it', fakeAsync(() => {
const spanHTMLElement: HTMLInputElement = element.querySelector('span');
fixture.detectChanges();
spyOn(document, 'execCommand');
spanHTMLElement.dispatchEvent(new Event('click'));
tick();
fixture.detectChanges();
expect(document.execCommand).toHaveBeenCalledWith('copy');
}));
it('should not copy the content of element when click it', fakeAsync(() => {
const spanHTMLElement: HTMLInputElement = element.querySelector('span');
fixture.detectChanges();
spyOn(document, 'execCommand');
spanHTMLElement.dispatchEvent(new Event('mouseleave'));
tick();
fixture.detectChanges();
expect(document.execCommand).not.toHaveBeenCalled();
}));
});