Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Overwrite store.registerModule #3909

Merged
merged 3 commits into from Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/app/index.js
Expand Up @@ -49,6 +49,11 @@ async function createApp (ssrContext) {
const store = createStore(ssrContext)
// Add this.$router into store actions/mutations
store.$router = router
<% if (mode === 'universal') { %>
// Fix SSR caveat https://github.com/nuxt/nuxt.js/issues/3757#issuecomment-414689141
const registerModule = store.registerModule
store.registerModule = (path, rawModule, options) => registerModule.call(store, path, rawModule, Object.assign({ preserveState: process.browser }, options))
<% } %>
<% } %>

// Create Root instance
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/basic.browser.test.js
Expand Up @@ -50,6 +50,11 @@ describe('basic browser', () => {
expect(await page.$text('h1')).toBe('My component!')
})

test('/store-module', async () => {
await page.nuxt.navigate('/store-module')
expect(await page.$text('h1')).toBe('mutated')
})

test('/css', async () => {
await page.nuxt.navigate('/css')

Expand Down
5 changes: 4 additions & 1 deletion test/fixtures/basic/nuxt.config.js
Expand Up @@ -8,6 +8,9 @@ export default {
maxAge: ((60 * 60 * 24 * 365) * 2)
}
},
plugins: [
'~/plugins/vuex-module'
],
router: {
extendRoutes(routes, resolve) {
return [{
Expand All @@ -28,7 +31,7 @@ export default {
'/async-data',
'/validate',
'/redirect',

'/store-module',
'/users/1',
'/users/2',
{ route: '/users/3', payload: { id: 3000 } }
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/basic/pages/store-module.vue
@@ -0,0 +1,11 @@
<template>
<h1>{{ $store.state.simpleModule.mutateMe }}</h1>
</template>

<script>
export default {
fetch({ store }) {
store.dispatch('simpleModule/mutate')
}
}
</script>
18 changes: 18 additions & 0 deletions test/fixtures/basic/plugins/vuex-module.js
@@ -0,0 +1,18 @@
export default function ({ store }) {
store.registerModule('simpleModule', {
namespaced: true,
state: () => ({
mutateMe: 'not mutated'
}),
actions: {
mutate({ commit }) {
commit('mutate')
}
},
mutations: {
mutate(state) {
state.mutateMe = 'mutated'
}
}
})
}
6 changes: 6 additions & 0 deletions test/unit/basic.generate.test.js
Expand Up @@ -89,6 +89,12 @@ describe('basic generate', () => {
expect(html.includes('<h1>My component!</h1>')).toBe(true)
})

test('/store-module', async () => {
const window = await generator.nuxt.renderAndGetWindow(url('/store-module'))
const html = window.document.body.innerHTML
expect(html.includes('<h1>mutated</h1>')).toBe(true)
})

test('/css', async () => {
const window = await generator.nuxt.renderAndGetWindow(url('/css'))

Expand Down
5 changes: 5 additions & 0 deletions test/unit/basic.ssr.test.js
Expand Up @@ -19,6 +19,11 @@ describe('basic ssr', () => {
expect(html.includes('<h1>My component!</h1>')).toBe(true)
})

test('/store-module', async () => {
const { html } = await nuxt.renderRoute('/store-module')
expect(html.includes('<h1>mutated</h1>')).toBe(true)
})

/*
** Example of testing via dom checking
*/
Expand Down