index.spec.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. describe('i18n', () => {
  2. const id = 'testContainer';
  3. const DEFAULT_LANGUAGE_CODE = 'en-US';
  4. const NOT_EXISTING_LANGUAGE_CODE = 'bs-GY';
  5. const NOT_EXISTING_LANGUAGE_CODE2 = 'dd-Da';
  6. const POLISH_LANGUAGE_CODE = 'pl-PL';
  7. const INSERT_ROW_ABOVE_IN_DEFAULT_LANGUAGE = 'Insert row above';
  8. const INSERT_ROW_ABOVE_IN_POLISH_LANGUAGE = 'Wstaw wiersz powyżej';
  9. beforeEach(function() {
  10. this.$container = $(`<div id="${id}"></div>`).appendTo('body');
  11. });
  12. afterEach(function() {
  13. if (this.$container) {
  14. destroy();
  15. this.$container.remove();
  16. }
  17. });
  18. it('should not propagate `language` key to meta of cells', () => {
  19. handsontable({
  20. language: POLISH_LANGUAGE_CODE
  21. });
  22. expect(getCellMeta(0, 0).language).toBeUndefined();
  23. });
  24. describe('Hook `beforeLanguageChange`', () => {
  25. it('should not call the `beforeLanguageChange` at start (`language` key have not been set)', () => {
  26. let beforeLanguageChangeCalled = false;
  27. handsontable({
  28. beforeLanguageChange() {
  29. beforeLanguageChangeCalled = true;
  30. }
  31. });
  32. expect(beforeLanguageChangeCalled).toEqual(false);
  33. });
  34. it('should not call the `beforeLanguageChange` at start (`language` key have been set)', () => {
  35. let beforeLanguageChangeCalled = false;
  36. handsontable({
  37. language: POLISH_LANGUAGE_CODE,
  38. beforeLanguageChange() {
  39. beforeLanguageChangeCalled = true;
  40. }
  41. });
  42. expect(beforeLanguageChangeCalled).toEqual(false);
  43. });
  44. it('should call the `beforeLanguageChange` before updating settings', () => {
  45. let languageInsideHook;
  46. handsontable({
  47. beforeLanguageChange() {
  48. const settings = this.getSettings();
  49. languageInsideHook = settings.language;
  50. }
  51. });
  52. updateSettings({
  53. language: POLISH_LANGUAGE_CODE
  54. });
  55. expect(languageInsideHook).toEqual(DEFAULT_LANGUAGE_CODE);
  56. });
  57. });
  58. describe('Hook `afterLanguageChange`', () => {
  59. it('should not call the `afterLanguageChange` at start (`language` key have not been set)', () => {
  60. let afterLanguageChangeCalled = false;
  61. handsontable({
  62. afterLanguageChange() {
  63. afterLanguageChangeCalled = true;
  64. }
  65. });
  66. expect(afterLanguageChangeCalled).toEqual(false);
  67. });
  68. it('should not call the `afterLanguageChange` at start (`language` key have been set)', () => {
  69. let afterLanguageChangeCalled = false;
  70. handsontable({
  71. language: POLISH_LANGUAGE_CODE,
  72. afterLanguageChange() {
  73. afterLanguageChangeCalled = true;
  74. }
  75. });
  76. expect(afterLanguageChangeCalled).toEqual(false);
  77. });
  78. it('should call the `afterLanguageChange` after updating settings', () => {
  79. let languageInsideHook;
  80. handsontable({
  81. afterLanguageChange() {
  82. const settings = this.getSettings();
  83. languageInsideHook = settings.language;
  84. }
  85. });
  86. updateSettings({
  87. language: POLISH_LANGUAGE_CODE
  88. });
  89. expect(languageInsideHook).toEqual(POLISH_LANGUAGE_CODE);
  90. });
  91. });
  92. describe('translation does not throw exceptions', () => {
  93. it('should not throw error when setting not existing language code at start', async() => {
  94. spyOn(console, 'error'); // overriding console.error
  95. const spy = spyOn(window, 'onerror');
  96. handsontable({
  97. language: NOT_EXISTING_LANGUAGE_CODE
  98. });
  99. await sleep(100);
  100. expect(spy).not.toHaveBeenCalled();
  101. });
  102. it('should not throw error when setting directly default language code at start', async() => {
  103. const spy = spyOn(window, 'onerror');
  104. handsontable({
  105. language: DEFAULT_LANGUAGE_CODE
  106. });
  107. await sleep(100);
  108. expect(spy).not.toHaveBeenCalled();
  109. });
  110. it('should not throw error when trying to set not existing language code by updateSettings', async() => {
  111. spyOn(console, 'error'); // overriding console.error
  112. const spy = spyOn(window, 'onerror');
  113. handsontable();
  114. updateSettings({ language: NOT_EXISTING_LANGUAGE_CODE });
  115. await sleep(100);
  116. expect(spy).not.toHaveBeenCalled();
  117. });
  118. it('should not throw error when trying to set directly default language code by updateSettings', async() => {
  119. const spy = spyOn(window, 'onerror');
  120. handsontable();
  121. updateSettings({ language: DEFAULT_LANGUAGE_CODE });
  122. await sleep(100);
  123. expect(spy).not.toHaveBeenCalled();
  124. });
  125. });
  126. describe('translation log error when needed', () => {
  127. it('should log error when setting not existing language code at start', () => {
  128. const spy = spyOn(console, 'error');
  129. handsontable({
  130. language: NOT_EXISTING_LANGUAGE_CODE
  131. });
  132. expect(spy).toHaveBeenCalled();
  133. });
  134. it('should log error when trying to set not existing language code by updateSettings', () => {
  135. const spy = spyOn(console, 'error');
  136. handsontable();
  137. updateSettings({ language: NOT_EXISTING_LANGUAGE_CODE });
  138. expect(spy).toHaveBeenCalled();
  139. });
  140. it('should not log error when setting directly default language code at start', () => {
  141. const spy = spyOn(console, 'error');
  142. handsontable({
  143. language: DEFAULT_LANGUAGE_CODE
  144. });
  145. expect(spy).not.toHaveBeenCalled();
  146. });
  147. it('should not log error when trying to set directly default language code by updateSettings', () => {
  148. const spy = spyOn(console, 'error');
  149. handsontable();
  150. updateSettings({ language: DEFAULT_LANGUAGE_CODE });
  151. expect(spy).not.toHaveBeenCalled();
  152. });
  153. });
  154. describe('settings', () => {
  155. it('should set default language code at start', () => {
  156. const hot = handsontable();
  157. expect(hot.getSettings().language).toEqual(DEFAULT_LANGUAGE_CODE);
  158. });
  159. it('should not set language code as own property of settings object at start', () => {
  160. const hot = handsontable();
  161. // eslint-disable-next-line no-prototype-builtins
  162. expect(hot.getSettings().hasOwnProperty('language')).toEqual(false);
  163. });
  164. it('should not set language code as own property of settings object when using updateSettings', () => {
  165. const hot = handsontable();
  166. updateSettings({ language: POLISH_LANGUAGE_CODE });
  167. // eslint-disable-next-line no-prototype-builtins
  168. expect(hot.getSettings().hasOwnProperty('language')).toEqual(false);
  169. });
  170. it('should set proper `language` key when trying to set not existing language code at start', () => {
  171. spyOn(console, 'error'); // overriding console.error
  172. const hot = handsontable({
  173. language: NOT_EXISTING_LANGUAGE_CODE
  174. });
  175. expect(hot.getSettings().language).toEqual(DEFAULT_LANGUAGE_CODE);
  176. });
  177. it('should set proper `language` key when trying to set not existing language code by updateSettings #1', () => {
  178. spyOn(console, 'error'); // overriding console.error
  179. const hot = handsontable();
  180. updateSettings({ language: NOT_EXISTING_LANGUAGE_CODE });
  181. expect(hot.getSettings().language).toEqual(DEFAULT_LANGUAGE_CODE);
  182. });
  183. it('should set proper `language` key when trying to set not existing language code by updateSettings #2', () => {
  184. spyOn(console, 'error'); // overriding console.error
  185. const hot = handsontable({
  186. language: POLISH_LANGUAGE_CODE
  187. });
  188. updateSettings({ language: NOT_EXISTING_LANGUAGE_CODE });
  189. expect(hot.getSettings().language).toEqual(POLISH_LANGUAGE_CODE);
  190. });
  191. it('should accept not normalized language code by default #1', () => {
  192. const hot = handsontable({
  193. language: POLISH_LANGUAGE_CODE.toLowerCase()
  194. });
  195. expect(hot.getSettings().language).toEqual(POLISH_LANGUAGE_CODE);
  196. });
  197. it('should accept not normalized language code by default #2', () => {
  198. const hot = handsontable();
  199. updateSettings({
  200. language: POLISH_LANGUAGE_CODE.toUpperCase()
  201. });
  202. expect(hot.getSettings().language).toEqual(POLISH_LANGUAGE_CODE);
  203. });
  204. it('should not change language when `language` key passed to `updateSettings` was not set', () => {
  205. const hot = handsontable({
  206. language: POLISH_LANGUAGE_CODE
  207. });
  208. updateSettings({
  209. fillHandle: true
  210. });
  211. expect(hot.getSettings().language).toEqual(POLISH_LANGUAGE_CODE);
  212. });
  213. });
  214. describe('contextMenu translation', () => {
  215. it('should translate contextMenu UI when setting existing language code at start', () => {
  216. handsontable({
  217. language: POLISH_LANGUAGE_CODE,
  218. contextMenu: ['row_above']
  219. });
  220. selectCell(0, 0);
  221. contextMenu();
  222. const $contextMenuItem = $('.htContextMenu tbody td:not(.htSeparator)');
  223. expect($contextMenuItem.text()).toEqual(INSERT_ROW_ABOVE_IN_POLISH_LANGUAGE);
  224. });
  225. it('should not change default contextMenu UI when trying to set not existing language code at start', () => {
  226. spyOn(console, 'error'); // overriding console.error
  227. handsontable({
  228. language: NOT_EXISTING_LANGUAGE_CODE,
  229. contextMenu: ['row_above']
  230. });
  231. selectCell(0, 0);
  232. contextMenu();
  233. const $contextMenuItem = $('.htContextMenu tbody td:not(.htSeparator)');
  234. expect($contextMenuItem.text()).toEqual(INSERT_ROW_ABOVE_IN_DEFAULT_LANGUAGE);
  235. });
  236. it('should translate contextMenu UI when setting existing language code by updateSettings', async() => {
  237. handsontable({
  238. contextMenu: ['row_above']
  239. });
  240. updateSettings({ language: POLISH_LANGUAGE_CODE });
  241. await sleep(0);
  242. contextMenu();
  243. const $contextMenuItem = $('.htContextMenu tbody td:not(.htSeparator)');
  244. expect($contextMenuItem.text()).toEqual(INSERT_ROW_ABOVE_IN_POLISH_LANGUAGE);
  245. });
  246. it('should not change default contextMenu UI when trying to set not existing language code by updateSettings #1', async() => {
  247. spyOn(console, 'error'); // overriding console.error
  248. handsontable({
  249. contextMenu: ['row_above']
  250. });
  251. updateSettings({ language: NOT_EXISTING_LANGUAGE_CODE });
  252. await sleep(0);
  253. contextMenu();
  254. const $contextMenuItem = $('.htContextMenu tbody td:not(.htSeparator)');
  255. expect($contextMenuItem.text()).toEqual(INSERT_ROW_ABOVE_IN_DEFAULT_LANGUAGE);
  256. });
  257. it('should not change default contextMenu UI when trying to set not existing language code by updateSettings #2', async() => {
  258. spyOn(console, 'error'); // overriding console.error
  259. handsontable({
  260. language: NOT_EXISTING_LANGUAGE_CODE,
  261. contextMenu: ['row_above']
  262. });
  263. updateSettings({ language: NOT_EXISTING_LANGUAGE_CODE2 });
  264. await sleep(0);
  265. contextMenu();
  266. const $contextMenuItem = $('.htContextMenu tbody td:not(.htSeparator)');
  267. expect($contextMenuItem.text()).toEqual(INSERT_ROW_ABOVE_IN_DEFAULT_LANGUAGE);
  268. });
  269. it('should not change previously translated contextMenu UI when trying to set not existing language code by updateSettings', async() => {
  270. spyOn(console, 'error'); // overriding console.error
  271. handsontable({
  272. language: POLISH_LANGUAGE_CODE,
  273. contextMenu: ['row_above']
  274. });
  275. updateSettings({ language: NOT_EXISTING_LANGUAGE_CODE });
  276. await sleep(0);
  277. contextMenu();
  278. const $contextMenuItem = $('.htContextMenu tbody td:not(.htSeparator)');
  279. expect($contextMenuItem.text()).toEqual(INSERT_ROW_ABOVE_IN_POLISH_LANGUAGE);
  280. });
  281. it('should translate multi-level menu properly', async() => {
  282. const ALIGN_LEFT_IN_POLISH = 'Do lewej';
  283. handsontable({
  284. language: POLISH_LANGUAGE_CODE,
  285. contextMenu: ['alignment']
  286. });
  287. selectCell(0, 0);
  288. contextMenu();
  289. const $menu = $('.htSubmenu');
  290. $menu.simulate('mouseover');
  291. await sleep(300);
  292. const $submenuItem = $('.htContextMenu').eq(1).find('tbody td:not(.htSeparator)').eq(0);
  293. expect($submenuItem.text()).toEqual(ALIGN_LEFT_IN_POLISH);
  294. });
  295. it('should choose proper form of phrase when translating', () => {
  296. const REMOVE_ROW_PLURAL_IN_DEFAULT_LANGUAGE = 'Remove rows';
  297. const REMOVE_COLUMN_PLURAL_IN_DEFAULT_LANGUAGE = 'Remove columns';
  298. handsontable({
  299. contextMenu: ['remove_row', 'remove_col']
  300. });
  301. selectCell(0, 0, 2, 2);
  302. contextMenu();
  303. const $removeRowItem = $('.htContextMenu').eq(0).find('tbody td:not(.htSeparator)').eq(0);
  304. const $removeColumnItem = $('.htContextMenu').eq(0).find('tbody td:not(.htSeparator)').eq(1);
  305. expect($removeRowItem.text()).toEqual(REMOVE_ROW_PLURAL_IN_DEFAULT_LANGUAGE);
  306. expect($removeColumnItem.text()).toEqual(REMOVE_COLUMN_PLURAL_IN_DEFAULT_LANGUAGE);
  307. });
  308. it('should translate item from enabled `freezeColumn` plugin when setting existing language code at start', () => {
  309. const FREEZE_COLUMN_IN_POLISH_LANGUAGE = 'Zablokuj kolumnę';
  310. handsontable({
  311. contextMenu: ['freeze_column'],
  312. manualColumnFreeze: true,
  313. language: POLISH_LANGUAGE_CODE,
  314. });
  315. selectCell(0, 0);
  316. contextMenu();
  317. const $freezeColumnMenuItem = $('.htContextMenu tbody td:not(.htSeparator)');
  318. expect($freezeColumnMenuItem.text()).toEqual(FREEZE_COLUMN_IN_POLISH_LANGUAGE);
  319. });
  320. it('should translate item from enabled `comments` plugin when setting existing language code at start', () => {
  321. const ADD_COMMENT_IN_POLISH_LANGUAGE = 'Dodaj komentarz';
  322. handsontable({
  323. contextMenu: ['commentsAddEdit'],
  324. comments: true,
  325. language: POLISH_LANGUAGE_CODE,
  326. });
  327. selectCell(0, 0);
  328. contextMenu();
  329. const $addCommentMenuItem = $('.htContextMenu tbody td:not(.htSeparator)');
  330. expect($addCommentMenuItem.text()).toEqual(ADD_COMMENT_IN_POLISH_LANGUAGE);
  331. });
  332. it('should translate item from enabled `customBorders` plugin when setting existing language code at start', () => {
  333. const BORDERS_IN_POLISH = 'Obramowanie';
  334. handsontable({
  335. language: POLISH_LANGUAGE_CODE,
  336. contextMenu: ['borders'],
  337. customBorders: true
  338. });
  339. selectCell(0, 0);
  340. contextMenu();
  341. const $bordersMenuItem = $('.htContextMenu tbody td:not(.htSeparator)');
  342. expect($bordersMenuItem.text()).toEqual(BORDERS_IN_POLISH);
  343. });
  344. it('should translate item from enabled `mergeCells` plugin when setting existing language code at start', () => {
  345. const MERGE_CELLS_IN_POLISH = 'Scal komórki';
  346. handsontable({
  347. language: POLISH_LANGUAGE_CODE,
  348. contextMenu: ['mergeCells'],
  349. mergeCells: true
  350. });
  351. selectCell(0, 0);
  352. contextMenu();
  353. const $mergeCellsMenuItem = $('.htContextMenu tbody td:not(.htSeparator)');
  354. expect($mergeCellsMenuItem.text()).toEqual(MERGE_CELLS_IN_POLISH);
  355. });
  356. it('should translate item from enabled `copyPaste` plugin when setting existing language code at start', () => {
  357. const COPY_IN_POLISH = 'Kopiuj';
  358. handsontable({
  359. language: POLISH_LANGUAGE_CODE,
  360. contextMenu: ['copy'],
  361. copyPaste: true
  362. });
  363. selectCell(0, 0);
  364. contextMenu();
  365. const $copyMenuItem = $('.htContextMenu tbody td:not(.htSeparator)');
  366. expect($copyMenuItem.text()).toEqual(COPY_IN_POLISH);
  367. });
  368. });
  369. });