@carbon/ibm-cloud-cognitive-cdai
Version:
Carbon for Cloud & Cognitive CD&AI UI components
47 lines (46 loc) • 1.76 kB
JavaScript
import _typeof from "@babel/runtime/helpers/typeof";
//
// 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');
import { copyToClipboard, copyContentToClipboard } from './Clipboard.js';
import sinon from 'sinon';
describe('Clipboard tests', function () {
var sandbox = sinon.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.stub().callsFake(function () {
elementContent = document.body.textContent;
});
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.stub().callsFake(function () {
elementContent = document.body.textContent;
});
var copyToClipboardFunc = copyContentToClipboard(content);
// confirm we get a function back
expect(_typeof(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);
});
});