TranslatorSingleton.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /* eslint no-console: 0 */
  20. import mockConsole from 'jest-mock-console';
  21. import { configure, resetTranslation, t, tn } from '@superset-ui/core';
  22. import Translator from '../../src/translation/Translator';
  23. import languagePackEn from './languagePacks/en';
  24. import languagePackZh from './languagePacks/zh';
  25. describe('TranslatorSingleton', () => {
  26. describe('before configure()', () => {
  27. beforeAll(() => {
  28. resetTranslation();
  29. });
  30. describe('t()', () => {
  31. it('returns untranslated input and issues a warning', () => {
  32. const restoreConsole = mockConsole();
  33. expect(t('second')).toEqual('second');
  34. expect(console.warn).toHaveBeenCalled();
  35. restoreConsole();
  36. });
  37. });
  38. describe('tn()', () => {
  39. it('returns untranslated input and issues a warning', () => {
  40. const restoreConsole = mockConsole();
  41. expect(tn('ox', 'oxen', 2)).toEqual('oxen');
  42. expect(console.warn).toHaveBeenCalled();
  43. restoreConsole();
  44. });
  45. });
  46. });
  47. describe('after configure()', () => {
  48. describe('configure()', () => {
  49. it('creates and returns a translator', () => {
  50. expect(configure()).toBeInstanceOf(Translator);
  51. });
  52. });
  53. describe('t()', () => {
  54. it('after configure() returns translated text', () => {
  55. configure({
  56. languagePack: languagePackZh,
  57. });
  58. expect(t('second')).toEqual('秒');
  59. });
  60. });
  61. describe('tn()', () => {
  62. it('after configure() returns translated text with singular/plural', () => {
  63. configure({
  64. languagePack: languagePackEn,
  65. });
  66. expect(tn('ox', 'oxen', 2)).toEqual('oxen');
  67. });
  68. });
  69. });
  70. it('should be reset translation setting', () => {
  71. configure();
  72. expect(t('second')).toEqual('second');
  73. resetTranslation();
  74. const restoreConsole = mockConsole();
  75. expect(t('second')).toEqual('second');
  76. resetTranslation();
  77. expect(t('second')).toEqual('second');
  78. expect(console.warn).toHaveBeenCalledTimes(2);
  79. restoreConsole();
  80. });
  81. });