fad25721e4f354080e99b5488c80589879848793e246e54f790d294f7dd0bce80248a270c29bb1bf81305d3d478b8afb93b76fea9f6b5ae7c1972b442d88c8 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490
  1. describe('UndoRedo', () => {
  2. var 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. describe('core features', () => {
  13. describe('Array data', () => {
  14. describe('undo', () => {
  15. it('should undo single change', () => {
  16. handsontable({
  17. data: Handsontable.helper.createSpreadsheetData(2, 2)
  18. });
  19. var HOT = getInstance();
  20. setDataAtCell(0, 0, 'X1');
  21. expect(getDataAtCell(0, 0)).toBe('X1');
  22. HOT.undo();
  23. expect(getDataAtCell(0, 0)).toBe('A1');
  24. });
  25. it('should undo single change on cell with validator', (done) => {
  26. handsontable({
  27. data: Handsontable.helper.createSpreadsheetData(2, 2),
  28. });
  29. var HOT = getInstance();
  30. setDataAtCell(0, 0, 'X1');
  31. setTimeout(() => {
  32. expect(getDataAtCell(0, 0)).toBe('X1');
  33. HOT.undo();
  34. }, 200);
  35. setTimeout(() => {
  36. expect(getDataAtCell(0, 0)).toBe('A1');
  37. done();
  38. }, 400);
  39. });
  40. it('should undo creation of a single row', () => {
  41. var HOT = handsontable({
  42. data: Handsontable.helper.createSpreadsheetData(2, 2)
  43. });
  44. expect(countRows()).toEqual(2);
  45. alter('insert_row');
  46. expect(countRows()).toEqual(3);
  47. HOT.undo();
  48. expect(countRows()).toEqual(2);
  49. });
  50. it('should undo creation of multiple rows', () => {
  51. var HOT = handsontable({
  52. data: Handsontable.helper.createSpreadsheetData(2, 2)
  53. });
  54. expect(countRows()).toEqual(2);
  55. alter('insert_row', 0, 5);
  56. expect(countRows()).toEqual(7);
  57. HOT.undo();
  58. expect(countRows()).toEqual(2);
  59. });
  60. it('should undo creation of multiple rows with minSpareRows', () => {
  61. var HOT = handsontable({
  62. data: Handsontable.helper.createSpreadsheetData(2, 1),
  63. minSpareRows: 2
  64. });
  65. expect(getData()).toEqual([['A1'], ['A2'], [null], [null]]);
  66. setDataAtCell(2, 0, 'A3');
  67. setDataAtCell(4, 0, 'A4');
  68. expect(getData()).toEqual([['A1'], ['A2'], ['A3'], [null], ['A4'], [null], [null]]);
  69. HOT.undo();
  70. HOT.undo();
  71. expect(getData()).toEqual([['A1'], ['A2'], [null], [null]]);
  72. });
  73. it('should undo removal of single row', () => {
  74. var HOT = handsontable({
  75. data: Handsontable.helper.createSpreadsheetData(3, 2)
  76. });
  77. expect(countRows()).toEqual(3);
  78. expect(getDataAtCell(0, 0)).toEqual('A1');
  79. expect(getDataAtCell(0, 1)).toEqual('B1');
  80. expect(getDataAtCell(1, 0)).toEqual('A2');
  81. expect(getDataAtCell(1, 1)).toEqual('B2');
  82. expect(getDataAtCell(2, 0)).toEqual('A3');
  83. expect(getDataAtCell(2, 1)).toEqual('B3');
  84. alter('remove_row', 1);
  85. expect(countRows()).toEqual(2);
  86. expect(getDataAtCell(0, 0)).toEqual('A1');
  87. expect(getDataAtCell(0, 1)).toEqual('B1');
  88. expect(getDataAtCell(1, 0)).toEqual('A3');
  89. expect(getDataAtCell(1, 1)).toEqual('B3');
  90. HOT.undo();
  91. expect(countRows()).toEqual(3);
  92. expect(getDataAtCell(0, 0)).toEqual('A1');
  93. expect(getDataAtCell(0, 1)).toEqual('B1');
  94. expect(getDataAtCell(1, 0)).toEqual('A2');
  95. expect(getDataAtCell(1, 1)).toEqual('B2');
  96. expect(getDataAtCell(2, 0)).toEqual('A3');
  97. expect(getDataAtCell(2, 1)).toEqual('B3');
  98. });
  99. it('should undo removal of multiple rows', () => {
  100. var HOT = handsontable({
  101. data: Handsontable.helper.createSpreadsheetData(4, 2)
  102. });
  103. expect(countRows()).toEqual(4);
  104. expect(getDataAtCell(0, 0)).toEqual('A1');
  105. expect(getDataAtCell(0, 1)).toEqual('B1');
  106. expect(getDataAtCell(1, 0)).toEqual('A2');
  107. expect(getDataAtCell(1, 1)).toEqual('B2');
  108. expect(getDataAtCell(2, 0)).toEqual('A3');
  109. expect(getDataAtCell(2, 1)).toEqual('B3');
  110. expect(getDataAtCell(3, 0)).toEqual('A4');
  111. expect(getDataAtCell(3, 1)).toEqual('B4');
  112. alter('remove_row', 1, 2);
  113. expect(countRows()).toEqual(2);
  114. expect(getDataAtCell(0, 0)).toEqual('A1');
  115. expect(getDataAtCell(0, 1)).toEqual('B1');
  116. expect(getDataAtCell(1, 0)).toEqual('A4');
  117. expect(getDataAtCell(1, 1)).toEqual('B4');
  118. HOT.undo();
  119. expect(countRows()).toEqual(4);
  120. expect(getDataAtCell(0, 0)).toEqual('A1');
  121. expect(getDataAtCell(0, 1)).toEqual('B1');
  122. expect(getDataAtCell(1, 0)).toEqual('A2');
  123. expect(getDataAtCell(1, 1)).toEqual('B2');
  124. expect(getDataAtCell(2, 0)).toEqual('A3');
  125. expect(getDataAtCell(2, 1)).toEqual('B3');
  126. expect(getDataAtCell(3, 0)).toEqual('A4');
  127. expect(getDataAtCell(3, 1)).toEqual('B4');
  128. });
  129. it('should undo creation of a single column (colHeaders: undefined)', () => {
  130. var HOT = handsontable({
  131. data: Handsontable.helper.createSpreadsheetData(2, 3)
  132. });
  133. expect(countCols()).toEqual(3);
  134. alter('insert_col');
  135. expect(countCols()).toEqual(4);
  136. HOT.undo();
  137. expect(countCols()).toEqual(3);
  138. });
  139. it('should undo creation of a single column (colHeaders: true)', () => {
  140. var HOT = handsontable({
  141. data: Handsontable.helper.createSpreadsheetData(2, 3),
  142. colHeaders: true
  143. });
  144. expect(countCols()).toEqual(3);
  145. expect(getColHeader()).toEqual(['A', 'B', 'C']);
  146. alter('insert_col');
  147. expect(countCols()).toEqual(4);
  148. expect(getColHeader()).toEqual(['A', 'B', 'C', 'D']);
  149. HOT.undo();
  150. expect(countCols()).toEqual(3);
  151. expect(getColHeader()).toEqual(['A', 'B', 'C']);
  152. });
  153. it('should undo creation of a single column (colHeaders: Array)', () => {
  154. var HOT = handsontable({
  155. data: Handsontable.helper.createSpreadsheetData(2, 3),
  156. colHeaders: ['Header1', 'Header2', 'Header3']
  157. });
  158. expect(countCols()).toEqual(3);
  159. expect(getColHeader()).toEqual(['Header1', 'Header2', 'Header3']);
  160. alter('insert_col', 1);
  161. expect(countCols()).toEqual(4);
  162. expect(getColHeader()).toEqual(['Header1', 'B', 'Header2', 'Header3']);
  163. HOT.undo();
  164. expect(countCols()).toEqual(3);
  165. expect(getColHeader()).toEqual(['Header1', 'Header2', 'Header3']);
  166. });
  167. it('should undo creation of multiple columns (colHeaders: undefined)', () => {
  168. var HOT = handsontable({
  169. data: Handsontable.helper.createSpreadsheetData(2, 2)
  170. });
  171. expect(countCols()).toEqual(2);
  172. alter('insert_col', 1, 5);
  173. expect(countCols()).toEqual(7);
  174. HOT.undo();
  175. expect(countCols()).toEqual(2);
  176. });
  177. it('should undo creation of multiple columns (colHeaders: true)', () => {
  178. var HOT = handsontable({
  179. data: Handsontable.helper.createSpreadsheetData(2, 2),
  180. colHeaders: true
  181. });
  182. expect(countCols()).toEqual(2);
  183. expect(getColHeader()).toEqual(['A', 'B']);
  184. alter('insert_col', 1, 5);
  185. expect(countCols()).toEqual(7);
  186. expect(getColHeader()).toEqual(['A', 'B', 'C', 'D', 'E', 'F', 'G']);
  187. HOT.undo();
  188. expect(countCols()).toEqual(2);
  189. expect(getColHeader()).toEqual(['A', 'B']);
  190. });
  191. it('should undo creation of multiple columns (colHeaders: Array)', () => {
  192. var HOT = handsontable({
  193. data: Handsontable.helper.createSpreadsheetData(2, 2),
  194. colHeaders: ['Header1', 'Header2']
  195. });
  196. expect(countCols()).toEqual(2);
  197. expect(getColHeader()).toEqual(['Header1', 'Header2']);
  198. alter('insert_col', 1, 5);
  199. expect(countCols()).toEqual(7);
  200. expect(getColHeader()).toEqual(['Header1', 'B', 'C', 'D', 'E', 'F', 'Header2']);
  201. HOT.undo();
  202. expect(countCols()).toEqual(2);
  203. expect(getColHeader()).toEqual(['Header1', 'Header2']);
  204. });
  205. it('should undo creation of multiple columns with minSpareCols', () => {
  206. var HOT = handsontable({
  207. data: Handsontable.helper.createSpreadsheetData(1, 1),
  208. minSpareCols: 2
  209. });
  210. expect(getData()).toEqual([['A1', null, null]]);
  211. setDataAtCell(0, 1, 'B1');
  212. setDataAtCell(0, 3, 'C1');
  213. expect(getData()).toEqual([['A1', 'B1', null, 'C1', null, null]]);
  214. HOT.undo();
  215. HOT.undo();
  216. expect(getData()).toEqual([['A1', null, null]]);
  217. });
  218. it('should undo removal of single column (colHeaders: undefined)', () => {
  219. var HOT = handsontable({
  220. data: Handsontable.helper.createSpreadsheetData(2, 3)
  221. });
  222. expect(countCols()).toEqual(3);
  223. expect(getDataAtCell(0, 0)).toEqual('A1');
  224. expect(getDataAtCell(0, 1)).toEqual('B1');
  225. expect(getDataAtCell(0, 2)).toEqual('C1');
  226. expect(getDataAtCell(1, 0)).toEqual('A2');
  227. expect(getDataAtCell(1, 1)).toEqual('B2');
  228. expect(getDataAtCell(1, 2)).toEqual('C2');
  229. alter('remove_col', 1);
  230. expect(countCols()).toEqual(2);
  231. expect(getDataAtCell(0, 0)).toEqual('A1');
  232. expect(getDataAtCell(0, 1)).toEqual('C1');
  233. expect(getDataAtCell(1, 0)).toEqual('A2');
  234. expect(getDataAtCell(1, 1)).toEqual('C2');
  235. HOT.undo();
  236. expect(countCols()).toEqual(3);
  237. expect(getDataAtCell(0, 0)).toEqual('A1');
  238. expect(getDataAtCell(0, 1)).toEqual('B1');
  239. expect(getDataAtCell(0, 2)).toEqual('C1');
  240. expect(getDataAtCell(1, 0)).toEqual('A2');
  241. expect(getDataAtCell(1, 1)).toEqual('B2');
  242. expect(getDataAtCell(1, 2)).toEqual('C2');
  243. });
  244. it('should undo removal of single column (colHeaders: true)', () => {
  245. var HOT = handsontable({
  246. data: Handsontable.helper.createSpreadsheetData(2, 2),
  247. colHeaders: true
  248. });
  249. expect(countCols()).toEqual(2);
  250. expect(getDataAtCell(0, 0)).toEqual('A1');
  251. expect(getDataAtCell(0, 1)).toEqual('B1');
  252. expect(getDataAtCell(1, 0)).toEqual('A2');
  253. expect(getDataAtCell(1, 1)).toEqual('B2');
  254. expect(getColHeader()).toEqual(['A', 'B']);
  255. alter('remove_col');
  256. expect(countCols()).toEqual(1);
  257. expect(getDataAtCell(0, 0)).toEqual('A1');
  258. expect(getDataAtCell(0, 1)).toBeNull();
  259. expect(getDataAtCell(1, 0)).toEqual('A2');
  260. expect(getDataAtCell(1, 1)).toBeNull();
  261. expect(getColHeader()).toEqual(['A']);
  262. HOT.undo();
  263. expect(countCols()).toEqual(2);
  264. expect(getDataAtCell(0, 0)).toEqual('A1');
  265. expect(getDataAtCell(0, 1)).toEqual('B1');
  266. expect(getDataAtCell(1, 0)).toEqual('A2');
  267. expect(getDataAtCell(1, 1)).toEqual('B2');
  268. expect(getColHeader()).toEqual(['A', 'B']);
  269. });
  270. it('should undo removal of single column (colHeaders: Array)', () => {
  271. var HOT = handsontable({
  272. data: Handsontable.helper.createSpreadsheetData(2, 2),
  273. colHeaders: ['Header1', 'Header2']
  274. });
  275. expect(countCols()).toEqual(2);
  276. expect(getDataAtCell(0, 0)).toEqual('A1');
  277. expect(getDataAtCell(0, 1)).toEqual('B1');
  278. expect(getDataAtCell(1, 0)).toEqual('A2');
  279. expect(getDataAtCell(1, 1)).toEqual('B2');
  280. expect(getColHeader()).toEqual(['Header1', 'Header2']);
  281. alter('remove_col');
  282. expect(countCols()).toEqual(1);
  283. expect(getDataAtCell(0, 0)).toEqual('A1');
  284. expect(getDataAtCell(0, 1)).toBeNull();
  285. expect(getDataAtCell(1, 0)).toEqual('A2');
  286. expect(getDataAtCell(1, 1)).toBeNull();
  287. expect(getColHeader()).toEqual(['Header1']);
  288. HOT.undo();
  289. expect(countCols()).toEqual(2);
  290. expect(getDataAtCell(0, 0)).toEqual('A1');
  291. expect(getDataAtCell(0, 1)).toEqual('B1');
  292. expect(getDataAtCell(1, 0)).toEqual('A2');
  293. expect(getDataAtCell(1, 1)).toEqual('B2');
  294. expect(getColHeader()).toEqual(['Header1', 'Header2']);
  295. });
  296. it('should undo removal of multiple columns (colHeaders: undefined)', () => {
  297. var HOT = handsontable({
  298. data: Handsontable.helper.createSpreadsheetData(2, 4)
  299. });
  300. expect(countCols()).toEqual(4);
  301. expect(getDataAtCell(0, 0)).toEqual('A1');
  302. expect(getDataAtCell(0, 1)).toEqual('B1');
  303. expect(getDataAtCell(0, 2)).toEqual('C1');
  304. expect(getDataAtCell(0, 3)).toEqual('D1');
  305. expect(getDataAtCell(1, 0)).toEqual('A2');
  306. expect(getDataAtCell(1, 1)).toEqual('B2');
  307. expect(getDataAtCell(1, 2)).toEqual('C2');
  308. expect(getDataAtCell(1, 3)).toEqual('D2');
  309. alter('remove_col', 1, 2);
  310. expect(countCols()).toEqual(2);
  311. expect(getDataAtCell(0, 0)).toEqual('A1');
  312. expect(getDataAtCell(0, 1)).toEqual('D1');
  313. expect(getDataAtCell(0, 2)).toBeNull();
  314. expect(getDataAtCell(0, 3)).toBeNull();
  315. expect(getDataAtCell(1, 0)).toEqual('A2');
  316. expect(getDataAtCell(1, 1)).toEqual('D2');
  317. expect(getDataAtCell(1, 2)).toBeNull();
  318. expect(getDataAtCell(1, 3)).toBeNull();
  319. HOT.undo();
  320. expect(countCols()).toEqual(4);
  321. expect(getDataAtCell(0, 0)).toEqual('A1');
  322. expect(getDataAtCell(0, 1)).toEqual('B1');
  323. expect(getDataAtCell(0, 2)).toEqual('C1');
  324. expect(getDataAtCell(0, 3)).toEqual('D1');
  325. expect(getDataAtCell(1, 0)).toEqual('A2');
  326. expect(getDataAtCell(1, 1)).toEqual('B2');
  327. expect(getDataAtCell(1, 2)).toEqual('C2');
  328. expect(getDataAtCell(1, 3)).toEqual('D2');
  329. });
  330. it('should undo removal of multiple columns (colHeaders: true)', () => {
  331. var HOT = handsontable({
  332. data: Handsontable.helper.createSpreadsheetData(2, 4),
  333. colHeaders: true
  334. });
  335. expect(countCols()).toEqual(4);
  336. expect(getDataAtCell(0, 0)).toEqual('A1');
  337. expect(getDataAtCell(0, 1)).toEqual('B1');
  338. expect(getDataAtCell(0, 2)).toEqual('C1');
  339. expect(getDataAtCell(0, 3)).toEqual('D1');
  340. expect(getDataAtCell(1, 0)).toEqual('A2');
  341. expect(getDataAtCell(1, 1)).toEqual('B2');
  342. expect(getDataAtCell(1, 2)).toEqual('C2');
  343. expect(getDataAtCell(1, 3)).toEqual('D2');
  344. expect(getColHeader()).toEqual(['A', 'B', 'C', 'D']);
  345. alter('remove_col', 1, 3);
  346. expect(countCols()).toEqual(1);
  347. expect(getDataAtCell(0, 0)).toEqual('A1');
  348. expect(getDataAtCell(0, 1)).toBeNull();
  349. expect(getDataAtCell(0, 2)).toBeNull();
  350. expect(getDataAtCell(0, 3)).toBeNull();
  351. expect(getDataAtCell(1, 0)).toEqual('A2');
  352. expect(getDataAtCell(1, 1)).toBeNull();
  353. expect(getDataAtCell(1, 2)).toBeNull();
  354. expect(getDataAtCell(1, 3)).toBeNull();
  355. expect(getColHeader()).toEqual(['A']);
  356. HOT.undo();
  357. expect(countCols()).toEqual(4);
  358. expect(getDataAtCell(0, 0)).toEqual('A1');
  359. expect(getDataAtCell(0, 1)).toEqual('B1');
  360. expect(getDataAtCell(0, 2)).toEqual('C1');
  361. expect(getDataAtCell(0, 3)).toEqual('D1');
  362. expect(getDataAtCell(1, 0)).toEqual('A2');
  363. expect(getDataAtCell(1, 1)).toEqual('B2');
  364. expect(getDataAtCell(1, 2)).toEqual('C2');
  365. expect(getDataAtCell(1, 3)).toEqual('D2');
  366. expect(getColHeader()).toEqual(['A', 'B', 'C', 'D']);
  367. });
  368. it('should undo removal of multiple columns (colHeaders: Array)', () => {
  369. var HOT = handsontable({
  370. data: Handsontable.helper.createSpreadsheetData(2, 4),
  371. colHeaders: ['Header1', 'Header2', 'Header3', 'Header4']
  372. });
  373. expect(countCols()).toEqual(4);
  374. expect(getDataAtCell(0, 0)).toEqual('A1');
  375. expect(getDataAtCell(0, 1)).toEqual('B1');
  376. expect(getDataAtCell(0, 2)).toEqual('C1');
  377. expect(getDataAtCell(0, 3)).toEqual('D1');
  378. expect(getDataAtCell(1, 0)).toEqual('A2');
  379. expect(getDataAtCell(1, 1)).toEqual('B2');
  380. expect(getDataAtCell(1, 2)).toEqual('C2');
  381. expect(getDataAtCell(1, 3)).toEqual('D2');
  382. expect(getColHeader()).toEqual(['Header1', 'Header2', 'Header3', 'Header4']);
  383. alter('remove_col', 1, 2);
  384. expect(countCols()).toEqual(2);
  385. expect(getDataAtCell(0, 0)).toEqual('A1');
  386. expect(getDataAtCell(0, 1)).toEqual('D1');
  387. expect(getDataAtCell(0, 2)).toBeNull();
  388. expect(getDataAtCell(0, 3)).toBeNull();
  389. expect(getDataAtCell(1, 0)).toEqual('A2');
  390. expect(getDataAtCell(1, 1)).toEqual('D2');
  391. expect(getDataAtCell(1, 2)).toBeNull();
  392. expect(getDataAtCell(1, 3)).toBeNull();
  393. expect(getColHeader()).toEqual(['Header1', 'Header4']);
  394. HOT.undo();
  395. expect(countCols()).toEqual(4);
  396. expect(getDataAtCell(0, 0)).toEqual('A1');
  397. expect(getDataAtCell(0, 1)).toEqual('B1');
  398. expect(getDataAtCell(0, 2)).toEqual('C1');
  399. expect(getDataAtCell(0, 3)).toEqual('D1');
  400. expect(getDataAtCell(1, 0)).toEqual('A2');
  401. expect(getDataAtCell(1, 1)).toEqual('B2');
  402. expect(getDataAtCell(1, 2)).toEqual('C2');
  403. expect(getDataAtCell(1, 3)).toEqual('D2');
  404. expect(getColHeader()).toEqual(['Header1', 'Header2', 'Header3', 'Header4']);
  405. });
  406. it('should undo removal of multiple columns (with a used manualColumnMove)', () => {
  407. var HOT = handsontable({
  408. data: Handsontable.helper.createSpreadsheetData(2, 7),
  409. manualColumnMove: [3, 2, 0, 6, 1, 5, 4]
  410. });
  411. expect(countCols()).toEqual(7);
  412. expect(getDataAtRow(0)).toEqual(['D1', 'C1', 'A1', 'G1', 'B1', 'F1', 'E1']);
  413. alter('remove_col', 1, 3);
  414. expect(countCols()).toEqual(4);
  415. expect(getDataAtRow(0)).toEqual(['D1', 'B1', 'F1', 'E1']);
  416. // HOT.undo();
  417. //
  418. // expect(countCols()).toEqual(7);
  419. // expect(getDataAtRow(0)).toEqual(['D1', 'C1', 'A1', 'G1', 'B1', 'F1', 'E1']);
  420. });
  421. it('should undo multiple changes', () => {
  422. handsontable({
  423. data: Handsontable.helper.createSpreadsheetData(2, 2)
  424. });
  425. var HOT = getInstance();
  426. setDataAtCell(0, 0, 'X1');
  427. setDataAtCell(1, 0, 'X2');
  428. setDataAtCell(0, 1, 'Y1');
  429. setDataAtCell(1, 1, 'Y2');
  430. expect(getDataAtCell(0, 0)).toBe('X1');
  431. expect(getDataAtCell(1, 0)).toBe('X2');
  432. expect(getDataAtCell(0, 1)).toBe('Y1');
  433. expect(getDataAtCell(1, 1)).toBe('Y2');
  434. HOT.undo();
  435. expect(getDataAtCell(0, 0)).toBe('X1');
  436. expect(getDataAtCell(1, 0)).toBe('X2');
  437. expect(getDataAtCell(0, 1)).toBe('Y1');
  438. expect(getDataAtCell(1, 1)).toBe('B2');
  439. HOT.undo();
  440. expect(getDataAtCell(0, 0)).toBe('X1');
  441. expect(getDataAtCell(1, 0)).toBe('X2');
  442. expect(getDataAtCell(0, 1)).toBe('B1');
  443. expect(getDataAtCell(1, 1)).toBe('B2');
  444. HOT.undo();
  445. expect(getDataAtCell(0, 0)).toBe('X1');
  446. expect(getDataAtCell(1, 0)).toBe('A2');
  447. expect(getDataAtCell(0, 1)).toBe('B1');
  448. expect(getDataAtCell(1, 1)).toBe('B2');
  449. HOT.undo();
  450. expect(getDataAtCell(0, 0)).toBe('A1');
  451. expect(getDataAtCell(1, 0)).toBe('A2');
  452. expect(getDataAtCell(0, 1)).toBe('B1');
  453. expect(getDataAtCell(1, 1)).toBe('B2');
  454. HOT.undo();
  455. expect(getDataAtCell(0, 0)).toBe('A1');
  456. expect(getDataAtCell(1, 0)).toBe('A2');
  457. expect(getDataAtCell(0, 1)).toBe('B1');
  458. expect(getDataAtCell(1, 1)).toBe('B2');
  459. });
  460. it('should undo multiple changes in cells with validators', (done) => {
  461. handsontable({
  462. data: Handsontable.helper.createSpreadsheetData(2, 2),
  463. });
  464. var HOT = getInstance();
  465. setDataAtCell(0, 0, 'X1');
  466. setDataAtCell(1, 0, 'X2');
  467. setDataAtCell(0, 1, 'Y1');
  468. setDataAtCell(1, 1, 'Y2');
  469. setTimeout(() => {
  470. expect(getDataAtCell(0, 0)).toBe('X1');
  471. expect(getDataAtCell(1, 0)).toBe('X2');
  472. expect(getDataAtCell(0, 1)).toBe('Y1');
  473. expect(getDataAtCell(1, 1)).toBe('Y2');
  474. HOT.undo();
  475. }, 200);
  476. setTimeout(() => {
  477. expect(getDataAtCell(0, 0)).toBe('X1');
  478. expect(getDataAtCell(1, 0)).toBe('X2');
  479. expect(getDataAtCell(0, 1)).toBe('Y1');
  480. expect(getDataAtCell(1, 1)).toBe('B2');
  481. HOT.undo();
  482. }, 400);
  483. setTimeout(() => {
  484. expect(getDataAtCell(0, 0)).toBe('X1');
  485. expect(getDataAtCell(1, 0)).toBe('X2');
  486. expect(getDataAtCell(0, 1)).toBe('B1');
  487. expect(getDataAtCell(1, 1)).toBe('B2');
  488. HOT.undo();
  489. }, 600);
  490. setTimeout(() => {
  491. expect(getDataAtCell(0, 0)).toBe('X1');
  492. expect(getDataAtCell(1, 0)).toBe('A2');
  493. expect(getDataAtCell(0, 1)).toBe('B1');
  494. expect(getDataAtCell(1, 1)).toBe('B2');
  495. HOT.undo();
  496. }, 800);
  497. setTimeout(() => {
  498. expect(getDataAtCell(0, 0)).toBe('A1');
  499. expect(getDataAtCell(1, 0)).toBe('A2');
  500. expect(getDataAtCell(0, 1)).toBe('B1');
  501. expect(getDataAtCell(1, 1)).toBe('B2');
  502. HOT.undo();
  503. }, 1000);
  504. setTimeout(() => {
  505. expect(getDataAtCell(0, 0)).toBe('A1');
  506. expect(getDataAtCell(1, 0)).toBe('A2');
  507. expect(getDataAtCell(0, 1)).toBe('B1');
  508. expect(getDataAtCell(1, 1)).toBe('B2');
  509. done();
  510. }, 1200);
  511. });
  512. it('should undo multiple row creations', () => {
  513. var HOT = handsontable({
  514. data: Handsontable.helper.createSpreadsheetData(2, 2)
  515. });
  516. expect(countRows()).toEqual(2);
  517. alter('insert_row');
  518. alter('insert_row');
  519. alter('insert_row');
  520. alter('insert_row');
  521. expect(countRows()).toEqual(6);
  522. HOT.undo();
  523. expect(countRows()).toEqual(5);
  524. HOT.undo();
  525. expect(countRows()).toEqual(4);
  526. HOT.undo();
  527. expect(countRows()).toEqual(3);
  528. HOT.undo();
  529. expect(countRows()).toEqual(2);
  530. HOT.undo();
  531. expect(countRows()).toEqual(2);
  532. });
  533. it('should undo multiple row removals', () => {
  534. var HOT = handsontable({
  535. data: Handsontable.helper.createSpreadsheetData(4, 2)
  536. });
  537. expect(countRows()).toEqual(4);
  538. expect(getDataAtCell(0, 0)).toEqual('A1');
  539. expect(getDataAtCell(0, 1)).toEqual('B1');
  540. expect(getDataAtCell(1, 0)).toEqual('A2');
  541. expect(getDataAtCell(1, 1)).toEqual('B2');
  542. expect(getDataAtCell(2, 0)).toEqual('A3');
  543. expect(getDataAtCell(2, 1)).toEqual('B3');
  544. expect(getDataAtCell(3, 0)).toEqual('A4');
  545. expect(getDataAtCell(3, 1)).toEqual('B4');
  546. alter('remove_row');
  547. alter('remove_row');
  548. alter('remove_row');
  549. expect(countRows()).toEqual(1);
  550. expect(getDataAtCell(0, 0)).toEqual('A1');
  551. expect(getDataAtCell(0, 1)).toEqual('B1');
  552. HOT.undo();
  553. expect(countRows()).toEqual(2);
  554. expect(getDataAtCell(0, 0)).toEqual('A1');
  555. expect(getDataAtCell(0, 1)).toEqual('B1');
  556. expect(getDataAtCell(1, 0)).toEqual('A2');
  557. expect(getDataAtCell(1, 1)).toEqual('B2');
  558. HOT.undo();
  559. expect(countRows()).toEqual(3);
  560. expect(getDataAtCell(0, 0)).toEqual('A1');
  561. expect(getDataAtCell(0, 1)).toEqual('B1');
  562. expect(getDataAtCell(1, 0)).toEqual('A2');
  563. expect(getDataAtCell(1, 1)).toEqual('B2');
  564. expect(getDataAtCell(2, 0)).toEqual('A3');
  565. expect(getDataAtCell(2, 1)).toEqual('B3');
  566. HOT.undo();
  567. expect(countRows()).toEqual(4);
  568. expect(getDataAtCell(0, 0)).toEqual('A1');
  569. expect(getDataAtCell(0, 1)).toEqual('B1');
  570. expect(getDataAtCell(1, 0)).toEqual('A2');
  571. expect(getDataAtCell(1, 1)).toEqual('B2');
  572. expect(getDataAtCell(2, 0)).toEqual('A3');
  573. expect(getDataAtCell(2, 1)).toEqual('B3');
  574. expect(getDataAtCell(3, 0)).toEqual('A4');
  575. expect(getDataAtCell(3, 1)).toEqual('B4');
  576. HOT.undo();
  577. expect(countRows()).toEqual(4);
  578. expect(getDataAtCell(0, 0)).toEqual('A1');
  579. expect(getDataAtCell(0, 1)).toEqual('B1');
  580. expect(getDataAtCell(1, 0)).toEqual('A2');
  581. expect(getDataAtCell(1, 1)).toEqual('B2');
  582. expect(getDataAtCell(2, 0)).toEqual('A3');
  583. expect(getDataAtCell(2, 1)).toEqual('B3');
  584. expect(getDataAtCell(3, 0)).toEqual('A4');
  585. expect(getDataAtCell(3, 1)).toEqual('B4');
  586. });
  587. it('should undo changes only for table where the change actually took place', function() {
  588. this.$container2 = $(`<div id="${id}-2"></div>`).appendTo('body');
  589. var hot1 = handsontable({
  590. data: [
  591. [1],
  592. [2],
  593. [3]
  594. ]
  595. });
  596. this.$container2.handsontable({
  597. data: [
  598. ['A'],
  599. ['B'],
  600. ['C']
  601. ]
  602. });
  603. var hot2 = this.$container2.handsontable('getInstance');
  604. hot1.setDataAtCell(0, 0, 4);
  605. expect(hot1.getDataAtCell(0, 0)).toEqual(4);
  606. expect(hot2.getDataAtCell(0, 0)).toEqual('A');
  607. hot2.undo();
  608. expect(hot2.getDataAtCell(0, 0)).toEqual('A');
  609. expect(hot1.getDataAtCell(0, 0)).toEqual(4);
  610. hot1.undo();
  611. expect(hot2.getDataAtCell(0, 0)).toEqual('A');
  612. expect(hot1.getDataAtCell(0, 0)).toEqual(1);
  613. hot2.destroy();
  614. this.$container2.remove();
  615. });
  616. });
  617. describe('redo', () => {
  618. it('should redo single change', () => {
  619. handsontable({
  620. data: Handsontable.helper.createSpreadsheetData(2, 2)
  621. });
  622. var HOT = getInstance();
  623. setDataAtCell(0, 0, 'new value');
  624. expect(getDataAtCell(0, 0)).toBe('new value');
  625. HOT.undo();
  626. expect(getDataAtCell(0, 0)).toBe('A1');
  627. HOT.redo();
  628. expect(getDataAtCell(0, 0)).toBe('new value');
  629. });
  630. it('should redo single change in cell with validator', (done) => {
  631. handsontable({
  632. data: Handsontable.helper.createSpreadsheetData(2, 2),
  633. });
  634. var HOT = getInstance();
  635. setDataAtCell(0, 0, 'new value');
  636. setTimeout(() => {
  637. expect(getDataAtCell(0, 0)).toBe('new value');
  638. HOT.undo();
  639. }, 200);
  640. setTimeout(() => {
  641. expect(getDataAtCell(0, 0)).toBe('A1');
  642. HOT.redo();
  643. }, 400);
  644. setTimeout(() => {
  645. expect(getDataAtCell(0, 0)).toBe('new value');
  646. done();
  647. }, 600);
  648. });
  649. it('should redo creation of a single row', () => {
  650. var HOT = handsontable({
  651. data: Handsontable.helper.createSpreadsheetData(2, 2)
  652. });
  653. expect(countRows()).toEqual(2);
  654. alter('insert_row');
  655. expect(countRows()).toEqual(3);
  656. HOT.undo();
  657. expect(countRows()).toEqual(2);
  658. HOT.redo();
  659. expect(countRows()).toEqual(3);
  660. });
  661. it('should redo creation of multiple rows', () => {
  662. var HOT = handsontable({
  663. data: Handsontable.helper.createSpreadsheetData(2, 2)
  664. });
  665. expect(countRows()).toEqual(2);
  666. alter('insert_row', 0, 5);
  667. expect(countRows()).toEqual(7);
  668. HOT.undo();
  669. expect(countRows()).toEqual(2);
  670. HOT.redo();
  671. expect(countRows()).toEqual(7);
  672. });
  673. it('should redo removal of single row', () => {
  674. var HOT = handsontable({
  675. data: Handsontable.helper.createSpreadsheetData(3, 2)
  676. });
  677. expect(countRows()).toEqual(3);
  678. expect(getDataAtCell(0, 0)).toEqual('A1');
  679. expect(getDataAtCell(0, 1)).toEqual('B1');
  680. expect(getDataAtCell(1, 0)).toEqual('A2');
  681. expect(getDataAtCell(1, 1)).toEqual('B2');
  682. expect(getDataAtCell(2, 0)).toEqual('A3');
  683. expect(getDataAtCell(2, 1)).toEqual('B3');
  684. alter('remove_row', 1);
  685. expect(countRows()).toEqual(2);
  686. expect(getDataAtCell(0, 0)).toEqual('A1');
  687. expect(getDataAtCell(0, 1)).toEqual('B1');
  688. expect(getDataAtCell(1, 0)).toEqual('A3');
  689. expect(getDataAtCell(1, 1)).toEqual('B3');
  690. HOT.undo();
  691. expect(countRows()).toEqual(3);
  692. expect(getDataAtCell(0, 0)).toEqual('A1');
  693. expect(getDataAtCell(0, 1)).toEqual('B1');
  694. expect(getDataAtCell(1, 0)).toEqual('A2');
  695. expect(getDataAtCell(1, 1)).toEqual('B2');
  696. expect(getDataAtCell(2, 0)).toEqual('A3');
  697. expect(getDataAtCell(2, 1)).toEqual('B3');
  698. HOT.redo();
  699. expect(countRows()).toEqual(2);
  700. expect(getDataAtCell(0, 0)).toEqual('A1');
  701. expect(getDataAtCell(0, 1)).toEqual('B1');
  702. expect(getDataAtCell(1, 0)).toEqual('A3');
  703. expect(getDataAtCell(1, 1)).toEqual('B3');
  704. });
  705. it('should redo removal of multiple rows', () => {
  706. var HOT = handsontable({
  707. data: Handsontable.helper.createSpreadsheetData(4, 2)
  708. });
  709. expect(countRows()).toEqual(4);
  710. expect(getDataAtCell(0, 0)).toEqual('A1');
  711. expect(getDataAtCell(0, 1)).toEqual('B1');
  712. expect(getDataAtCell(1, 0)).toEqual('A2');
  713. expect(getDataAtCell(1, 1)).toEqual('B2');
  714. expect(getDataAtCell(2, 0)).toEqual('A3');
  715. expect(getDataAtCell(2, 1)).toEqual('B3');
  716. expect(getDataAtCell(3, 0)).toEqual('A4');
  717. expect(getDataAtCell(3, 1)).toEqual('B4');
  718. alter('remove_row', 1, 2);
  719. expect(countRows()).toEqual(2);
  720. expect(getDataAtCell(0, 0)).toEqual('A1');
  721. expect(getDataAtCell(0, 1)).toEqual('B1');
  722. expect(getDataAtCell(1, 0)).toEqual('A4');
  723. expect(getDataAtCell(1, 1)).toEqual('B4');
  724. HOT.undo();
  725. expect(countRows()).toEqual(4);
  726. expect(getDataAtCell(0, 0)).toEqual('A1');
  727. expect(getDataAtCell(0, 1)).toEqual('B1');
  728. expect(getDataAtCell(1, 0)).toEqual('A2');
  729. expect(getDataAtCell(1, 1)).toEqual('B2');
  730. expect(getDataAtCell(2, 0)).toEqual('A3');
  731. expect(getDataAtCell(2, 1)).toEqual('B3');
  732. expect(getDataAtCell(3, 0)).toEqual('A4');
  733. expect(getDataAtCell(3, 1)).toEqual('B4');
  734. HOT.redo();
  735. expect(countRows()).toEqual(2);
  736. expect(getDataAtCell(0, 0)).toEqual('A1');
  737. expect(getDataAtCell(0, 1)).toEqual('B1');
  738. expect(getDataAtCell(1, 0)).toEqual('A4');
  739. expect(getDataAtCell(1, 1)).toEqual('B4');
  740. });
  741. it('should redo creation of a single column', () => {
  742. var HOT = handsontable({
  743. data: Handsontable.helper.createSpreadsheetData(2, 2)
  744. });
  745. expect(countCols()).toEqual(2);
  746. alter('insert_col');
  747. expect(countCols()).toEqual(3);
  748. HOT.undo();
  749. expect(countCols()).toEqual(2);
  750. HOT.redo();
  751. expect(countCols()).toEqual(3);
  752. });
  753. it('should redo creation of multiple columns', () => {
  754. var HOT = handsontable({
  755. data: Handsontable.helper.createSpreadsheetData(2, 2)
  756. });
  757. expect(countCols()).toEqual(2);
  758. alter('insert_col', 1, 5);
  759. expect(countCols()).toEqual(7);
  760. HOT.undo();
  761. expect(countCols()).toEqual(2);
  762. HOT.redo();
  763. expect(countCols()).toEqual(7);
  764. });
  765. it('should redo removal of single column', () => {
  766. var HOT = handsontable({
  767. data: Handsontable.helper.createSpreadsheetData(2, 2)
  768. });
  769. expect(countCols()).toEqual(2);
  770. expect(getDataAtCell(0, 0)).toEqual('A1');
  771. expect(getDataAtCell(0, 1)).toEqual('B1');
  772. expect(getDataAtCell(1, 0)).toEqual('A2');
  773. expect(getDataAtCell(1, 1)).toEqual('B2');
  774. alter('remove_col');
  775. expect(countCols()).toEqual(1);
  776. expect(getDataAtCell(0, 0)).toEqual('A1');
  777. expect(getDataAtCell(0, 1)).toBeNull();
  778. expect(getDataAtCell(1, 0)).toEqual('A2');
  779. expect(getDataAtCell(1, 1)).toBeNull();
  780. HOT.undo();
  781. expect(countCols()).toEqual(2);
  782. expect(getDataAtCell(0, 0)).toEqual('A1');
  783. expect(getDataAtCell(0, 1)).toEqual('B1');
  784. expect(getDataAtCell(1, 0)).toEqual('A2');
  785. expect(getDataAtCell(1, 1)).toEqual('B2');
  786. HOT.redo();
  787. expect(countCols()).toEqual(1);
  788. expect(getDataAtCell(0, 0)).toEqual('A1');
  789. expect(getDataAtCell(0, 1)).toBeNull();
  790. expect(getDataAtCell(1, 0)).toEqual('A2');
  791. expect(getDataAtCell(1, 1)).toBeNull();
  792. });
  793. it('should redo removal of multiple columns', () => {
  794. var HOT = handsontable({
  795. data: Handsontable.helper.createSpreadsheetData(2, 4)
  796. });
  797. expect(countCols()).toEqual(4);
  798. expect(getDataAtCell(0, 0)).toEqual('A1');
  799. expect(getDataAtCell(0, 1)).toEqual('B1');
  800. expect(getDataAtCell(0, 2)).toEqual('C1');
  801. expect(getDataAtCell(0, 3)).toEqual('D1');
  802. expect(getDataAtCell(1, 0)).toEqual('A2');
  803. expect(getDataAtCell(1, 1)).toEqual('B2');
  804. expect(getDataAtCell(1, 2)).toEqual('C2');
  805. expect(getDataAtCell(1, 3)).toEqual('D2');
  806. alter('remove_col', 1, 3);
  807. expect(countCols()).toEqual(1);
  808. expect(getDataAtCell(0, 0)).toEqual('A1');
  809. expect(getDataAtCell(0, 1)).toBeNull();
  810. expect(getDataAtCell(0, 2)).toBeNull();
  811. expect(getDataAtCell(0, 3)).toBeNull();
  812. expect(getDataAtCell(1, 0)).toEqual('A2');
  813. expect(getDataAtCell(1, 1)).toBeNull();
  814. expect(getDataAtCell(1, 2)).toBeNull();
  815. expect(getDataAtCell(1, 3)).toBeNull();
  816. HOT.undo();
  817. expect(countCols()).toEqual(4);
  818. expect(getDataAtCell(0, 0)).toEqual('A1');
  819. expect(getDataAtCell(0, 1)).toEqual('B1');
  820. expect(getDataAtCell(0, 2)).toEqual('C1');
  821. expect(getDataAtCell(0, 3)).toEqual('D1');
  822. expect(getDataAtCell(1, 0)).toEqual('A2');
  823. expect(getDataAtCell(1, 1)).toEqual('B2');
  824. expect(getDataAtCell(1, 2)).toEqual('C2');
  825. expect(getDataAtCell(1, 3)).toEqual('D2');
  826. HOT.redo();
  827. expect(countCols()).toEqual(1);
  828. expect(getDataAtCell(0, 0)).toEqual('A1');
  829. expect(getDataAtCell(0, 1)).toBeNull();
  830. expect(getDataAtCell(0, 2)).toBeNull();
  831. expect(getDataAtCell(0, 3)).toBeNull();
  832. expect(getDataAtCell(1, 0)).toEqual('A2');
  833. expect(getDataAtCell(1, 1)).toBeNull();
  834. expect(getDataAtCell(1, 2)).toBeNull();
  835. expect(getDataAtCell(1, 3)).toBeNull();
  836. });
  837. it('should redo multiple changes', () => {
  838. handsontable({
  839. data: Handsontable.helper.createSpreadsheetData(2, 2)
  840. });
  841. var HOT = getInstance();
  842. setDataAtCell(0, 0, 'X1');
  843. setDataAtCell(1, 0, 'X2');
  844. setDataAtCell(0, 1, 'Y1');
  845. setDataAtCell(1, 1, 'Y2');
  846. expect(getDataAtCell(0, 0)).toBe('X1');
  847. expect(getDataAtCell(1, 0)).toBe('X2');
  848. expect(getDataAtCell(0, 1)).toBe('Y1');
  849. expect(getDataAtCell(1, 1)).toBe('Y2');
  850. HOT.undo();
  851. HOT.undo();
  852. HOT.undo();
  853. HOT.undo();
  854. expect(getDataAtCell(0, 0)).toBe('A1');
  855. expect(getDataAtCell(1, 0)).toBe('A2');
  856. expect(getDataAtCell(0, 1)).toBe('B1');
  857. expect(getDataAtCell(1, 1)).toBe('B2');
  858. HOT.redo();
  859. expect(getDataAtCell(0, 0)).toBe('X1');
  860. expect(getDataAtCell(1, 0)).toBe('A2');
  861. expect(getDataAtCell(0, 1)).toBe('B1');
  862. expect(getDataAtCell(1, 1)).toBe('B2');
  863. HOT.redo();
  864. expect(getDataAtCell(0, 0)).toBe('X1');
  865. expect(getDataAtCell(1, 0)).toBe('X2');
  866. expect(getDataAtCell(0, 1)).toBe('B1');
  867. expect(getDataAtCell(1, 1)).toBe('B2');
  868. HOT.redo();
  869. expect(getDataAtCell(0, 0)).toBe('X1');
  870. expect(getDataAtCell(1, 0)).toBe('X2');
  871. expect(getDataAtCell(0, 1)).toBe('Y1');
  872. expect(getDataAtCell(1, 1)).toBe('B2');
  873. HOT.redo();
  874. expect(getDataAtCell(0, 0)).toBe('X1');
  875. expect(getDataAtCell(1, 0)).toBe('X2');
  876. expect(getDataAtCell(0, 1)).toBe('Y1');
  877. expect(getDataAtCell(1, 1)).toBe('Y2');
  878. HOT.redo();
  879. expect(getDataAtCell(0, 0)).toBe('X1');
  880. expect(getDataAtCell(1, 0)).toBe('X2');
  881. expect(getDataAtCell(0, 1)).toBe('Y1');
  882. expect(getDataAtCell(1, 1)).toBe('Y2');
  883. });
  884. it('should redo multiple changes in cell with validator', (done) => {
  885. var HOT = handsontable({
  886. data: Handsontable.helper.createSpreadsheetData(2, 2),
  887. });
  888. setDataAtCell(0, 0, 'X1');
  889. setDataAtCell(1, 0, 'X2');
  890. setDataAtCell(0, 1, 'Y1');
  891. setDataAtCell(1, 1, 'Y2');
  892. setTimeout(() => {
  893. expect(getDataAtCell(0, 0)).toBe('X1');
  894. expect(getDataAtCell(1, 0)).toBe('X2');
  895. expect(getDataAtCell(0, 1)).toBe('Y1');
  896. expect(getDataAtCell(1, 1)).toBe('Y2');
  897. HOT.undo();
  898. }, 200);
  899. setTimeout(() => {
  900. HOT.undo();
  901. }, 400);
  902. setTimeout(() => {
  903. HOT.undo();
  904. }, 600);
  905. setTimeout(() => {
  906. HOT.undo();
  907. }, 800);
  908. setTimeout(() => {
  909. expect(getDataAtCell(0, 0)).toBe('A1');
  910. expect(getDataAtCell(1, 0)).toBe('A2');
  911. expect(getDataAtCell(0, 1)).toBe('B1');
  912. expect(getDataAtCell(1, 1)).toBe('B2');
  913. HOT.redo();
  914. }, 1000);
  915. setTimeout(() => {
  916. expect(getDataAtCell(0, 0)).toBe('X1');
  917. expect(getDataAtCell(1, 0)).toBe('A2');
  918. expect(getDataAtCell(0, 1)).toBe('B1');
  919. expect(getDataAtCell(1, 1)).toBe('B2');
  920. HOT.redo();
  921. }, 1200);
  922. setTimeout(() => {
  923. expect(getDataAtCell(0, 0)).toBe('X1');
  924. expect(getDataAtCell(1, 0)).toBe('X2');
  925. expect(getDataAtCell(0, 1)).toBe('B1');
  926. expect(getDataAtCell(1, 1)).toBe('B2');
  927. HOT.redo();
  928. }, 1400);
  929. setTimeout(() => {
  930. expect(getDataAtCell(0, 0)).toBe('X1');
  931. expect(getDataAtCell(1, 0)).toBe('X2');
  932. expect(getDataAtCell(0, 1)).toBe('Y1');
  933. expect(getDataAtCell(1, 1)).toBe('B2');
  934. HOT.redo();
  935. }, 1600);
  936. setTimeout(() => {
  937. expect(getDataAtCell(0, 0)).toBe('X1');
  938. expect(getDataAtCell(1, 0)).toBe('X2');
  939. expect(getDataAtCell(0, 1)).toBe('Y1');
  940. expect(getDataAtCell(1, 1)).toBe('Y2');
  941. HOT.redo();
  942. }, 1800);
  943. setTimeout(() => {
  944. expect(getDataAtCell(0, 0)).toBe('X1');
  945. expect(getDataAtCell(1, 0)).toBe('X2');
  946. expect(getDataAtCell(0, 1)).toBe('Y1');
  947. expect(getDataAtCell(1, 1)).toBe('Y2');
  948. done();
  949. }, 2000);
  950. });
  951. it('should redo multiple row creations', () => {
  952. var HOT = handsontable({
  953. data: Handsontable.helper.createSpreadsheetData(2, 2)
  954. });
  955. expect(countRows()).toEqual(2);
  956. alter('insert_row');
  957. alter('insert_row');
  958. alter('insert_row');
  959. alter('insert_row');
  960. expect(countRows()).toEqual(6);
  961. HOT.undo();
  962. HOT.undo();
  963. HOT.undo();
  964. HOT.undo();
  965. expect(countRows()).toEqual(2);
  966. HOT.redo();
  967. expect(countRows()).toEqual(3);
  968. HOT.redo();
  969. expect(countRows()).toEqual(4);
  970. HOT.redo();
  971. expect(countRows()).toEqual(5);
  972. HOT.redo();
  973. expect(countRows()).toEqual(6);
  974. HOT.redo();
  975. expect(countRows()).toEqual(6);
  976. });
  977. it('should undo multiple row removals', () => {
  978. var HOT = handsontable({
  979. data: Handsontable.helper.createSpreadsheetData(4, 2)
  980. });
  981. expect(countRows()).toEqual(4);
  982. expect(getDataAtCell(0, 0)).toEqual('A1');
  983. expect(getDataAtCell(0, 1)).toEqual('B1');
  984. expect(getDataAtCell(1, 0)).toEqual('A2');
  985. expect(getDataAtCell(1, 1)).toEqual('B2');
  986. expect(getDataAtCell(2, 0)).toEqual('A3');
  987. expect(getDataAtCell(2, 1)).toEqual('B3');
  988. expect(getDataAtCell(3, 0)).toEqual('A4');
  989. expect(getDataAtCell(3, 1)).toEqual('B4');
  990. alter('remove_row');
  991. alter('remove_row');
  992. alter('remove_row');
  993. expect(countRows()).toEqual(1);
  994. expect(getDataAtCell(0, 0)).toEqual('A1');
  995. expect(getDataAtCell(0, 1)).toEqual('B1');
  996. HOT.undo();
  997. HOT.undo();
  998. HOT.undo();
  999. expect(countRows()).toEqual(4);
  1000. expect(getDataAtCell(0, 0)).toEqual('A1');
  1001. expect(getDataAtCell(0, 1)).toEqual('B1');
  1002. expect(getDataAtCell(1, 0)).toEqual('A2');
  1003. expect(getDataAtCell(1, 1)).toEqual('B2');
  1004. expect(getDataAtCell(2, 0)).toEqual('A3');
  1005. expect(getDataAtCell(2, 1)).toEqual('B3');
  1006. expect(getDataAtCell(3, 0)).toEqual('A4');
  1007. expect(getDataAtCell(3, 1)).toEqual('B4');
  1008. HOT.redo();
  1009. expect(countRows()).toEqual(3);
  1010. expect(getDataAtCell(0, 0)).toEqual('A1');
  1011. expect(getDataAtCell(0, 1)).toEqual('B1');
  1012. expect(getDataAtCell(1, 0)).toEqual('A2');
  1013. expect(getDataAtCell(1, 1)).toEqual('B2');
  1014. expect(getDataAtCell(2, 0)).toEqual('A3');
  1015. expect(getDataAtCell(2, 1)).toEqual('B3');
  1016. HOT.redo();
  1017. expect(countRows()).toEqual(2);
  1018. expect(getDataAtCell(0, 0)).toEqual('A1');
  1019. expect(getDataAtCell(0, 1)).toEqual('B1');
  1020. expect(getDataAtCell(1, 0)).toEqual('A2');
  1021. expect(getDataAtCell(1, 1)).toEqual('B2');
  1022. HOT.redo();
  1023. expect(countRows()).toEqual(1);
  1024. expect(getDataAtCell(0, 0)).toEqual('A1');
  1025. expect(getDataAtCell(0, 1)).toEqual('B1');
  1026. HOT.redo();
  1027. expect(countRows()).toEqual(1);
  1028. expect(getDataAtCell(0, 0)).toEqual('A1');
  1029. expect(getDataAtCell(0, 1)).toEqual('B1');
  1030. });
  1031. it('should redo changes only for table where the change actually took place', function() {
  1032. this.$container2 = $(`<div id="${id}-2"></div>`).appendTo('body');
  1033. var hot1 = handsontable({
  1034. data: [
  1035. [1],
  1036. [2],
  1037. [3]
  1038. ]
  1039. });
  1040. this.$container2.handsontable({
  1041. data: [
  1042. ['A'],
  1043. ['B'],
  1044. ['C']
  1045. ]
  1046. });
  1047. var hot2 = this.$container2.handsontable('getInstance');
  1048. hot1.setDataAtCell(0, 0, 4);
  1049. expect(hot1.getDataAtCell(0, 0)).toEqual(4);
  1050. expect(hot2.getDataAtCell(0, 0)).toEqual('A');
  1051. hot1.undo();
  1052. expect(hot1.getDataAtCell(0, 0)).toEqual(1);
  1053. expect(hot2.getDataAtCell(0, 0)).toEqual('A');
  1054. hot2.redo();
  1055. expect(hot1.getDataAtCell(0, 0)).toEqual(1);
  1056. expect(hot2.getDataAtCell(0, 0)).toEqual('A');
  1057. hot1.redo();
  1058. expect(hot1.getDataAtCell(0, 0)).toEqual(4);
  1059. expect(hot2.getDataAtCell(0, 0)).toEqual('A');
  1060. hot2.destroy();
  1061. this.$container2.remove();
  1062. });
  1063. });
  1064. });
  1065. describe('Object data', () => {
  1066. function createObjectData() {
  1067. return [
  1068. {name: 'Timothy', surname: 'Dalton'},
  1069. {name: 'Sean', surname: 'Connery'},
  1070. {name: 'Roger', surname: 'Moore'}
  1071. ];
  1072. }
  1073. describe('undo', () => {
  1074. it('should undo single change', () => {
  1075. handsontable({
  1076. data: createObjectData()
  1077. });
  1078. var HOT = getInstance();
  1079. setDataAtRowProp(0, 0, 'Pearce');
  1080. expect(getDataAtRowProp(0, 0)).toBe('Pearce');
  1081. HOT.undo();
  1082. expect(getDataAtCell(0, 0)).toBe('Timothy');
  1083. });
  1084. it('should undo single change in cell with validator', (done) => {
  1085. handsontable({
  1086. data: createObjectData(),
  1087. });
  1088. var HOT = getInstance();
  1089. setDataAtRowProp(0, 0, 'Pearce');
  1090. setTimeout(() => {
  1091. expect(getDataAtRowProp(0, 0)).toBe('Pearce');
  1092. HOT.undo();
  1093. }, 200);
  1094. setTimeout(() => {
  1095. expect(getDataAtCell(0, 0)).toBe('Timothy');
  1096. done();
  1097. }, 400);
  1098. });
  1099. it('should undo creation of a single row', () => {
  1100. var HOT = handsontable({
  1101. data: createObjectData().slice(0, 2)
  1102. });
  1103. expect(countRows()).toEqual(2);
  1104. alter('insert_row');
  1105. expect(countRows()).toEqual(3);
  1106. HOT.undo();
  1107. expect(countRows()).toEqual(2);
  1108. });
  1109. it('should undo creation of multiple rows', () => {
  1110. var HOT = handsontable({
  1111. data: createObjectData().slice(0, 2)
  1112. });
  1113. expect(countRows()).toEqual(2);
  1114. alter('insert_row', 0, 5);
  1115. expect(countRows()).toEqual(7);
  1116. HOT.undo();
  1117. expect(countRows()).toEqual(2);
  1118. });
  1119. it('should undo removal of single row', () => {
  1120. var HOT = handsontable({
  1121. data: createObjectData().slice(0, 2)
  1122. });
  1123. expect(countRows()).toEqual(2);
  1124. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1125. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1126. expect(getDataAtRowProp(1, 'name')).toEqual('Sean');
  1127. expect(getDataAtRowProp(1, 'surname')).toEqual('Connery');
  1128. alter('remove_row');
  1129. expect(countRows()).toEqual(1);
  1130. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1131. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1132. expect(getDataAtRowProp(1, 'name')).toBeNull();
  1133. expect(getDataAtRowProp(1, 'surname')).toBeNull();
  1134. HOT.undo();
  1135. expect(countRows()).toEqual(2);
  1136. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1137. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1138. expect(getDataAtRowProp(1, 'name')).toEqual('Sean');
  1139. expect(getDataAtRowProp(1, 'surname')).toEqual('Connery');
  1140. });
  1141. it('should undo removal of multiple rows', () => {
  1142. var HOT = handsontable({
  1143. data: createObjectData()
  1144. });
  1145. expect(countRows()).toEqual(3);
  1146. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1147. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1148. expect(getDataAtRowProp(1, 'name')).toEqual('Sean');
  1149. expect(getDataAtRowProp(1, 'surname')).toEqual('Connery');
  1150. expect(getDataAtRowProp(2, 'name')).toEqual('Roger');
  1151. expect(getDataAtRowProp(2, 'surname')).toEqual('Moore');
  1152. alter('remove_row', 1, 2);
  1153. expect(countRows()).toEqual(1);
  1154. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1155. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1156. expect(getDataAtRowProp(1, 'name')).toBeNull();
  1157. expect(getDataAtRowProp(1, 'surname')).toBeNull();
  1158. expect(getDataAtRowProp(2, 'name')).toBeNull();
  1159. expect(getDataAtRowProp(2, 'surname')).toBeNull();
  1160. HOT.undo();
  1161. expect(countRows()).toEqual(3);
  1162. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1163. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1164. expect(getDataAtRowProp(1, 'name')).toEqual('Sean');
  1165. expect(getDataAtRowProp(1, 'surname')).toEqual('Connery');
  1166. expect(getDataAtRowProp(2, 'name')).toEqual('Roger');
  1167. expect(getDataAtRowProp(2, 'surname')).toEqual('Moore');
  1168. });
  1169. it('should undo multiple changes', () => {
  1170. handsontable({
  1171. data: createObjectData().slice(0, 2)
  1172. });
  1173. var HOT = getInstance();
  1174. setDataAtRowProp(0, 'name', 'Pierce');
  1175. setDataAtRowProp(0, 'surname', 'Brosnan');
  1176. setDataAtRowProp(1, 'name', 'Daniel');
  1177. setDataAtRowProp(1, 'surname', 'Craig');
  1178. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1179. expect(getDataAtRowProp(0, 'surname')).toBe('Brosnan');
  1180. expect(getDataAtRowProp(1, 'name')).toBe('Daniel');
  1181. expect(getDataAtRowProp(1, 'surname')).toBe('Craig');
  1182. HOT.undo();
  1183. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1184. expect(getDataAtRowProp(0, 'surname')).toBe('Brosnan');
  1185. expect(getDataAtRowProp(1, 'name')).toBe('Daniel');
  1186. expect(getDataAtRowProp(1, 'surname')).toBe('Connery');
  1187. HOT.undo();
  1188. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1189. expect(getDataAtRowProp(0, 'surname')).toBe('Brosnan');
  1190. expect(getDataAtRowProp(1, 'name')).toBe('Sean');
  1191. expect(getDataAtRowProp(1, 'surname')).toBe('Connery');
  1192. HOT.undo();
  1193. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1194. expect(getDataAtRowProp(0, 'surname')).toBe('Dalton');
  1195. expect(getDataAtRowProp(1, 'name')).toBe('Sean');
  1196. expect(getDataAtRowProp(1, 'surname')).toBe('Connery');
  1197. HOT.undo();
  1198. expect(getDataAtRowProp(0, 'name')).toBe('Timothy');
  1199. expect(getDataAtRowProp(0, 'surname')).toBe('Dalton');
  1200. expect(getDataAtRowProp(1, 'name')).toBe('Sean');
  1201. expect(getDataAtRowProp(1, 'surname')).toBe('Connery');
  1202. HOT.undo();
  1203. expect(getDataAtRowProp(0, 'name')).toBe('Timothy');
  1204. expect(getDataAtRowProp(0, 'surname')).toBe('Dalton');
  1205. expect(getDataAtRowProp(1, 'name')).toBe('Sean');
  1206. expect(getDataAtRowProp(1, 'surname')).toBe('Connery');
  1207. });
  1208. it('should undo multiple changes in cells with validators', (done) => {
  1209. handsontable({
  1210. data: createObjectData().slice(0, 2),
  1211. });
  1212. var HOT = getInstance();
  1213. setDataAtRowProp(0, 'name', 'Pierce');
  1214. setDataAtRowProp(0, 'surname', 'Brosnan');
  1215. setDataAtRowProp(1, 'name', 'Daniel');
  1216. setDataAtRowProp(1, 'surname', 'Craig');
  1217. setTimeout(() => {
  1218. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1219. expect(getDataAtRowProp(0, 'surname')).toBe('Brosnan');
  1220. expect(getDataAtRowProp(1, 'name')).toBe('Daniel');
  1221. expect(getDataAtRowProp(1, 'surname')).toBe('Craig');
  1222. HOT.undo();
  1223. }, 200);
  1224. setTimeout(() => {
  1225. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1226. expect(getDataAtRowProp(0, 'surname')).toBe('Brosnan');
  1227. expect(getDataAtRowProp(1, 'name')).toBe('Daniel');
  1228. expect(getDataAtRowProp(1, 'surname')).toBe('Connery');
  1229. HOT.undo();
  1230. }, 400);
  1231. setTimeout(() => {
  1232. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1233. expect(getDataAtRowProp(0, 'surname')).toBe('Brosnan');
  1234. expect(getDataAtRowProp(1, 'name')).toBe('Sean');
  1235. expect(getDataAtRowProp(1, 'surname')).toBe('Connery');
  1236. HOT.undo();
  1237. }, 600);
  1238. setTimeout(() => {
  1239. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1240. expect(getDataAtRowProp(0, 'surname')).toBe('Dalton');
  1241. expect(getDataAtRowProp(1, 'name')).toBe('Sean');
  1242. expect(getDataAtRowProp(1, 'surname')).toBe('Connery');
  1243. HOT.undo();
  1244. }, 800);
  1245. setTimeout(() => {
  1246. expect(getDataAtRowProp(0, 'name')).toBe('Timothy');
  1247. expect(getDataAtRowProp(0, 'surname')).toBe('Dalton');
  1248. expect(getDataAtRowProp(1, 'name')).toBe('Sean');
  1249. expect(getDataAtRowProp(1, 'surname')).toBe('Connery');
  1250. HOT.undo();
  1251. }, 1000);
  1252. setTimeout(() => {
  1253. expect(getDataAtRowProp(0, 'name')).toBe('Timothy');
  1254. expect(getDataAtRowProp(0, 'surname')).toBe('Dalton');
  1255. expect(getDataAtRowProp(1, 'name')).toBe('Sean');
  1256. expect(getDataAtRowProp(1, 'surname')).toBe('Connery');
  1257. done();
  1258. }, 1200);
  1259. });
  1260. it('should undo multiple row creations', () => {
  1261. var HOT = handsontable({
  1262. data: createObjectData().slice(0, 2)
  1263. });
  1264. expect(countRows()).toEqual(2);
  1265. alter('insert_row');
  1266. alter('insert_row');
  1267. alter('insert_row');
  1268. alter('insert_row');
  1269. expect(countRows()).toEqual(6);
  1270. HOT.undo();
  1271. expect(countRows()).toEqual(5);
  1272. HOT.undo();
  1273. expect(countRows()).toEqual(4);
  1274. HOT.undo();
  1275. expect(countRows()).toEqual(3);
  1276. HOT.undo();
  1277. expect(countRows()).toEqual(2);
  1278. HOT.undo();
  1279. expect(countRows()).toEqual(2);
  1280. });
  1281. it('should undo multiple row removals', () => {
  1282. var HOT = handsontable({
  1283. data: createObjectData()
  1284. });
  1285. expect(countRows()).toEqual(3);
  1286. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1287. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1288. expect(getDataAtRowProp(1, 'name')).toEqual('Sean');
  1289. expect(getDataAtRowProp(1, 'surname')).toEqual('Connery');
  1290. expect(getDataAtRowProp(2, 'name')).toEqual('Roger');
  1291. expect(getDataAtRowProp(2, 'surname')).toEqual('Moore');
  1292. alter('remove_row');
  1293. alter('remove_row');
  1294. expect(countRows()).toEqual(1);
  1295. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1296. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1297. HOT.undo();
  1298. expect(countRows()).toEqual(2);
  1299. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1300. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1301. expect(getDataAtRowProp(1, 'name')).toEqual('Sean');
  1302. expect(getDataAtRowProp(1, 'surname')).toEqual('Connery');
  1303. HOT.undo();
  1304. expect(countRows()).toEqual(3);
  1305. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1306. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1307. expect(getDataAtRowProp(1, 'name')).toEqual('Sean');
  1308. expect(getDataAtRowProp(1, 'surname')).toEqual('Connery');
  1309. expect(getDataAtRowProp(2, 'name')).toEqual('Roger');
  1310. expect(getDataAtRowProp(2, 'surname')).toEqual('Moore');
  1311. HOT.undo();
  1312. expect(countRows()).toEqual(3);
  1313. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1314. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1315. expect(getDataAtRowProp(1, 'name')).toEqual('Sean');
  1316. expect(getDataAtRowProp(1, 'surname')).toEqual('Connery');
  1317. expect(getDataAtRowProp(2, 'name')).toEqual('Roger');
  1318. expect(getDataAtRowProp(2, 'surname')).toEqual('Moore');
  1319. });
  1320. });
  1321. describe('redo', () => {
  1322. it('should redo single change', () => {
  1323. handsontable({
  1324. data: createObjectData()
  1325. });
  1326. var HOT = getInstance();
  1327. setDataAtRowProp(0, 0, 'Pearce');
  1328. expect(getDataAtRowProp(0, 0)).toBe('Pearce');
  1329. HOT.undo();
  1330. expect(getDataAtCell(0, 0)).toBe('Timothy');
  1331. HOT.redo();
  1332. expect(getDataAtRowProp(0, 0)).toBe('Pearce');
  1333. });
  1334. it('should redo single change in cell with validator', (done) => {
  1335. handsontable({
  1336. data: createObjectData(),
  1337. });
  1338. var HOT = getInstance();
  1339. setDataAtRowProp(0, 0, 'Pearce');
  1340. setTimeout(() => {
  1341. expect(getDataAtRowProp(0, 0)).toBe('Pearce');
  1342. HOT.undo();
  1343. }, 200);
  1344. setTimeout(() => {
  1345. expect(getDataAtCell(0, 0)).toBe('Timothy');
  1346. HOT.redo();
  1347. }, 400);
  1348. setTimeout(() => {
  1349. expect(getDataAtRowProp(0, 0)).toBe('Pearce');
  1350. done();
  1351. }, 600);
  1352. });
  1353. it('should redo creation of a single row', () => {
  1354. var HOT = handsontable({
  1355. data: createObjectData().slice(0, 2)
  1356. });
  1357. expect(countRows()).toEqual(2);
  1358. alter('insert_row');
  1359. expect(countRows()).toEqual(3);
  1360. HOT.undo();
  1361. expect(countRows()).toEqual(2);
  1362. HOT.redo();
  1363. expect(countRows()).toEqual(3);
  1364. });
  1365. it('should redo creation of multiple rows', () => {
  1366. var HOT = handsontable({
  1367. data: createObjectData().slice(0, 2)
  1368. });
  1369. expect(countRows()).toEqual(2);
  1370. alter('insert_row', 0, 5);
  1371. expect(countRows()).toEqual(7);
  1372. HOT.undo();
  1373. expect(countRows()).toEqual(2);
  1374. HOT.redo();
  1375. expect(countRows()).toEqual(7);
  1376. });
  1377. it('should redo removal of single row', () => {
  1378. var HOT = handsontable({
  1379. data: createObjectData().slice(0, 2)
  1380. });
  1381. expect(countRows()).toEqual(2);
  1382. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1383. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1384. expect(getDataAtRowProp(1, 'name')).toEqual('Sean');
  1385. expect(getDataAtRowProp(1, 'surname')).toEqual('Connery');
  1386. alter('remove_row');
  1387. expect(countRows()).toEqual(1);
  1388. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1389. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1390. expect(getDataAtRowProp(1, 'name')).toBeNull();
  1391. expect(getDataAtRowProp(1, 'surname')).toBeNull();
  1392. HOT.undo();
  1393. expect(countRows()).toEqual(2);
  1394. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1395. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1396. expect(getDataAtRowProp(1, 'name')).toEqual('Sean');
  1397. expect(getDataAtRowProp(1, 'surname')).toEqual('Connery');
  1398. HOT.redo();
  1399. expect(countRows()).toEqual(1);
  1400. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1401. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1402. expect(getDataAtRowProp(1, 'name')).toBeNull();
  1403. expect(getDataAtRowProp(1, 'surname')).toBeNull();
  1404. });
  1405. it('should redo removal of multiple rows', () => {
  1406. var HOT = handsontable({
  1407. data: createObjectData()
  1408. });
  1409. expect(countRows()).toEqual(3);
  1410. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1411. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1412. expect(getDataAtRowProp(1, 'name')).toEqual('Sean');
  1413. expect(getDataAtRowProp(1, 'surname')).toEqual('Connery');
  1414. expect(getDataAtRowProp(2, 'name')).toEqual('Roger');
  1415. expect(getDataAtRowProp(2, 'surname')).toEqual('Moore');
  1416. alter('remove_row', 1, 2);
  1417. expect(countRows()).toEqual(1);
  1418. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1419. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1420. expect(getDataAtRowProp(1, 'name')).toBeNull();
  1421. expect(getDataAtRowProp(1, 'surname')).toBeNull();
  1422. expect(getDataAtRowProp(2, 'name')).toBeNull();
  1423. expect(getDataAtRowProp(2, 'surname')).toBeNull();
  1424. HOT.undo();
  1425. expect(countRows()).toEqual(3);
  1426. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1427. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1428. expect(getDataAtRowProp(1, 'name')).toEqual('Sean');
  1429. expect(getDataAtRowProp(1, 'surname')).toEqual('Connery');
  1430. expect(getDataAtRowProp(2, 'name')).toEqual('Roger');
  1431. expect(getDataAtRowProp(2, 'surname')).toEqual('Moore');
  1432. HOT.redo();
  1433. expect(countRows()).toEqual(1);
  1434. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1435. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1436. expect(getDataAtRowProp(1, 'name')).toBeNull();
  1437. expect(getDataAtRowProp(1, 'surname')).toBeNull();
  1438. expect(getDataAtRowProp(2, 'name')).toBeNull();
  1439. expect(getDataAtRowProp(2, 'surname')).toBeNull();
  1440. });
  1441. it('should redo multiple changes', () => {
  1442. handsontable({
  1443. data: createObjectData().slice(0, 2)
  1444. });
  1445. var HOT = getInstance();
  1446. setDataAtRowProp(0, 'name', 'Pierce');
  1447. setDataAtRowProp(0, 'surname', 'Brosnan');
  1448. setDataAtRowProp(1, 'name', 'Daniel');
  1449. setDataAtRowProp(1, 'surname', 'Craig');
  1450. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1451. expect(getDataAtRowProp(0, 'surname')).toBe('Brosnan');
  1452. expect(getDataAtRowProp(1, 'name')).toBe('Daniel');
  1453. expect(getDataAtRowProp(1, 'surname')).toBe('Craig');
  1454. HOT.undo();
  1455. HOT.undo();
  1456. HOT.undo();
  1457. HOT.undo();
  1458. expect(getDataAtRowProp(0, 'name')).toBe('Timothy');
  1459. expect(getDataAtRowProp(0, 'surname')).toBe('Dalton');
  1460. expect(getDataAtRowProp(1, 'name')).toBe('Sean');
  1461. expect(getDataAtRowProp(1, 'surname')).toBe('Connery');
  1462. HOT.redo();
  1463. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1464. expect(getDataAtRowProp(0, 'surname')).toBe('Dalton');
  1465. expect(getDataAtRowProp(1, 'name')).toBe('Sean');
  1466. expect(getDataAtRowProp(1, 'surname')).toBe('Connery');
  1467. HOT.redo();
  1468. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1469. expect(getDataAtRowProp(0, 'surname')).toBe('Brosnan');
  1470. expect(getDataAtRowProp(1, 'name')).toBe('Sean');
  1471. expect(getDataAtRowProp(1, 'surname')).toBe('Connery');
  1472. HOT.redo();
  1473. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1474. expect(getDataAtRowProp(0, 'surname')).toBe('Brosnan');
  1475. expect(getDataAtRowProp(1, 'name')).toBe('Daniel');
  1476. expect(getDataAtRowProp(1, 'surname')).toBe('Connery');
  1477. HOT.redo();
  1478. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1479. expect(getDataAtRowProp(0, 'surname')).toBe('Brosnan');
  1480. expect(getDataAtRowProp(1, 'name')).toBe('Daniel');
  1481. expect(getDataAtRowProp(1, 'surname')).toBe('Craig');
  1482. HOT.redo();
  1483. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1484. expect(getDataAtRowProp(0, 'surname')).toBe('Brosnan');
  1485. expect(getDataAtRowProp(1, 'name')).toBe('Daniel');
  1486. expect(getDataAtRowProp(1, 'surname')).toBe('Craig');
  1487. });
  1488. it('should redo multiple changes in cells with validators', (done) => {
  1489. handsontable({
  1490. data: createObjectData().slice(0, 2),
  1491. });
  1492. var HOT = getInstance();
  1493. setDataAtRowProp(0, 'name', 'Pierce');
  1494. setDataAtRowProp(0, 'surname', 'Brosnan');
  1495. setDataAtRowProp(1, 'name', 'Daniel');
  1496. setDataAtRowProp(1, 'surname', 'Craig');
  1497. setTimeout(() => {
  1498. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1499. expect(getDataAtRowProp(0, 'surname')).toBe('Brosnan');
  1500. expect(getDataAtRowProp(1, 'name')).toBe('Daniel');
  1501. expect(getDataAtRowProp(1, 'surname')).toBe('Craig');
  1502. HOT.undo();
  1503. }, 200);
  1504. setTimeout(() => {
  1505. HOT.undo();
  1506. }, 400);
  1507. setTimeout(() => {
  1508. HOT.undo();
  1509. }, 600);
  1510. setTimeout(() => {
  1511. HOT.undo();
  1512. }, 800);
  1513. setTimeout(() => {
  1514. expect(getDataAtRowProp(0, 'name')).toBe('Timothy');
  1515. expect(getDataAtRowProp(0, 'surname')).toBe('Dalton');
  1516. expect(getDataAtRowProp(1, 'name')).toBe('Sean');
  1517. expect(getDataAtRowProp(1, 'surname')).toBe('Connery');
  1518. HOT.redo();
  1519. }, 1000);
  1520. setTimeout(() => {
  1521. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1522. expect(getDataAtRowProp(0, 'surname')).toBe('Dalton');
  1523. expect(getDataAtRowProp(1, 'name')).toBe('Sean');
  1524. expect(getDataAtRowProp(1, 'surname')).toBe('Connery');
  1525. HOT.redo();
  1526. }, 1200);
  1527. setTimeout(() => {
  1528. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1529. expect(getDataAtRowProp(0, 'surname')).toBe('Brosnan');
  1530. expect(getDataAtRowProp(1, 'name')).toBe('Sean');
  1531. expect(getDataAtRowProp(1, 'surname')).toBe('Connery');
  1532. HOT.redo();
  1533. }, 1400);
  1534. setTimeout(() => {
  1535. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1536. expect(getDataAtRowProp(0, 'surname')).toBe('Brosnan');
  1537. expect(getDataAtRowProp(1, 'name')).toBe('Daniel');
  1538. expect(getDataAtRowProp(1, 'surname')).toBe('Connery');
  1539. HOT.redo();
  1540. }, 1600);
  1541. setTimeout(() => {
  1542. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1543. expect(getDataAtRowProp(0, 'surname')).toBe('Brosnan');
  1544. expect(getDataAtRowProp(1, 'name')).toBe('Daniel');
  1545. expect(getDataAtRowProp(1, 'surname')).toBe('Craig');
  1546. HOT.redo();
  1547. }, 1800);
  1548. setTimeout(() => {
  1549. expect(getDataAtRowProp(0, 'name')).toBe('Pierce');
  1550. expect(getDataAtRowProp(0, 'surname')).toBe('Brosnan');
  1551. expect(getDataAtRowProp(1, 'name')).toBe('Daniel');
  1552. expect(getDataAtRowProp(1, 'surname')).toBe('Craig');
  1553. done();
  1554. }, 2000);
  1555. });
  1556. it('should redo multiple row creations', () => {
  1557. var HOT = handsontable({
  1558. data: createObjectData().slice(0, 2)
  1559. });
  1560. expect(countRows()).toEqual(2);
  1561. alter('insert_row');
  1562. alter('insert_row');
  1563. alter('insert_row');
  1564. alter('insert_row');
  1565. expect(countRows()).toEqual(6);
  1566. HOT.undo();
  1567. HOT.undo();
  1568. HOT.undo();
  1569. HOT.undo();
  1570. expect(countRows()).toEqual(2);
  1571. HOT.redo();
  1572. expect(countRows()).toEqual(3);
  1573. HOT.redo();
  1574. expect(countRows()).toEqual(4);
  1575. HOT.redo();
  1576. expect(countRows()).toEqual(5);
  1577. HOT.redo();
  1578. expect(countRows()).toEqual(6);
  1579. HOT.redo();
  1580. expect(countRows()).toEqual(6);
  1581. });
  1582. it('should undo multiple row removals', () => {
  1583. var HOT = handsontable({
  1584. data: createObjectData()
  1585. });
  1586. expect(countRows()).toEqual(3);
  1587. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1588. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1589. expect(getDataAtRowProp(1, 'name')).toEqual('Sean');
  1590. expect(getDataAtRowProp(1, 'surname')).toEqual('Connery');
  1591. expect(getDataAtRowProp(2, 'name')).toEqual('Roger');
  1592. expect(getDataAtRowProp(2, 'surname')).toEqual('Moore');
  1593. alter('remove_row');
  1594. alter('remove_row');
  1595. expect(countRows()).toEqual(1);
  1596. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1597. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1598. HOT.undo();
  1599. HOT.undo();
  1600. HOT.undo();
  1601. expect(countRows()).toEqual(3);
  1602. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1603. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1604. expect(getDataAtRowProp(1, 'name')).toEqual('Sean');
  1605. expect(getDataAtRowProp(1, 'surname')).toEqual('Connery');
  1606. expect(getDataAtRowProp(2, 'name')).toEqual('Roger');
  1607. expect(getDataAtRowProp(2, 'surname')).toEqual('Moore');
  1608. HOT.redo();
  1609. expect(countRows()).toEqual(2);
  1610. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1611. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1612. expect(getDataAtRowProp(1, 'name')).toEqual('Sean');
  1613. expect(getDataAtRowProp(1, 'surname')).toEqual('Connery');
  1614. HOT.redo();
  1615. expect(countRows()).toEqual(1);
  1616. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1617. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1618. HOT.redo();
  1619. expect(countRows()).toEqual(1);
  1620. expect(getDataAtRowProp(0, 'name')).toEqual('Timothy');
  1621. expect(getDataAtRowProp(0, 'surname')).toEqual('Dalton');
  1622. });
  1623. });
  1624. });
  1625. });
  1626. describe('plugin features', () => {
  1627. describe('cell alignment', () => {
  1628. it('should undo a sequence of aligning cells', () => {
  1629. var hot = handsontable({
  1630. data: Handsontable.helper.createSpreadsheetData(9, 9),
  1631. contextMenu: true,
  1632. colWidths: [50, 50, 50, 50, 50, 50, 50, 50, 50],
  1633. rowHeights: [50, 50, 50, 50, 50, 50, 50, 50, 50]
  1634. });
  1635. // top 3 rows center
  1636. selectCell(0, 0, 2, 8);
  1637. hot.getPlugin('contextMenu').executeCommand('alignment:center');
  1638. // middle 3 rows unchanged - left
  1639. // bottom 3 rows right
  1640. selectCell(6, 0, 8, 8);
  1641. hot.getPlugin('contextMenu').executeCommand('alignment:right');
  1642. // left 3 columns - middle
  1643. selectCell(0, 0, 8, 2);
  1644. hot.getPlugin('contextMenu').executeCommand('alignment:middle');
  1645. // middle 3 columns unchanged - top
  1646. // right 3 columns - bottom
  1647. selectCell(0, 6, 8, 8);
  1648. hot.getPlugin('contextMenu').executeCommand('alignment:bottom');
  1649. var cellMeta = hot.getCellMeta(0, 0);
  1650. expect(cellMeta.className.indexOf('htCenter')).toBeGreaterThan(-1);
  1651. expect(cellMeta.className.indexOf('htMiddle')).toBeGreaterThan(-1);
  1652. cellMeta = hot.getCellMeta(0, 7);
  1653. expect(cellMeta.className.indexOf('htCenter')).toBeGreaterThan(-1);
  1654. expect(cellMeta.className.indexOf('htBottom')).toBeGreaterThan(-1);
  1655. cellMeta = hot.getCellMeta(5, 1);
  1656. expect(cellMeta.className.indexOf('htMiddle')).toBeGreaterThan(-1);
  1657. cellMeta = hot.getCellMeta(5, 7);
  1658. expect(cellMeta.className.indexOf('htBottom')).toBeGreaterThan(-1);
  1659. cellMeta = hot.getCellMeta(7, 1);
  1660. expect(cellMeta.className.indexOf('htRight')).toBeGreaterThan(-1);
  1661. expect(cellMeta.className.indexOf('htMiddle')).toBeGreaterThan(-1);
  1662. cellMeta = hot.getCellMeta(7, 5);
  1663. expect(cellMeta.className.indexOf('htRight')).toBeGreaterThan(-1);
  1664. cellMeta = hot.getCellMeta(7, 7);
  1665. expect(cellMeta.className.indexOf('htRight')).toBeGreaterThan(-1);
  1666. expect(cellMeta.className.indexOf('htBottom')).toBeGreaterThan(-1);
  1667. hot.undo();
  1668. cellMeta = hot.getCellMeta(0, 7);
  1669. expect(cellMeta.className.indexOf('htCenter')).toBeGreaterThan(-1);
  1670. expect(cellMeta.className.indexOf('htBottom')).toEqual(-1);
  1671. cellMeta = hot.getCellMeta(5, 7);
  1672. expect(cellMeta.className.indexOf('htBottom')).toEqual(-1);
  1673. cellMeta = hot.getCellMeta(7, 7);
  1674. expect(cellMeta.className.indexOf('htRight')).toBeGreaterThan(-1);
  1675. expect(cellMeta.className.indexOf('htBottom')).toEqual(-1);
  1676. hot.undo();
  1677. cellMeta = hot.getCellMeta(0, 0);
  1678. expect(cellMeta.className.indexOf('htCenter')).toBeGreaterThan(-1);
  1679. expect(cellMeta.className.indexOf('htMiddle')).toEqual(-1);
  1680. cellMeta = hot.getCellMeta(5, 1);
  1681. expect(cellMeta.className.indexOf('htMiddle')).toEqual(-1);
  1682. cellMeta = hot.getCellMeta(7, 1);
  1683. expect(cellMeta.className.indexOf('htRight')).toBeGreaterThan(-1);
  1684. expect(cellMeta.className.indexOf('htMiddle')).toEqual(-1);
  1685. hot.undo();
  1686. cellMeta = hot.getCellMeta(7, 1);
  1687. expect(cellMeta.className.indexOf('htRight')).toEqual(-1);
  1688. expect(cellMeta.className.indexOf('htMiddle')).toEqual(-1);
  1689. cellMeta = hot.getCellMeta(7, 5);
  1690. expect(cellMeta.className.indexOf('htRight')).toEqual(-1);
  1691. cellMeta = hot.getCellMeta(7, 7);
  1692. expect(cellMeta.className.indexOf('htRight')).toEqual(-1);
  1693. expect(cellMeta.className.indexOf('htBottom')).toEqual(-1);
  1694. hot.undo();
  1695. // check if all cells are either non-adjusted or adjusted to the left (as default)
  1696. var finish;
  1697. for (var i = 0; i < 9; i++) {
  1698. for (var j = 0; j < 9; j++) {
  1699. cellMeta = hot.getCellMeta(i, j);
  1700. finish = cellMeta.className === void 0 || cellMeta.className.trim() === '' || cellMeta.className.trim() === 'htLeft';
  1701. expect(finish).toBe(true);
  1702. }
  1703. }
  1704. });
  1705. it('should redo a sequence of aligning cells', () => {
  1706. var hot = handsontable({
  1707. data: Handsontable.helper.createSpreadsheetData(9, 9),
  1708. contextMenu: true,
  1709. colWidths: [50, 50, 50, 50, 50, 50, 50, 50, 50],
  1710. rowHeights: [50, 50, 50, 50, 50, 50, 50, 50, 50]
  1711. });
  1712. // top 3 rows center
  1713. selectCell(0, 0, 2, 8);
  1714. hot.getPlugin('contextMenu').executeCommand('alignment:center');
  1715. // middle 3 rows unchanged - left
  1716. // bottom 3 rows right
  1717. selectCell(6, 0, 8, 8);
  1718. hot.getPlugin('contextMenu').executeCommand('alignment:right');
  1719. // left 3 columns - middle
  1720. selectCell(0, 0, 8, 2);
  1721. hot.getPlugin('contextMenu').executeCommand('alignment:middle');
  1722. // middle 3 columns unchanged - top
  1723. // right 3 columns - bottom
  1724. selectCell(0, 6, 8, 8);
  1725. hot.getPlugin('contextMenu').executeCommand('alignment:bottom');
  1726. var cellMeta = hot.getCellMeta(0, 0);
  1727. expect(cellMeta.className.indexOf('htCenter')).toBeGreaterThan(-1);
  1728. expect(cellMeta.className.indexOf('htMiddle')).toBeGreaterThan(-1);
  1729. cellMeta = hot.getCellMeta(0, 7);
  1730. expect(cellMeta.className.indexOf('htCenter')).toBeGreaterThan(-1);
  1731. expect(cellMeta.className.indexOf('htBottom')).toBeGreaterThan(-1);
  1732. cellMeta = hot.getCellMeta(5, 1);
  1733. expect(cellMeta.className.indexOf('htMiddle')).toBeGreaterThan(-1);
  1734. cellMeta = hot.getCellMeta(5, 7);
  1735. expect(cellMeta.className.indexOf('htBottom')).toBeGreaterThan(-1);
  1736. cellMeta = hot.getCellMeta(7, 1);
  1737. expect(cellMeta.className.indexOf('htRight')).toBeGreaterThan(-1);
  1738. expect(cellMeta.className.indexOf('htMiddle')).toBeGreaterThan(-1);
  1739. cellMeta = hot.getCellMeta(7, 5);
  1740. expect(cellMeta.className.indexOf('htRight')).toBeGreaterThan(-1);
  1741. cellMeta = hot.getCellMeta(7, 7);
  1742. expect(cellMeta.className.indexOf('htRight')).toBeGreaterThan(-1);
  1743. expect(cellMeta.className.indexOf('htBottom')).toBeGreaterThan(-1);
  1744. hot.undo();
  1745. hot.undo();
  1746. hot.undo();
  1747. hot.undo();
  1748. // check if all cells are either non-adjusted or adjusted to the left (as default)
  1749. var finish;
  1750. for (var i = 0; i < 9; i++) {
  1751. for (var j = 0; j < 9; j++) {
  1752. cellMeta = hot.getCellMeta(i, j);
  1753. finish = cellMeta.className === void 0 || cellMeta.className.trim() === '' || cellMeta.className.trim() === 'htLeft';
  1754. expect(finish).toBe(true);
  1755. }
  1756. }
  1757. hot.redo();
  1758. cellMeta = hot.getCellMeta(0, 0);
  1759. expect(cellMeta.className.indexOf('htCenter')).toBeGreaterThan(-1);
  1760. cellMeta = hot.getCellMeta(1, 5);
  1761. expect(cellMeta.className.indexOf('htCenter')).toBeGreaterThan(-1);
  1762. cellMeta = hot.getCellMeta(2, 8);
  1763. expect(cellMeta.className.indexOf('htCenter')).toBeGreaterThan(-1);
  1764. hot.redo();
  1765. cellMeta = hot.getCellMeta(6, 0);
  1766. expect(cellMeta.className.indexOf('htRight')).toBeGreaterThan(-1);
  1767. cellMeta = hot.getCellMeta(7, 5);
  1768. expect(cellMeta.className.indexOf('htRight')).toBeGreaterThan(-1);
  1769. cellMeta = hot.getCellMeta(8, 8);
  1770. expect(cellMeta.className.indexOf('htRight')).toBeGreaterThan(-1);
  1771. hot.redo();
  1772. cellMeta = hot.getCellMeta(0, 0);
  1773. expect(cellMeta.className.indexOf('htMiddle')).toBeGreaterThan(-1);
  1774. expect(cellMeta.className.indexOf('htCenter')).toBeGreaterThan(-1);
  1775. cellMeta = hot.getCellMeta(5, 1);
  1776. expect(cellMeta.className.indexOf('htMiddle')).toBeGreaterThan(-1);
  1777. cellMeta = hot.getCellMeta(8, 2);
  1778. expect(cellMeta.className.indexOf('htMiddle')).toBeGreaterThan(-1);
  1779. expect(cellMeta.className.indexOf('htRight')).toBeGreaterThan(-1);
  1780. hot.redo();
  1781. cellMeta = hot.getCellMeta(0, 6);
  1782. expect(cellMeta.className.indexOf('htBottom')).toBeGreaterThan(-1);
  1783. expect(cellMeta.className.indexOf('htCenter')).toBeGreaterThan(-1);
  1784. cellMeta = hot.getCellMeta(5, 7);
  1785. expect(cellMeta.className.indexOf('htBottom')).toBeGreaterThan(-1);
  1786. cellMeta = hot.getCellMeta(8, 8);
  1787. expect(cellMeta.className.indexOf('htBottom')).toBeGreaterThan(-1);
  1788. expect(cellMeta.className.indexOf('htRight')).toBeGreaterThan(-1);
  1789. });
  1790. });
  1791. it('should exposed new methods when plugin is enabled', () => {
  1792. var hot = handsontable({
  1793. undo: false
  1794. });
  1795. expect(hot.undo).toBeUndefined();
  1796. expect(hot.redo).toBeUndefined();
  1797. expect(hot.isUndoAvailable).toBeUndefined();
  1798. expect(hot.isRedoAvailable).toBeUndefined();
  1799. expect(hot.clearUndo).toBeUndefined();
  1800. updateSettings({
  1801. undo: true
  1802. });
  1803. expect(typeof hot.undo).toEqual('function');
  1804. expect(typeof hot.redo).toEqual('function');
  1805. expect(typeof hot.isUndoAvailable).toEqual('function');
  1806. expect(typeof hot.isRedoAvailable).toEqual('function');
  1807. expect(typeof hot.clearUndo).toEqual('function');
  1808. });
  1809. it('should remove exposed methods when plugin is disbaled', () => {
  1810. var hot = handsontable({
  1811. undo: true
  1812. });
  1813. expect(typeof hot.undo).toEqual('function');
  1814. expect(typeof hot.redo).toEqual('function');
  1815. expect(typeof hot.isUndoAvailable).toEqual('function');
  1816. expect(typeof hot.isRedoAvailable).toEqual('function');
  1817. expect(typeof hot.clearUndo).toEqual('function');
  1818. updateSettings({
  1819. undo: false
  1820. });
  1821. expect(hot.undo).toBeUndefined();
  1822. expect(hot.redo).toBeUndefined();
  1823. expect(hot.isUndoAvailable).toBeUndefined();
  1824. expect(hot.isRedoAvailable).toBeUndefined();
  1825. expect(hot.clearUndo).toBeUndefined();
  1826. });
  1827. describe('Keyboard shortcuts', () => {
  1828. it('should undo single change after hitting CTRL+Z', function() {
  1829. handsontable({
  1830. data: Handsontable.helper.createSpreadsheetData(2, 2)
  1831. });
  1832. var HOT = getInstance();
  1833. selectCell(0, 0);
  1834. setDataAtCell(0, 0, 'new value');
  1835. this.$container.simulate('keydown', {ctrlKey: true, keyCode: 'Z'.charCodeAt(0)});
  1836. expect(getDataAtCell(0, 0)).toBe('A1');
  1837. });
  1838. it('should redo single change after hitting CTRL+Y', function() {
  1839. handsontable({
  1840. data: Handsontable.helper.createSpreadsheetData(2, 2)
  1841. });
  1842. var HOT = getInstance();
  1843. selectCell(0, 0);
  1844. setDataAtCell(0, 0, 'new value');
  1845. expect(getDataAtCell(0, 0)).toBe('new value');
  1846. HOT.undo();
  1847. expect(getDataAtCell(0, 0)).toBe('A1');
  1848. this.$container.simulate('keydown', {ctrlKey: true, keyCode: 'Y'.charCodeAt(0)});
  1849. expect(getDataAtCell(0, 0)).toBe('new value');
  1850. });
  1851. it('should redo single change after hitting CTRL+SHIFT+Z', function() {
  1852. handsontable({
  1853. data: Handsontable.helper.createSpreadsheetData(2, 2)
  1854. });
  1855. var HOT = getInstance();
  1856. selectCell(0, 0);
  1857. setDataAtCell(0, 0, 'new value');
  1858. expect(getDataAtCell(0, 0)).toBe('new value');
  1859. HOT.undo();
  1860. expect(getDataAtCell(0, 0)).toBe('A1');
  1861. this.$container.simulate('keydown', {ctrlKey: true, shiftKey: true, keyCode: 'Z'.charCodeAt(0)});
  1862. expect(getDataAtCell(0, 0)).toBe('new value');
  1863. });
  1864. });
  1865. });
  1866. describe('Hooks', () => {
  1867. it('should fire a `beforeUndo` hook after the undo process begins', (done) => {
  1868. var beforeUndoSpy = jasmine.createSpy('beforeUndo');
  1869. var hot = handsontable({
  1870. data: Handsontable.helper.createSpreadsheetData(2, 2),
  1871. });
  1872. var hookData = null;
  1873. hot.addHook('beforeUndo', beforeUndoSpy);
  1874. hot.addHook('beforeUndo', (data) => {
  1875. hookData = data;
  1876. });
  1877. alter('remove_row', 1);
  1878. setTimeout(() => {
  1879. hot.undo();
  1880. }, 10);
  1881. setTimeout(() => {
  1882. expect(beforeUndoSpy.calls.count()).toEqual(1);
  1883. expect(hookData).not.toBe(null);
  1884. expect(hookData.actionType).toEqual('remove_row');
  1885. expect(hookData.data).toEqual([['A2', 'B2']]);
  1886. done();
  1887. }, 100);
  1888. });
  1889. it('should fire a `beforeRedo` hook before the redo process begins', (done) => {
  1890. var beforeRedoSpy = jasmine.createSpy('beforeRedo');
  1891. var hot = handsontable({
  1892. data: Handsontable.helper.createSpreadsheetData(2, 2),
  1893. });
  1894. var hookData = null;
  1895. hot.addHook('beforeRedo', beforeRedoSpy);
  1896. hot.addHook('beforeRedo', (data) => {
  1897. hookData = data;
  1898. });
  1899. alter('remove_row', 1);
  1900. setTimeout(() => {
  1901. hot.undo();
  1902. hot.redo();
  1903. }, 10);
  1904. setTimeout(() => {
  1905. expect(beforeRedoSpy.calls.count()).toEqual(1);
  1906. expect(hookData).not.toBe(null);
  1907. expect(hookData.actionType).toEqual('remove_row');
  1908. expect(hookData.data).toEqual([['A2', 'B2']]);
  1909. done();
  1910. }, 100);
  1911. });
  1912. it('should fire a `afterRedo` hook after the redo process begins', (done) => {
  1913. var afterRedoSpy = jasmine.createSpy('afterRedo');
  1914. var hot = handsontable({
  1915. data: Handsontable.helper.createSpreadsheetData(2, 2),
  1916. });
  1917. var hookData = null;
  1918. hot.addHook('beforeRedo', afterRedoSpy);
  1919. hot.addHook('beforeRedo', (data) => {
  1920. hookData = data;
  1921. });
  1922. alter('remove_row', 1);
  1923. setTimeout(() => {
  1924. hot.undo();
  1925. hot.redo();
  1926. }, 10);
  1927. setTimeout(() => {
  1928. expect(afterRedoSpy.calls.count()).toEqual(1);
  1929. expect(hookData).not.toBe(null);
  1930. expect(hookData.actionType).toEqual('remove_row');
  1931. expect(hookData.data).toEqual([['A2', 'B2']]);
  1932. done();
  1933. }, 100);
  1934. });
  1935. });
  1936. });