Skip to content

Commit 1668bd6

Browse files
committed
tests wip
1 parent 1c0c963 commit 1668bd6

File tree

3 files changed

+115
-122
lines changed

3 files changed

+115
-122
lines changed

src/override.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ export default function (Vue) {
1414
// vuex option handling
1515
const vuex = options.vuex || componentOptions.vuex
1616
if (vuex) {
17+
if (!this.$store) {
18+
console.warn(
19+
'[vuex] store not injected. make sure to ' +
20+
'provide the store option in your root component.'
21+
)
22+
}
1723
const { state, actions } = vuex
1824
// state
1925
if (state) {

src/util.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
1-
/**
2-
* Create a actual callable action function.
3-
*
4-
* @param {String|Function} action
5-
* @param {Vuex} store
6-
* @return {Function} [description]
7-
*/
8-
9-
export function createAction (action, store) {
10-
if (typeof action === 'string') {
11-
// simple action string shorthand
12-
return (...payload) => store.dispatch(action, ...payload)
13-
} else if (typeof action === 'function') {
14-
// normal action
15-
return (...payload) => action(store, ...payload)
16-
}
17-
}
18-
191
/**
202
* Merge an array of objects into one.
213
*

test/test.js

Lines changed: 109 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -23,152 +23,157 @@ describe('Vuex', () => {
2323
expect(store.state.a).to.equal(3)
2424
})
2525

26-
it('simple action', function () {
26+
it('injecting state and action to components', function () {
2727
const store = new Vuex.Store({
2828
state: {
2929
a: 1
3030
},
31-
actions: {
32-
test: TEST
33-
},
34-
getters: {
35-
getA (state) {
36-
return state.a
37-
}
38-
},
3931
mutations: {
4032
[TEST] (state, n) {
4133
state.a += n
4234
}
4335
}
4436
})
45-
store.actions.test(2)
37+
const vm = new Vue({
38+
store,
39+
vuex: {
40+
state: {
41+
a: state => state.a
42+
},
43+
actions: {
44+
test: ({ dispatch }, n) => dispatch(TEST, n)
45+
}
46+
}
47+
})
48+
vm.test(2)
49+
expect(vm.a).to.equal(3)
4650
expect(store.state.a).to.equal(3)
47-
expect(store.getters.getA()).to.equal(3)
4851
})
4952

50-
it('async action', function (done) {
51-
const TEST = 'TEST'
53+
it('modules', function () {
54+
const mutations = {
55+
[TEST] (state, n) {
56+
state.a += n
57+
}
58+
}
5259
const store = new Vuex.Store({
5360
state: {
54-
a: 1,
55-
timeout: 10
56-
},
57-
actions: {
58-
test: ({ dispatch, state }, n) => {
59-
setTimeout(() => {
60-
dispatch(TEST, n)
61-
}, state.timeout)
62-
}
61+
a: 1
6362
},
64-
mutations: {
65-
[TEST] (state, n) {
66-
state.a += n
63+
mutations,
64+
modules: {
65+
one: {
66+
state: { a: 2 },
67+
mutations
68+
},
69+
two: {
70+
state: { a: 3 },
71+
mutations
6772
}
6873
}
6974
})
70-
store.actions.test(2)
71-
setTimeout(() => {
72-
expect(store.state.a).to.equal(3)
73-
done()
74-
}, store.state.timeout)
75+
store.dispatch(TEST, 1)
76+
expect(store.state.a).to.equal(2)
77+
expect(store.state.one.a).to.equal(3)
78+
expect(store.state.two.a).to.equal(4)
7579
})
7680

77-
it('array option syntax', function () {
78-
const TEST2 = 'TEST2'
81+
it('hot reload', function () {
82+
const mutations = {
83+
[TEST] (state, n) {
84+
state.a += n
85+
}
86+
}
7987
const store = new Vuex.Store({
8088
state: {
81-
a: 1,
82-
b: 1,
83-
c: 1
89+
a: 1
8490
},
85-
actions: [{ test: TEST }, { test2: TEST2 }],
86-
mutations: [
87-
{
88-
[TEST] (state, n) {
89-
state.a += n
90-
}
91+
mutations,
92+
modules: {
93+
one: {
94+
state: { a: 2 },
95+
mutations
9196
},
92-
// allow multiple handlers for the same mutation type
93-
{
94-
[TEST] (state, n) {
95-
state.b += n
96-
},
97-
[TEST2] (state, n) {
98-
state.c += n
99-
}
100-
}
101-
],
102-
getters: [
103-
{
104-
getA (state) {
105-
return state.a
106-
}
107-
},
108-
{
109-
getB (state) {
110-
return state.b
111-
},
112-
113-
getC (state) {
114-
return state.c
115-
}
97+
two: {
98+
state: { a: 3 },
99+
mutations
116100
}
117-
]
101+
}
118102
})
119-
store.actions.test(2)
120-
expect(store.state.a).to.equal(3)
121-
expect(store.state.b).to.equal(3)
122-
expect(store.state.c).to.equal(1)
123-
expect(store.getters.getA()).to.equal(3)
124-
expect(store.getters.getB()).to.equal(3)
125-
expect(store.getters.getC()).to.equal(1)
126-
store.actions.test2(2)
127-
expect(store.state.c).to.equal(3)
128-
})
103+
store.dispatch(TEST, 1)
104+
expect(store.state.a).to.equal(2)
105+
expect(store.state.one.a).to.equal(3)
106+
expect(store.state.two.a).to.equal(4)
129107

130-
it('hot reload', function () {
131-
const store = new Vuex.Store({
132-
state: {
133-
a: 1,
134-
b: 2
135-
},
136-
actions: {
137-
test: TEST
138-
},
108+
// hot reload only root mutations
109+
store.hotUpdate({
139110
mutations: {
140111
[TEST] (state, n) {
141-
state.a += n
112+
state.a = n
142113
}
143-
},
144-
getters: {
145-
getA (state) {
146-
return state.b
114+
}
115+
})
116+
store.dispatch(TEST, 1)
117+
expect(store.state.a).to.equal(1) // only root mutation updated
118+
expect(store.state.one.a).to.equal(4)
119+
expect(store.state.two.a).to.equal(5)
120+
121+
// hot reload modules
122+
store.hotUpdate({
123+
modules: {
124+
one: {
125+
state: { a: 234 },
126+
mutations: {
127+
[TEST] (state, n) {
128+
state.a += n
129+
}
130+
}
131+
},
132+
two: {
133+
state: { a: 345 },
134+
mutations: {
135+
[TEST] (state, n) {
136+
state.a -= n
137+
}
138+
}
147139
}
148140
}
149141
})
150-
const test = store.actions.test
151-
test(2)
152-
expect(store.state.a).to.equal(3)
153-
expect(store.getters.getA()).to.equal(2)
142+
store.dispatch(TEST, 2)
143+
expect(store.state.a).to.equal(2)
144+
expect(store.state.one.a).to.equal(6) // should not reload initial state
145+
expect(store.state.two.a).to.equal(3) // should not reload initial state
146+
147+
// hot reload all
154148
store.hotUpdate({
155-
actions: {
156-
test: ({ dispatch }, n) => dispatch(TEST, n + 1)
157-
},
158149
mutations: {
159150
[TEST] (state, n) {
160-
state.a = n
151+
state.a -= n
161152
}
162153
},
163-
getters: {
164-
getA (state) {
165-
return state.a
154+
modules: {
155+
one: {
156+
state: { a: 234 },
157+
mutations: {
158+
[TEST] (state, n) {
159+
state.a = n
160+
}
161+
}
162+
},
163+
two: {
164+
state: { a: 345 },
165+
mutations: {
166+
[TEST] (state, n) {
167+
state.a = n
168+
}
169+
}
166170
}
167171
}
168172
})
169-
test(999)
170-
expect(store.state.a).to.equal(1000)
171-
expect(store.getters.getA()).to.equal(1000)
173+
store.dispatch(TEST, 3)
174+
expect(store.state.a).to.equal(-1)
175+
expect(store.state.one.a).to.equal(3)
176+
expect(store.state.two.a).to.equal(3)
172177
})
173178

174179
it('middleware', function () {

0 commit comments

Comments
 (0)