ZeroClipboard.tests.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*global ZeroClipboard */
  2. (function(module, test) {
  3. "use strict";
  4. var originalConfig, originalFlashDetect;
  5. // Helper functions
  6. var TestUtils = {
  7. getHtmlBridge: function() {
  8. return document.getElementById(ZeroClipboard.config("containerId"));
  9. }
  10. };
  11. module("ZeroClipboard.js (built) unit tests - Core", {
  12. setup: function() {
  13. // Store
  14. originalConfig = ZeroClipboard.config();
  15. originalFlashDetect = ZeroClipboard.isFlashUnusable;
  16. // Modify
  17. ZeroClipboard.isFlashUnusable = function() {
  18. return false;
  19. };
  20. ZeroClipboard.config({ swfPath: originalConfig.swfPath.replace(/\/(?:src|test)\/.*$/i, "/dist/ZeroClipboard.swf") });
  21. },
  22. teardown: function() {
  23. // Restore
  24. ZeroClipboard.destroy();
  25. ZeroClipboard.config(originalConfig);
  26. ZeroClipboard.isFlashUnusable = originalFlashDetect;
  27. }
  28. });
  29. test("`swfPath` finds the expected default URL", function(assert) {
  30. assert.expect(1);
  31. // Assert, act, assert
  32. var rootOrigin = window.location.protocol + "//" + window.location.host + "/";
  33. var indexOfTest = window.location.pathname.toLowerCase().indexOf("/test/");
  34. var rootDir = window.location.pathname.slice(1, indexOfTest + 1);
  35. var rootPath = rootOrigin + rootDir;
  36. //var zcJsUrl = rootPath + "dist/ZeroClipboard.js";
  37. var swfPathBasedOnZeroClipboardJsPath = rootPath + "dist/ZeroClipboard.swf";
  38. // Test that the client has the expected default URL [even if it's not correct]
  39. assert.strictEqual(ZeroClipboard.config("swfPath"), swfPathBasedOnZeroClipboardJsPath);
  40. });
  41. test("`destroy` destroys the bridge", function(assert) {
  42. assert.expect(3);
  43. // Arrange
  44. ZeroClipboard.isFlashUnusable = function() {
  45. return false;
  46. };
  47. // Assert, arrange, assert, act, assert
  48. assert.equal(TestUtils.getHtmlBridge(), null, "The bridge does not exist before creating a client");
  49. /*jshint nonew:false */
  50. new ZeroClipboard();
  51. assert.notEqual(TestUtils.getHtmlBridge(), null, "The bridge does exist after creating a client");
  52. ZeroClipboard.destroy();
  53. assert.equal(TestUtils.getHtmlBridge(), null, "The bridge does not exist after calling `destroy`");
  54. });
  55. module("ZeroClipboard.js (built) unit tests - Client", {
  56. setup: function() {
  57. // Store
  58. originalConfig = ZeroClipboard.config();
  59. originalFlashDetect = ZeroClipboard.isFlashUnusable;
  60. // Modify
  61. ZeroClipboard.isFlashUnusable = function() {
  62. return false;
  63. };
  64. ZeroClipboard.config({ swfPath: originalConfig.swfPath.replace(/\/(?:src|test)\/.*$/i, "/dist/ZeroClipboard.swf") });
  65. },
  66. teardown: function() {
  67. // Restore
  68. ZeroClipboard.destroy();
  69. ZeroClipboard.config(originalConfig);
  70. ZeroClipboard.isFlashUnusable = originalFlashDetect;
  71. }
  72. });
  73. test("`ZeroClipboard` exists", function(assert) {
  74. assert.expect(1);
  75. // Arrange -> N/A
  76. // Act -> N/A
  77. // Assert
  78. assert.ok(ZeroClipboard);
  79. });
  80. test("Client is created properly", function(assert) {
  81. assert.expect(2);
  82. // Arrange & Act
  83. var client = new ZeroClipboard();
  84. // Assert
  85. assert.ok(client);
  86. assert.ok(client.id);
  87. });
  88. test("Client without selector doesn't have elements", function(assert) {
  89. assert.expect(2);
  90. // Arrange & Act
  91. var client = new ZeroClipboard();
  92. // Assert
  93. assert.ok(client);
  94. assert.deepEqual(client.elements(), []);
  95. });
  96. test("Object has a title", function(assert) {
  97. assert.expect(1);
  98. // Arrange
  99. var client = new ZeroClipboard();
  100. var currentEl = document.getElementById("d_clip_button");
  101. // Act
  102. client.clip(currentEl);
  103. ZeroClipboard.activate(currentEl);
  104. // Assert
  105. assert.strictEqual(TestUtils.getHtmlBridge().getAttribute("title"), "Click me to copy to clipboard.");
  106. // Revert
  107. ZeroClipboard.deactivate();
  108. });
  109. test("Object has no title", function(assert) {
  110. assert.expect(1);
  111. // Arrange
  112. var client = new ZeroClipboard();
  113. var currentEl = document.getElementById("d_clip_button_no_title");
  114. // Act
  115. client.clip(currentEl);
  116. ZeroClipboard.activate(currentEl);
  117. // Assert
  118. assert.ok(!TestUtils.getHtmlBridge().getAttribute("title"));
  119. });
  120. test("Object doesn't have data-clipboard-text", function(assert) {
  121. assert.expect(1);
  122. // Arrange
  123. var client = new ZeroClipboard();
  124. var currentEl = document.getElementById("d_clip_button_no_text");
  125. // Act
  126. client.clip(currentEl);
  127. ZeroClipboard.activate(currentEl);
  128. // Assert
  129. assert.ok(!TestUtils.getHtmlBridge().getAttribute("data-clipboard-text"));
  130. });
  131. test("New client is not the same client (no singleton) but does share the same bridge", function(assert) {
  132. assert.expect(6);
  133. // Assert, arrange, assert, act, assert
  134. var containerClass = "." + ZeroClipboard.config("containerClass");
  135. assert.strictEqual($(containerClass).length, 0);
  136. var client1 = new ZeroClipboard();
  137. assert.ok(client1.id);
  138. assert.strictEqual($(containerClass).length, 1);
  139. var client2 = new ZeroClipboard();
  140. assert.strictEqual($(containerClass).length, 1);
  141. assert.notEqual(client2.id, client1.id);
  142. assert.notEqual(client2, client1);
  143. });
  144. test("Calculations based on borderWidth never return NaN", function(assert) {
  145. assert.expect(4);
  146. // Arrange
  147. var client = new ZeroClipboard();
  148. var currentEl = document.getElementById("d_clip_button");
  149. // Act
  150. client.clip(currentEl);
  151. ZeroClipboard.activate(currentEl);
  152. // Assert
  153. var htmlBridge = TestUtils.getHtmlBridge();
  154. assert.strictEqual(/^-?[0-9\.]+px$/.test(htmlBridge.style.top), true);
  155. assert.strictEqual(/^-?[0-9\.]+px$/.test(htmlBridge.style.left), true);
  156. assert.strictEqual(/^-?[0-9\.]+px$/.test(htmlBridge.style.width), true);
  157. assert.strictEqual(/^-?[0-9\.]+px$/.test(htmlBridge.style.height), true);
  158. });
  159. test("No more client singleton!", function(assert) {
  160. assert.expect(7);
  161. // Arrange
  162. ZeroClipboard.isFlashUnusable = function() {
  163. return false;
  164. };
  165. // Assert, arrange, assert, act, assert
  166. assert.ok(!ZeroClipboard.prototype._singleton, "The client singleton does not exist on the prototype before creating a client");
  167. var client1 = new ZeroClipboard();
  168. assert.ok(!ZeroClipboard.prototype._singleton, "The client singleton does not exist on the prototype after creating a client");
  169. assert.ok(!client1._singleton, "The client singleton does not exist on the client instance after creating a client");
  170. var client2 = new ZeroClipboard();
  171. assert.ok(!ZeroClipboard.prototype._singleton, "The client singleton does not exist on the prototype after creating a second client");
  172. assert.ok(!client1._singleton, "The client singleton does not exist on the first client instance after creating a second client");
  173. assert.ok(!client2._singleton, "The client singleton does not exist on the second client instance after creating a second client");
  174. ZeroClipboard.destroy();
  175. assert.ok(!ZeroClipboard.prototype._singleton, "The client singleton does not exist on the prototype after calling `destroy`");
  176. });
  177. })(QUnit.module, QUnit.test);