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