Skip to content

Commit

Permalink
Ensure that addDefaultTranslation gets called if the defaultLanguage …
Browse files Browse the repository at this point in the history
…changes from undefined to a value. (#99)
  • Loading branch information
thchia authored and ryandrewjohnson committed Jun 25, 2018
1 parent 7b18367 commit 1a726d4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/Translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
getActiveLanguage,
getTranslationsForActiveLanguage
} from './localize';
import { storeDidChange } from './utils';
import { get, storeDidChange } from './utils';
import { LocalizeContext, type LocalizeContextProps } from './LocalizeContext';
import { withLocalize } from './withLocalize';
import type {
Expand Down Expand Up @@ -39,7 +39,18 @@ class WrappedTranslate extends React.Component<TranslateWithContextProps> {
}

componentDidUpdate(prevProps: TranslateWithContextProps) {
if (this.props.id && prevProps.id !== this.props.id) {
const idChanged = this.props.id && prevProps.id !== this.props.id;

const noDefaultLanguage =
!get(prevProps, 'context.defaultLanguage') &&
!get(prevProps, 'options.language');
const incomingLanguage =
get(this.props, 'context.defaultLanguage') ||
get(this.props, 'options.language');

const defaultLanguageSet = noDefaultLanguage && incomingLanguage;

if (idChanged || defaultLanguageSet) {
this.addDefaultTranslation();
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ export const getSingleToMultilanguageTranslation = (
return singleLanguageTranslations;
};

export const get = (obj: Object, path: string, defaultValue: any = undefined) => {
const pathArr = path.split('.').filter(Boolean);
return pathArr.reduce((ret, key) => {
return ret && ret[key] ? ret[key] : defaultValue;
}, obj);
};

// Thanks react-redux for utility function
// https://github.com/reactjs/react-redux/blob/master/src/utils/warning.js
export const warning = (message: string) => {
Expand Down
18 changes: 17 additions & 1 deletion tests/Translate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('<Translate />', () => {
translate: getTranslate(localizeState),
languages: getLanguages(localizeState),
defaultLanguage:
state.options.defaultLanguage || getLanguages(localizeState)[0].code,
state.options.defaultLanguage || (getLanguages(localizeState)[0] && getLanguages(localizeState)[0].code),
activeLanguage: getActiveLanguage(localizeState),
initialize: jest.fn(),
addTranslation: jest.fn(),
Expand Down Expand Up @@ -178,6 +178,22 @@ describe('<Translate />', () => {
);
});

it("should add <Translate>'s children to translations when default language is set", () => {
const Translate = getTranslateWithContext({...initialState, languages: [] });
const wrapper = mount(
<Translate id='no_translation'>
Default Translation
</Translate>
);

wrapper.setProps({options: {language: 'en'}});

expect(defaultContext.addTranslationForLanguage).toHaveBeenLastCalledWith(
{ 'no_translation': 'Default Translation' },
'en'
);
});

it("should add <Translate>'s children to translations under options.defaultLanguage for id", () => {
const Translate = getTranslateWithContext();
const wrapper = mount(
Expand Down
20 changes: 20 additions & 0 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,24 @@ describe('locale utils', () => {
expect(() => utils.validateOptions(options)).toThrow();
});
});

describe('get', () => {

const obj = { a: { b: { c: 'd' } } };

it('gets value at path', () => {
const path = 'a.b.c';
expect(utils.get(obj, path)).toBe('d');
});

it('returns passed default value', () => {
const path = 'foo';
expect(utils.get(obj, path, 'default')).toBe('default');
});

it('falls back to undefined', () => {
const path = 'foo';
expect(utils.get(obj, path)).toBeUndefined();
})
});
});

0 comments on commit 1a726d4

Please sign in to comment.