test.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import { expect } from 'chai'
  2. import Vue from 'vue'
  3. import Vuex from '../src'
  4. import * as util from '../src/util'
  5. Vue.use(Vuex)
  6. const TEST = 'TEST'
  7. describe('Vuex', () => {
  8. it('direct dispatch', () => {
  9. const store = new Vuex.Store({
  10. state: {
  11. a: 1
  12. },
  13. mutations: {
  14. [TEST] (state, n) {
  15. state.a += n
  16. }
  17. }
  18. })
  19. store.dispatch(TEST, 2)
  20. expect(store.state.a).to.equal(3)
  21. })
  22. it('simple action', function () {
  23. const store = new Vuex.Store({
  24. state: {
  25. a: 1
  26. },
  27. actions: {
  28. test: TEST
  29. },
  30. getters: {
  31. getA (state) {
  32. return state.a
  33. }
  34. },
  35. mutations: {
  36. [TEST] (state, n) {
  37. state.a += n
  38. }
  39. }
  40. })
  41. store.actions.test(2)
  42. expect(store.state.a).to.equal(3)
  43. expect(store.getters.getA()).to.equal(3)
  44. })
  45. it('async action', function (done) {
  46. const TEST = 'TEST'
  47. const store = new Vuex.Store({
  48. state: {
  49. a: 1,
  50. timeout: 10
  51. },
  52. actions: {
  53. test: ({ dispatch, state }, n) => {
  54. setTimeout(() => {
  55. dispatch(TEST, n)
  56. }, state.timeout)
  57. }
  58. },
  59. mutations: {
  60. [TEST] (state, n) {
  61. state.a += n
  62. }
  63. }
  64. })
  65. store.actions.test(2)
  66. setTimeout(() => {
  67. expect(store.state.a).to.equal(3)
  68. done()
  69. }, store.state.timeout)
  70. })
  71. it('array option syntax', function () {
  72. const TEST2 = 'TEST2'
  73. const store = new Vuex.Store({
  74. state: {
  75. a: 1,
  76. b: 1,
  77. c: 1
  78. },
  79. actions: [{ test: TEST }, { test2: TEST2 }],
  80. mutations: [
  81. {
  82. [TEST] (state, n) {
  83. state.a += n
  84. }
  85. },
  86. // allow multiple handlers for the same mutation type
  87. {
  88. [TEST] (state, n) {
  89. state.b += n
  90. },
  91. [TEST2] (state, n) {
  92. state.c += n
  93. }
  94. }
  95. ],
  96. getters: [
  97. {
  98. getA (state) {
  99. return state.a
  100. }
  101. },
  102. {
  103. getB (state) {
  104. return state.b
  105. },
  106. getC (state) {
  107. return state.c
  108. }
  109. }
  110. ]
  111. })
  112. store.actions.test(2)
  113. expect(store.state.a).to.equal(3)
  114. expect(store.state.b).to.equal(3)
  115. expect(store.state.c).to.equal(1)
  116. expect(store.getters.getA()).to.equal(3)
  117. expect(store.getters.getB()).to.equal(3)
  118. expect(store.getters.getC()).to.equal(1)
  119. store.actions.test2(2)
  120. expect(store.state.c).to.equal(3)
  121. })
  122. it('hot reload', function () {
  123. const store = new Vuex.Store({
  124. state: {
  125. a: 1,
  126. b: 2
  127. },
  128. actions: {
  129. test: TEST
  130. },
  131. mutations: {
  132. [TEST] (state, n) {
  133. state.a += n
  134. }
  135. },
  136. getters: {
  137. getA (state) {
  138. return state.b
  139. }
  140. }
  141. })
  142. const test = store.actions.test
  143. test(2)
  144. expect(store.state.a).to.equal(3)
  145. expect(store.getters.getA()).to.equal(2)
  146. store.hotUpdate({
  147. actions: {
  148. test: ({ dispatch }, n) => dispatch(TEST, n + 1)
  149. },
  150. mutations: {
  151. [TEST] (state, n) {
  152. state.a = n
  153. }
  154. },
  155. getters: {
  156. getA (state) {
  157. return state.a
  158. }
  159. }
  160. })
  161. test(999)
  162. expect(store.state.a).to.equal(1000)
  163. expect(store.getters.getA()).to.equal(1000)
  164. })
  165. it('middleware', function () {
  166. let initState
  167. const mutations = []
  168. const store = new Vuex.Store({
  169. state: {
  170. a: 1
  171. },
  172. actions: {
  173. test: TEST
  174. },
  175. mutations: {
  176. [TEST] (state, n) {
  177. state.a += n
  178. }
  179. },
  180. middlewares: [
  181. {
  182. onInit (state) {
  183. initState = state
  184. },
  185. onMutation (mut, state) {
  186. expect(state).to.equal(store.state)
  187. mutations.push(mut)
  188. }
  189. }
  190. ]
  191. })
  192. expect(initState).to.equal(store.state)
  193. store.actions.test(2)
  194. expect(mutations.length).to.equal(1)
  195. expect(mutations[0].type).to.equal(TEST)
  196. expect(mutations[0].payload[0]).to.equal(2)
  197. })
  198. it('middleware with snapshot', function () {
  199. let initState
  200. const mutations = []
  201. const store = new Vuex.Store({
  202. state: {
  203. a: 1
  204. },
  205. actions: {
  206. test: TEST
  207. },
  208. mutations: {
  209. [TEST] (state, n) {
  210. state.a += n
  211. }
  212. },
  213. middlewares: [
  214. {
  215. snapshot: true,
  216. onInit (state) {
  217. initState = state
  218. },
  219. onMutation (mutation, nextState, prevState) {
  220. mutations.push({
  221. mutation,
  222. nextState,
  223. prevState
  224. })
  225. }
  226. }
  227. ]
  228. })
  229. expect(initState).not.to.equal(store.state)
  230. expect(initState.a).to.equal(1)
  231. store.actions.test(2)
  232. expect(mutations.length).to.equal(1)
  233. expect(mutations[0].mutation.type).to.equal(TEST)
  234. expect(mutations[0].mutation.payload[0]).to.equal(2)
  235. expect(mutations[0].nextState).not.to.equal(store.state)
  236. expect(mutations[0].prevState.a).to.equal(1)
  237. expect(mutations[0].nextState.a).to.equal(3)
  238. })
  239. it('strict mode: warn mutations outside of handlers', function () {
  240. const store = new Vuex.Store({
  241. state: {
  242. a: 1
  243. },
  244. actions: {
  245. test: ({ dispatch, state }) => {
  246. state.a++
  247. }
  248. },
  249. strict: true
  250. })
  251. expect(() => {
  252. store.actions.test(2)
  253. }).to.throw(/Do not mutate vuex store state outside mutation handlers/)
  254. })
  255. })