Skip to content

Commit

Permalink
fix: Don't inject to store if store is not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl committed Sep 24, 2019
1 parent b28acc7 commit e547639
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/plugins/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,15 @@ export default async (context) => {
// Inject seo function
Vue.prototype.$nuxtI18nSeo = nuxtI18nSeo

// Inject in store.
store.$i18n = app.i18n
if (store) {
// Inject in store.
store.$i18n = app.i18n

if (store && store.state.localeDomains) {
app.i18n.locales.forEach(locale => {
locale.domain = store.state.localeDomains[locale.code]
})
if (store.state.localeDomains) {
app.i18n.locales.forEach(locale => {
locale.domain = store.state.localeDomains[locale.code]
})
}
}

let locale = app.i18n.defaultLocale || null
Expand Down
4 changes: 4 additions & 0 deletions test/fixture/base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ module.exports = {
}
},
fallbackLocale: 'en'
},
vuex: {
syncLocale: true,
syncMessages: true
}
}
}
18 changes: 18 additions & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,24 @@ describe('basic', () => {
})
})

describe('store', () => {
test('injects $i18n in store', async () => {
const window = await nuxt.renderAndGetWindow(url('/'))
expect(window.$nuxt.$store.$i18n).toBeDefined()
})

test('syncs i18n locale and messages', async () => {
const window = await nuxt.renderAndGetWindow(url('/'))
expect(window.$nuxt.$store.state.i18n).toBeDefined()
expect(window.$nuxt.$store.state.i18n.locale).toBe('en')
expect(window.$nuxt.$store.state.i18n.messages).toEqual(expect.objectContaining({
about: 'About us',
home: 'Homepage',
posts: 'Posts'
}))
})
})

test('/dynamicNested/1/2/3 contains link to /fr/imbrication-dynamique/1/2/3', async () => {
const html = await get('/dynamicNested/1/2/3')
expect(cleanUpScripts(html)).toMatchSnapshot()
Expand Down

0 comments on commit e547639

Please sign in to comment.