UNPKG

@carbon/ibm-cloud-cognitive-cdai

Version:
50 lines (48 loc) 1.96 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); var _Clipboard = require("./Clipboard.js"); var _sinon = _interopRequireDefault(require("sinon")); // // Copyright IBM Corp. 2020, 2020 // // This source code is licensed under the Apache-2.0 license found in the // LICENSE file in the root directory of this source tree. // jest.unmock('./Clipboard.js'); describe('Clipboard tests', function () { var sandbox = _sinon.default.createSandbox(); beforeEach(function () { sandbox.restore(); }); var testEvent = { currentTarget: { parentNode: document.body } }; it('content is copied to clipboard', function () { var content = 'this is a test value'; var elementContent = null; document.execCommand = _sinon.default.stub().callsFake(function () { elementContent = document.body.textContent; }); (0, _Clipboard.copyToClipboard)(testEvent, content); expect(document.execCommand.calledOnce).toBe(true); expect(elementContent).toEqual(content); }); it('copyContentToClipboard() returns a function which copies the provided content to the clipboard', function () { // setup - define some content and stub out the execCommand function var content = 'some test content'; var elementContent = null; document.execCommand = _sinon.default.stub().callsFake(function () { elementContent = document.body.textContent; }); var copyToClipboardFunc = (0, _Clipboard.copyContentToClipboard)(content); // confirm we get a function back expect((0, _typeof2.default)(copyToClipboardFunc)).toBe('function'); // invoke the function and confirm it calls the stubbed function with the correct content copyToClipboardFunc(testEvent); expect(document.execCommand.calledOnce).toBe(true); expect(elementContent).toEqual(content); }); });