1
0

test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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, watchedValueTwo
  291. store.watch(({ a }) => a, val => {
  292. watchedValueOne = val
  293. })
  294. store.watch('a', val => {
  295. watchedValueTwo = val
  296. })
  297. store.dispatch(TEST)
  298. Vue.nextTick(() => {
  299. expect(watchedValueOne).to.equal(2)
  300. expect(watchedValueTwo).to.equal(2)
  301. done()
  302. })
  303. })
  304. it('strict mode: warn mutations outside of handlers', function () {
  305. const store = new Vuex.Store({
  306. state: {
  307. a: 1
  308. },
  309. strict: true
  310. })
  311. expect(() => {
  312. store.state.a++
  313. }).to.throw(/Do not mutate vuex store state outside mutation handlers/)
  314. })
  315. it('option merging', function () {
  316. const store = new Vuex.Store({
  317. state: {
  318. a: 1,
  319. b: 2
  320. },
  321. mutations: {
  322. [TEST] (state, n) {
  323. state.a += n
  324. }
  325. }
  326. })
  327. const Comp = Vue.extend({
  328. vuex: {
  329. getters: {
  330. a: state => state.a
  331. },
  332. actions: {
  333. test: ({ dispatch }, n) => dispatch(TEST, n)
  334. }
  335. },
  336. mixins: [{
  337. vuex: {
  338. getters: {
  339. b: state => state.b
  340. },
  341. actions: {
  342. testPlusOne: ({ dispatch }, n) => dispatch(TEST, n + 1)
  343. }
  344. }
  345. }]
  346. })
  347. const vm = new Comp({ store })
  348. expect(vm.a).to.equal(1)
  349. expect(vm.b).to.equal(2)
  350. vm.test(2)
  351. expect(vm.a).to.equal(3)
  352. expect(store.state.a).to.equal(3)
  353. vm.testPlusOne(2)
  354. expect(vm.a).to.equal(6)
  355. expect(store.state.a).to.equal(6)
  356. })
  357. it('shared getters should evaluate only once', function (done) {
  358. const store = new Vuex.Store({
  359. state: {
  360. a: 1
  361. },
  362. mutations: {
  363. [TEST] (state) {
  364. state.a++
  365. }
  366. }
  367. })
  368. let getterCalls = 0
  369. let watcherCalls = 0
  370. const getter = state => {
  371. getterCalls++
  372. return state.a
  373. }
  374. const vm1 = new Vue({
  375. store,
  376. vuex: {
  377. getters: {
  378. a: getter
  379. }
  380. },
  381. watch: {
  382. a: () => {
  383. watcherCalls++
  384. }
  385. }
  386. })
  387. const vm2 = new Vue({
  388. store,
  389. vuex: {
  390. getters: {
  391. a: getter
  392. }
  393. },
  394. watch: {
  395. a: () => {
  396. watcherCalls++
  397. }
  398. }
  399. })
  400. expect(vm1.a).to.equal(1)
  401. expect(vm2.a).to.equal(1)
  402. expect(getterCalls).to.equal(1)
  403. expect(watcherCalls).to.equal(0)
  404. store.dispatch('TEST')
  405. Vue.nextTick(() => {
  406. expect(vm1.a).to.equal(2)
  407. expect(vm2.a).to.equal(2)
  408. expect(getterCalls).to.equal(2)
  409. expect(watcherCalls).to.equal(2)
  410. done()
  411. })
  412. })
  413. it('object-format mutations', () => {
  414. const store = new Vuex.Store({
  415. state: {
  416. a: 1
  417. },
  418. mutations: {
  419. [TEST] (state, amount) {
  420. state.a += amount
  421. }
  422. }
  423. })
  424. store.dispatch({
  425. type: TEST,
  426. payload: 2
  427. })
  428. expect(store.state.a).to.equal(3)
  429. })
  430. it('console.warn when action is not a function', function () {
  431. sinon.spy(console, 'warn')
  432. new Vue({
  433. vuex: {
  434. actions: {
  435. test: undefined
  436. }
  437. }
  438. })
  439. expect(console.warn).to.have.been.calledWith('[vuex] Action bound to key \'vuex.actions.test\' is not a function.')
  440. console.warn.restore()
  441. })
  442. it('console.warn when getter is not a function', function () {
  443. const store = new Vuex.Store({
  444. state: {
  445. a: 1
  446. },
  447. mutations: {
  448. [TEST] (state, amount) {
  449. state.a += amount
  450. }
  451. }
  452. })
  453. sinon.spy(console, 'warn')
  454. new Vue({
  455. store,
  456. vuex: {
  457. getters: {
  458. test: undefined
  459. }
  460. }
  461. })
  462. expect(console.warn).to.have.been.calledWith('[vuex] Getter bound to key \'vuex.getters.test\' is not a function.')
  463. console.warn.restore()
  464. })
  465. })