api.tests.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*global ZeroClipboard, _globalConfig:true, _flashState, _clipData, _clipDataFormatMap, _deleteOwnProperties */
  2. (function(module, test) {
  3. "use strict";
  4. // Helper functions
  5. var TestUtils = {
  6. getHtmlBridge: function() {
  7. return document.getElementById("global-zeroclipboard-html-bridge");
  8. }
  9. };
  10. var originalConfig, originalFlashDetect;
  11. module("core/api.js unit tests - state");
  12. test("`state` produces expected result", function(assert) {
  13. assert.expect(8);
  14. // Act
  15. var result = ZeroClipboard.state();
  16. // Assert
  17. assert.deepEqual(Object.keys(result), ["browser", "flash", "zeroclipboard"], "Has all expected keys");
  18. assert.strictEqual(typeof result.browser, "object", ".browser is an object");
  19. assert.notStrictEqual(result.browser, null, ".browser is a non-null object");
  20. assert.strictEqual(typeof result.flash, "object", ".flash is an object");
  21. assert.notStrictEqual(result.flash, null, ".flash is a non-null object");
  22. assert.strictEqual(typeof result.zeroclipboard, "object", ".zeroclipboard is an object");
  23. assert.notStrictEqual(result.zeroclipboard, null, ".zeroclipboard is a non-null object");
  24. assert.deepEqual(Object.keys(result.zeroclipboard), ["version", "config"], ".zeroclipboard has all expected keys");
  25. });
  26. module("core/api.js unit tests - config", {
  27. setup: function() {
  28. originalConfig = ZeroClipboard.config();
  29. },
  30. teardown: function() {
  31. _globalConfig = originalConfig;
  32. }
  33. });
  34. test("`swfPath` finds the expected default URL", function(assert) {
  35. assert.expect(1);
  36. // Assert, act, assert
  37. var rootOrigin = window.location.protocol + "//" + window.location.host + "/";
  38. var indexOfTest = window.location.pathname.toLowerCase().indexOf("/test/");
  39. var rootDir = window.location.pathname.slice(1, indexOfTest + 1);
  40. var rootPath = rootOrigin + rootDir;
  41. //var stateJsUrl = rootPath + "src/js/core/state.js";
  42. // This is, for the record, a totally incorrect path due to being the development
  43. // file structure but it IS the correct URL based on calculated assumption of using
  44. // the built distributable versions of the library
  45. var swfPathBasedOnStateJsPath = rootPath + "src/js/core/ZeroClipboard.swf";
  46. // Test that the client has the expected default URL [even if it's not correct]
  47. assert.strictEqual(ZeroClipboard.config("swfPath"), swfPathBasedOnStateJsPath);
  48. });
  49. test("Changing `trustedDomains` works", function(assert) {
  50. assert.expect(5);
  51. // Arrange
  52. var currentHost = window.location.host;
  53. var originalValue = currentHost ? [currentHost] : [];
  54. var updatedValue = currentHost ? [currentHost, "otherDomain.com"] : ["otherDomain.com"];
  55. // Assert, act, assert
  56. // Test that the client has the default value
  57. assert.deepEqual(ZeroClipboard.config("trustedDomains"), originalValue);
  58. assert.deepEqual(ZeroClipboard.config().trustedDomains, originalValue);
  59. // Change the value
  60. var updatedConfig = ZeroClipboard.config({ trustedDomains: updatedValue });
  61. // Test that the client has the changed value
  62. assert.deepEqual(updatedConfig.trustedDomains, updatedValue);
  63. assert.deepEqual(ZeroClipboard.config("trustedDomains"), updatedValue);
  64. assert.deepEqual(ZeroClipboard.config().trustedDomains, updatedValue);
  65. });
  66. test("Some config values are ignored if SWF is actively embedded", function(assert) {
  67. assert.expect(2);
  68. // Arrange
  69. var _swfPath = ZeroClipboard.config("swfPath");
  70. var expectedBefore = {
  71. swfPath: _swfPath,
  72. trustedDomains: window.location.host ? [window.location.host] : [],
  73. cacheBust: true,
  74. forceEnhancedClipboard: false,
  75. flashLoadTimeout: 30000,
  76. autoActivate: true,
  77. containerId: "global-zeroclipboard-html-bridge",
  78. containerClass: "global-zeroclipboard-container",
  79. swfObjectId: "global-zeroclipboard-flash-bridge",
  80. hoverClass: "zeroclipboard-is-hover",
  81. activeClass: "zeroclipboard-is-active",
  82. // These configuration values CAN be modified while a SWF is actively embedded.
  83. bubbleEvents: true,
  84. forceHandCursor: false,
  85. title: null,
  86. zIndex: 999999999
  87. };
  88. var expectedAfter = {
  89. swfPath: _swfPath,
  90. trustedDomains: window.location.host ? [window.location.host] : [],
  91. cacheBust: true,
  92. forceEnhancedClipboard: false,
  93. flashLoadTimeout: 30000,
  94. autoActivate: true,
  95. containerId: "global-zeroclipboard-html-bridge",
  96. containerClass: "global-zeroclipboard-container",
  97. swfObjectId: "global-zeroclipboard-flash-bridge",
  98. hoverClass: "zeroclipboard-is-hover",
  99. activeClass: "zeroclipboard-is-active",
  100. // These configuration values CAN be modified while a SWF is actively embedded.
  101. bubbleEvents: false,
  102. forceHandCursor: true,
  103. title: "test",
  104. zIndex: 1000
  105. };
  106. // Act
  107. var actualBefore = ZeroClipboard.config();
  108. _flashState.bridge = {};
  109. var actualAfter = ZeroClipboard.config({
  110. swfPath: "/path/to/test.swf",
  111. trustedDomains: ["test.domain.com"],
  112. cacheBust: false,
  113. forceEnhancedClipboard: true,
  114. flashLoadTimeout: 15000,
  115. autoActivate: false,
  116. containerId: "test-id",
  117. containerClass: "test-class",
  118. swfObjectId: "test-swf",
  119. hoverClass: "test-hover",
  120. activeClass: "test-active",
  121. // These configuration values CAN be modified while a SWF is actively embedded.
  122. bubbleEvents: false,
  123. forceHandCursor: true,
  124. title: "test",
  125. zIndex: 1000
  126. });
  127. // Assert
  128. assert.deepEqual(actualBefore, expectedBefore, "Original config is as expected");
  129. assert.deepEqual(actualAfter, expectedAfter, "Updated config is as expected");
  130. });
  131. module("core/api.js unit tests - clipboard", {
  132. teardown: function() {
  133. _deleteOwnProperties(_clipData);
  134. }
  135. });
  136. test("`setData` works", function(assert) {
  137. assert.expect(4);
  138. // Assert, Act, repeat ad nauseam
  139. assert.deepEqual(_clipData, {}, "`_clipData` is empty");
  140. ZeroClipboard.setData("text/plain", "zc4evar");
  141. assert.deepEqual(_clipData, { "text/plain": "zc4evar" }, "`_clipData` contains expected text");
  142. ZeroClipboard.setData("text/x-markdown", "**ZeroClipboard**");
  143. assert.deepEqual(_clipData, { "text/plain": "zc4evar", "text/x-markdown": "**ZeroClipboard**" }, "`_clipData` contains expected text and custom format");
  144. ZeroClipboard.setData({ "text/html": "<b>Win</b>" });
  145. assert.deepEqual(_clipData, { "text/html": "<b>Win</b>" }, "`_clipData` contains expected HTML and cleared out old data because an object was passed in");
  146. });
  147. test("`clearData` works", function(assert) {
  148. assert.expect(4);
  149. // Assert
  150. assert.deepEqual(_clipData, {}, "`_clipData` is empty");
  151. // Arrange & Assert
  152. _clipData["text/plain"] = "zc4evar";
  153. _clipData["text/html"] = "<b>Win</b>";
  154. _clipData["text/x-markdown"] = "**ZeroClipboard**";
  155. assert.deepEqual(_clipData, {
  156. "text/plain": "zc4evar",
  157. "text/html": "<b>Win</b>",
  158. "text/x-markdown": "**ZeroClipboard**"
  159. }, "`_clipData` contains all expected data");
  160. // Act & Assert
  161. ZeroClipboard.clearData("text/html");
  162. assert.deepEqual(_clipData, {
  163. "text/plain": "zc4evar",
  164. "text/x-markdown": "**ZeroClipboard**"
  165. }, "`_clipData` had 'text/html' successfully removed");
  166. // Act & Assert
  167. ZeroClipboard.clearData();
  168. assert.deepEqual(_clipData, {}, "`_clipData` had all data successfully removed");
  169. });
  170. module("core/api.js unit tests - flash", {
  171. setup: function() {
  172. // Store
  173. originalFlashDetect = ZeroClipboard.isFlashUnusable;
  174. // Modify
  175. ZeroClipboard.isFlashUnusable = function() {
  176. return false;
  177. };
  178. },
  179. teardown: function() {
  180. // Restore
  181. ZeroClipboard.isFlashUnusable = originalFlashDetect;
  182. ZeroClipboard.destroy();
  183. }
  184. });
  185. test("Flash object is ready after emitting `ready`", function(assert) {
  186. assert.expect(2);
  187. // Arrange
  188. ZeroClipboard.isFlashUnusable = function() {
  189. return false;
  190. };
  191. ZeroClipboard.create();
  192. // Assert, act, assert
  193. assert.strictEqual(_flashState.ready, false);
  194. // `emit`-ing event handlers are async (generally) but the internal `ready` state is set synchronously
  195. ZeroClipboard.emit("ready");
  196. assert.strictEqual(_flashState.ready, true);
  197. });
  198. test("Object has a title", function(assert) {
  199. assert.expect(1);
  200. // Arrange
  201. var currentEl = document.getElementById("d_clip_button");
  202. ZeroClipboard.create();
  203. // Act
  204. ZeroClipboard.activate(currentEl);
  205. // Assert
  206. assert.strictEqual(TestUtils.getHtmlBridge().getAttribute("title"), "Click me to copy to clipboard.");
  207. // Revert
  208. ZeroClipboard.deactivate();
  209. });
  210. test("Object has no title", function(assert) {
  211. assert.expect(1);
  212. // Arrange
  213. var currentEl = document.getElementById("d_clip_button_no_title");
  214. ZeroClipboard.create();
  215. // Act
  216. ZeroClipboard.activate(currentEl);
  217. // Assert
  218. assert.ok(!TestUtils.getHtmlBridge().getAttribute("title"));
  219. // Revert
  220. ZeroClipboard.deactivate();
  221. });
  222. test("Object has data-clipboard-text", function(assert) {
  223. assert.expect(3);
  224. // Arrange
  225. var currentEl = document.getElementById("d_clip_button");
  226. ZeroClipboard.create();
  227. // Act
  228. ZeroClipboard.activate(currentEl);
  229. var pendingText = ZeroClipboard.emit("copy");
  230. // Assert
  231. assert.deepEqual(_clipData, { "text/plain": "Copy me!" });
  232. assert.deepEqual(pendingText, { "text": "Copy me!" });
  233. assert.deepEqual(_clipDataFormatMap, { "text": "text/plain" });
  234. // Revert
  235. ZeroClipboard.deactivate();
  236. });
  237. test("Object has data-clipboard-target textarea", function(assert) {
  238. assert.expect(3);
  239. // Arrange
  240. var currentEl = document.getElementById("d_clip_button_textarea_text");
  241. var expectedText =
  242. "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\n"+
  243. "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,\n"+
  244. "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo\n"+
  245. "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse\n"+
  246. "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non\n"+
  247. "proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
  248. ZeroClipboard.create();
  249. // Act
  250. ZeroClipboard.activate(currentEl);
  251. var pendingText = ZeroClipboard.emit("copy");
  252. // Assert
  253. assert.strictEqual(_clipData["text/plain"].replace(/\r\n/g, "\n"), expectedText);
  254. assert.strictEqual(pendingText.text.replace(/\r\n/g, "\n"), expectedText);
  255. assert.deepEqual(_clipDataFormatMap, { "text": "text/plain" });
  256. // Revert
  257. ZeroClipboard.deactivate();
  258. });
  259. test("Object has data-clipboard-target pre", function(assert) {
  260. assert.expect(5);
  261. // Arrange
  262. var currentEl = document.getElementById("d_clip_button_pre_text");
  263. var expectedText =
  264. "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\n"+
  265. "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,\n"+
  266. "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo\n"+
  267. "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse\n"+
  268. "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non\n"+
  269. "proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
  270. var expectedHtml =
  271. "<pre id=\"clipboard_pre\">"+
  272. "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\n"+
  273. "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,\n"+
  274. "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo\n"+
  275. "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse\n"+
  276. "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non\n"+
  277. "proident, sunt in culpa qui officia deserunt mollit anim id est laborum."+
  278. "</pre>";
  279. ZeroClipboard.create();
  280. // Act
  281. ZeroClipboard.activate(currentEl);
  282. var pendingText = ZeroClipboard.emit("copy");
  283. // Assert
  284. assert.strictEqual(_clipData["text/plain"].replace(/\r\n/g, "\n"), expectedText);
  285. assert.strictEqual(
  286. _clipData["text/html"]
  287. .replace(/\r\n/g, "\n")
  288. .replace(/<\/?pre(?:\s+[^>]*)?>/gi, function($0) { return $0.toLowerCase(); }),
  289. expectedHtml
  290. );
  291. assert.strictEqual(pendingText.text.replace(/\r\n/g, "\n"), expectedText);
  292. assert.strictEqual(
  293. pendingText.html
  294. .replace(/\r\n/g, "\n")
  295. .replace(/<\/?pre(?:\s+[^>]*)?>/gi, function($0) { return $0.toLowerCase(); }),
  296. expectedHtml
  297. );
  298. assert.deepEqual(_clipDataFormatMap, { "text": "text/plain", "html": "text/html" });
  299. // Revert
  300. ZeroClipboard.deactivate();
  301. });
  302. test("Object has data-clipboard-target input", function(assert) {
  303. assert.expect(3);
  304. // Arrange
  305. var currentEl = document.getElementById("d_clip_button_input_text");
  306. ZeroClipboard.create();
  307. // Act
  308. ZeroClipboard.activate(currentEl);
  309. var pendingText = ZeroClipboard.emit("copy");
  310. // Assert
  311. assert.deepEqual(_clipData, { "text/plain": "Clipboard Text" });
  312. assert.deepEqual(pendingText, { "text": "Clipboard Text" });
  313. assert.deepEqual(_clipDataFormatMap, { "text": "text/plain" });
  314. // Revert
  315. ZeroClipboard.deactivate();
  316. });
  317. test("Object doesn't have data-clipboard-text", function(assert) {
  318. assert.expect(1);
  319. // Arrange
  320. var currentEl = document.getElementById("d_clip_button_no_text");
  321. ZeroClipboard.create();
  322. // Act
  323. ZeroClipboard.activate(currentEl);
  324. // Assert
  325. assert.ok(!TestUtils.getHtmlBridge().getAttribute("data-clipboard-text"));
  326. // Revert
  327. ZeroClipboard.deactivate();
  328. });
  329. test("Calculations based on borderWidth never return NaN", function(assert) {
  330. assert.expect(4);
  331. // Arrange
  332. var currentEl = document.getElementById("d_clip_button");
  333. ZeroClipboard.create();
  334. // Act
  335. ZeroClipboard.activate(currentEl);
  336. // Assert
  337. assert.strictEqual(/^-?[0-9\.]+px$/.test(TestUtils.getHtmlBridge().style.top), true);
  338. assert.strictEqual(/^-?[0-9\.]+px$/.test(TestUtils.getHtmlBridge().style.left), true);
  339. assert.strictEqual(/^-?[0-9\.]+px$/.test(TestUtils.getHtmlBridge().style.width), true);
  340. assert.strictEqual(/^-?[0-9\.]+px$/.test(TestUtils.getHtmlBridge().style.height), true);
  341. });
  342. })(QUnit.module, QUnit.test);