test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. import chai, { expect } from 'chai'
  2. import sinonChai from 'sinon-chai'
  3. import sinon from 'sinon'
  4. import Vue from 'vue'
  5. import Vuex from '../../src'
  6. import * as util from '../../src/util'
  7. Vue.use(Vuex)
  8. chai.use(sinonChai)
  9. const TEST = 'TEST'
  10. describe('Vuex', () => {
  11. it('direct dispatch', () => {
  12. const store = new Vuex.Store({
  13. state: {
  14. a: 1
  15. },
  16. mutations: {
  17. [TEST] (state, n) {
  18. state.a += n
  19. }
  20. }
  21. })
  22. store.dispatch(TEST, 2)
  23. expect(store.state.a).to.equal(3)
  24. })
  25. it('injecting state and action to components', function () {
  26. const store = new Vuex.Store({
  27. state: {
  28. a: 1
  29. },
  30. mutations: {
  31. [TEST] (state, n) {
  32. state.a += n
  33. }
  34. }
  35. })
  36. const vm = new Vue({
  37. store,
  38. vuex: {
  39. getters: {
  40. a: state => state.a
  41. },
  42. actions: {
  43. test: ({ dispatch }, n) => dispatch(TEST, n)
  44. }
  45. }
  46. })
  47. vm.test(2)
  48. expect(vm.a).to.equal(3)
  49. expect(store.state.a).to.equal(3)
  50. })
  51. it('modules', function () {
  52. const mutations = {
  53. [TEST] (state, n) {
  54. state.a += n
  55. }
  56. }
  57. const store = new Vuex.Store({
  58. state: {
  59. a: 1
  60. },
  61. mutations,
  62. modules: {
  63. one: {
  64. state: { a: 2 },
  65. mutations
  66. },
  67. two: {
  68. state: { a: 3 },
  69. mutations
  70. }
  71. }
  72. })
  73. store.dispatch(TEST, 1)
  74. expect(store.state.a).to.equal(2)
  75. expect(store.state.one.a).to.equal(3)
  76. expect(store.state.two.a).to.equal(4)
  77. })
  78. it('hot reload', function () {
  79. const mutations = {
  80. [TEST] (state, n) {
  81. state.a += n
  82. }
  83. }
  84. const store = new Vuex.Store({
  85. state: {
  86. a: 1
  87. },
  88. mutations,
  89. modules: {
  90. one: {
  91. state: { a: 2 },
  92. mutations
  93. },
  94. two: {
  95. state: { a: 3 },
  96. mutations
  97. }
  98. }
  99. })
  100. store.dispatch(TEST, 1)
  101. expect(store.state.a).to.equal(2)
  102. expect(store.state.one.a).to.equal(3)
  103. expect(store.state.two.a).to.equal(4)
  104. // hot reload only root mutations
  105. store.hotUpdate({
  106. mutations: {
  107. [TEST] (state, n) {
  108. state.a = n
  109. }
  110. }
  111. })
  112. store.dispatch(TEST, 1)
  113. expect(store.state.a).to.equal(1) // only root mutation updated
  114. expect(store.state.one.a).to.equal(4)
  115. expect(store.state.two.a).to.equal(5)
  116. // hot reload modules
  117. store.hotUpdate({
  118. modules: {
  119. one: {
  120. state: { a: 234 },
  121. mutations: {
  122. [TEST] (state, n) {
  123. state.a += n
  124. }
  125. }
  126. },
  127. two: {
  128. state: { a: 345 },
  129. mutations: {
  130. [TEST] (state, n) {
  131. state.a -= n
  132. }
  133. }
  134. }
  135. }
  136. })
  137. store.dispatch(TEST, 2)
  138. expect(store.state.a).to.equal(2)
  139. expect(store.state.one.a).to.equal(6) // should not reload initial state
  140. expect(store.state.two.a).to.equal(3) // should not reload initial state
  141. // hot reload all
  142. store.hotUpdate({
  143. mutations: {
  144. [TEST] (state, n) {
  145. state.a -= n
  146. }
  147. },
  148. modules: {
  149. one: {
  150. state: { a: 234 },
  151. mutations: {
  152. [TEST] (state, n) {
  153. state.a = n
  154. }
  155. }
  156. },
  157. two: {
  158. state: { a: 345 },
  159. mutations: {
  160. [TEST] (state, n) {
  161. state.a = n
  162. }
  163. }
  164. }
  165. }
  166. })
  167. store.dispatch(TEST, 3)
  168. expect(store.state.a).to.equal(-1)
  169. expect(store.state.one.a).to.equal(3)
  170. expect(store.state.two.a).to.equal(3)
  171. })
  172. it('middleware', function () {
  173. let initState
  174. const mutations = []
  175. const store = new Vuex.Store({
  176. state: {
  177. a: 1
  178. },
  179. mutations: {
  180. [TEST] (state, n) {
  181. state.a += n
  182. }
  183. },
  184. middlewares: [
  185. {
  186. onInit (state) {
  187. initState = state
  188. },
  189. onMutation (mut, state) {
  190. expect(state).to.equal(store.state)
  191. mutations.push(mut)
  192. }
  193. }
  194. ]
  195. })
  196. expect(initState).to.equal(store.state)
  197. store.dispatch(TEST, 2)
  198. expect(mutations.length).to.equal(1)
  199. expect(mutations[0].type).to.equal(TEST)
  200. expect(mutations[0].payload[0]).to.equal(2)
  201. })
  202. it('middleware with snapshot', function () {
  203. let initState
  204. const mutations = []
  205. const store = new Vuex.Store({
  206. state: {
  207. a: 1
  208. },
  209. mutations: {
  210. [TEST] (state, n) {
  211. state.a += n
  212. }
  213. },
  214. middlewares: [
  215. {
  216. snapshot: true,
  217. onInit (state) {
  218. initState = state
  219. },
  220. onMutation (mutation, nextState, prevState) {
  221. mutations.push({
  222. mutation,
  223. nextState,
  224. prevState
  225. })
  226. }
  227. }
  228. ]
  229. })
  230. expect(initState).not.to.equal(store.state)
  231. expect(initState.a).to.equal(1)
  232. store.dispatch(TEST, 2)
  233. expect(mutations.length).to.equal(1)
  234. expect(mutations[0].mutation.type).to.equal(TEST)
  235. expect(mutations[0].mutation.payload[0]).to.equal(2)
  236. expect(mutations[0].nextState).not.to.equal(store.state)
  237. expect(mutations[0].prevState.a).to.equal(1)
  238. expect(mutations[0].nextState.a).to.equal(3)
  239. })
  240. it('middleware should ignore silent mutations', function () {
  241. let initState
  242. const mutations = []
  243. const store = new Vuex.Store({
  244. state: {
  245. a: 1
  246. },
  247. mutations: {
  248. [TEST] (state, n) {
  249. state.a += n
  250. }
  251. },
  252. middlewares: [
  253. {
  254. onInit (state) {
  255. initState = state
  256. },
  257. onMutation (mut, state) {
  258. expect(state).to.equal(store.state)
  259. mutations.push(mut)
  260. }
  261. }
  262. ]
  263. })
  264. expect(initState).to.equal(store.state)
  265. store.dispatch(TEST, 1)
  266. store.dispatch({
  267. type: TEST,
  268. payload: 2
  269. })
  270. store.dispatch({
  271. type: TEST,
  272. silent: true,
  273. payload: 3
  274. })
  275. expect(mutations.length).to.equal(2)
  276. expect(mutations[0].type).to.equal(TEST)
  277. expect(mutations[1].type).to.equal(TEST)
  278. expect(mutations[0].payload[0]).to.equal(1)
  279. expect(mutations[1].payload[0]).to.equal(2)
  280. })
  281. it('watch', function (done) {
  282. const store = new Vuex.Store({
  283. state: {
  284. a: 1
  285. },
  286. mutations: {
  287. [TEST]: state => state.a++
  288. }
  289. })
  290. let watchedValueOne
  291. store.watch(({ a }) => a, val => {
  292. watchedValueOne = val
  293. })
  294. store.dispatch(TEST)
  295. Vue.nextTick(() => {
  296. expect(watchedValueOne).to.equal(2)
  297. done()
  298. })
  299. })
  300. it('strict mode: warn mutations outside of handlers', function () {
  301. const store = new Vuex.Store({
  302. state: {
  303. a: 1
  304. },
  305. strict: true
  306. })
  307. expect(() => {
  308. store.state.a++
  309. }).to.throw(/Do not mutate vuex store state outside mutation handlers/)
  310. })
  311. it('option merging', function () {
  312. const store = new Vuex.Store({
  313. state: {
  314. a: 1,
  315. b: 2
  316. },
  317. mutations: {
  318. [TEST] (state, n) {
  319. state.a += n
  320. }
  321. }
  322. })
  323. const Comp = Vue.extend({
  324. vuex: {
  325. getters: {
  326. a: state => state.a
  327. },
  328. actions: {
  329. test: ({ dispatch }, n) => dispatch(TEST, n)
  330. }
  331. },
  332. mixins: [{
  333. vuex: {
  334. getters: {
  335. b: state => state.b
  336. },
  337. actions: {
  338. testPlusOne: ({ dispatch }, n) => dispatch(TEST, n + 1)
  339. }
  340. }
  341. }]
  342. })
  343. const vm = new Comp({ store })
  344. expect(vm.a).to.equal(1)
  345. expect(vm.b).to.equal(2)
  346. vm.test(2)
  347. expect(vm.a).to.equal(3)
  348. expect(store.state.a).to.equal(3)
  349. vm.testPlusOne(2)
  350. expect(vm.a).to.equal(6)
  351. expect(store.state.a).to.equal(6)
  352. })
  353. it('shared getters should evaluate only once', function (done) {
  354. const store = new Vuex.Store({
  355. state: {
  356. a: 1
  357. },
  358. mutations: {
  359. [TEST] (state) {
  360. state.a++
  361. }
  362. }
  363. })
  364. let getterCalls = 0
  365. let watcherCalls = 0
  366. const getter = state => {
  367. getterCalls++
  368. return state.a
  369. }
  370. const vm1 = new Vue({
  371. store,
  372. vuex: {
  373. getters: {
  374. a: getter
  375. }
  376. },
  377. watch: {
  378. a: () => {
  379. watcherCalls++
  380. }
  381. }
  382. })
  383. const vm2 = new Vue({
  384. store,
  385. vuex: {
  386. getters: {
  387. a: getter
  388. }
  389. },
  390. watch: {
  391. a: () => {
  392. watcherCalls++
  393. }
  394. }
  395. })
  396. expect(vm1.a).to.equal(1)
  397. expect(vm2.a).to.equal(1)
  398. expect(getterCalls).to.equal(1)
  399. expect(watcherCalls).to.equal(0)
  400. store.dispatch('TEST')
  401. Vue.nextTick(() => {
  402. expect(vm1.a).to.equal(2)
  403. expect(vm2.a).to.equal(2)
  404. expect(getterCalls).to.equal(2)
  405. expect(watcherCalls).to.equal(2)
  406. done()
  407. })
  408. })
  409. it('object-format mutations', () => {
  410. const store = new Vuex.Store({
  411. state: {
  412. a: 1
  413. },
  414. mutations: {
  415. [TEST] (state, amount) {
  416. state.a += amount
  417. }
  418. }
  419. })
  420. store.dispatch({
  421. type: TEST,
  422. payload: 2
  423. })
  424. expect(store.state.a).to.equal(3)
  425. })
  426. it('console.warn when action is not a function', function () {
  427. sinon.spy(console, 'warn')
  428. new Vue({
  429. vuex: {
  430. actions: {
  431. test: undefined
  432. }
  433. }
  434. })
  435. expect(console.warn).to.have.been.calledWith('[vuex] Action bound to key \'vuex.actions.test\' is not a function.')
  436. console.warn.restore()
  437. })
  438. it('console.warn when getter is not a function', function () {
  439. const store = new Vuex.Store({
  440. state: {
  441. a: 1
  442. },
  443. mutations: {
  444. [TEST] (state, amount) {
  445. state.a += amount
  446. }
  447. }
  448. })
  449. sinon.spy(console, 'warn')
  450. new Vue({
  451. store,
  452. vuex: {
  453. getters: {
  454. test: undefined
  455. }
  456. }
  457. })
  458. expect(console.warn).to.have.been.calledWith('[vuex] Getter bound to key \'vuex.getters.test\' is not a function.')
  459. console.warn.restore()
  460. })
  461. })