clipboard.js 3.5 KB

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