test.js 4.7 KB

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