copy.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import ClipboardActionCopy from '../../src/actions/copy';
  2. describe('ClipboardActionCopy', () => {
  3. before(() => {
  4. global.input = document.createElement('input');
  5. global.input.setAttribute('id', 'input');
  6. global.input.setAttribute('value', 'abc');
  7. document.body.appendChild(global.input);
  8. global.paragraph = document.createElement('p');
  9. global.paragraph.setAttribute('id', 'paragraph');
  10. global.paragraph.textContent = 'abc';
  11. document.body.appendChild(global.paragraph);
  12. });
  13. after(() => {
  14. document.body.innerHTML = '';
  15. });
  16. describe('#selectText', () => {
  17. it('should select its value based on input target', () => {
  18. const selectedText = ClipboardActionCopy(
  19. document.querySelector('#input'),
  20. {
  21. container: document.body,
  22. }
  23. );
  24. assert.equal(selectedText, document.querySelector('#input').value);
  25. });
  26. it('should select its value based on element target', () => {
  27. const selectedText = ClipboardActionCopy(
  28. document.querySelector('#paragraph'),
  29. {
  30. container: document.body,
  31. }
  32. );
  33. assert.equal(
  34. selectedText,
  35. document.querySelector('#paragraph').textContent
  36. );
  37. });
  38. it('should select its value based on text', () => {
  39. const text = 'abc';
  40. const selectedText = ClipboardActionCopy(text, {
  41. container: document.body,
  42. });
  43. assert.equal(selectedText, text);
  44. });
  45. it('should select its value in a input number based on text', () => {
  46. const value = 1;
  47. document.querySelector('#input').setAttribute('type', 'number');
  48. document.querySelector('#input').setAttribute('value', value);
  49. const selectedText = ClipboardActionCopy(
  50. document.querySelector('#input'),
  51. {
  52. container: document.body,
  53. }
  54. );
  55. assert.equal(Number(selectedText), value);
  56. });
  57. });
  58. });