{"size": 786, "ext": "js", "max_stars_repo_path": "www/src/utils/__tests__/copy-to-clipboard.js", "max_stars_repo_name": "JQuinnie/gatsby", "max_stars_repo_stars_event_min_datetime": "2018-09-28T13:09:20.000Z", "max_stars_repo_stars_event_max_datetime": "2020-10-29T00:20:07.000Z", "max_issues_repo_path": "www/src/utils/__tests__/copy-to-clipboard.js", "max_issues_repo_name": "tanmaylaud/gatsby", "max_issues_repo_issues_event_min_datetime": "2019-02-14T23:19:06.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-03T19:11:38.000Z", "max_forks_repo_path": "www/src/utils/__tests__/copy-to-clipboard.js", "max_forks_repo_name": "tanmaylaud/gatsby", "max_forks_repo_forks_event_min_datetime": "2018-09-23T06:59:58.000Z", "max_forks_repo_forks_event_max_datetime": "2020-05-12T07:17:56.000Z"}
{"max_stars_count": 19, "max_issues_count": 79, "max_forks_count": 8, "avg_line_length": 23.8181818182, "max_line_length": 79, "alphanum_fraction": 0.6844783715}
import copyToClipboard from "../copy-to-clipboard"

beforeEach(() => {
  window.navigator.clipboard = {
    writeText: jest.fn(),
  }

  document.execCommand = jest.fn()
  document.createRange = jest.fn()
  window.getSelection = jest.fn(() => {
    return {
      removeAllRanges: jest.fn(),
      addRange: jest.fn(),
    }
  })
})

test(`it uses writeText API, by default`, async () => {
  const str = `waddup`
  await copyToClipboard(str)

  expect(window.navigator.clipboard.writeText).toHaveBeenCalledWith(str)
})

test(`it falls back to execCommand, if writeText is not defined`, async () => {
  window.navigator.clipboard = {}

  await copyToClipboard(`sup`)

  expect(document.execCommand).toHaveBeenCalledWith(`copy`)
  expect(document.execCommand).toHaveBeenCalledTimes(1)
})
