tests_base.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. export default function (qunit, $, Inputmask) {
  2. qunit.module("Simple masking");
  3. qunit.test("inputmask(\"99-99-99\", { clearMaskOnLostFocus: false}", function (assert) {
  4. var $fixture = $("#qunit-fixture");
  5. $fixture.append('<input type="text" id="testmask" />');
  6. var testmask = document.getElementById("testmask");
  7. Inputmask({
  8. mask: "99-99-99",
  9. clearMaskOnLostFocus: false
  10. }).mask(testmask);
  11. assert.equal(testmask.inputmask._valueGet(), "__-__-__", "Result " + testmask.inputmask._valueGet());
  12. });
  13. qunit.test("inputmask(\"99-99-99\", { clearMaskOnLostFocus: true}", function (assert) {
  14. var done = assert.async(),
  15. $fixture = $("#qunit-fixture");
  16. $fixture.append('<input type="text" id="testmask" />');
  17. var testmask = document.getElementById("testmask");
  18. Inputmask({
  19. mask: "99-99-99",
  20. clearMaskOnLostFocus: false
  21. }).mask(testmask);
  22. testmask.blur();
  23. setTimeout(function () {
  24. assert.equal(testmask.value, "", "Result " + testmask.value);
  25. done();
  26. }, 0);
  27. });
  28. qunit.test("inputmask(\"999.999.999\")", function (assert) {
  29. var $fixture = $("#qunit-fixture");
  30. $fixture.append('<input type="text" id="testmask" />');
  31. var testmask = document.getElementById("testmask");
  32. Inputmask("999.999.999").mask(testmask);
  33. testmask.focus();
  34. $("#testmask").Type("123");
  35. assert.equal(testmask.value, "123.___.___", "Result " + testmask.value);
  36. });
  37. qunit.test("inputmask(\"999.999.999\") + backspace", function (assert) {
  38. var $fixture = $("#qunit-fixture");
  39. $fixture.append('<input type="text" id="testmask" />');
  40. var testmask = document.getElementById("testmask");
  41. Inputmask("999.999.999").mask(testmask);
  42. testmask.focus();
  43. $("#testmask").Type("123");
  44. $("#testmask").SendKey(Inputmask.keyCode.BACKSPACE);
  45. assert.equal(testmask.value, "12_.___.___", "Result " + testmask.value);
  46. });
  47. qunit.test("inputmask(\"999.999.999\", { oncomplete: ... })", function (assert) {
  48. var done = assert.async(),
  49. $fixture = $("#qunit-fixture");
  50. $fixture.append('<input type="text" id="testmask" />');
  51. var testmask = document.getElementById("testmask");
  52. Inputmask("999.999.999", {
  53. oncomplete: function () {
  54. assert.equal(testmask.value, "123.456.789", "Result " + testmask.value);
  55. testmask.inputmask.remove();
  56. done();
  57. }
  58. }).mask(testmask);
  59. testmask.focus();
  60. $("#testmask").Type("123456789");
  61. });
  62. qunit.test("inputmask(\"9-AAA.999\") - change event", function (assert) {
  63. var done = assert.async(),
  64. $fixture = $("#qunit-fixture");
  65. $fixture.append('<input type="text" id="testmask" />');
  66. var testmask = document.getElementById("testmask");
  67. $("#testmask").on("change", function () {
  68. assert.ok(true, "Change triggered");
  69. done();
  70. });
  71. Inputmask("9-AAA.999").mask(testmask);
  72. testmask.focus();
  73. setTimeout(function () {
  74. $("#testmask").Type("1abc12");
  75. testmask.blur();
  76. }, 0);
  77. });
  78. qunit.test("inputmask(\"9-AAA.999\", { onincomplete: ... })", function (assert) {
  79. var done = assert.async(),
  80. $fixture = $("#qunit-fixture");
  81. $fixture.append('<input type="text" id="testmask" />');
  82. var testmask = document.getElementById("testmask");
  83. Inputmask("9-AAA.999", {
  84. onincomplete: function () {
  85. assert.equal(testmask.value, "1-ABC.12_", "Result " + testmask.value);
  86. testmask.inputmask.remove();
  87. done();
  88. }
  89. }).mask(testmask);
  90. testmask.focus();
  91. setTimeout(function () {
  92. $("#testmask").Type("1abc12");
  93. testmask.blur();
  94. }, 0);
  95. });
  96. qunit.test("inputmask(\"999.999.999\") - delete 2nd with backspace, continue the mask", function (assert) {
  97. var $fixture = $("#qunit-fixture");
  98. $fixture.append('<input type="text" id="testmask" />');
  99. var testmask = document.getElementById("testmask");
  100. Inputmask("999.999.999").mask(testmask);
  101. testmask.focus();
  102. $("#testmask").Type("123");
  103. $("#testmask").SendKey(Inputmask.keyCode.LEFT);
  104. $("#testmask").SendKey(Inputmask.keyCode.LEFT);
  105. $("#testmask").SendKey(Inputmask.keyCode.BACKSPACE);
  106. $("#testmask").Type("4");
  107. $("#testmask").SendKey(Inputmask.keyCode.RIGHT);
  108. $("#testmask").Type("56");
  109. assert.equal(testmask.value, "143.56_.___", "Result " + testmask.value);
  110. });
  111. qunit.test("inputmask(\"999.999.999\") - delete 2nd with delete, continue the mask", function (assert) {
  112. var $fixture = $("#qunit-fixture");
  113. $fixture.append('<input type="text" id="testmask" />');
  114. var testmask = document.getElementById("testmask");
  115. Inputmask("999.999.999").mask(testmask);
  116. testmask.focus();
  117. $("#testmask").SendKey(49);
  118. $("#testmask").SendKey(50);
  119. $("#testmask").SendKey(51);
  120. $("#testmask").SendKey(Inputmask.keyCode.LEFT);
  121. $("#testmask").SendKey(Inputmask.keyCode.LEFT);
  122. $("#testmask").SendKey(Inputmask.keyCode.LEFT);
  123. $("#testmask").SendKey(Inputmask.keyCode.DELETE);
  124. $("#testmask").SendKey(52);
  125. $("#testmask").SendKey(Inputmask.keyCode.RIGHT);
  126. $("#testmask").SendKey(53);
  127. $("#testmask").SendKey(54);
  128. assert.equal(testmask.value, "143.56_.___", "Result " + testmask.value);
  129. });
  130. qunit.test("inputmask(\"999.999.999\") - delete selection start with nomask", function (assert) {
  131. var $fixture = $("#qunit-fixture");
  132. $fixture.append('<input type="text" id="testmask" />');
  133. var testmask = document.getElementById("testmask");
  134. Inputmask("999.999.999").mask(testmask);
  135. testmask.focus();
  136. $("#testmask").Type("123456789");
  137. $.caret(testmask, 3, 7);
  138. $("#testmask").SendKey(Inputmask.keyCode.DELETE);
  139. assert.equal(testmask.value, "123.789.___", "Result " + testmask.value);
  140. });
  141. qunit.test("inputmask(\"999.999.999\") - backspace selection start with nomask", function (assert) {
  142. var $fixture = $("#qunit-fixture");
  143. $fixture.append('<input type="text" id="testmask" />');
  144. var testmask = document.getElementById("testmask");
  145. Inputmask("999.999.999").mask(testmask);
  146. testmask.focus();
  147. $("#testmask").Type("123456789");
  148. $.caret(testmask, 3, 7);
  149. $("#testmask").SendKey(Inputmask.keyCode.DELETE);
  150. assert.equal(testmask.value, "123.789.___", "Result " + testmask.value);
  151. });
  152. qunit.test("inputmask(\"999.999.999\") - overtype selection start with nomask", function (assert) {
  153. var $fixture = $("#qunit-fixture");
  154. $fixture.append('<input type="text" id="testmask" />');
  155. var testmask = document.getElementById("testmask");
  156. Inputmask("999.999.999").mask(testmask);
  157. testmask.focus();
  158. $("#testmask").Type("123456789");
  159. $.caret(testmask, 3, 7);
  160. $("#testmask").Type("1");
  161. assert.equal(testmask.value, "123.178.9__", "Result " + testmask.value);
  162. });
  163. qunit.test("inputmask(\"*****\")", function (assert) {
  164. var $fixture = $("#qunit-fixture");
  165. $fixture.append('<input type="text" id="testmask" />');
  166. var testmask = document.getElementById("testmask");
  167. Inputmask("*****").mask(testmask);
  168. testmask.focus();
  169. $("#testmask").Type("abe");
  170. $("#testmask").SendKey(Inputmask.keyCode.LEFT);
  171. $("#testmask").Type("cd");
  172. assert.equal(testmask.value, "abcde", "Result " + testmask.value);
  173. });
  174. qunit.test("inputmask(\"(999)999-9999\") - ruslanfedoseenko mask", function (assert) {
  175. var $fixture = $("#qunit-fixture");
  176. $fixture.append('<input type="text" id="testmask" />');
  177. var testmask = document.getElementById("testmask");
  178. Inputmask("(999)999-9999").mask(testmask);
  179. testmask.focus();
  180. testmask.value = "9999999999";
  181. $.caret(testmask, 4, 5);
  182. $("#testmask").Type("7");
  183. assert.equal(testmask.value, "(999)999-9999", "Result " + testmask.value);
  184. });
  185. qunit.test("inputmask(\"(999)999-9999\") - insert false - ruslanfedoseenko mask", function (assert) {
  186. var $fixture = $("#qunit-fixture");
  187. $fixture.append('<input type="text" id="testmask" />');
  188. var testmask = document.getElementById("testmask");
  189. Inputmask("(999)999-9999", {
  190. insertMode: false
  191. }).mask(testmask);
  192. testmask.focus();
  193. testmask.value = "9999999999";
  194. $.caret(testmask, 4, 5);
  195. $("#testmask").Type("7");
  196. assert.equal(testmask.value, "(999)999-9999", "Result " + testmask.value);
  197. });
  198. qunit.test("inputmask(\"\") - empty mask - andywolk", function (assert) {
  199. var $fixture = $("#qunit-fixture");
  200. $fixture.append('<input type="text" id="testmask" />');
  201. var testmask = document.getElementById("testmask");
  202. Inputmask("").mask(testmask);
  203. testmask.focus();
  204. $("#testmask").val("123");
  205. assert.equal(testmask.value, "123", "Result " + testmask.value);
  206. });
  207. qunit.test("Intergroup selection - dhilt", function (assert) {
  208. var $fixture = $("#qunit-fixture");
  209. $fixture.append('<input type="text" id="testmask" />');
  210. var testmask = document.getElementById("testmask");
  211. Inputmask("dd/mm/yyyy").mask(testmask);
  212. testmask.focus();
  213. $("#testmask").Type("23314");
  214. $.caret(testmask, 4, 7);
  215. $("#testmask").SendKey("6");
  216. assert.equal(testmask.value, "23/03/2014", "Result " + testmask.value);
  217. });
  218. qunit.test("Intergroup selection - dhilt", function (assert) {
  219. var $fixture = $("#qunit-fixture");
  220. $fixture.append('<input type="text" id="testmask" />');
  221. var testmask = document.getElementById("testmask");
  222. Inputmask("dd/mm/yyyy").mask(testmask);
  223. testmask.focus();
  224. $("#testmask").Type("23314");
  225. $.caret(testmask, 4, 6);
  226. $("#testmask").SendKey("6");
  227. assert.equal(testmask.value, "23/06/y014", "Result " + testmask.value);
  228. });
  229. qunit.test("Delete selection with non-masks", function (assert) {
  230. var $fixture = $("#qunit-fixture");
  231. $fixture.append('<input type="text" id="testmask" />');
  232. var testmask = document.getElementById("testmask");
  233. Inputmask("(999)999-9999").mask(testmask);
  234. testmask.focus();
  235. $("#testmask").Type("9999999999");
  236. $.caret(testmask, 8, 11);
  237. $("#testmask").SendKey(Inputmask.keyCode.DELETE);
  238. assert.equal(testmask.value, "(999)999-99__", "Result " + testmask.value);
  239. });
  240. qunit.test("Selection and backspace also deletes previous - kenaku", function (assert) {
  241. var $fixture = $("#qunit-fixture");
  242. $fixture.append('<input type="text" id="testmask" />');
  243. var testmask = document.getElementById("testmask");
  244. Inputmask("999 99 99 999").mask(testmask);
  245. testmask.focus();
  246. $("#testmask").Type("1234567890");
  247. $.caret(testmask, 2, 3);
  248. $("#testmask").SendKey(Inputmask.keyCode.BACKSPACE);
  249. assert.equal(testmask.value, "124 56 78 90_", "Result " + testmask.value);
  250. });
  251. qunit.module("Non-greedy masks");
  252. qunit.test("inputmask(\"*\", { greedy: false, repeat: \"*\" }) - replace cd with 1", function (assert) {
  253. var $fixture = $("#qunit-fixture");
  254. $fixture.append('<input type="text" id="testmask" />');
  255. var testmask = document.getElementById("testmask");
  256. Inputmask("*", {
  257. greedy: false,
  258. repeat: "*"
  259. }).mask(testmask);
  260. testmask.focus();
  261. $("#testmask").Type("abcdef");
  262. $.caret(testmask, 2, 4);
  263. $("#testmask").SendKey("1");
  264. assert.equal(testmask.value, "ab1ef", "Result " + testmask.value);
  265. });
  266. qunit.test("inputmask(\"*\", { greedy: false, repeat: \"*\" }) - type abcdef", function (assert) {
  267. var $fixture = $("#qunit-fixture");
  268. $fixture.append('<input type="text" id="testmask" />');
  269. var testmask = document.getElementById("testmask");
  270. Inputmask("*", {
  271. greedy: false,
  272. repeat: "*"
  273. }).mask(testmask);
  274. testmask.focus();
  275. $("#testmask").Type("abcdef");
  276. assert.equal(testmask.value, "abcdef", "Result " + testmask.value);
  277. });
  278. qunit.test("inputmask(\"A.\", { repeat: \"*\" }) - type abc - joostburg", function (assert) {
  279. var $fixture = $("#qunit-fixture");
  280. $fixture.append('<input type="text" id="testmask" />');
  281. var testmask = document.getElementById("testmask");
  282. Inputmask("A.", {
  283. repeat: "*"
  284. }).mask(testmask);
  285. testmask.focus();
  286. $("#testmask").Type("abc");
  287. assert.equal(testmask.value, "A.B.C.", "Result " + testmask.value);
  288. });
  289. qunit.test("{ mask: \"A\", placeholder: \"\", repeat: 16 }) - type testtest - glosswordteam", function (assert) {
  290. var $fixture = $("#qunit-fixture");
  291. $fixture.append('<input type="text" id="testmask" />');
  292. var testmask = document.getElementById("testmask");
  293. Inputmask({
  294. mask: "A",
  295. placeholder: "",
  296. repeat: 16
  297. }).mask(testmask);
  298. testmask.focus();
  299. $("#testmask").Type("testtest");
  300. assert.equal(testmask.value, "TESTTEST", "Result " + testmask.value);
  301. });
  302. qunit.test("{ mask: \"A\", repeat: 16, greedy: false }) - type testtest - glosswordteam", function (assert) {
  303. var $fixture = $("#qunit-fixture");
  304. $fixture.append('<input type="text" id="testmask" />');
  305. var testmask = document.getElementById("testmask");
  306. Inputmask({
  307. mask: "A",
  308. repeat: 16,
  309. greedy: false
  310. }).mask(testmask);
  311. testmask.focus();
  312. $("#testmask").Type("testtest");
  313. assert.equal(testmask.value, "TESTTEST", "Result " + testmask.value);
  314. });
  315. qunit.module("greedy masks");
  316. qunit.test("inputmask(\"*\", { greedy: true, repeat: 10, clearMaskOnLostFocus: false })", function (assert) {
  317. var $fixture = $("#qunit-fixture");
  318. $fixture.append('<input type="text" id="testmask" />');
  319. var testmask = document.getElementById("testmask");
  320. Inputmask("*", {
  321. greedy: true,
  322. repeat: 10,
  323. clearMaskOnLostFocus: false
  324. }).mask(testmask);
  325. testmask.focus();
  326. assert.equal($("#testmask")[0].inputmask._valueGet(), "__________", "Result " + $("#testmask")[0].inputmask._valueGet());
  327. });
  328. qunit.test("inputmask(\"*\", { greedy: true, repeat: 10 }) - type 12345678901234567890", function (assert) {
  329. var $fixture = $("#qunit-fixture");
  330. $fixture.append('<input type="text" id="testmask" />');
  331. var testmask = document.getElementById("testmask");
  332. Inputmask("*", {
  333. greedy: true,
  334. repeat: 10
  335. }).mask(testmask);
  336. testmask.focus();
  337. $("#testmask").Type("12345678901234567890");
  338. assert.equal(testmask.value, "1234567890", "Result " + testmask.value);
  339. });
  340. qunit.test("inputmask(\"9,99\", { greedy: true, repeat: 5 }) - type 12345678901234567890", function (assert) {
  341. var $fixture = $("#qunit-fixture");
  342. $fixture.append('<input type="text" id="testmask" />');
  343. var testmask = document.getElementById("testmask");
  344. Inputmask("9,99", {
  345. greedy: true,
  346. repeat: 5
  347. }).mask(testmask);
  348. testmask.focus();
  349. $("#testmask").Type("12345678901234567890");
  350. assert.equal(testmask.value, "1,234,567,890,123,45", "Result " + testmask.value);
  351. });
  352. qunit.test("inputmask({ mask: \"9\", repeat: 10, placeholder: \"\", numericInput: true }) - greedy true with empty placeholder - type 12345", function (assert) {
  353. var $fixture = $("#qunit-fixture");
  354. $fixture.append('<input type="text" id="testmask" />');
  355. var testmask = document.getElementById("testmask");
  356. Inputmask({
  357. "mask": "9",
  358. repeat: 10,
  359. placeholder: "",
  360. numericInput: true
  361. }).mask(testmask);
  362. testmask.focus();
  363. $("#testmask").Type("12345");
  364. assert.equal(testmask.value, "12345", "Result " + testmask.value);
  365. });
  366. qunit.test("creditcard switch - pchelailya", function (assert) {
  367. var done = assert.async(),
  368. $fixture = $("#qunit-fixture");
  369. $fixture.append('<input type="text" id="testmask" />');
  370. var testmask = document.getElementById("testmask");
  371. Inputmask("9999 9999 9999 9999").mask(testmask);
  372. $("#testmask").on("keyup", function (event) {
  373. var value = this.inputmask.unmaskedvalue();
  374. if (value != null && value.length === 2 && value === "37") {
  375. Inputmask("9999 999999 99999").mask(this);
  376. }
  377. });
  378. testmask.focus();
  379. $("#testmask").Type("37");
  380. setTimeout(function () {
  381. $("#testmask").Type("12");
  382. assert.equal(testmask.value, "3712 ______ _____", "Result " + testmask.value);
  383. done();
  384. }, 0);
  385. });
  386. qunit.test("maskscache - same mask diff definitions - StonesEditeurs", function (assert) {
  387. var $fixture = $("#qunit-fixture");
  388. $fixture.append('<input type="text" id="testmask" />');
  389. var testmask = document.getElementById("testmask");
  390. Inputmask({
  391. mask: "Z{1,*}",
  392. definitions: {
  393. 'Z': {
  394. validator: function (chrs, buffer, pos, strict, opts) {
  395. return {
  396. pos: pos,
  397. c: 'A'
  398. };
  399. },
  400. }
  401. }
  402. }).mask(testmask);
  403. Inputmask({
  404. mask: "Z{1,*}", // <= Same mask
  405. definitions: {
  406. 'Z': {
  407. validator: function (chrs, buffer, pos, strict, opts) {
  408. return {
  409. pos: pos,
  410. c: 'B'
  411. }; // <= another definition
  412. },
  413. }
  414. }
  415. }).mask(testmask);
  416. $("#testmask").Type("abcdef");
  417. assert.equal(document.getElementById("testmask").inputmask._valueGet(), "BBBBBB", "Result " + document.getElementById("testmask").inputmask._valueGet());
  418. });
  419. qunit.test("autoUnmask not work in newest release #1109 - danilG", function (assert) {
  420. var $fixture = $("#qunit-fixture");
  421. $fixture.append('<input type="text" id="testmask" />');
  422. var testmask = document.getElementById("testmask");
  423. Inputmask({
  424. mask: "+7 999 999-99-99",
  425. autoUnmask: true
  426. }).mask(testmask);
  427. $(testmask).val("9226845186");
  428. //Let's get value exact immediate - this crack's
  429. $(testmask).val();
  430. $(testmask).trigger("mouseenter");
  431. assert.equal(document.getElementById("testmask").inputmask._valueGet(), "+7 922 684-51-86", "Result " + document.getElementById("testmask").inputmask._valueGet());
  432. });
  433. qunit.test("Title Case - Especially", function (assert) {
  434. var $fixture = $("#qunit-fixture");
  435. $fixture.append('<input type="text" id="testmask" />');
  436. var testmask = document.getElementById("testmask");
  437. Inputmask("p{1,10}", {
  438. definitions: {
  439. "p": {
  440. validator: "[A-Za-z\u0410-\u044F\u0401\u0451\u00C0-\u00FF\u00B5 ]",
  441. cardinality: 1,
  442. casing: "title" //auto uppercasing
  443. }
  444. },
  445. }).mask(testmask);
  446. $(testmask).val("title case");
  447. assert.equal(document.getElementById("testmask").inputmask._valueGet(), "Title Case", "Result " + document.getElementById("testmask").inputmask._valueGet());
  448. });
  449. qunit.test("Bug when typing after a fixed character #1299 - gayanj", function (assert) {
  450. var $fixture = $("#qunit-fixture");
  451. $fixture.append('<input type="text" id="testmask" />');
  452. var testmask = document.getElementById("testmask");
  453. Inputmask("AaaBAaaVaa").mask(testmask);
  454. testmask.focus();
  455. $.caret(testmask, 4);
  456. $("#testmask").Type("a");
  457. assert.equal(document.getElementById("testmask").inputmask._valueGet(), "___BA__V__", "Result " + document.getElementById("testmask").inputmask._valueGet());
  458. });
  459. };