1
0

test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. nested: {
  64. state: { a: 2 },
  65. mutations,
  66. modules: {
  67. one: {
  68. state: { a: 3 },
  69. mutations
  70. },
  71. nested: {
  72. modules: {
  73. two: {
  74. state: { a: 4 },
  75. mutations
  76. },
  77. three: {
  78. state: { a: 5 },
  79. mutations
  80. }
  81. }
  82. }
  83. }
  84. },
  85. four: {
  86. state: { a: 6 },
  87. mutations
  88. }
  89. }
  90. })
  91. store.dispatch(TEST, 1)
  92. expect(store.state.a).to.equal(2)
  93. expect(store.state.nested.a).to.equal(3)
  94. expect(store.state.nested.one.a).to.equal(4)
  95. expect(store.state.nested.nested.two.a).to.equal(5)
  96. expect(store.state.nested.nested.three.a).to.equal(6)
  97. expect(store.state.four.a).to.equal(7)
  98. })
  99. it('hot reload', function () {
  100. const mutations = {
  101. [TEST] (state, n) {
  102. state.a += n
  103. }
  104. }
  105. const store = new Vuex.Store({
  106. state: {
  107. a: 1
  108. },
  109. mutations,
  110. modules: {
  111. nested: {
  112. state: { a: 2 },
  113. mutations,
  114. modules: {
  115. one: {
  116. state: { a: 3 },
  117. mutations
  118. },
  119. nested: {
  120. modules: {
  121. two: {
  122. state: { a: 4 },
  123. mutations
  124. },
  125. three: {
  126. state: { a: 5 },
  127. mutations
  128. }
  129. }
  130. }
  131. }
  132. },
  133. four: {
  134. state: { a: 6 },
  135. mutations
  136. }
  137. }
  138. })
  139. store.dispatch(TEST, 1)
  140. expect(store.state.a).to.equal(2)
  141. expect(store.state.nested.a).to.equal(3)
  142. expect(store.state.nested.one.a).to.equal(4)
  143. expect(store.state.nested.nested.two.a).to.equal(5)
  144. expect(store.state.nested.nested.three.a).to.equal(6)
  145. expect(store.state.four.a).to.equal(7)
  146. // hot reload only root mutations
  147. store.hotUpdate({
  148. mutations: {
  149. [TEST] (state, n) {
  150. state.a = n
  151. }
  152. }
  153. })
  154. store.dispatch(TEST, 1)
  155. expect(store.state.a).to.equal(1) // only root mutation updated
  156. expect(store.state.nested.a).to.equal(4)
  157. expect(store.state.nested.one.a).to.equal(5)
  158. expect(store.state.nested.nested.two.a).to.equal(6)
  159. expect(store.state.nested.nested.three.a).to.equal(7)
  160. expect(store.state.four.a).to.equal(8)
  161. // hot reload modules
  162. store.hotUpdate({
  163. modules: {
  164. nested: {
  165. state: { a: 234 },
  166. mutations,
  167. modules: {
  168. one: {
  169. state: { a: 345 },
  170. mutations
  171. },
  172. nested: {
  173. modules: {
  174. two: {
  175. state: { a: 456 },
  176. mutations
  177. },
  178. three: {
  179. state: { a: 567 },
  180. mutations
  181. }
  182. }
  183. }
  184. }
  185. },
  186. four: {
  187. state: { a: 678 },
  188. mutations
  189. }
  190. }
  191. })
  192. store.dispatch(TEST, 2)
  193. expect(store.state.a).to.equal(2)
  194. expect(store.state.nested.a).to.equal(6) // should not reload initial state
  195. expect(store.state.nested.one.a).to.equal(7) // should not reload initial state
  196. expect(store.state.nested.nested.two.a).to.equal(8) // should not reload initial state
  197. expect(store.state.nested.nested.three.a).to.equal(9) // should not reload initial state
  198. expect(store.state.four.a).to.equal(10) // should not reload initial state
  199. // hot reload all
  200. store.hotUpdate({
  201. mutations: {
  202. [TEST] (state, n) {
  203. state.a -= n
  204. }
  205. },
  206. modules: {
  207. nested: {
  208. state: { a: 234 },
  209. mutations: {
  210. [TEST] (state, n) {
  211. state.a += n
  212. }
  213. },
  214. modules: {
  215. one: {
  216. state: { a: 345 },
  217. mutations: {
  218. [TEST] (state, n) {
  219. state.a += n
  220. }
  221. }
  222. },
  223. nested: {
  224. modules: {
  225. two: {
  226. state: { a: 456 },
  227. mutations: {
  228. [TEST] (state, n) {
  229. state.a += n
  230. }
  231. }
  232. },
  233. three: {
  234. state: { a: 567 },
  235. mutations: {
  236. [TEST] (state, n) {
  237. state.a -= n
  238. }
  239. }
  240. }
  241. }
  242. }
  243. }
  244. },
  245. four: {
  246. state: { a: 678 },
  247. mutations: {
  248. [TEST] (state, n) {
  249. state.a -= n
  250. }
  251. }
  252. }
  253. }
  254. })
  255. store.dispatch(TEST, 3)
  256. expect(store.state.a).to.equal(-1)
  257. expect(store.state.nested.a).to.equal(9)
  258. expect(store.state.nested.one.a).to.equal(10)
  259. expect(store.state.nested.nested.two.a).to.equal(11)
  260. expect(store.state.nested.nested.three.a).to.equal(6)
  261. expect(store.state.four.a).to.equal(7)
  262. })
  263. it('plugins', function () {
  264. let initState
  265. const mutations = []
  266. const store = new Vuex.Store({
  267. state: {
  268. a: 1
  269. },
  270. mutations: {
  271. [TEST] (state, n) {
  272. state.a += n
  273. }
  274. },
  275. plugins: [
  276. store => {
  277. initState = store.state
  278. store.subscribe((mut, state) => {
  279. expect(state).to.equal(store.state)
  280. mutations.push(mut)
  281. })
  282. }
  283. ]
  284. })
  285. expect(initState).to.equal(store.state)
  286. store.dispatch(TEST, 2)
  287. expect(mutations.length).to.equal(1)
  288. expect(mutations[0].type).to.equal(TEST)
  289. expect(mutations[0].payload[0]).to.equal(2)
  290. })
  291. it('plugins should ignore silent mutations', function () {
  292. let initState
  293. const mutations = []
  294. const store = new Vuex.Store({
  295. state: {
  296. a: 1
  297. },
  298. mutations: {
  299. [TEST] (state, { payload }) {
  300. state.a += payload
  301. }
  302. },
  303. plugins: [
  304. store => {
  305. initState = store.state
  306. store.subscribe((mut, state) => {
  307. expect(state).to.equal(store.state)
  308. mutations.push(mut)
  309. })
  310. }
  311. ]
  312. })
  313. expect(initState).to.equal(store.state)
  314. store.dispatch(TEST, 1)
  315. store.dispatch({
  316. type: TEST,
  317. payload: 2
  318. })
  319. store.dispatch({
  320. type: TEST,
  321. silent: true,
  322. payload: 3
  323. })
  324. expect(mutations.length).to.equal(2)
  325. expect(mutations[0].type).to.equal(TEST)
  326. expect(mutations[1].type).to.equal(TEST)
  327. expect(mutations[0].payload[0]).to.equal(1) // normal dispatch
  328. expect(mutations[1].payload).to.equal(2) // object dispatch
  329. })
  330. it('watch', function (done) {
  331. const store = new Vuex.Store({
  332. state: {
  333. a: 1
  334. },
  335. mutations: {
  336. [TEST]: state => state.a++
  337. }
  338. })
  339. let watchedValueOne
  340. store.watch(({ a }) => a, val => {
  341. watchedValueOne = val
  342. })
  343. store.dispatch(TEST)
  344. Vue.nextTick(() => {
  345. expect(watchedValueOne).to.equal(2)
  346. done()
  347. })
  348. })
  349. it('strict mode: warn mutations outside of handlers', function () {
  350. const store = new Vuex.Store({
  351. state: {
  352. a: 1
  353. },
  354. strict: true
  355. })
  356. expect(() => {
  357. store.state.a++
  358. }).to.throw(/Do not mutate vuex store state outside mutation handlers/)
  359. })
  360. it('option merging', function () {
  361. const store = new Vuex.Store({
  362. state: {
  363. a: 1,
  364. b: 2
  365. },
  366. mutations: {
  367. [TEST] (state, n) {
  368. state.a += n
  369. }
  370. }
  371. })
  372. const Comp = Vue.extend({
  373. vuex: {
  374. getters: {
  375. a: state => state.a
  376. },
  377. actions: {
  378. test: ({ dispatch }, n) => dispatch(TEST, n)
  379. }
  380. },
  381. mixins: [{
  382. vuex: {
  383. getters: {
  384. b: state => state.b
  385. },
  386. actions: {
  387. testPlusOne: ({ dispatch }, n) => dispatch(TEST, n + 1)
  388. }
  389. }
  390. }]
  391. })
  392. const vm = new Comp({ store })
  393. expect(vm.a).to.equal(1)
  394. expect(vm.b).to.equal(2)
  395. vm.test(2)
  396. expect(vm.a).to.equal(3)
  397. expect(store.state.a).to.equal(3)
  398. vm.testPlusOne(2)
  399. expect(vm.a).to.equal(6)
  400. expect(store.state.a).to.equal(6)
  401. })
  402. it('shared getters should evaluate only once', function (done) {
  403. const store = new Vuex.Store({
  404. state: {
  405. a: 1
  406. },
  407. mutations: {
  408. [TEST] (state) {
  409. state.a++
  410. }
  411. }
  412. })
  413. let getterCalls = 0
  414. let watcherCalls = 0
  415. const getter = state => {
  416. getterCalls++
  417. return state.a
  418. }
  419. const vm1 = new Vue({
  420. store,
  421. vuex: {
  422. getters: {
  423. a: getter
  424. }
  425. },
  426. watch: {
  427. a: () => {
  428. watcherCalls++
  429. }
  430. }
  431. })
  432. const vm2 = new Vue({
  433. store,
  434. vuex: {
  435. getters: {
  436. a: getter
  437. }
  438. },
  439. watch: {
  440. a: () => {
  441. watcherCalls++
  442. }
  443. }
  444. })
  445. expect(vm1.a).to.equal(1)
  446. expect(vm2.a).to.equal(1)
  447. expect(getterCalls).to.equal(1)
  448. expect(watcherCalls).to.equal(0)
  449. store.dispatch('TEST')
  450. Vue.nextTick(() => {
  451. expect(vm1.a).to.equal(2)
  452. expect(vm2.a).to.equal(2)
  453. expect(getterCalls).to.equal(2)
  454. expect(watcherCalls).to.equal(2)
  455. done()
  456. })
  457. })
  458. it('object-format mutations', () => {
  459. const store = new Vuex.Store({
  460. state: {
  461. a: 1
  462. },
  463. mutations: {
  464. [TEST] (state, { by }) {
  465. state.a += by
  466. }
  467. }
  468. })
  469. store.dispatch({
  470. type: TEST,
  471. by: 2
  472. })
  473. expect(store.state.a).to.equal(3)
  474. })
  475. it('console.warn when action is not a function', function () {
  476. sinon.spy(console, 'warn')
  477. new Vue({
  478. vuex: {
  479. actions: {
  480. test: undefined
  481. }
  482. }
  483. })
  484. expect(console.warn).to.have.been.calledWith('[vuex] Action bound to key \'vuex.actions.test\' is not a function.')
  485. console.warn.restore()
  486. })
  487. it('console.warn when getter is not a function', function () {
  488. const store = new Vuex.Store({
  489. state: {
  490. a: 1
  491. },
  492. mutations: {
  493. [TEST] (state, amount) {
  494. state.a += amount
  495. }
  496. }
  497. })
  498. sinon.spy(console, 'warn')
  499. new Vue({
  500. store,
  501. vuex: {
  502. getters: {
  503. test: undefined
  504. }
  505. }
  506. })
  507. expect(console.warn).to.have.been.calledWith('[vuex] Getter bound to key \'vuex.getters.test\' is not a function.')
  508. console.warn.restore()
  509. })
  510. })