import { ClipboardCopyDirective } from './clipboard-copy.directive'; import * as Clipboard from 'clipboard'; import { Mock } from 'ts-mocks'; import Spy = jasmine.Spy; describe("ClipboardCopyDirective", () => { var directive: ClipboardCopyDirective; var $elementMock: any; var $timeoutMock: any; var $documentMock: any; var clipboardFactory: any; var clipboardMock: Mock; beforeEach(() => { $elementMock = new Mock(); $timeoutMock = jasmine.createSpy('$timeoutSpy').and.callFake((fn: () => void, delay) => fn()); $documentMock = new Mock(); clipboardMock = new Mock(); clipboardMock.setup(mock => mock.on).is((eventName: string, callback: (event) => void) => {}); clipboardFactory = jasmine.createSpy('clipboardFactory').and.returnValue(clipboardMock.Object); directive = new ClipboardCopyDirective([$elementMock.Object], $timeoutMock, [$documentMock.Object], clipboardFactory); directive.copyTargetSelector = "#copy-input-box-0"; }); describe("ngAfterContentInit", () => { it("initializes new Clipboard instance", () => { const target = new Mock(); $documentMock.setup(mock => mock.querySelector).is(selector => target.Object); directive.ngAfterContentInit(); expect(clipboardFactory).toHaveBeenCalled(); expect((clipboardFactory.calls.argsFor(0)[0])).toEqual($elementMock.Object); expect((clipboardFactory.calls.argsFor(0)[1]['target']())).toEqual(target.Object); }); it("sets error callback for Clipboard instance", () => { directive.ngAfterContentInit(); expect((clipboardMock.Object.on.calls.argsFor(0)[0])).toEqual('error'); expect((clipboardMock.Object.on.calls.argsFor(0)[1])).toBeDefined(); }); it("sets success callback for Clipboard instance", (done) => { directive.ngAfterContentInit(); expect((clipboardMock.Object.on.calls.argsFor(1)[0])).toEqual('success'); expect((clipboardMock.Object.on.calls.argsFor(1)[1])).toBeDefined(); done(); }); }); describe("ngOnDestroy", () => { beforeEach(() => { clipboardMock.setup(mock => mock.destroy).is(() => null); }); it("calls method to destroy Clipboard instance if set", (done) => { directive.ngAfterContentInit(); directive.ngOnDestroy(); expect((clipboardMock.Object.destroy)).toHaveBeenCalled(); done(); }); it("does not call method to destroy Clipboard instance if not set", () => { directive.ngOnDestroy(); expect((clipboardMock.Object.destroy)).not.toHaveBeenCalled(); }); }); });