Skip to content

Commit

Permalink
fix: localeProperties undefined when <i18n> component used (#1043)
Browse files Browse the repository at this point in the history
  • Loading branch information
iifawzi committed Jan 25, 2021
1 parent 9cd9452 commit ff56a35
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/templates/plugin.main.js
Expand Up @@ -252,6 +252,7 @@ export default async (context) => {

const extendVueI18nInstance = i18n => {
i18n.locales = locales
i18n.localeProperties = klona(locales.find(l => l[LOCALE_CODE_KEY] === i18n.locale) || { code: i18n.locale })
i18n.defaultLocale = defaultLocale
i18n.differentDomains = differentDomains
i18n.beforeLanguageSwitch = beforeLanguageSwitch
Expand Down
4 changes: 4 additions & 0 deletions test/fixture/basic/pages/loader-yaml.vue
Expand Up @@ -2,6 +2,7 @@
<div>
<p id="title">{{ $t('hello') }}</p>
<p id="locales">{{ locales }}</p>
<p id="localeProperties">{{ JSON.stringify(localeProperties) }}</p>
</div>
</template>

Expand All @@ -10,6 +11,9 @@ export default {
computed: {
locales () {
return this.$i18n.locales || []
},
localeProperties () {
return this.$i18n.localeProperties || {}
}
}
}
Expand Down
20 changes: 12 additions & 8 deletions test/module.test.js
Expand Up @@ -732,13 +732,17 @@ describe('hreflang', () => {
expect(seoTags).toEqual(expectedSeoTags)
})

test('localeProperties object exists and is set to the correct value', async () => {
const window = await nuxt.renderAndGetWindow(url('/'))
expect(window.$nuxt.$i18n.localeProperties).toEqual({
code: 'en',
iso: 'en',
name: 'English'
})
test('localeProperties object exists and is set to the correct object', async () => {
const html = await get('/loader-yaml')
const dom = getDom(html)
const localeProperties = dom.querySelector('p#localeProperties')
const localePropertiesContent = localeProperties?.textContent || '{}'
expect(JSON.parse(localePropertiesContent)).toMatchObject(
{
code: 'en',
iso: 'en',
name: 'English'
})
})

afterAll(async () => {
Expand Down Expand Up @@ -825,7 +829,7 @@ describe('with empty configuration', () => {
await nuxt.renderAndGetWindow(url('/about'))
})

test('localeProperties object exists and is set to an empty object ', async () => {
test('localeProperties object exists and is set to an object with no code', async () => {
const window = await nuxt.renderAndGetWindow(url('/'))
expect(window.$nuxt.$i18n.localeProperties).toEqual({ code: '' })
})
Expand Down

0 comments on commit ff56a35

Please sign in to comment.