default.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import ClipboardActionDefault from '../../src/actions/default';
  2. describe('ClipboardActionDefault', () => {
  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('#resolveOptions', () => {
  17. it('should set base properties', () => {
  18. const selectedText = ClipboardActionDefault({
  19. container: document.body,
  20. text: 'foo',
  21. });
  22. assert.equal(selectedText, 'foo');
  23. });
  24. });
  25. describe('#set action', () => {
  26. it('should throw an error since "action" is invalid', (done) => {
  27. try {
  28. let clip = ClipboardActionDefault({
  29. text: 'foo',
  30. action: 'paste',
  31. });
  32. } catch (e) {
  33. assert.equal(
  34. e.message,
  35. 'Invalid "action" value, use either "copy" or "cut"'
  36. );
  37. done();
  38. }
  39. });
  40. });
  41. describe('#set target', () => {
  42. it('should throw an error since "target" do not match any element', (done) => {
  43. try {
  44. let clip = ClipboardActionDefault({
  45. target: document.querySelector('#foo'),
  46. });
  47. } catch (e) {
  48. assert.equal(e.message, 'Invalid "target" value, use a valid Element');
  49. done();
  50. }
  51. });
  52. });
  53. describe('#selectedText', () => {
  54. it('should select text from editable element', () => {
  55. const selectedText = ClipboardActionDefault({
  56. container: document.body,
  57. target: document.querySelector('#input'),
  58. });
  59. assert.equal(selectedText, 'abc');
  60. });
  61. it('should select text from non-editable element', () => {
  62. const selectedText = ClipboardActionDefault({
  63. container: document.body,
  64. target: document.querySelector('#paragraph'),
  65. });
  66. assert.equal(selectedText, 'abc');
  67. });
  68. });
  69. });