diff --git a/src/js/components/CopyToClipboard.js b/src/js/components/CopyToClipboard.js
index 2608e38..75951b5 100644
--- a/src/js/components/CopyToClipboard.js
+++ b/src/js/components/CopyToClipboard.js
@@ -37,7 +37,7 @@ export default class extends React.PureComponent {
handleCopy = () => {
const { clickCallback, src, namespace } = this.props
- const textToCopy = JSON.stringify(this.clipboardValue(src), null, ' ')
+ const textToCopy = this.clipboardText(src)
if (navigator.clipboard) {
navigator.clipboard.writeText(textToCopy).catch(() => {
@@ -94,6 +94,16 @@ export default class extends React.PureComponent {
}
}
+ clipboardText = value => {
+ const clipboardValue = this.clipboardValue(value)
+
+ if (typeof clipboardValue === 'string') {
+ return clipboardValue
+ }
+
+ return JSON.stringify(clipboardValue, null, ' ')
+ }
+
render () {
const { theme, hidden, rowHovered } = this.props
const style = Theme(theme, 'copy-to-clipboard').style
diff --git a/test/tests/js/components/CopyToClipboard-test.js b/test/tests/js/components/CopyToClipboard-test.js
index 5e1f125..2332fb1 100644
--- a/test/tests/js/components/CopyToClipboard-test.js
+++ b/test/tests/js/components/CopyToClipboard-test.js
@@ -4,6 +4,36 @@ import { expect } from 'chai'
import CopyToClipboard from './../../../../src/js/components/CopyToClipboard'
+function copyToClipboard (src) {
+ const copied = []
+ const { clipboard } = global.navigator
+
+ global.navigator.clipboard = {
+ writeText: textToCopy => {
+ copied.push(textToCopy)
+ return Promise.resolve()
+ }
+ }
+
+ const wrapper = shallow(
+
+ )
+
+ try {
+ wrapper.find('.copy-to-clipboard-container').childAt(0).simulate('click')
+ } finally {
+ wrapper.unmount()
+ global.navigator.clipboard = clipboard
+ }
+
+ return copied
+}
+
describe('', function () {
it('CopyToClipboard clipboard should exist', function () {
const wrapper = shallow(
@@ -29,4 +59,41 @@ describe('', function () {
// not sure how to test css attribute
expect(wrapper.find('.copy-to-clipboard-container')).to.have.length(1)
})
+
+ it('CopyToClipboard copies a string without quotes', function () {
+ expect(copyToClipboard('a string')).to.deep.equal(['a string'])
+ })
+
+ it('CopyToClipboard copies an object as JSON', function () {
+ expect(copyToClipboard({ test: true })).to.deep.equal([
+ JSON.stringify({ test: true }, null, ' ')
+ ])
+ })
+
+ it('CopyToClipboard copies an array as JSON', function () {
+ expect(copyToClipboard(['a string', 1])).to.deep.equal([
+ JSON.stringify(['a string', 1], null, ' ')
+ ])
+ })
+
+ it('CopyToClipboard copies a number as JSON', function () {
+ expect(copyToClipboard(1)).to.deep.equal(['1'])
+ })
+
+ it('CopyToClipboard copies a boolean as JSON', function () {
+ expect(copyToClipboard(true)).to.deep.equal(['true'])
+ })
+
+ it('CopyToClipboard copies null as JSON', function () {
+ expect(copyToClipboard(null)).to.deep.equal(['null'])
+ })
+
+ it('CopyToClipboard copies a function as source text', function () {
+ const noop = function () {}
+ expect(copyToClipboard(noop)).to.deep.equal([noop.toString()])
+ })
+
+ it('CopyToClipboard copies a regexp as source text', function () {
+ expect(copyToClipboard(/pattern/g)).to.deep.equal(['/pattern/g'])
+ })
})