test.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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('injecting state and action to components', function () {
  23. const store = new Vuex.Store({
  24. state: {
  25. a: 1
  26. },
  27. mutations: {
  28. [TEST] (state, n) {
  29. state.a += n
  30. }
  31. }
  32. })
  33. const vm = new Vue({
  34. store,
  35. vuex: {
  36. state: {
  37. a: state => state.a
  38. },
  39. actions: {
  40. test: ({ dispatch }, n) => dispatch(TEST, n)
  41. }
  42. }
  43. })
  44. vm.test(2)
  45. expect(vm.a).to.equal(3)
  46. expect(store.state.a).to.equal(3)
  47. })
  48. it('modules', function () {
  49. const mutations = {
  50. [TEST] (state, n) {
  51. state.a += n
  52. }
  53. }
  54. const store = new Vuex.Store({
  55. state: {
  56. a: 1
  57. },
  58. mutations,
  59. modules: {
  60. one: {
  61. state: { a: 2 },
  62. mutations
  63. },
  64. two: {
  65. state: { a: 3 },
  66. mutations
  67. }
  68. }
  69. })
  70. store.dispatch(TEST, 1)
  71. expect(store.state.a).to.equal(2)
  72. expect(store.state.one.a).to.equal(3)
  73. expect(store.state.two.a).to.equal(4)
  74. })
  75. it('hot reload', function () {
  76. const mutations = {
  77. [TEST] (state, n) {
  78. state.a += n
  79. }
  80. }
  81. const store = new Vuex.Store({
  82. state: {
  83. a: 1
  84. },
  85. mutations,
  86. modules: {
  87. one: {
  88. state: { a: 2 },
  89. mutations
  90. },
  91. two: {
  92. state: { a: 3 },
  93. mutations
  94. }
  95. }
  96. })
  97. store.dispatch(TEST, 1)
  98. expect(store.state.a).to.equal(2)
  99. expect(store.state.one.a).to.equal(3)
  100. expect(store.state.two.a).to.equal(4)
  101. // hot reload only root mutations
  102. store.hotUpdate({
  103. mutations: {
  104. [TEST] (state, n) {
  105. state.a = n
  106. }
  107. }
  108. })
  109. store.dispatch(TEST, 1)
  110. expect(store.state.a).to.equal(1) // only root mutation updated
  111. expect(store.state.one.a).to.equal(4)
  112. expect(store.state.two.a).to.equal(5)
  113. // hot reload modules
  114. store.hotUpdate({
  115. modules: {
  116. one: {
  117. state: { a: 234 },
  118. mutations: {
  119. [TEST] (state, n) {
  120. state.a += n
  121. }
  122. }
  123. },
  124. two: {
  125. state: { a: 345 },
  126. mutations: {
  127. [TEST] (state, n) {
  128. state.a -= n
  129. }
  130. }
  131. }
  132. }
  133. })
  134. store.dispatch(TEST, 2)
  135. expect(store.state.a).to.equal(2)
  136. expect(store.state.one.a).to.equal(6) // should not reload initial state
  137. expect(store.state.two.a).to.equal(3) // should not reload initial state
  138. // hot reload all
  139. store.hotUpdate({
  140. mutations: {
  141. [TEST] (state, n) {
  142. state.a -= n
  143. }
  144. },
  145. modules: {
  146. one: {
  147. state: { a: 234 },
  148. mutations: {
  149. [TEST] (state, n) {
  150. state.a = n
  151. }
  152. }
  153. },
  154. two: {
  155. state: { a: 345 },
  156. mutations: {
  157. [TEST] (state, n) {
  158. state.a = n
  159. }
  160. }
  161. }
  162. }
  163. })
  164. store.dispatch(TEST, 3)
  165. expect(store.state.a).to.equal(-1)
  166. expect(store.state.one.a).to.equal(3)
  167. expect(store.state.two.a).to.equal(3)
  168. })
  169. it('middleware', function () {
  170. let initState
  171. const mutations = []
  172. const store = new Vuex.Store({
  173. state: {
  174. a: 1
  175. },
  176. mutations: {
  177. [TEST] (state, n) {
  178. state.a += n
  179. }
  180. },
  181. middlewares: [
  182. {
  183. onInit (state) {
  184. initState = state
  185. },
  186. onMutation (mut, state) {
  187. expect(state).to.equal(store.state)
  188. mutations.push(mut)
  189. }
  190. }
  191. ]
  192. })
  193. expect(initState).to.equal(store.state)
  194. store.dispatch(TEST, 2)
  195. expect(mutations.length).to.equal(1)
  196. expect(mutations[0].type).to.equal(TEST)
  197. expect(mutations[0].payload[0]).to.equal(2)
  198. })
  199. it('middleware with snapshot', function () {
  200. let initState
  201. const mutations = []
  202. const store = new Vuex.Store({
  203. state: {
  204. a: 1
  205. },
  206. mutations: {
  207. [TEST] (state, n) {
  208. state.a += n
  209. }
  210. },
  211. middlewares: [
  212. {
  213. snapshot: true,
  214. onInit (state) {
  215. initState = state
  216. },
  217. onMutation (mutation, nextState, prevState) {
  218. mutations.push({
  219. mutation,
  220. nextState,
  221. prevState
  222. })
  223. }
  224. }
  225. ]
  226. })
  227. expect(initState).not.to.equal(store.state)
  228. expect(initState.a).to.equal(1)
  229. store.dispatch(TEST, 2)
  230. expect(mutations.length).to.equal(1)
  231. expect(mutations[0].mutation.type).to.equal(TEST)
  232. expect(mutations[0].mutation.payload[0]).to.equal(2)
  233. expect(mutations[0].nextState).not.to.equal(store.state)
  234. expect(mutations[0].prevState.a).to.equal(1)
  235. expect(mutations[0].nextState.a).to.equal(3)
  236. })
  237. it('watch', function (done) {
  238. const store = new Vuex.Store({
  239. state: {
  240. a: 1
  241. },
  242. mutations: {
  243. [TEST]: state => state.a++
  244. }
  245. })
  246. let watchedValueOne, watchedValueTwo
  247. store.watch(({ a }) => a, val => {
  248. watchedValueOne = val
  249. })
  250. store.watch('a', val => {
  251. watchedValueTwo = val
  252. })
  253. store.dispatch(TEST)
  254. Vue.nextTick(() => {
  255. expect(watchedValueOne).to.equal(2)
  256. expect(watchedValueTwo).to.equal(2)
  257. done()
  258. })
  259. })
  260. it('strict mode: warn mutations outside of handlers', function () {
  261. const store = new Vuex.Store({
  262. state: {
  263. a: 1
  264. },
  265. strict: true
  266. })
  267. expect(() => {
  268. store.state.a++
  269. }).to.throw(/Do not mutate vuex store state outside mutation handlers/)
  270. })
  271. })