Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/js/components/CopyToClipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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
Expand Down
67 changes: 67 additions & 0 deletions test/tests/js/components/CopyToClipboard-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<CopyToClipboard
src={src}
namespace={['root']}
theme='rjv-default'
hidden={false}
/>
)

try {
wrapper.find('.copy-to-clipboard-container').childAt(0).simulate('click')
} finally {
wrapper.unmount()
global.navigator.clipboard = clipboard
}

return copied
}

describe('<CopyToClipboard />', function () {
it('CopyToClipboard clipboard should exist', function () {
const wrapper = shallow(
Expand All @@ -29,4 +59,41 @@ describe('<CopyToClipboard />', 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()])
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.

it('CopyToClipboard copies a regexp as source text', function () {
expect(copyToClipboard(/pattern/g)).to.deep.equal(['/pattern/g'])
})
})