|
@@ -9,7 +9,7 @@ const TEST = 'TEST'
|
|
|
|
|
|
describe('Vuex', () => {
|
|
describe('Vuex', () => {
|
|
it('direct dispatch', () => {
|
|
it('direct dispatch', () => {
|
|
- const vuex = new Vuex({
|
|
|
|
|
|
+ const store = new Vuex.Store({
|
|
state: {
|
|
state: {
|
|
a: 1
|
|
a: 1
|
|
},
|
|
},
|
|
@@ -19,12 +19,12 @@ describe('Vuex', () => {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
})
|
|
- vuex.dispatch(TEST, 2)
|
|
|
|
- expect(vuex.state.a).to.equal(3)
|
|
|
|
|
|
+ store.dispatch(TEST, 2)
|
|
|
|
+ expect(store.state.a).to.equal(3)
|
|
})
|
|
})
|
|
|
|
|
|
it('simple action', function () {
|
|
it('simple action', function () {
|
|
- const vuex = new Vuex({
|
|
|
|
|
|
+ const store = new Vuex.Store({
|
|
state: {
|
|
state: {
|
|
a: 1
|
|
a: 1
|
|
},
|
|
},
|
|
@@ -37,19 +37,19 @@ describe('Vuex', () => {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
})
|
|
- vuex.actions.test(2)
|
|
|
|
- expect(vuex.state.a).to.equal(3)
|
|
|
|
|
|
+ store.actions.test(2)
|
|
|
|
+ expect(store.state.a).to.equal(3)
|
|
})
|
|
})
|
|
|
|
|
|
it('async action', function (done) {
|
|
it('async action', function (done) {
|
|
const TEST = 'TEST'
|
|
const TEST = 'TEST'
|
|
- const vuex = new Vuex({
|
|
|
|
|
|
+ const store = new Vuex.Store({
|
|
state: {
|
|
state: {
|
|
a: 1,
|
|
a: 1,
|
|
timeout: 10
|
|
timeout: 10
|
|
},
|
|
},
|
|
actions: {
|
|
actions: {
|
|
- test: (n) => (dispatch, state) => {
|
|
|
|
|
|
+ test: ({ dispatch, state }, n) => {
|
|
setTimeout(() => {
|
|
setTimeout(() => {
|
|
dispatch(TEST, n)
|
|
dispatch(TEST, n)
|
|
}, state.timeout)
|
|
}, state.timeout)
|
|
@@ -61,16 +61,16 @@ describe('Vuex', () => {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
})
|
|
- vuex.actions.test(2)
|
|
|
|
|
|
+ store.actions.test(2)
|
|
setTimeout(() => {
|
|
setTimeout(() => {
|
|
- expect(vuex.state.a).to.equal(3)
|
|
|
|
|
|
+ expect(store.state.a).to.equal(3)
|
|
done()
|
|
done()
|
|
- }, vuex.state.timeout)
|
|
|
|
|
|
+ }, store.state.timeout)
|
|
})
|
|
})
|
|
|
|
|
|
it('array option syntax', function () {
|
|
it('array option syntax', function () {
|
|
const TEST2 = 'TEST2'
|
|
const TEST2 = 'TEST2'
|
|
- const vuex = new Vuex({
|
|
|
|
|
|
+ const store = new Vuex.Store({
|
|
state: {
|
|
state: {
|
|
a: 1,
|
|
a: 1,
|
|
b: 1,
|
|
b: 1,
|
|
@@ -94,16 +94,16 @@ describe('Vuex', () => {
|
|
}
|
|
}
|
|
]
|
|
]
|
|
})
|
|
})
|
|
- vuex.actions.test(2)
|
|
|
|
- expect(vuex.state.a).to.equal(3)
|
|
|
|
- expect(vuex.state.b).to.equal(3)
|
|
|
|
- expect(vuex.state.c).to.equal(1)
|
|
|
|
- vuex.actions.test2(2)
|
|
|
|
- expect(vuex.state.c).to.equal(3)
|
|
|
|
|
|
+ store.actions.test(2)
|
|
|
|
+ expect(store.state.a).to.equal(3)
|
|
|
|
+ expect(store.state.b).to.equal(3)
|
|
|
|
+ expect(store.state.c).to.equal(1)
|
|
|
|
+ store.actions.test2(2)
|
|
|
|
+ expect(store.state.c).to.equal(3)
|
|
})
|
|
})
|
|
|
|
|
|
it('hot reload', function () {
|
|
it('hot reload', function () {
|
|
- const vuex = new Vuex({
|
|
|
|
|
|
+ const store = new Vuex.Store({
|
|
state: {
|
|
state: {
|
|
a: 1
|
|
a: 1
|
|
},
|
|
},
|
|
@@ -116,12 +116,12 @@ describe('Vuex', () => {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
})
|
|
- const test = vuex.actions.test
|
|
|
|
|
|
+ const test = store.actions.test
|
|
test(2)
|
|
test(2)
|
|
- expect(vuex.state.a).to.equal(3)
|
|
|
|
- vuex.hotUpdate({
|
|
|
|
|
|
+ expect(store.state.a).to.equal(3)
|
|
|
|
+ store.hotUpdate({
|
|
actions: {
|
|
actions: {
|
|
- test: n => dispatch => dispatch(TEST, n + 1)
|
|
|
|
|
|
+ test: ({ dispatch }, n) => dispatch(TEST, n + 1)
|
|
},
|
|
},
|
|
mutations: {
|
|
mutations: {
|
|
[TEST] (state, n) {
|
|
[TEST] (state, n) {
|
|
@@ -130,13 +130,13 @@ describe('Vuex', () => {
|
|
}
|
|
}
|
|
})
|
|
})
|
|
test(999)
|
|
test(999)
|
|
- expect(vuex.state.a).to.equal(1000)
|
|
|
|
|
|
+ expect(store.state.a).to.equal(1000)
|
|
})
|
|
})
|
|
|
|
|
|
it('middleware', function () {
|
|
it('middleware', function () {
|
|
let initState
|
|
let initState
|
|
const mutations = []
|
|
const mutations = []
|
|
- const vuex = new Vuex({
|
|
|
|
|
|
+ const store = new Vuex.Store({
|
|
state: {
|
|
state: {
|
|
a: 1
|
|
a: 1
|
|
},
|
|
},
|
|
@@ -154,14 +154,14 @@ describe('Vuex', () => {
|
|
initState = state
|
|
initState = state
|
|
},
|
|
},
|
|
onMutation (mut, state) {
|
|
onMutation (mut, state) {
|
|
- expect(state).to.equal(vuex.state)
|
|
|
|
|
|
+ expect(state).to.equal(store.state)
|
|
mutations.push(mut)
|
|
mutations.push(mut)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
]
|
|
]
|
|
})
|
|
})
|
|
- expect(initState).to.equal(vuex.state)
|
|
|
|
- vuex.actions.test(2)
|
|
|
|
|
|
+ expect(initState).to.equal(store.state)
|
|
|
|
+ store.actions.test(2)
|
|
expect(mutations.length).to.equal(1)
|
|
expect(mutations.length).to.equal(1)
|
|
expect(mutations[0].type).to.equal(TEST)
|
|
expect(mutations[0].type).to.equal(TEST)
|
|
expect(mutations[0].payload[0]).to.equal(2)
|
|
expect(mutations[0].payload[0]).to.equal(2)
|
|
@@ -170,7 +170,7 @@ describe('Vuex', () => {
|
|
it('middleware with snapshot', function () {
|
|
it('middleware with snapshot', function () {
|
|
let initState
|
|
let initState
|
|
const mutations = []
|
|
const mutations = []
|
|
- const vuex = new Vuex({
|
|
|
|
|
|
+ const store = new Vuex.Store({
|
|
state: {
|
|
state: {
|
|
a: 1
|
|
a: 1
|
|
},
|
|
},
|
|
@@ -198,31 +198,31 @@ describe('Vuex', () => {
|
|
}
|
|
}
|
|
]
|
|
]
|
|
})
|
|
})
|
|
- expect(initState).not.to.equal(vuex.state)
|
|
|
|
|
|
+ expect(initState).not.to.equal(store.state)
|
|
expect(initState.a).to.equal(1)
|
|
expect(initState.a).to.equal(1)
|
|
- vuex.actions.test(2)
|
|
|
|
|
|
+ store.actions.test(2)
|
|
expect(mutations.length).to.equal(1)
|
|
expect(mutations.length).to.equal(1)
|
|
expect(mutations[0].mutation.type).to.equal(TEST)
|
|
expect(mutations[0].mutation.type).to.equal(TEST)
|
|
expect(mutations[0].mutation.payload[0]).to.equal(2)
|
|
expect(mutations[0].mutation.payload[0]).to.equal(2)
|
|
- expect(mutations[0].nextState).not.to.equal(vuex.state)
|
|
|
|
|
|
+ expect(mutations[0].nextState).not.to.equal(store.state)
|
|
expect(mutations[0].prevState.a).to.equal(1)
|
|
expect(mutations[0].prevState.a).to.equal(1)
|
|
expect(mutations[0].nextState.a).to.equal(3)
|
|
expect(mutations[0].nextState.a).to.equal(3)
|
|
})
|
|
})
|
|
|
|
|
|
it('strict mode: warn mutations outside of handlers', function () {
|
|
it('strict mode: warn mutations outside of handlers', function () {
|
|
- const vuex = new Vuex({
|
|
|
|
|
|
+ const store = new Vuex.Store({
|
|
state: {
|
|
state: {
|
|
a: 1
|
|
a: 1
|
|
},
|
|
},
|
|
actions: {
|
|
actions: {
|
|
- test: () => (dispatch, state) => {
|
|
|
|
|
|
+ test: ({ dispatch, state }) => {
|
|
state.a++
|
|
state.a++
|
|
}
|
|
}
|
|
},
|
|
},
|
|
strict: true
|
|
strict: true
|
|
})
|
|
})
|
|
expect(() => {
|
|
expect(() => {
|
|
- vuex.actions.test(2)
|
|
|
|
- }).to.throw(/Do not mutate vuex state outside mutation handlers/)
|
|
|
|
|
|
+ store.actions.test(2)
|
|
|
|
+ }).to.throw(/Do not mutate vuex store state outside mutation handlers/)
|
|
})
|
|
})
|
|
})
|
|
})
|