Core_validate.spec.js 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122
  1. describe('Core_validate', () => {
  2. const id = 'testContainer';
  3. beforeEach(function() {
  4. this.$container = $(`<div id="${id}"></div>`).appendTo('body');
  5. });
  6. afterEach(function() {
  7. if (this.$container) {
  8. destroy();
  9. this.$container.remove();
  10. }
  11. });
  12. const arrayOfObjects = function() {
  13. return [
  14. { id: 1, name: 'Ted', lastName: 'Right' },
  15. { id: 2, name: 'Frank', lastName: 'Honest' },
  16. { id: 3, name: 'Joan', lastName: 'Well' },
  17. { id: 4, name: 'Sid', lastName: 'Strong' },
  18. { id: 5, name: 'Jane', lastName: 'Neat' },
  19. { id: 6, name: 'Chuck', lastName: 'Jackson' },
  20. { id: 7, name: 'Meg', lastName: 'Jansen' },
  21. { id: 8, name: 'Rob', lastName: 'Norris' },
  22. { id: 9, name: 'Sean', lastName: 'O\'Hara' },
  23. { id: 10, name: 'Eve', lastName: 'Branson' }
  24. ];
  25. };
  26. it('should call beforeValidate', () => {
  27. let fired = null;
  28. handsontable({
  29. data: arrayOfObjects(),
  30. columns: [
  31. { data: 'id', type: 'numeric' },
  32. { data: 'name' },
  33. { data: 'lastName' }
  34. ],
  35. beforeValidate() {
  36. fired = true;
  37. }
  38. });
  39. setDataAtCell(2, 0, 'test');
  40. expect(fired).toEqual(true);
  41. });
  42. it('should call beforeValidate when columns is a function', () => {
  43. let fired = null;
  44. handsontable({
  45. data: arrayOfObjects(),
  46. columns(column) {
  47. let colMeta = {};
  48. if (column === 0) {
  49. colMeta.data = 'id';
  50. colMeta.type = 'numeric';
  51. } else if (column === 1) {
  52. colMeta.data = 'name';
  53. } else if (column === 2) {
  54. colMeta.data = 'lastName';
  55. } else {
  56. colMeta = null;
  57. }
  58. return colMeta;
  59. },
  60. beforeValidate() {
  61. fired = true;
  62. }
  63. });
  64. setDataAtCell(2, 0, 'test');
  65. expect(fired).toBe(true);
  66. });
  67. it('should call afterValidate', (done) => {
  68. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  69. handsontable({
  70. data: arrayOfObjects(),
  71. columns: [
  72. { data: 'id', type: 'numeric' },
  73. { data: 'name' },
  74. { data: 'lastName' }
  75. ],
  76. afterValidate: onAfterValidate
  77. });
  78. setDataAtCell(2, 0, 'test');
  79. setTimeout(() => {
  80. expect(onAfterValidate.calls.count()).toBe(1);
  81. done();
  82. }, 200);
  83. });
  84. it('should call afterValidate when columns is a function', (done) => {
  85. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  86. handsontable({
  87. data: arrayOfObjects(),
  88. columns(column) {
  89. let colMeta = {};
  90. if (column === 0) {
  91. colMeta.data = 'id';
  92. colMeta.type = 'numeric';
  93. } else if (column === 1) {
  94. colMeta.data = 'name';
  95. } else if (column === 2) {
  96. colMeta.data = 'lastName';
  97. } else {
  98. colMeta = null;
  99. }
  100. return colMeta;
  101. },
  102. afterValidate: onAfterValidate
  103. });
  104. setDataAtCell(2, 0, 'test');
  105. setTimeout(() => {
  106. expect(onAfterValidate.calls.count()).toBe(1);
  107. done();
  108. }, 200);
  109. });
  110. it('beforeValidate can manipulate value', (done) => {
  111. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  112. let result = null;
  113. onAfterValidate.and.callFake((valid, value) => {
  114. result = value;
  115. });
  116. handsontable({
  117. data: arrayOfObjects(),
  118. columns: [
  119. { data: 'id', type: 'numeric' },
  120. { data: 'name' },
  121. { data: 'lastName' }
  122. ],
  123. beforeValidate() {
  124. return 999;
  125. },
  126. afterValidate: onAfterValidate
  127. });
  128. setDataAtCell(2, 0, 123);
  129. setTimeout(() => {
  130. expect(result).toBe(999);
  131. done();
  132. }, 200);
  133. });
  134. it('beforeValidate can manipulate value when columns is a function', (done) => {
  135. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  136. let result = null;
  137. onAfterValidate.and.callFake((valid, value) => {
  138. result = value;
  139. });
  140. handsontable({
  141. data: arrayOfObjects(),
  142. columns(column) {
  143. let colMeta = {};
  144. if (column === 0) {
  145. colMeta.data = 'id';
  146. colMeta.type = 'numeric';
  147. } else if (column === 1) {
  148. colMeta.data = 'name';
  149. } else if (column === 2) {
  150. colMeta.data = 'lastName';
  151. } else {
  152. colMeta = null;
  153. }
  154. return colMeta;
  155. },
  156. beforeValidate() {
  157. return 999;
  158. },
  159. afterValidate: onAfterValidate
  160. });
  161. setDataAtCell(2, 0, 123);
  162. setTimeout(() => {
  163. expect(result).toBe(999);
  164. done();
  165. }, 200);
  166. });
  167. it('should be able to define custom validator function', (done) => {
  168. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  169. handsontable({
  170. data: arrayOfObjects(),
  171. columns: [
  172. { data: 'id',
  173. validator(value, cb) {
  174. cb(true);
  175. } },
  176. { data: 'name' },
  177. { data: 'lastName' }
  178. ],
  179. afterValidate: onAfterValidate
  180. });
  181. setDataAtCell(2, 0, 123);
  182. setTimeout(() => {
  183. expect(onAfterValidate).toHaveBeenCalledWith(true, 123, 2, 'id', undefined, undefined);
  184. done();
  185. }, 200);
  186. });
  187. it('should be able to define custom validator function when columns is a function', (done) => {
  188. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  189. handsontable({
  190. data: arrayOfObjects(),
  191. columns(column) {
  192. let colMeta = null;
  193. if (column === 0) {
  194. colMeta = {
  195. data: 'id',
  196. validator(value, cb) {
  197. cb(true);
  198. }
  199. };
  200. } else if (column === 1) {
  201. colMeta = { data: 'name' };
  202. } else if (column === 2) {
  203. colMeta = { data: 'lastName' };
  204. }
  205. return colMeta;
  206. },
  207. afterValidate: onAfterValidate
  208. });
  209. setDataAtCell(2, 0, 123);
  210. setTimeout(() => {
  211. expect(onAfterValidate).toHaveBeenCalledWith(true, 123, 2, 'id', undefined, undefined);
  212. done();
  213. }, 200);
  214. });
  215. it('should be able to define custom validator RegExp', (done) => {
  216. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  217. handsontable({
  218. data: arrayOfObjects(),
  219. columns: [
  220. { data: 'id', validator: /^\d+$/ },
  221. { data: 'name' },
  222. { data: 'lastName' }
  223. ],
  224. afterValidate: onAfterValidate
  225. });
  226. setDataAtCell(2, 0, 'test');
  227. setTimeout(() => {
  228. expect(onAfterValidate).toHaveBeenCalledWith(false, 'test', 2, 'id', undefined, undefined);
  229. done();
  230. }, 200);
  231. });
  232. it('should be able to define custom validator RegExp when columns is a function', (done) => {
  233. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  234. handsontable({
  235. data: arrayOfObjects(),
  236. columns(column) {
  237. let colMeta = null;
  238. if (column === 0) {
  239. colMeta = { data: 'id', validator: /^\d+$/ };
  240. } else if (column === 1) {
  241. colMeta = { data: 'name' };
  242. } else if (column === 2) {
  243. colMeta = { data: 'lastName' };
  244. }
  245. return colMeta;
  246. },
  247. afterValidate: onAfterValidate
  248. });
  249. setDataAtCell(2, 0, 'test');
  250. setTimeout(() => {
  251. expect(onAfterValidate).toHaveBeenCalledWith(false, 'test', 2, 'id', undefined, undefined);
  252. done();
  253. }, 200);
  254. });
  255. it('this in validator should point to cellProperties', (done) => {
  256. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  257. let result = null;
  258. handsontable({
  259. data: arrayOfObjects(),
  260. columns: [
  261. {
  262. data: 'id',
  263. validator(value, cb) {
  264. result = this;
  265. cb(true);
  266. }
  267. },
  268. { data: 'name' },
  269. { data: 'lastName' }
  270. ],
  271. afterValidate: onAfterValidate
  272. });
  273. setDataAtCell(2, 0, 123);
  274. setTimeout(() => {
  275. expect(result.instance).toEqual(getInstance());
  276. done();
  277. }, 200);
  278. });
  279. it('this in validator should point to cellProperties when columns is a function', (done) => {
  280. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  281. let result = null;
  282. handsontable({
  283. data: arrayOfObjects(),
  284. columns(column) {
  285. let colMeta = null;
  286. if (column === 0) {
  287. colMeta = {
  288. data: 'id',
  289. validator(value, cb) {
  290. result = this;
  291. cb(true);
  292. }
  293. };
  294. } else if (column === 1) {
  295. colMeta = { data: 'name' };
  296. } else if (column === 2) {
  297. colMeta = { data: 'lastName' };
  298. }
  299. return colMeta;
  300. },
  301. afterValidate: onAfterValidate
  302. });
  303. setDataAtCell(2, 0, 123);
  304. setTimeout(() => {
  305. expect(result.instance).toEqual(getInstance());
  306. done();
  307. }, 200);
  308. });
  309. it('should not throw error after calling validateCells without first argument', (done) => {
  310. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  311. const hot = handsontable({
  312. data: Handsontable.helper.createSpreadsheetData(2, 2),
  313. validator(value, callb) {
  314. if (value === 'B1') {
  315. callb(false);
  316. } else {
  317. callb(true);
  318. }
  319. },
  320. afterValidate: onAfterValidate
  321. });
  322. expect(() => hot.validateCells()).not.toThrow();
  323. setTimeout(() => {
  324. expect(spec().$container.find('td.htInvalid').length).toEqual(1);
  325. expect(spec().$container.find('td:not(.htInvalid)').length).toEqual(3);
  326. done();
  327. }, 200);
  328. });
  329. it('should throw error after calling validateRows first argument not array', (done) => {
  330. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  331. const hot = handsontable({
  332. data: Handsontable.helper.createSpreadsheetData(2, 2),
  333. validator(value, callb) {
  334. callb(true);
  335. },
  336. afterValidate: onAfterValidate
  337. });
  338. expect(() => hot.validateRows()).toThrow();
  339. expect(() => hot.validateRows(0, () => {})).toThrow();
  340. expect(() => hot.validateRows({}, () => {})).toThrow();
  341. expect(() => hot.validateRows(() => {})).toThrow();
  342. done();
  343. });
  344. it('should throw error after calling validateColumns first argument not array', (done) => {
  345. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  346. const hot = handsontable({
  347. data: Handsontable.helper.createSpreadsheetData(2, 2),
  348. validator(value, callb) {
  349. callb(true);
  350. },
  351. afterValidate: onAfterValidate
  352. });
  353. expect(() => hot.validateColumns()).toThrow();
  354. expect(() => hot.validateColumns(0, () => {})).toThrow();
  355. expect(() => hot.validateColumns({}, () => {})).toThrow();
  356. expect(() => hot.validateColumns(() => {})).toThrow();
  357. done();
  358. });
  359. it('should not throw error after calling validateRows without second argument', (done) => {
  360. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  361. const hot = handsontable({
  362. data: Handsontable.helper.createSpreadsheetData(2, 2),
  363. validator(value, callb) {
  364. callb(true);
  365. },
  366. afterValidate: onAfterValidate
  367. });
  368. expect(() => hot.validateRows([])).not.toThrow();
  369. expect(() => hot.validateRows([0, 1])).not.toThrow();
  370. expect(() => hot.validateRows([100, 101])).not.toThrow();
  371. done();
  372. });
  373. it('should not throw error after calling validateColumns without second argument', (done) => {
  374. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  375. const hot = handsontable({
  376. data: Handsontable.helper.createSpreadsheetData(2, 2),
  377. validator(value, callb) {
  378. callb(true);
  379. },
  380. afterValidate: onAfterValidate
  381. });
  382. expect(() => hot.validateColumns([])).not.toThrow();
  383. expect(() => hot.validateColumns([0, 1])).not.toThrow();
  384. expect(() => hot.validateColumns([100, 101])).not.toThrow();
  385. done();
  386. });
  387. it('should add class name `htInvalid` to an cell that does not validate - on validateCells', (done) => {
  388. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  389. const hot = handsontable({
  390. data: Handsontable.helper.createSpreadsheetData(2, 2),
  391. validator(value, callb) {
  392. if (value === 'B1') {
  393. callb(false);
  394. } else {
  395. callb(true);
  396. }
  397. },
  398. afterValidate: onAfterValidate
  399. });
  400. hot.validateCells(() => {
  401. hot.render();
  402. });
  403. setTimeout(() => {
  404. expect(spec().$container.find('td.htInvalid').length).toEqual(1);
  405. expect(spec().$container.find('td:not(.htInvalid)').length).toEqual(3);
  406. done();
  407. }, 200);
  408. });
  409. it('should add class name `htInvalid` to an cell that does not validate - on validateRows', (done) => {
  410. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  411. const hot = handsontable({
  412. data: Handsontable.helper.createSpreadsheetData(2, 2),
  413. validator(value, callb) {
  414. if (value === 'B1') {
  415. callb(false);
  416. } else {
  417. callb(true);
  418. }
  419. },
  420. afterValidate: onAfterValidate
  421. });
  422. hot.validateRows([], () => {
  423. hot.render();
  424. });
  425. setTimeout(() => {
  426. expect(spec().$container.find('td.htInvalid').length).toEqual(0);
  427. expect(spec().$container.find('td:not(.htInvalid)').length).toEqual(4);
  428. hot.updateSettings({
  429. data: Handsontable.helper.createSpreadsheetData(2, 2)
  430. });
  431. hot.validateRows([0], () => {
  432. hot.render();
  433. });
  434. }, 100);
  435. setTimeout(() => {
  436. expect(spec().$container.find('td.htInvalid').length).toEqual(1);
  437. expect(spec().$container.find('td:not(.htInvalid)').length).toEqual(3);
  438. hot.updateSettings({
  439. data: Handsontable.helper.createSpreadsheetData(2, 2)
  440. });
  441. hot.validateRows([1], () => {
  442. hot.render();
  443. });
  444. }, 200);
  445. setTimeout(() => {
  446. expect(spec().$container.find('td.htInvalid').length).toEqual(0);
  447. expect(spec().$container.find('td:not(.htInvalid)').length).toEqual(4);
  448. hot.updateSettings({
  449. data: Handsontable.helper.createSpreadsheetData(2, 2)
  450. });
  451. hot.validateRows([0, 1], () => {
  452. hot.render();
  453. });
  454. }, 300);
  455. setTimeout(() => {
  456. expect(spec().$container.find('td.htInvalid').length).toEqual(1);
  457. expect(spec().$container.find('td:not(.htInvalid)').length).toEqual(3);
  458. hot.updateSettings({
  459. data: Handsontable.helper.createSpreadsheetData(2, 2)
  460. });
  461. hot.validateRows([0, 1, 100], () => {
  462. hot.render();
  463. });
  464. }, 400);
  465. setTimeout(() => {
  466. expect(spec().$container.find('td.htInvalid').length).toEqual(1);
  467. expect(spec().$container.find('td:not(.htInvalid)').length).toEqual(3);
  468. hot.updateSettings({
  469. data: Handsontable.helper.createSpreadsheetData(2, 2)
  470. });
  471. hot.validateRows([100, 101], () => {
  472. hot.render();
  473. });
  474. }, 500);
  475. setTimeout(() => {
  476. expect(spec().$container.find('td.htInvalid').length).toEqual(0);
  477. expect(spec().$container.find('td:not(.htInvalid)').length).toEqual(4);
  478. done();
  479. }, 600);
  480. });
  481. it('should add class name `htInvalid` to an cell that does not validate - on validateColumns', async() => {
  482. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  483. const hot = handsontable({
  484. data: Handsontable.helper.createSpreadsheetData(2, 2),
  485. validator(value, callb) {
  486. if (value === 'B1') {
  487. callb(false);
  488. } else {
  489. callb(true);
  490. }
  491. },
  492. afterValidate: onAfterValidate
  493. });
  494. await promisfy(resolve => hot.validateColumns([], resolve));
  495. expect(spec().$container.find('td.htInvalid').length).toEqual(0);
  496. expect(spec().$container.find('td:not(.htInvalid)').length).toEqual(4);
  497. hot.updateSettings({
  498. data: Handsontable.helper.createSpreadsheetData(2, 2)
  499. });
  500. await promisfy(resolve => hot.validateColumns([0], resolve));
  501. expect(spec().$container.find('td.htInvalid').length).toEqual(0);
  502. expect(spec().$container.find('td:not(.htInvalid)').length).toEqual(4);
  503. hot.updateSettings({
  504. data: Handsontable.helper.createSpreadsheetData(2, 2)
  505. });
  506. await promisfy(resolve => hot.validateColumns([1], resolve));
  507. expect(spec().$container.find('td.htInvalid').length).toEqual(1);
  508. expect(spec().$container.find('td:not(.htInvalid)').length).toEqual(3);
  509. hot.updateSettings({
  510. data: Handsontable.helper.createSpreadsheetData(2, 2)
  511. });
  512. await promisfy(resolve => hot.validateColumns([0, 1], resolve));
  513. expect(spec().$container.find('td.htInvalid').length).toEqual(1);
  514. expect(spec().$container.find('td:not(.htInvalid)').length).toEqual(3);
  515. hot.updateSettings({
  516. data: Handsontable.helper.createSpreadsheetData(2, 2)
  517. });
  518. await promisfy(resolve => hot.validateColumns([0, 1, 100], resolve));
  519. expect(spec().$container.find('td.htInvalid').length).toEqual(1);
  520. expect(spec().$container.find('td:not(.htInvalid)').length).toEqual(3);
  521. hot.updateSettings({
  522. data: Handsontable.helper.createSpreadsheetData(2, 2)
  523. });
  524. await promisfy(resolve => hot.validateColumns([100, 101], resolve));
  525. expect(spec().$container.find('td.htInvalid').length).toEqual(0);
  526. expect(spec().$container.find('td:not(.htInvalid)').length).toEqual(4);
  527. });
  528. it('should add class name `htInvalid` to an cell that does not validate - when we trigger validateCell', async() => {
  529. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  530. const hot = handsontable({
  531. data: Handsontable.helper.createSpreadsheetData(2, 2),
  532. validator(value, cb) {
  533. cb(false);
  534. },
  535. afterValidate: onAfterValidate
  536. });
  537. expect(spec().$container.find('td:not(.htInvalid)').length).toEqual(4);
  538. hot.validateCell(hot.getDataAtCell(1, 1), hot.getCellMeta(1, 1), () => {});
  539. await sleep(200);
  540. expect(spec().$container.find('td.htInvalid').length).toEqual(1);
  541. expect(spec().$container.find('td:not(.htInvalid)').length).toEqual(3);
  542. });
  543. it('should remove class name `htInvalid` from an cell that does validate - when we change validator rules', (done) => {
  544. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  545. let isValid = false;
  546. const validator = function() {
  547. return isValid;
  548. };
  549. const hot = handsontable({
  550. data: Handsontable.helper.createSpreadsheetData(2, 2),
  551. validator(value, cb) {
  552. cb(validator());
  553. },
  554. afterValidate: onAfterValidate
  555. });
  556. hot.validateCells(() => {});
  557. setTimeout(() => {
  558. expect(spec().$container.find('td.htInvalid').length).toEqual(4);
  559. expect(spec().$container.find('td:not(.htInvalid)').length).toEqual(0);
  560. isValid = true;
  561. onAfterValidate.calls.reset();
  562. hot.validateCell(hot.getDataAtCell(1, 1), hot.getCellMeta(1, 1), () => {});
  563. }, 200);
  564. setTimeout(() => {
  565. expect(spec().$container.find('td.htInvalid').length).toEqual(3);
  566. expect(spec().$container.find('td:not(.htInvalid)').length).toEqual(1);
  567. done();
  568. }, 400);
  569. });
  570. it('should add class name `htInvalid` to an cell that does not validate - on edit', (done) => {
  571. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  572. handsontable({
  573. data: Handsontable.helper.createSpreadsheetData(2, 2),
  574. validator(value, callb) {
  575. if (value === 'test') {
  576. callb(false);
  577. } else {
  578. callb(true);
  579. }
  580. },
  581. afterValidate: onAfterValidate
  582. });
  583. setDataAtCell(0, 0, 'test');
  584. setTimeout(() => {
  585. expect(spec().$container.find('td.htInvalid').length).toEqual(1);
  586. expect(spec().$container.find('tr:eq(0) td:eq(0)').hasClass('htInvalid')).toEqual(true);
  587. done();
  588. }, 200);
  589. });
  590. it('should add class name `htInvalid` to a cell without removing other classes', (done) => {
  591. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  592. const validator = jasmine.createSpy('validator');
  593. validator.and.callFake((value, callb) => {
  594. if (value === 123) {
  595. callb(false);
  596. } else {
  597. callb(true);
  598. }
  599. });
  600. handsontable({
  601. data: Handsontable.helper.createSpreadsheetData(2, 2),
  602. type: 'numeric',
  603. validator,
  604. afterValidate: onAfterValidate
  605. });
  606. setDataAtCell(0, 0, 123);
  607. setTimeout(() => {
  608. expect(validator.calls.count()).toEqual(1);
  609. expect(spec().$container.find('tr:eq(0) td:eq(0)').hasClass('htInvalid')).toEqual(true);
  610. expect(spec().$container.find('tr:eq(0) td:eq(0)').hasClass('htNumeric')).toEqual(true);
  611. onAfterValidate.calls.reset();
  612. setDataAtCell(0, 0, 124);
  613. }, 200);
  614. setTimeout(() => {
  615. expect(spec().$container.find('tr:eq(0) td:eq(0)').hasClass('htInvalid')).toEqual(false);
  616. expect(spec().$container.find('tr:eq(0) td:eq(0)').hasClass('htNumeric')).toEqual(true);
  617. done();
  618. }, 400);
  619. });
  620. it('should add class name `htInvalid` to an cell that does not validate - after validateCells', (done) => {
  621. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  622. const hot = handsontable({
  623. data: Handsontable.helper.createSpreadsheetData(2, 2),
  624. afterValidate: onAfterValidate
  625. });
  626. setDataAtCell(0, 0, 'test');
  627. setTimeout(() => {
  628. expect(spec().$container.find('td.htInvalid').length).toEqual(0);
  629. updateSettings({
  630. validator(value, callb) {
  631. if (value === 'test') {
  632. callb(false);
  633. } else {
  634. callb(true);
  635. }
  636. }
  637. });
  638. onAfterValidate.calls.reset();
  639. hot.validateCells(() => {});
  640. }, 200);
  641. setTimeout(() => {
  642. expect(spec().$container.find('td.htInvalid').length).toEqual(1);
  643. expect(spec().$container.find('tr:eq(0) td:eq(0)').hasClass('htInvalid')).toEqual(true);
  644. done();
  645. }, 400);
  646. });
  647. it('should add class name `htInvalid` to an cell that does not validate - after validateRows', (done) => {
  648. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  649. const hot = handsontable({
  650. data: Handsontable.helper.createSpreadsheetData(2, 2),
  651. afterValidate: onAfterValidate
  652. });
  653. setDataAtCell(0, 0, 'test');
  654. setTimeout(() => {
  655. expect(spec().$container.find('td.htInvalid').length).toEqual(0);
  656. updateSettings({
  657. validator(value, callb) {
  658. if (value === 'test') {
  659. callb(false);
  660. } else {
  661. callb(true);
  662. }
  663. }
  664. });
  665. onAfterValidate.calls.reset();
  666. hot.validateRows([0], () => {});
  667. }, 200);
  668. setTimeout(() => {
  669. expect(spec().$container.find('td.htInvalid').length).toEqual(1);
  670. expect(spec().$container.find('tr:eq(0) td:eq(0)').hasClass('htInvalid')).toEqual(true);
  671. done();
  672. }, 400);
  673. });
  674. it('should add class name `htInvalid` to an cell that does not validate - after validateColumns', (done) => {
  675. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  676. const hot = handsontable({
  677. data: Handsontable.helper.createSpreadsheetData(2, 2),
  678. afterValidate: onAfterValidate
  679. });
  680. setDataAtCell(0, 0, 'test');
  681. setTimeout(() => {
  682. expect(spec().$container.find('td.htInvalid').length).toEqual(0);
  683. updateSettings({
  684. validator(value, callb) {
  685. if (value === 'test') {
  686. callb(false);
  687. } else {
  688. callb(true);
  689. }
  690. }
  691. });
  692. onAfterValidate.calls.reset();
  693. hot.validateColumns([0], () => {});
  694. }, 200);
  695. setTimeout(() => {
  696. expect(spec().$container.find('td.htInvalid').length).toEqual(1);
  697. expect(spec().$container.find('tr:eq(0) td:eq(0)').hasClass('htInvalid')).toEqual(true);
  698. done();
  699. }, 400);
  700. });
  701. it('should remove class name `htInvalid` when cell is edited to validate', (done) => {
  702. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  703. const hot = handsontable({
  704. data: Handsontable.helper.createSpreadsheetData(2, 2),
  705. validator(value, callb) {
  706. if (value === 'A1') {
  707. callb(false);
  708. } else {
  709. callb(true);
  710. }
  711. },
  712. afterValidate: onAfterValidate
  713. });
  714. hot.validateCells(() => {
  715. hot.render();
  716. });
  717. setTimeout(() => {
  718. expect(spec().$container.find('tr:eq(0) td:eq(0)').hasClass('htInvalid')).toEqual(true);
  719. onAfterValidate.calls.reset();
  720. setDataAtCell(0, 0, 'test');
  721. }, 200);
  722. setTimeout(() => {
  723. expect(spec().$container.find('tr:eq(0) td:eq(0)').hasClass('htInvalid')).toEqual(false);
  724. done();
  725. }, 400);
  726. });
  727. it('should call callback with first argument as `true` if all cells are valid', (done) => {
  728. const onValidate = jasmine.createSpy('onValidate');
  729. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  730. const hot = handsontable({
  731. data: Handsontable.helper.createSpreadsheetData(2, 2),
  732. validator(value, callback) {
  733. callback(true);
  734. },
  735. afterValidate: onAfterValidate
  736. });
  737. hot.validateCells(onValidate);
  738. setTimeout(() => {
  739. expect(onValidate).toHaveBeenCalledWith(true);
  740. done();
  741. }, 200);
  742. });
  743. it('should call callback with first argument as `true` if all cells are valid - on validateRows', (done) => {
  744. const onValidate = jasmine.createSpy('onValidate');
  745. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  746. const hot = handsontable({
  747. data: Handsontable.helper.createSpreadsheetData(2, 2),
  748. validator(value, callback) {
  749. callback(true);
  750. },
  751. afterValidate: onAfterValidate
  752. });
  753. hot.validateRows([0, 1], onValidate);
  754. setTimeout(() => {
  755. expect(onValidate).toHaveBeenCalledWith(true);
  756. done();
  757. }, 200);
  758. });
  759. it('should call callback with first argument as `true` if all cells are valid - on validateColumns', (done) => {
  760. const onValidate = jasmine.createSpy('onValidate');
  761. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  762. const hot = handsontable({
  763. data: Handsontable.helper.createSpreadsheetData(2, 2),
  764. validator(value, callback) {
  765. callback(true);
  766. },
  767. afterValidate: onAfterValidate
  768. });
  769. hot.validateColumns([0, 1], onValidate);
  770. setTimeout(() => {
  771. expect(onValidate).toHaveBeenCalledWith(true);
  772. done();
  773. }, 200);
  774. });
  775. it('should call callback with first argument as `false` if one of cells is invalid', (done) => {
  776. const onValidate = jasmine.createSpy('onValidate');
  777. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  778. const hot = handsontable({
  779. data: Handsontable.helper.createSpreadsheetData(2, 2),
  780. validator(value, callback) {
  781. callback(false);
  782. },
  783. afterValidate: onAfterValidate
  784. });
  785. hot.validateCells(onValidate);
  786. setTimeout(() => {
  787. expect(onValidate).toHaveBeenCalledWith(false);
  788. done();
  789. }, 200);
  790. });
  791. it('should call callback with first argument as `false` if one of cells is invalid - on validateRows', (done) => {
  792. const onValidate = jasmine.createSpy('onValidate');
  793. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  794. const hot = handsontable({
  795. data: Handsontable.helper.createSpreadsheetData(2, 2),
  796. validator(value, callback) {
  797. callback(false);
  798. },
  799. afterValidate: onAfterValidate
  800. });
  801. hot.validateRows([0, 1], onValidate);
  802. setTimeout(() => {
  803. expect(onValidate).toHaveBeenCalledWith(false);
  804. done();
  805. }, 200);
  806. });
  807. it('should call callback with first argument as `false` if one of cells is invalid - on validateColumns', (done) => {
  808. const onValidate = jasmine.createSpy('onValidate');
  809. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  810. const hot = handsontable({
  811. data: Handsontable.helper.createSpreadsheetData(2, 2),
  812. validator(value, callback) {
  813. callback(false);
  814. },
  815. afterValidate: onAfterValidate
  816. });
  817. hot.validateColumns([0, 1], onValidate);
  818. setTimeout(() => {
  819. expect(onValidate).toHaveBeenCalledWith(false);
  820. done();
  821. }, 200);
  822. });
  823. it('should not allow for changes where data is invalid (multiple changes, async)', (done) => {
  824. let validatedChanges;
  825. handsontable({
  826. data: Handsontable.helper.createSpreadsheetData(5, 2),
  827. allowInvalid: false,
  828. validator(value, callb) {
  829. setTimeout(() => {
  830. if (value === 'fail') {
  831. callb(false);
  832. } else {
  833. callb(true);
  834. }
  835. }, 10);
  836. },
  837. afterChange(changes, source) {
  838. if (source !== 'loadData') {
  839. validatedChanges = changes;
  840. }
  841. }
  842. });
  843. populateFromArray(0, 0, [
  844. ['A1-new'],
  845. ['fail'],
  846. ['A3-new']
  847. ]);
  848. setTimeout(() => {
  849. expect(validatedChanges.length).toEqual(2);
  850. expect(validatedChanges[0]).toEqual([0, 0, 'A1', 'A1-new']);
  851. expect(validatedChanges[1]).toEqual([2, 0, 'A3', 'A3-new']);
  852. expect(getDataAtCell(0, 0)).toEqual('A1-new');
  853. expect(getDataAtCell(1, 0)).toEqual('A2');
  854. expect(getDataAtCell(2, 0)).toEqual('A3-new');
  855. expect(getCellMeta(0, 0).valid).toBe(true);
  856. expect(getCellMeta(1, 0).valid).toBe(true);
  857. expect(getCellMeta(2, 0).valid).toBe(true);
  858. done();
  859. }, 200);
  860. });
  861. it('should call beforeChange exactly once after cell value edit and validator is synchronous', (done) => {
  862. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  863. const onBeforeChange = jasmine.createSpy('onBeforeChange');
  864. const hot = handsontable({
  865. data: Handsontable.helper.createSpreadsheetData(5, 2),
  866. allowInvalid: false,
  867. validator(value, callback) {
  868. callback(true);
  869. },
  870. beforeChange: onBeforeChange,
  871. afterValidate: onAfterValidate
  872. });
  873. expect(onBeforeChange.calls.count()).toEqual(0);
  874. hot.setDataAtCell(0, 0, 10);
  875. setTimeout(() => {
  876. expect(onBeforeChange.calls.count()).toEqual(1);
  877. done();
  878. }, 200);
  879. });
  880. it('should call beforeChange exactly once after cell value edit and validator is asynchronous', (done) => {
  881. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  882. const onBeforeChange = jasmine.createSpy('onBeforeChange');
  883. const hot = handsontable({
  884. data: Handsontable.helper.createSpreadsheetData(5, 2),
  885. allowInvalid: false,
  886. validator(value, callback) {
  887. setTimeout(() => {
  888. callback(true);
  889. }, 10);
  890. },
  891. beforeChange: onBeforeChange,
  892. afterValidate: onAfterValidate
  893. });
  894. expect(onBeforeChange.calls.count()).toEqual(0);
  895. hot.setDataAtCell(0, 0, 10);
  896. setTimeout(() => {
  897. expect(onBeforeChange.calls.count()).toEqual(1);
  898. done();
  899. }, 200);
  900. });
  901. it('should call afterChange exactly once after cell value edit and validator is synchronous', (done) => {
  902. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  903. const onAfterChange = jasmine.createSpy('onAfterChange');
  904. const hot = handsontable({
  905. data: Handsontable.helper.createSpreadsheetData(5, 2),
  906. allowInvalid: false,
  907. validator(value, callback) {
  908. callback(true);
  909. },
  910. afterChange: onAfterChange,
  911. afterValidate: onAfterValidate
  912. });
  913. expect(onAfterChange.calls.count()).toEqual(1); // loadData
  914. hot.setDataAtCell(0, 0, 10);
  915. setTimeout(() => {
  916. expect(onAfterChange.calls.count()).toEqual(2);
  917. done();
  918. }, 200);
  919. });
  920. it('should call afterChange exactly once after cell value edit and validator is asynchronous', (done) => {
  921. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  922. const onAfterChange = jasmine.createSpy('onAfterChange');
  923. const hot = handsontable({
  924. data: Handsontable.helper.createSpreadsheetData(5, 2),
  925. allowInvalid: false,
  926. validator(value, callback) {
  927. setTimeout(() => {
  928. callback(true);
  929. }, 10);
  930. },
  931. afterChange: onAfterChange,
  932. afterValidate: onAfterValidate
  933. });
  934. expect(onAfterChange.calls.count()).toEqual(1); // loadData
  935. hot.setDataAtCell(0, 0, 10);
  936. setTimeout(() => {
  937. expect(onAfterChange.calls.count()).toEqual(2);
  938. done();
  939. }, 200);
  940. });
  941. it('edited cell should stay on screen until value is validated', (done) => {
  942. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  943. const onAfterChange = jasmine.createSpy('onAfterChange');
  944. let isEditorVisibleBeforeChange;
  945. let isEditorVisibleAfterChange;
  946. onAfterValidate.and.callFake(() => {
  947. isEditorVisibleBeforeChange = isEditorVisible();
  948. });
  949. onAfterChange.and.callFake(() => {
  950. isEditorVisibleAfterChange = isEditorVisible();
  951. });
  952. handsontable({
  953. data: Handsontable.helper.createSpreadsheetData(5, 2),
  954. allowInvalid: false,
  955. afterValidate: onAfterValidate,
  956. afterChange: onAfterChange,
  957. validator(value, callback) {
  958. setTimeout(() => {
  959. callback(true);
  960. }, 100);
  961. }
  962. });
  963. selectCell(0, 0);
  964. keyDown('enter');
  965. document.activeElement.value = 'Ted';
  966. onAfterValidate.calls.reset();
  967. onAfterChange.calls.reset();
  968. keyDown('enter');
  969. expect(document.activeElement.nodeName).toEqual('TEXTAREA');
  970. setTimeout(() => {
  971. expect(isEditorVisibleBeforeChange).toBe(true);
  972. expect(isEditorVisibleAfterChange).toBe(true);
  973. expect(isEditorVisible()).toBe(false);
  974. done();
  975. }, 200);
  976. });
  977. it('should validate edited cell after selecting another cell', async() => {
  978. let validatedValue;
  979. handsontable({
  980. data: Handsontable.helper.createSpreadsheetData(5, 2),
  981. allowInvalid: false,
  982. validator(value, callback) {
  983. setTimeout(() => {
  984. validatedValue = value;
  985. callback(true);
  986. }, 50);
  987. }
  988. });
  989. selectCell(0, 0);
  990. keyDown('enter');
  991. document.activeElement.value = 'Ted';
  992. selectCell(0, 1);
  993. await sleep(150);
  994. expect(validatedValue).toEqual('Ted');
  995. });
  996. it('should leave the new value in editor if it does not validate (async validation), after hitting ENTER', (done) => {
  997. let validationResult;
  998. handsontable({
  999. data: Handsontable.helper.createSpreadsheetData(5, 2),
  1000. allowInvalid: false,
  1001. validator(value, callback) {
  1002. setTimeout(() => {
  1003. validationResult = value.length === 2;
  1004. callback(validationResult);
  1005. }, 100);
  1006. }
  1007. });
  1008. selectCell(0, 0);
  1009. keyDown('enter');
  1010. document.activeElement.value = 'Ted';
  1011. keyDown('enter');
  1012. setTimeout(() => {
  1013. expect(validationResult).toBe(false);
  1014. expect(document.activeElement.value).toEqual('Ted');
  1015. done();
  1016. }, 200);
  1017. });
  1018. it('should leave the new value in editor if it does not validate (sync validation), after hitting ENTER', (done) => {
  1019. let validationResult;
  1020. handsontable({
  1021. data: Handsontable.helper.createSpreadsheetData(5, 2),
  1022. allowInvalid: false,
  1023. validator(value, callback) {
  1024. validationResult = value.length === 2;
  1025. callback(validationResult);
  1026. }
  1027. });
  1028. selectCell(0, 0);
  1029. keyDown('enter');
  1030. document.activeElement.value = 'Ted';
  1031. keyDown('enter');
  1032. setTimeout(() => {
  1033. expect(validationResult).toBe(false);
  1034. expect(document.activeElement.value).toEqual('Ted');
  1035. done();
  1036. }, 200);
  1037. });
  1038. it('should leave the new value in editor if it does not validate (async validation), after selecting another cell', (done) => {
  1039. let validationResult;
  1040. handsontable({
  1041. data: Handsontable.helper.createSpreadsheetData(5, 2),
  1042. allowInvalid: false,
  1043. validator(value, callback) {
  1044. setTimeout(() => {
  1045. validationResult = value.length === 2;
  1046. callback(validationResult);
  1047. }, 100);
  1048. }
  1049. });
  1050. selectCell(0, 0);
  1051. keyDown('enter');
  1052. document.activeElement.value = 'Ted';
  1053. selectCell(1, 0);
  1054. setTimeout(() => {
  1055. expect(validationResult).toBe(false);
  1056. expect(document.activeElement.value).toEqual('Ted');
  1057. done();
  1058. }, 200);
  1059. });
  1060. it('should leave the new value in editor if it does not validate (sync validation), after selecting another cell', (done) => {
  1061. let validationResult;
  1062. handsontable({
  1063. data: Handsontable.helper.createSpreadsheetData(5, 2),
  1064. allowInvalid: false,
  1065. validator(value, callback) {
  1066. validationResult = value.length === 2;
  1067. callback(validationResult);
  1068. }
  1069. });
  1070. selectCell(0, 0);
  1071. keyDown('enter');
  1072. document.activeElement.value = 'Ted';
  1073. selectCell(1, 0);
  1074. setTimeout(() => {
  1075. expect(validationResult).toBe(false);
  1076. expect(document.activeElement.value).toEqual('Ted');
  1077. done();
  1078. }, 200);
  1079. });
  1080. it('should remove htInvalid class properly after cancelling change, when physical indexes are not equal to visual indexes', (done) => {
  1081. handsontable({
  1082. data: Handsontable.helper.createSpreadsheetData(5, 2),
  1083. columnSorting: {
  1084. column: 0,
  1085. sortOrder: 'desc'
  1086. },
  1087. allowInvalid: false,
  1088. validator(value, callback) {
  1089. setTimeout(() => {
  1090. callback(value.length === 2);
  1091. }, 100);
  1092. }
  1093. });
  1094. selectCell(0, 0);
  1095. keyDown('enter');
  1096. document.activeElement.value = 'Ted';
  1097. keyDown('enter');
  1098. setTimeout(() => {
  1099. const $cell = $(getCell(0, 0));
  1100. expect($cell.hasClass('htInvalid')).toEqual(false);
  1101. done();
  1102. }, 200);
  1103. });
  1104. it('should not attempt to remove the htInvalid class if the validated cell is no longer rendered', (done) => {
  1105. handsontable({
  1106. data: Handsontable.helper.createSpreadsheetData(20, 2),
  1107. columnSorting: {
  1108. column: 0,
  1109. sortOrder: 'desc'
  1110. },
  1111. allowInvalid: false,
  1112. validator(value, callback) {
  1113. setTimeout(() => {
  1114. callback(value.length === 2);
  1115. }, 100);
  1116. },
  1117. height: 40
  1118. });
  1119. selectCell(0, 0);
  1120. keyDown('enter');
  1121. document.activeElement.value = 'Ted';
  1122. selectCell(19, 0);
  1123. setTimeout(() => {
  1124. const $cell = $(getCell(0, 0));
  1125. expect($cell.hasClass('htInvalid')).toEqual(false);
  1126. done();
  1127. }, 200);
  1128. });
  1129. it('should close the editor and save the new value if validation fails and allowInvalid is set to "true"', (done) => {
  1130. let validationResult;
  1131. handsontable({
  1132. data: Handsontable.helper.createSpreadsheetData(5, 2),
  1133. allowInvalid: true,
  1134. validator(value, callback) {
  1135. setTimeout(() => {
  1136. validationResult = value.length === 2;
  1137. callback(validationResult);
  1138. }, 100);
  1139. }
  1140. });
  1141. selectCell(0, 0);
  1142. keyDown('enter');
  1143. document.activeElement.value = 'Ted';
  1144. selectCell(1, 0);
  1145. setTimeout(() => {
  1146. expect(validationResult).toBe(false);
  1147. expect(getDataAtCell(0, 0)).toEqual('Ted');
  1148. expect(getCell(0, 0).className).toMatch(/htInvalid/);
  1149. done();
  1150. }, 200);
  1151. });
  1152. it('should close the editor and save the new value after double clicking on a cell, if the previously edited cell validated correctly', async() => {
  1153. let validationResult;
  1154. handsontable({
  1155. data: Handsontable.helper.createSpreadsheetData(5, 2),
  1156. allowInvalid: false,
  1157. validator(value, callback) {
  1158. setTimeout(() => {
  1159. validationResult = value.length === 2;
  1160. callback(validationResult);
  1161. }, 100);
  1162. }
  1163. });
  1164. selectCell(0, 0);
  1165. keyDown('enter');
  1166. expect(isEditorVisible()).toBe(true);
  1167. document.activeElement.value = 'AA';
  1168. expect(document.activeElement.value).toEqual('AA');
  1169. const cell = $(getCell(1, 0));
  1170. await sleep();
  1171. mouseDown(cell);
  1172. mouseUp(cell);
  1173. await sleep(100);
  1174. mouseDown(cell);
  1175. mouseUp(cell);
  1176. await sleep(200);
  1177. expect(isEditorVisible()).toBe(false);
  1178. expect(validationResult).toBe(true);
  1179. expect(getDataAtCell(0, 0)).toEqual('AA');
  1180. });
  1181. it('should close the editor and restore the original value after double clicking on a cell, if the previously edited cell have not validated', (done) => {
  1182. let validationResult;
  1183. handsontable({
  1184. data: Handsontable.helper.createSpreadsheetData(5, 2),
  1185. allowInvalid: false,
  1186. validator(value, callback) {
  1187. setTimeout(() => {
  1188. validationResult = value.length === 2;
  1189. callback(validationResult);
  1190. }, 100);
  1191. }
  1192. });
  1193. selectCell(0, 0);
  1194. keyDown('enter');
  1195. document.activeElement.value = 'AAA';
  1196. expect(document.activeElement.value).toEqual('AAA');
  1197. const cell = $(getCell(1, 0));
  1198. setTimeout(() => {
  1199. mouseDown(cell);
  1200. mouseUp(cell);
  1201. }, 0);
  1202. setTimeout(() => {
  1203. mouseDown(cell);
  1204. mouseUp(cell);
  1205. }, 100);
  1206. setTimeout(() => {
  1207. expect(validationResult).toBe(false);
  1208. expect(getDataAtCell(0, 0)).toEqual('A1');
  1209. done();
  1210. }, 300);
  1211. });
  1212. it('should listen to key changes after cell is corrected (allowInvalid: false)', async() => {
  1213. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  1214. handsontable({
  1215. data: arrayOfObjects(),
  1216. allowInvalid: false,
  1217. columns: [
  1218. {
  1219. data: 'id',
  1220. type: 'numeric',
  1221. validator(val, cb) {
  1222. cb(parseInt(val, 10) > 100);
  1223. }
  1224. },
  1225. { data: 'name' },
  1226. { data: 'lastName' }
  1227. ],
  1228. afterValidate: onAfterValidate
  1229. });
  1230. selectCell(2, 0);
  1231. keyDownUp('enter');
  1232. document.activeElement.value = '99';
  1233. onAfterValidate.calls.reset();
  1234. keyDownUp('enter'); // should be ignored
  1235. await sleep(200);
  1236. expect(isEditorVisible()).toBe(true);
  1237. document.activeElement.value = '999';
  1238. onAfterValidate.calls.reset();
  1239. keyDownUp('enter'); // should be accepted
  1240. await sleep(200);
  1241. expect(isEditorVisible()).toBe(false);
  1242. expect(getSelected()).toEqual([[3, 0, 3, 0]]);
  1243. keyDownUp('arrow_up');
  1244. expect(getSelected()).toEqual([[2, 0, 2, 0]]);
  1245. });
  1246. it('should allow keyboard movement when cell is being validated (move DOWN)', async() => {
  1247. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  1248. handsontable({
  1249. data: arrayOfObjects(),
  1250. allowInvalid: false,
  1251. columns: [
  1252. { data: 'id',
  1253. type: 'numeric',
  1254. validator(val, cb) {
  1255. setTimeout(() => {
  1256. cb(parseInt(val, 10) > 100);
  1257. }, 100);
  1258. } },
  1259. { data: 'name' },
  1260. { data: 'lastName' }
  1261. ],
  1262. afterValidate: onAfterValidate
  1263. });
  1264. selectCell(2, 0);
  1265. keyDownUp('enter');
  1266. document.activeElement.value = '999';
  1267. keyDownUp('enter');
  1268. expect(getSelected()).toEqual([[3, 0, 3, 0]]);
  1269. keyDownUp('arrow_down');
  1270. keyDownUp('arrow_down');
  1271. expect(isEditorVisible()).toBe(true);
  1272. expect(getSelected()).toEqual([[5, 0, 5, 0]]);
  1273. await sleep(200);
  1274. expect(isEditorVisible()).toBe(false);
  1275. expect(getSelected()).toEqual([[5, 0, 5, 0]]); // only enterMove and first arrow_down is performed
  1276. });
  1277. it('should not allow keyboard movement until cell is validated (move UP)', (done) => {
  1278. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  1279. handsontable({
  1280. data: arrayOfObjects(),
  1281. allowInvalid: false,
  1282. columns: [
  1283. { data: 'id',
  1284. type: 'numeric',
  1285. validator(val, cb) {
  1286. setTimeout(() => {
  1287. cb(parseInt(val, 10) > 100);
  1288. }, 100);
  1289. } },
  1290. { data: 'name' },
  1291. { data: 'lastName' }
  1292. ],
  1293. afterValidate: onAfterValidate
  1294. });
  1295. selectCell(2, 0);
  1296. keyDownUp('enter');
  1297. document.activeElement.value = '999';
  1298. keyDownUp('enter');
  1299. expect(getSelected()).toEqual([[3, 0, 3, 0]]);
  1300. keyDownUp('arrow_up');
  1301. keyDownUp('arrow_up');
  1302. expect(isEditorVisible()).toBe(true);
  1303. expect(getSelected()).toEqual([[1, 0, 1, 0]]);
  1304. setTimeout(() => {
  1305. expect(isEditorVisible()).toBe(false);
  1306. expect(getSelected()).toEqual([[1, 0, 1, 0]]);
  1307. done();
  1308. }, 200);
  1309. });
  1310. it('should not allow keyboard movement until cell is validated (move RIGHT)', (done) => {
  1311. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  1312. handsontable({
  1313. data: arrayOfObjects(),
  1314. allowInvalid: false,
  1315. columns: [
  1316. { data: 'id',
  1317. type: 'numeric',
  1318. validator(val, cb) {
  1319. setTimeout(() => {
  1320. cb(parseInt(val, 10) > 100);
  1321. }, 100);
  1322. } },
  1323. { data: 'name' },
  1324. { data: 'lastName' }
  1325. ],
  1326. afterValidate: onAfterValidate
  1327. });
  1328. selectCell(2, 0);
  1329. keyDownUp('enter');
  1330. document.activeElement.value = '999';
  1331. keyDownUp('enter'); // should be accepted but only after 100 ms
  1332. expect(getSelected()).toEqual([[3, 0, 3, 0]]);
  1333. keyDownUp('arrow_right');
  1334. keyDownUp('arrow_right');
  1335. expect(isEditorVisible()).toBe(true);
  1336. expect(getSelected()).toEqual([[3, 2, 3, 2]]);
  1337. setTimeout(() => {
  1338. expect(isEditorVisible()).toBe(false);
  1339. expect(getSelected()).toEqual([[3, 2, 3, 2]]);
  1340. done();
  1341. }, 200);
  1342. });
  1343. it('should not allow keyboard movement until cell is validated (move LEFT)', async() => {
  1344. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  1345. hot = handsontable({
  1346. data: arrayOfObjects(),
  1347. allowInvalid: false,
  1348. columns: [
  1349. { data: 'name' },
  1350. { data: 'lastName' },
  1351. { data: 'id',
  1352. type: 'numeric',
  1353. validator(val, cb) {
  1354. setTimeout(() => {
  1355. cb(parseInt(val, 10) > 100);
  1356. }, 100);
  1357. } }
  1358. ],
  1359. afterValidate: onAfterValidate
  1360. });
  1361. selectCell(2, 2);
  1362. keyDownUp('enter');
  1363. document.activeElement.value = '999';
  1364. keyDownUp('enter'); // should be accepted but only after 100 ms
  1365. expect(getSelected()).toEqual([[3, 2, 3, 2]]);
  1366. spec().$container.simulate('keydown', { keyCode: Handsontable.helper.KEY_CODES.ARROW_LEFT });
  1367. spec().$container.simulate('keyup', { keyCode: Handsontable.helper.KEY_CODES.ARROW_LEFT });
  1368. spec().$container.simulate('keydown', { keyCode: Handsontable.helper.KEY_CODES.ARROW_LEFT });
  1369. spec().$container.simulate('keyup', { keyCode: Handsontable.helper.KEY_CODES.ARROW_LEFT });
  1370. expect(isEditorVisible()).toBe(true);
  1371. expect(getSelected()).toEqual([[3, 0, 3, 0]]);
  1372. await sleep(200);
  1373. expect(isEditorVisible()).toBe(false);
  1374. expect(getSelected()).toEqual([[3, 0, 3, 0]]);
  1375. });
  1376. it('should not validate cell if editing has been canceled', (done) => {
  1377. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  1378. handsontable({
  1379. data: arrayOfObjects(),
  1380. columns: [
  1381. { data: 'id' },
  1382. { data: 'name' },
  1383. { data: 'lastName' }
  1384. ],
  1385. afterValidate: onAfterValidate
  1386. });
  1387. selectCell(0, 0);
  1388. keyDownUp(Handsontable.helper.KEY_CODES.ENTER); // open editor
  1389. keyDownUp(Handsontable.helper.KEY_CODES.ESCAPE); // cancel editing
  1390. setTimeout(() => {
  1391. expect(onAfterValidate).not.toHaveBeenCalled();
  1392. done();
  1393. }, 100);
  1394. });
  1395. it('should not validate cell if editing has been canceled when columns is a function', (done) => {
  1396. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  1397. handsontable({
  1398. data: arrayOfObjects(),
  1399. columns(column) {
  1400. let colMeta = null;
  1401. if (column === 0) {
  1402. colMeta = { data: 'id' };
  1403. } else if (column === 1) {
  1404. colMeta = { data: 'name' };
  1405. } else if (column === 2) {
  1406. colMeta = { data: 'lastName' };
  1407. }
  1408. return colMeta;
  1409. },
  1410. afterValidate: onAfterValidate
  1411. });
  1412. selectCell(0, 0);
  1413. keyDownUp(Handsontable.helper.KEY_CODES.ENTER); // open editor
  1414. keyDownUp(Handsontable.helper.KEY_CODES.ESCAPE); // cancel editing
  1415. setTimeout(() => {
  1416. expect(onAfterValidate).not.toHaveBeenCalled();
  1417. done();
  1418. }, 100);
  1419. });
  1420. it('should leave cell invalid if editing has been canceled', (done) => {
  1421. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  1422. handsontable({
  1423. data: arrayOfObjects(),
  1424. columns: [
  1425. { data: 'id',
  1426. validator(value, cb) {
  1427. cb(false);
  1428. } },
  1429. { data: 'name' },
  1430. { data: 'lastName' }
  1431. ],
  1432. afterValidate: onAfterValidate
  1433. });
  1434. setDataAtCell(0, 0, 'foo');
  1435. setTimeout(() => {
  1436. expect(getCellMeta(0, 0).valid).toBe(false);
  1437. selectCell(0, 0);
  1438. keyDownUp(Handsontable.helper.KEY_CODES.ENTER); // open editor
  1439. keyDownUp(Handsontable.helper.KEY_CODES.ESCAPE); // cancel editing
  1440. expect(getCellMeta(0, 0).valid).toBe(false);
  1441. done();
  1442. }, 200);
  1443. });
  1444. it('should leave cell invalid if editing has been canceled when columns is a function', (done) => {
  1445. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  1446. handsontable({
  1447. data: arrayOfObjects(),
  1448. columns(column) {
  1449. let colMeta = null;
  1450. if (column === 0) {
  1451. colMeta = {
  1452. data: 'id',
  1453. validator(value, cb) {
  1454. cb(false);
  1455. }
  1456. };
  1457. } else if (column === 1) {
  1458. colMeta = { data: 'name' };
  1459. } else if (column === 2) {
  1460. colMeta = { data: 'lastName' };
  1461. }
  1462. return colMeta;
  1463. },
  1464. afterValidate: onAfterValidate
  1465. });
  1466. setDataAtCell(0, 0, 'foo');
  1467. setTimeout(() => {
  1468. expect(getCellMeta(0, 0).valid).toBe(false);
  1469. selectCell(0, 0);
  1470. keyDownUp(Handsontable.helper.KEY_CODES.ENTER); // open editor
  1471. keyDownUp(Handsontable.helper.KEY_CODES.ESCAPE); // cancel editing
  1472. expect(getCellMeta(0, 0).valid).toBe(false);
  1473. done();
  1474. }, 200);
  1475. });
  1476. it('should open an appropriate editor after cell value is valid again', (done) => {
  1477. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  1478. const hot = handsontable({
  1479. data: arrayOfObjects(),
  1480. columns: [
  1481. {
  1482. data: 'id',
  1483. validator(value, cb) {
  1484. // eslint-disable-next-line
  1485. cb(value == parseInt(value, 10));
  1486. },
  1487. allowInvalid: false
  1488. },
  1489. { data: 'name' },
  1490. { data: 'lastName' }
  1491. ],
  1492. afterValidate: onAfterValidate
  1493. });
  1494. selectCell(0, 0);
  1495. let activeEditor = hot.getActiveEditor();
  1496. expect(activeEditor.row).toEqual(0);
  1497. expect(activeEditor.col).toEqual(0);
  1498. keyDownUp(Handsontable.helper.KEY_CODES.ENTER); // open editor
  1499. activeEditor.setValue('foo');
  1500. keyDownUp(Handsontable.helper.KEY_CODES.ENTER); // save changes, close editor
  1501. setTimeout(() => {
  1502. onAfterValidate.calls.reset();
  1503. activeEditor = hot.getActiveEditor();
  1504. expect(activeEditor.isOpened()).toBe(true); // value is invalid, so editor stays opened
  1505. expect(activeEditor.row).toEqual(0);
  1506. expect(activeEditor.col).toEqual(0);
  1507. activeEditor.setValue(2);
  1508. keyDownUp(Handsontable.helper.KEY_CODES.ENTER); // save changes and move to cell below (row: 1, col: ś0)
  1509. }, 200);
  1510. setTimeout(() => {
  1511. keyDownUp(Handsontable.helper.KEY_CODES.ENTER); // open editor
  1512. activeEditor = hot.getActiveEditor();
  1513. expect(activeEditor.row).toEqual(1);
  1514. expect(activeEditor.col).toEqual(0);
  1515. done();
  1516. }, 400);
  1517. });
  1518. it('should open an appropriate editor after cell value is valid again when columns is a function', (done) => {
  1519. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  1520. const hot = handsontable({
  1521. data: arrayOfObjects(),
  1522. columns(column) {
  1523. let colMeta = null;
  1524. if (column === 0) {
  1525. colMeta = {
  1526. data: 'id',
  1527. validator(value, cb) {
  1528. // eslint-disable-next-line
  1529. cb(value == parseInt(value, 10));
  1530. },
  1531. allowInvalid: false
  1532. };
  1533. } else if (column === 1) {
  1534. colMeta = { data: 'name' };
  1535. } else if (column === 2) {
  1536. colMeta = { data: 'lastName' };
  1537. }
  1538. return colMeta;
  1539. },
  1540. afterValidate: onAfterValidate
  1541. });
  1542. selectCell(0, 0);
  1543. let activeEditor = hot.getActiveEditor();
  1544. expect(activeEditor.row).toEqual(0);
  1545. expect(activeEditor.col).toEqual(0);
  1546. keyDownUp(Handsontable.helper.KEY_CODES.ENTER); // open editor
  1547. activeEditor.setValue('foo');
  1548. keyDownUp(Handsontable.helper.KEY_CODES.ENTER); // save changes, close editor
  1549. setTimeout(() => {
  1550. onAfterValidate.calls.reset();
  1551. activeEditor = hot.getActiveEditor();
  1552. expect(activeEditor.isOpened()).toBe(true); // value is invalid, so editor stays opened
  1553. expect(activeEditor.row).toEqual(0);
  1554. expect(activeEditor.col).toEqual(0);
  1555. activeEditor.setValue(2);
  1556. keyDownUp(Handsontable.helper.KEY_CODES.ENTER); // save changes and move to cell below (row: 1, col: ś0)
  1557. }, 200);
  1558. setTimeout(() => {
  1559. keyDownUp(Handsontable.helper.KEY_CODES.ENTER); // open editor
  1560. activeEditor = hot.getActiveEditor();
  1561. expect(activeEditor.row).toEqual(1);
  1562. expect(activeEditor.col).toEqual(0);
  1563. done();
  1564. }, 400);
  1565. });
  1566. it('should call the validation callback only once, when using the validateCells method on a mixed set of data', (done) => {
  1567. const onValidate = jasmine.createSpy('onValidate');
  1568. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  1569. const hot = handsontable({
  1570. data: [
  1571. { id: 'sth', name: 'Steve' },
  1572. { id: 'sth else', name: 'Bob' }
  1573. ],
  1574. columns: [
  1575. {
  1576. data: 'id',
  1577. validator(value, cb) {
  1578. cb(value === parseInt(value, 10));
  1579. }
  1580. },
  1581. { data: 'name' }
  1582. ],
  1583. afterValidate: onAfterValidate
  1584. });
  1585. hot.validateCells(onValidate);
  1586. setTimeout(() => {
  1587. expect(onValidate).toHaveBeenCalledWith(false);
  1588. expect(onValidate.calls.count()).toEqual(1);
  1589. done();
  1590. }, 200);
  1591. });
  1592. it('should call the validation callback only once, when using the validateRows method on a mixed set of data', (done) => {
  1593. const onValidate = jasmine.createSpy('onValidate');
  1594. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  1595. const hot = handsontable({
  1596. data: [
  1597. { id: 'sth', name: 'Steve' },
  1598. { id: 'sth else', name: 'Bob' }
  1599. ],
  1600. columns: [
  1601. {
  1602. data: 'id',
  1603. validator(value, cb) {
  1604. cb(value === parseInt(value, 10));
  1605. }
  1606. },
  1607. { data: 'name' }
  1608. ],
  1609. afterValidate: onAfterValidate
  1610. });
  1611. hot.validateRows([0, 1], onValidate);
  1612. setTimeout(() => {
  1613. expect(onValidate).toHaveBeenCalledWith(false);
  1614. expect(onValidate.calls.count()).toEqual(1);
  1615. done();
  1616. }, 200);
  1617. });
  1618. it('should call the validation callback only once, when using the validateColumns method on a mixed set of data', (done) => {
  1619. const onValidate = jasmine.createSpy('onValidate');
  1620. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  1621. const hot = handsontable({
  1622. data: [
  1623. { id: 'sth', name: 'Steve' },
  1624. { id: 'sth else', name: 'Bob' }
  1625. ],
  1626. columns: [
  1627. {
  1628. data: 'id',
  1629. validator(value, cb) {
  1630. cb(value === parseInt(value, 10));
  1631. }
  1632. },
  1633. { data: 'name' }
  1634. ],
  1635. afterValidate: onAfterValidate
  1636. });
  1637. hot.validateColumns([0, 1], onValidate);
  1638. setTimeout(() => {
  1639. expect(onValidate).toHaveBeenCalledWith(false);
  1640. expect(onValidate.calls.count()).toEqual(1);
  1641. done();
  1642. }, 200);
  1643. });
  1644. it('should call the validation callback only once, when using the validateCells method on a mixed set of data and when columns is a function', (done) => {
  1645. const onValidate = jasmine.createSpy('onValidate');
  1646. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  1647. const hot = handsontable({
  1648. data: [
  1649. { id: 'sth', name: 'Steve' },
  1650. { id: 'sth else', name: 'Bob' }
  1651. ],
  1652. columns(column) {
  1653. let colMeta = null;
  1654. if (column === 0) {
  1655. colMeta = {
  1656. data: 'id',
  1657. validator(value, cb) {
  1658. cb(value === parseInt(value, 10));
  1659. }
  1660. };
  1661. } else if (column === 1) {
  1662. colMeta = { data: 'name' };
  1663. }
  1664. return colMeta;
  1665. },
  1666. afterValidate: onAfterValidate
  1667. });
  1668. hot.validateCells(onValidate);
  1669. setTimeout(() => {
  1670. expect(onValidate).toHaveBeenCalledWith(false);
  1671. expect(onValidate.calls.count()).toEqual(1);
  1672. done();
  1673. }, 200);
  1674. });
  1675. it('should call the validation callback only once, when using the validateRows method on a mixed set of data and when columns is a function', (done) => {
  1676. const onValidate = jasmine.createSpy('onValidate');
  1677. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  1678. const hot = handsontable({
  1679. data: [
  1680. { id: 'sth', name: 'Steve' },
  1681. { id: 'sth else', name: 'Bob' }
  1682. ],
  1683. columns(column) {
  1684. let colMeta = null;
  1685. if (column === 0) {
  1686. colMeta = {
  1687. data: 'id',
  1688. validator(value, cb) {
  1689. cb(value === parseInt(value, 10));
  1690. }
  1691. };
  1692. } else if (column === 1) {
  1693. colMeta = { data: 'name' };
  1694. }
  1695. return colMeta;
  1696. },
  1697. afterValidate: onAfterValidate
  1698. });
  1699. hot.validateRows([0, 1], onValidate);
  1700. setTimeout(() => {
  1701. expect(onValidate).toHaveBeenCalledWith(false);
  1702. expect(onValidate.calls.count()).toEqual(1);
  1703. done();
  1704. }, 200);
  1705. });
  1706. it('should call the validation callback only once, when using the validateColumns method on a mixed set of data and when columns is a function', (done) => {
  1707. const onValidate = jasmine.createSpy('onValidate');
  1708. const onAfterValidate = jasmine.createSpy('onAfterValidate');
  1709. const hot = handsontable({
  1710. data: [
  1711. { id: 'sth', name: 'Steve' },
  1712. { id: 'sth else', name: 'Bob' }
  1713. ],
  1714. columns(column) {
  1715. let colMeta = null;
  1716. if (column === 0) {
  1717. colMeta = {
  1718. data: 'id',
  1719. validator(value, cb) {
  1720. cb(value === parseInt(value, 10));
  1721. }
  1722. };
  1723. } else if (column === 1) {
  1724. colMeta = { data: 'name' };
  1725. }
  1726. return colMeta;
  1727. },
  1728. afterValidate: onAfterValidate
  1729. });
  1730. hot.validateColumns([0, 1], onValidate);
  1731. setTimeout(() => {
  1732. expect(onValidate).toHaveBeenCalledWith(false);
  1733. expect(onValidate.calls.count()).toEqual(1);
  1734. done();
  1735. }, 200);
  1736. });
  1737. });