File tree Expand file tree Collapse file tree 3 files changed +23
-5
lines changed Expand file tree Collapse file tree 3 files changed +23
-5
lines changed Original file line number Diff line number Diff line change 11machine :
22 node :
33 version : 5
4+ environment :
5+ BABEL_ENV : development
Original file line number Diff line number Diff line change 1- export function mapState ( map ) {
1+ export function mapState ( states ) {
22 const res = { }
3- Object . keys ( map ) . forEach ( key => {
4- const fn = map [ key ]
3+ normalizeMap ( states ) . forEach ( ( { key, val } ) => {
54 res [ key ] = function mappedState ( ) {
6- return fn . call ( this , this . $store . state , this . $store . getters )
5+ return typeof val === 'function'
6+ ? val . call ( this , this . $store . state , this . $store . getters )
7+ : this . $store . state [ val ]
78 }
89 } )
910 return res
Original file line number Diff line number Diff line change @@ -225,7 +225,22 @@ describe('Vuex', () => {
225225 expect ( child . $store ) . toBe ( store )
226226 } )
227227
228- it ( 'helper: mapState' , ( ) => {
228+ it ( 'helper: mapState (array)' , ( ) => {
229+ const store = new Vuex . Store ( {
230+ state : {
231+ a : 1
232+ }
233+ } )
234+ const vm = new Vue ( {
235+ store,
236+ computed : mapState ( [ 'a' ] )
237+ } )
238+ expect ( vm . a ) . toBe ( 1 )
239+ store . state . a ++
240+ expect ( vm . a ) . toBe ( 2 )
241+ } )
242+
243+ it ( 'helper: mapState (object)' , ( ) => {
229244 const store = new Vuex . Store ( {
230245 state : {
231246 a : 1
You can’t perform that action at this time.
0 commit comments