1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
import { TestBed } from '@angular/core/testing';
import * as BrowserDetect from 'detect-browser';
import { ToastrService } from 'ngx-toastr';
import { configureTestBed } from '~/testing/unit-test-helper';
import { Copy2ClipboardButtonComponent } from './copy2clipboard-button.component';
describe('Copy2ClipboardButtonComponent', () => {
let component: Copy2ClipboardButtonComponent;
configureTestBed({
providers: [
{
provide: ToastrService,
useValue: {
error: () => true,
success: () => true
}
}
]
});
it('should create an instance', () => {
component = new Copy2ClipboardButtonComponent(null);
expect(component).toBeTruthy();
});
describe('test onClick behaviours', () => {
let toastrService: ToastrService;
let queryFn: jasmine.Spy;
let writeTextFn: jasmine.Spy;
beforeEach(() => {
toastrService = TestBed.inject(ToastrService);
component = new Copy2ClipboardButtonComponent(toastrService);
spyOn<any>(component, 'getText').and.returnValue('foo');
Object.assign(navigator, {
permissions: { query: jest.fn() },
clipboard: {
writeText: jest.fn()
}
});
queryFn = spyOn(navigator.permissions, 'query');
});
it('should not call permissions API', () => {
spyOn(BrowserDetect, 'detect').and.returnValue({ name: 'firefox' });
writeTextFn = spyOn(navigator.clipboard, 'writeText').and.returnValue(
new Promise<void>((resolve, _) => {
resolve();
})
);
component.onClick();
expect(queryFn).not.toHaveBeenCalled();
expect(writeTextFn).toHaveBeenCalledWith('foo');
});
it('should call permissions API', () => {
spyOn(BrowserDetect, 'detect').and.returnValue({ name: 'chrome' });
component.onClick();
expect(queryFn).toHaveBeenCalled();
});
});
});
|