Skip to content

Commit

Permalink
Merge pull request #56 from gradecam/feature/actions-should-return
Browse files Browse the repository at this point in the history
#36 Actions should return the value through vuex
  • Loading branch information
championswimmer committed Nov 24, 2018
2 parents 5b671ca + f07f0e0 commit 108e7ec
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/action.ts
Expand Up @@ -37,6 +37,7 @@ function actionDecoratorFactory<T>(params?: ActionDecoratorParams): MethodDecora
if (commit) {
context.commit(commit, actionPayload)
}
return actionPayload
} catch (e) {
throw rawError
? e
Expand Down
55 changes: 55 additions & 0 deletions test/action_with_inner_promise.ts
@@ -0,0 +1,55 @@
import Vuex from 'vuex'
import Vue from 'vue'
Vue.use(Vuex)
import { Action, Module, MutationAction, VuexModule } from '..'
import { expect } from 'chai'

@Module
class MyModule extends VuexModule {
foo = ''

@Action
public value() {
return 'Test1'
}

@Action
public promise() {
return Promise.resolve('Test2')
}

@MutationAction({ mutate: ['foo'], rawError: true })
public promise2(payload: string) {
return Promise.resolve({ foo: payload })
}
}

const store = new Vuex.Store({
modules: {
mm: MyModule
}
})

describe('actions return inner promises', () => {
it('should return resolved value', async function() {
const value = await store.dispatch('value')
expect(value).to.equal('Test1')
})

it('should return resolved promise', async function() {
const value = await store.dispatch('promise')
expect(value).to.equal('Test2')
})

it('should not return resolved promise on a MutationAction', async function() {
// Purposefully not returning value from the action on a MutationAction because it is
// designed to mutate the state and you should access the value off the state
const {
state: { mm }
} = store
let value = 'Test3'
const resp = await store.dispatch('promise2', value)
expect(resp).to.equal(void 0)
expect(mm.foo).to.equal(value)
})
})

1 comment on commit 108e7ec

@rumbcam
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@championswimmer could we get a new release on npm that includes this?

Thanks

Please sign in to comment.