Ensure that addDefaultTranslation gets called #99
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
For #97
This change implements a fix based on the solution described in the linked issue.
Changes
utils.get
I added a helper to access values nested in objects. This is just to avoid verbose checks like:
Inspired by
lodash.get
, but probably much simpler.Translate.js
I added some logic to check if the defaultLanguage has gone from undefined to something. It looks a bit verbose but there is a need to check both
props.context
andprops.options
I think. @ryandrewjohnson I don't know if there is a need to address the case where the language changes from one language to another?Translate.test.js
In
getTranslateWithContext()
, I amended the default language to better reflect how this value is arrived in real use.As far as I can tell, in real use,
defaultLanguage
may start off as undefined, beforeinitialize()
has completed. It is impossible to recreate this ingetTranslateWithContext()
because it has a default to ensure thatdefaultLanguage
is always defined.To simulate real use, you can explicitly pass an empty language option array to the initialState in
getTranslateWithContext(initialState)
. This then throws because the default I mentioned above is accessinglanguages[0].code
. So i just added a check to see if there is an element atlanguages[0]
, otherwise returnundefined
.This matches how the default language is determined in
LocalizeContext
:To simulate the defaultLanguage changing from undefined to something, in the test case I created
Translate
with an empty languages array, then explicitly set theoptions
prop on Translate to contain a language. Then we can assert thataddTranslationForLanguage()
was called.