clipboard.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import Clipboard from '../src/clipboard';
  2. import ClipboardAction from '../src/clipboard-action';
  3. import listen from 'good-listener';
  4. describe('Clipboard', () => {
  5. before(() => {
  6. global.button = document.createElement('button');
  7. global.button.setAttribute('class', 'btn');
  8. global.button.setAttribute('data-clipboard-text', 'foo');
  9. document.body.appendChild(global.button);
  10. global.span = document.createElement('span');
  11. global.span.innerHTML = 'bar';
  12. global.button.appendChild(span);
  13. global.event = {
  14. target: global.button,
  15. currentTarget: global.button
  16. };
  17. });
  18. after(() => {
  19. document.body.innerHTML = '';
  20. });
  21. describe('#resolveOptions', () => {
  22. before(() => {
  23. global.fn = () => {};
  24. });
  25. it('should set action as a function', () => {
  26. let clipboard = new Clipboard('.btn', {
  27. action: global.fn
  28. });
  29. assert.equal(global.fn, clipboard.action);
  30. });
  31. it('should set target as a function', () => {
  32. let clipboard = new Clipboard('.btn', {
  33. target: global.fn
  34. });
  35. assert.equal(global.fn, clipboard.target);
  36. });
  37. it('should set text as a function', () => {
  38. let clipboard = new Clipboard('.btn', {
  39. text: global.fn
  40. });
  41. assert.equal(global.fn, clipboard.text);
  42. });
  43. it('should set container as an object', () => {
  44. let clipboard = new Clipboard('.btn', {
  45. container: document.body
  46. });
  47. assert.equal(document.body, clipboard.container);
  48. });
  49. it('should set container as body by default', () => {
  50. let clipboard = new Clipboard('.btn');
  51. assert.equal(document.body, clipboard.container);
  52. });
  53. });
  54. describe('#listenClick', () => {
  55. it('should add a click event listener to the passed selector', () => {
  56. let clipboard = new Clipboard('.btn');
  57. assert.isObject(clipboard.listener);
  58. });
  59. });
  60. describe('#onClick', () => {
  61. it('should create a new instance of ClipboardAction', () => {
  62. let clipboard = new Clipboard('.btn');
  63. clipboard.onClick(global.event);
  64. assert.instanceOf(clipboard.clipboardAction, ClipboardAction);
  65. });
  66. it('should use an event\'s currentTarget when not equal to target', () => {
  67. let clipboard = new Clipboard('.btn');
  68. let bubbledEvent = { target: global.span, currentTarget: global.button };
  69. clipboard.onClick(bubbledEvent);
  70. assert.instanceOf(clipboard.clipboardAction, ClipboardAction);
  71. });
  72. it('should throw an exception when target is invalid', done => {
  73. try {
  74. const clipboard = new Clipboard('.btn', {
  75. target() {
  76. return null;
  77. }
  78. });
  79. clipboard.onClick(global.event);
  80. }
  81. catch(e) {
  82. assert.equal(e.message, 'Invalid "target" value, use a valid Element');
  83. done();
  84. }
  85. });
  86. });
  87. describe('#static isSupported', () => {
  88. it('should return the support of the given action', () => {
  89. assert.equal(Clipboard.isSupported('copy'), true);
  90. assert.equal(Clipboard.isSupported('cut'), true);
  91. });
  92. it('should return the support of the cut and copy actions', () => {
  93. assert.equal(Clipboard.isSupported(), true);
  94. });
  95. });
  96. describe('#destroy', () => {
  97. it('should destroy an existing instance of ClipboardAction', () => {
  98. let clipboard = new Clipboard('.btn');
  99. clipboard.onClick(global.event);
  100. clipboard.destroy();
  101. assert.equal(clipboard.clipboardAction, null);
  102. });
  103. });
  104. });