Skip to content

Commit

Permalink
Merge pull request #16 from apache-superset/kristw--wran
Browse files Browse the repository at this point in the history
Make the translation module less strict and won't crash the app if not configured.
  • Loading branch information
kristw authored and zhaoyongjie committed Nov 26, 2021
1 parent 267c7c0 commit 3594d09
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
},
"homepage": "https://github.com/apache-superset/superset-ui#readme",
"devDependencies": {
"@data-ui/build-config": "^0.0.23"
"@data-ui/build-config": "^0.0.23",
"jest-mock-console": "^0.4.0"
},
"dependencies": {
"@babel/runtime": "^7.1.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
/* eslint no-console: 0 */
import Translator from './Translator';

let singleton;
let isConfigured = false;

function configure(config) {
singleton = new Translator(config);
isConfigured = true;

return singleton;
}

function getInstance() {
if (!singleton) {
throw new Error('You must call configure(...) before calling other methods');
if (!isConfigured) {
console.warn('You must call configure(...) before calling other methods');
if (!singleton) {
singleton = new Translator();
}
}

return singleton;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
/* eslint no-console: 0 */
import mockConsole from 'jest-mock-console';
import Translator from '../src/Translator';
import { configure, t, tn } from '../src/TranslatorSingleton';
import languagePackZh from './languagePacks/zh.json';

describe('TranslatorSingleton', () => {
describe('before configure()', () => {
describe('t()', () => {
it('throws error', () => {
expect(() => t('second')).toThrow();
it('returns untranslated input and issues a warning', () => {
const restoreConsole = mockConsole();
expect(t('second')).toEqual('second');
expect(console.warn).toHaveBeenCalled();
restoreConsole();
});
});
describe('tn()', () => {
it('throws error', () => {
expect(() => tn('ox', 'oxen', 2)).toThrow();
it('returns untranslated input and issues a warning', () => {
const restoreConsole = mockConsole();
expect(tn('ox', 'oxen', 2)).toEqual('oxen');
expect(console.warn).toHaveBeenCalled();
restoreConsole();
});
});
});
Expand Down

0 comments on commit 3594d09

Please sign in to comment.