dictionariesManager.spec.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import {
  2. getLanguageDictionary,
  3. getLanguagesDictionaries,
  4. registerLanguageDictionary,
  5. hasLanguageDictionary,
  6. DEFAULT_LANGUAGE_CODE
  7. } from 'handsontable/i18n/dictionariesManager';
  8. import plPL from 'handsontable/i18n/languages/pl-PL';
  9. import enUS from 'handsontable/i18n/languages/en-US';
  10. import * as constants from 'handsontable/i18n/constants';
  11. describe('i18n dictionariesManager', () => {
  12. it('should register automatically default language', () => {
  13. const allLanguages = getLanguagesDictionaries();
  14. const defaultLanguageIsRegistered = allLanguages.some(dictionary => dictionary.languageCode === DEFAULT_LANGUAGE_CODE);
  15. expect(defaultLanguageIsRegistered).toEqual(true);
  16. });
  17. it('should not register automatically imported /src languages', () => {
  18. expect(hasLanguageDictionary(plPL.languageCode)).toEqual(false);
  19. });
  20. it('should return `null` when trying to get not registered language', () => {
  21. expect(getLanguageDictionary(plPL.languageCode)).toEqual(null);
  22. });
  23. it('should register language', () => {
  24. // Note: please keep in mind that this language will be registered also for next unit tests (within this file)!
  25. // It's stored globally for already loaded Handsontable library.
  26. registerLanguageDictionary(plPL);
  27. expect(getLanguagesDictionaries().length).toEqual(2);
  28. });
  29. it('should register language only once', () => {
  30. registerLanguageDictionary(plPL);
  31. registerLanguageDictionary(plPL);
  32. registerLanguageDictionary(enUS);
  33. registerLanguageDictionary(enUS);
  34. expect(getLanguagesDictionaries().length).toEqual(2);
  35. });
  36. it('should not give opportunity to change registered language by reference', () => {
  37. const registeredLanguageDictionary1 = registerLanguageDictionary(plPL);
  38. const registeredLanguageDictionary2 = registerLanguageDictionary(plPL);
  39. registeredLanguageDictionary1.newExtraKey1 = 'hello world1';
  40. registeredLanguageDictionary2.newExtraKey2 = 'hello world2';
  41. expect(getLanguageDictionary(plPL.languageCode).newExtraKey).toBeUndefined();
  42. expect(getLanguageDictionary(plPL.languageCode).newExtraKey2).toBeUndefined();
  43. expect(registeredLanguageDictionary1).not.toBe(plPL);
  44. expect(registeredLanguageDictionary2).not.toBe(plPL);
  45. expect(registeredLanguageDictionary1).not.toBe(registeredLanguageDictionary2);
  46. plPL.newExtraKey3 = 'hello world3';
  47. expect(getLanguageDictionary(plPL.languageCode).newExtraKey3).toBeUndefined();
  48. delete plPL.newExtraKey3;
  49. });
  50. it('should return `true` when checking existence of previously registered language', () => {
  51. expect(hasLanguageDictionary(plPL.languageCode)).toEqual(true);
  52. });
  53. it('should return `true` when checking existence of default language', () => {
  54. expect(hasLanguageDictionary(DEFAULT_LANGUAGE_CODE)).toEqual(true);
  55. });
  56. it('should return copy of registered dictionary when getting previously registered language', () => {
  57. expect(getLanguageDictionary(plPL.languageCode)).toEqual(plPL);
  58. expect(getLanguageDictionary(plPL.languageCode)).not.toBe(plPL);
  59. });
  60. it('should extend registered language by default language dictionary', () => {
  61. const defaultLanguageDictionary = getLanguageDictionary(DEFAULT_LANGUAGE_CODE);
  62. const dictionaryKey1 = constants.CONTEXTMENU_ITEMS_ROW_ABOVE;
  63. const dictionaryKey2 = constants.CONTEXTMENU_ITEMS_COLUMN_RIGHT;
  64. const registeredLanguage = registerLanguageDictionary({
  65. languageCode: 'kl-PU',
  66. [dictionaryKey1]: 'Hello world'
  67. });
  68. expect(registeredLanguage[dictionaryKey1]).toEqual('Hello world');
  69. expect(registeredLanguage[dictionaryKey2]).toEqual(defaultLanguageDictionary[dictionaryKey2]);
  70. });
  71. });