test.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. import 'babel-polyfill'
  2. import chai, { expect } from 'chai'
  3. import sinonChai from 'sinon-chai'
  4. import sinon from 'sinon'
  5. import Vue from 'vue'
  6. import Vuex, { mapState, mapMutations, mapGetters, mapActions } from '../../build/dev-entry'
  7. Vue.use(Vuex)
  8. chai.use(sinonChai)
  9. const TEST = 'TEST'
  10. const TEST2 = 'TEST2'
  11. describe('Vuex', () => {
  12. it('committing mutations', () => {
  13. const store = new Vuex.Store({
  14. state: {
  15. a: 1
  16. },
  17. mutations: {
  18. [TEST] (state, n) {
  19. state.a += n
  20. }
  21. }
  22. })
  23. store.commit(TEST, 2)
  24. expect(store.state.a).to.equal(3)
  25. })
  26. it('dispatching actions, sync', () => {
  27. const store = new Vuex.Store({
  28. state: {
  29. a: 1
  30. },
  31. mutations: {
  32. [TEST] (state, n) {
  33. state.a += n
  34. }
  35. },
  36. actions: {
  37. [TEST] ({ commit }, n) {
  38. commit(TEST, n)
  39. }
  40. }
  41. })
  42. store.dispatch(TEST, 2)
  43. expect(store.state.a).to.equal(3)
  44. })
  45. it('dispatching actions, with returned Promise', done => {
  46. const store = new Vuex.Store({
  47. state: {
  48. a: 1
  49. },
  50. mutations: {
  51. [TEST] (state, n) {
  52. state.a += n
  53. }
  54. },
  55. actions: {
  56. [TEST] ({ commit }, n) {
  57. return new Promise(resolve => {
  58. setTimeout(() => {
  59. commit(TEST, n)
  60. resolve()
  61. }, 0)
  62. })
  63. }
  64. }
  65. })
  66. expect(store.state.a).to.equal(1)
  67. store.dispatch(TEST, 2).then(() => {
  68. expect(store.state.a).to.equal(3)
  69. done()
  70. })
  71. })
  72. it('composing actions with async/await', done => {
  73. const store = new Vuex.Store({
  74. state: {
  75. a: 1
  76. },
  77. mutations: {
  78. [TEST] (state, n) {
  79. state.a += n
  80. }
  81. },
  82. actions: {
  83. [TEST] ({ commit }, n) {
  84. return new Promise(resolve => {
  85. setTimeout(() => {
  86. commit(TEST, n)
  87. resolve()
  88. }, 0)
  89. })
  90. },
  91. two: async ({ commit, dispatch }, n) => {
  92. await dispatch(TEST, 1)
  93. expect(store.state.a).to.equal(2)
  94. commit(TEST, n)
  95. }
  96. }
  97. })
  98. expect(store.state.a).to.equal(1)
  99. store.dispatch('two', 3).then(() => {
  100. expect(store.state.a).to.equal(5)
  101. done()
  102. })
  103. })
  104. it('detecting action Promise errors', done => {
  105. const store = new Vuex.Store({
  106. actions: {
  107. [TEST] () {
  108. return new Promise((resolve, reject) => {
  109. reject('no')
  110. })
  111. }
  112. }
  113. })
  114. const spy = sinon.spy()
  115. store._devtoolHook = {
  116. emit: spy
  117. }
  118. const thenSpy = sinon.spy()
  119. store.dispatch(TEST)
  120. .then(thenSpy)
  121. .catch(err => {
  122. expect(thenSpy).not.to.have.been.called
  123. expect(err).to.equal('no')
  124. expect(spy).to.have.been.calledWith('vuex:error', 'no')
  125. done()
  126. })
  127. })
  128. it('onActionsResolved', done => {
  129. const store = new Vuex.Store({
  130. state: {
  131. count: 0
  132. },
  133. mutations: {
  134. inc: state => state.count++
  135. },
  136. actions: {
  137. one ({ commit }) {
  138. return new Promise(r => {
  139. commit('inc')
  140. r(1)
  141. })
  142. },
  143. two ({ commit }) {
  144. return new Promise(r => {
  145. setTimeout(() => {
  146. commit('inc')
  147. r(2)
  148. }, 0)
  149. })
  150. }
  151. }
  152. })
  153. store.dispatch('one')
  154. store.dispatch('two')
  155. expect(store.state.count).to.equal(1)
  156. expect(store._pendingActions.length).to.equal(2)
  157. store.onActionsResolved(res => {
  158. expect(store._pendingActions.length).to.equal(0)
  159. expect(store.state.count).to.equal(2)
  160. expect(res.length).to.equal(2)
  161. expect(res[0]).to.equal(1)
  162. expect(res[1]).to.equal(2)
  163. done()
  164. })
  165. })
  166. it('getters', () => {
  167. const store = new Vuex.Store({
  168. state: {
  169. a: 1
  170. },
  171. getters: {
  172. hasAny: state => state.a > 1
  173. },
  174. mutations: {
  175. [TEST] (state, n) {
  176. state.a += n
  177. }
  178. },
  179. actions: {
  180. check ({ getters }, value) {
  181. // check for exposing getters into actions
  182. expect(getters.hasAny).to.equal(value)
  183. }
  184. }
  185. })
  186. expect(store.getters.hasAny).to.equal(false)
  187. store.dispatch('check', false)
  188. store.commit(TEST, 1)
  189. expect(store.getters.hasAny).to.equal(true)
  190. store.dispatch('check', true)
  191. })
  192. it('dynamic module registration', () => {
  193. const store = new Vuex.Store({
  194. strict: true
  195. })
  196. expect(() => {
  197. store.module('hi', {
  198. state: { a: 1 },
  199. mutations: { inc: state => state.a++ }
  200. })
  201. }).not.to.throw()
  202. expect(store.state.hi.a).to.equal(1)
  203. store.commit('inc')
  204. expect(store.state.hi.a).to.equal(2)
  205. })
  206. it('store injection', () => {
  207. const store = new Vuex.Store()
  208. const vm = new Vue({
  209. store
  210. })
  211. const child = new Vue({ parent: vm })
  212. expect(child.$store).to.equal(store)
  213. })
  214. it('helper: mapState', () => {
  215. const store = new Vuex.Store({
  216. state: {
  217. a: 1
  218. },
  219. getters: {
  220. b: () => 2
  221. }
  222. })
  223. const vm = new Vue({
  224. store,
  225. computed: mapState({
  226. a: (state, getters) => {
  227. return state.a + getters.b
  228. }
  229. })
  230. })
  231. expect(vm.a).to.equal(3)
  232. store.state.a++
  233. expect(vm.a).to.equal(4)
  234. })
  235. it('helper: mapMutations (array)', () => {
  236. const store = new Vuex.Store({
  237. state: { count: 0 },
  238. mutations: {
  239. inc: state => state.count++,
  240. dec: state => state.count--
  241. }
  242. })
  243. const vm = new Vue({
  244. store,
  245. methods: mapMutations(['inc', 'dec'])
  246. })
  247. vm.inc()
  248. expect(store.state.count).to.equal(1)
  249. vm.dec()
  250. expect(store.state.count).to.equal(0)
  251. })
  252. it('helper: mapMutations (object)', () => {
  253. const store = new Vuex.Store({
  254. state: { count: 0 },
  255. mutations: {
  256. inc: state => state.count++,
  257. dec: state => state.count--
  258. }
  259. })
  260. const vm = new Vue({
  261. store,
  262. methods: mapMutations({
  263. plus: 'inc',
  264. minus: 'dec'
  265. })
  266. })
  267. vm.plus()
  268. expect(store.state.count).to.equal(1)
  269. vm.minus()
  270. expect(store.state.count).to.equal(0)
  271. })
  272. it('helper: mapGetters (array)', () => {
  273. const store = new Vuex.Store({
  274. state: { count: 0 },
  275. mutations: {
  276. inc: state => state.count++,
  277. dec: state => state.count--
  278. },
  279. getters: {
  280. hasAny: ({ count }) => count > 0,
  281. negative: ({ count }) => count < 0
  282. }
  283. })
  284. const vm = new Vue({
  285. store,
  286. computed: mapGetters(['hasAny', 'negative'])
  287. })
  288. expect(vm.hasAny).to.equal(false)
  289. expect(vm.negative).to.equal(false)
  290. store.commit('inc')
  291. expect(vm.hasAny).to.equal(true)
  292. expect(vm.negative).to.equal(false)
  293. store.commit('dec')
  294. store.commit('dec')
  295. expect(vm.hasAny).to.equal(false)
  296. expect(vm.negative).to.equal(true)
  297. })
  298. it('helper: mapGetters (object)', () => {
  299. const store = new Vuex.Store({
  300. state: { count: 0 },
  301. mutations: {
  302. inc: state => state.count++,
  303. dec: state => state.count--
  304. },
  305. getters: {
  306. hasAny: ({ count }) => count > 0,
  307. negative: ({ count }) => count < 0
  308. }
  309. })
  310. const vm = new Vue({
  311. store,
  312. computed: mapGetters({
  313. a: 'hasAny',
  314. b: 'negative'
  315. })
  316. })
  317. expect(vm.a).to.equal(false)
  318. expect(vm.b).to.equal(false)
  319. store.commit('inc')
  320. expect(vm.a).to.equal(true)
  321. expect(vm.b).to.equal(false)
  322. store.commit('dec')
  323. store.commit('dec')
  324. expect(vm.a).to.equal(false)
  325. expect(vm.b).to.equal(true)
  326. })
  327. it('helper: mapActions (array)', () => {
  328. const a = sinon.spy()
  329. const b = sinon.spy()
  330. const store = new Vuex.Store({
  331. actions: {
  332. a,
  333. b
  334. }
  335. })
  336. const vm = new Vue({
  337. store,
  338. methods: mapActions(['a', 'b'])
  339. })
  340. vm.a()
  341. expect(a).to.have.been.called
  342. expect(b).not.to.have.been.called
  343. vm.b()
  344. expect(b).to.have.been.called
  345. })
  346. it('helper: mapActions (object)', () => {
  347. const a = sinon.spy()
  348. const b = sinon.spy()
  349. const store = new Vuex.Store({
  350. actions: {
  351. a,
  352. b
  353. }
  354. })
  355. const vm = new Vue({
  356. store,
  357. methods: mapActions({
  358. foo: 'a',
  359. bar: 'b'
  360. })
  361. })
  362. vm.foo()
  363. expect(a).to.have.been.called
  364. expect(b).not.to.have.been.called
  365. vm.bar()
  366. expect(b).to.have.been.called
  367. })
  368. it('module: mutation', function () {
  369. const mutations = {
  370. [TEST] (state, n) {
  371. state.a += n
  372. }
  373. }
  374. const store = new Vuex.Store({
  375. state: {
  376. a: 1
  377. },
  378. mutations,
  379. modules: {
  380. nested: {
  381. state: { a: 2 },
  382. mutations,
  383. modules: {
  384. one: {
  385. state: { a: 3 },
  386. mutations
  387. },
  388. nested: {
  389. modules: {
  390. two: {
  391. state: { a: 4 },
  392. mutations
  393. },
  394. three: {
  395. state: { a: 5 },
  396. mutations
  397. }
  398. }
  399. }
  400. }
  401. },
  402. four: {
  403. state: { a: 6 },
  404. mutations
  405. }
  406. }
  407. })
  408. store.commit(TEST, 1)
  409. expect(store.state.a).to.equal(2)
  410. expect(store.state.nested.a).to.equal(3)
  411. expect(store.state.nested.one.a).to.equal(4)
  412. expect(store.state.nested.nested.two.a).to.equal(5)
  413. expect(store.state.nested.nested.three.a).to.equal(6)
  414. expect(store.state.four.a).to.equal(7)
  415. })
  416. it('module: action', function () {
  417. let calls = 0
  418. const makeAction = n => {
  419. return {
  420. [TEST] ({ state, rootState }) {
  421. calls++
  422. expect(state.a).to.equal(n)
  423. expect(rootState).to.equal(store.state)
  424. }
  425. }
  426. }
  427. const store = new Vuex.Store({
  428. state: {
  429. a: 1
  430. },
  431. actions: makeAction(1),
  432. modules: {
  433. nested: {
  434. state: { a: 2 },
  435. actions: makeAction(2),
  436. modules: {
  437. one: {
  438. state: { a: 3 },
  439. actions: makeAction(3)
  440. },
  441. nested: {
  442. modules: {
  443. two: {
  444. state: { a: 4 },
  445. actions: makeAction(4)
  446. },
  447. three: {
  448. state: { a: 5 },
  449. actions: makeAction(5)
  450. }
  451. }
  452. }
  453. }
  454. },
  455. four: {
  456. state: { a: 6 },
  457. actions: makeAction(6)
  458. }
  459. }
  460. })
  461. store.dispatch(TEST)
  462. expect(calls).to.equal(6)
  463. })
  464. it('module: getters', function () {
  465. const makeGetter = n => ({
  466. [`getter${n}`]: (state, getters, rootState) => {
  467. expect(getters.constant).to.equal(0)
  468. expect(rootState).to.equal(store.state)
  469. return state.a
  470. }
  471. })
  472. const store = new Vuex.Store({
  473. state: {
  474. a: 1
  475. },
  476. getters: {
  477. constant: () => 0,
  478. ...makeGetter(1)
  479. },
  480. modules: {
  481. nested: {
  482. state: { a: 2 },
  483. getters: makeGetter(2),
  484. modules: {
  485. one: {
  486. state: { a: 3 },
  487. getters: makeGetter(3)
  488. },
  489. nested: {
  490. modules: {
  491. two: {
  492. state: { a: 4 },
  493. getters: makeGetter(4)
  494. },
  495. three: {
  496. state: { a: 5 },
  497. getters: makeGetter(5)
  498. }
  499. }
  500. }
  501. }
  502. },
  503. four: {
  504. state: { a: 6 },
  505. getters: makeGetter(6)
  506. }
  507. }
  508. })
  509. ;[1, 2, 3, 4, 5, 6].forEach(n => {
  510. expect(store.getters[`getter${n}`]).to.equal(n)
  511. })
  512. })
  513. it('dispatching multiple actions in different modules', done => {
  514. const store = new Vuex.Store({
  515. modules: {
  516. a: {
  517. actions: {
  518. [TEST] () {
  519. return 1
  520. }
  521. }
  522. },
  523. b: {
  524. actions: {
  525. [TEST] () {
  526. return new Promise(r => r(2))
  527. }
  528. }
  529. }
  530. }
  531. })
  532. store.dispatch(TEST).then(res => {
  533. expect(res[0]).to.equal(1)
  534. expect(res[1]).to.equal(2)
  535. done()
  536. })
  537. })
  538. it('plugins', function () {
  539. let initState
  540. const mutations = []
  541. const store = new Vuex.Store({
  542. state: {
  543. a: 1
  544. },
  545. mutations: {
  546. [TEST] (state, n) {
  547. state.a += n
  548. }
  549. },
  550. plugins: [
  551. store => {
  552. initState = store.state
  553. store.subscribe((mut, state) => {
  554. expect(state).to.equal(store.state)
  555. mutations.push(mut)
  556. })
  557. }
  558. ]
  559. })
  560. expect(initState).to.equal(store.state)
  561. store.commit(TEST, 2)
  562. expect(mutations.length).to.equal(1)
  563. expect(mutations[0].type).to.equal(TEST)
  564. expect(mutations[0].payload).to.equal(2)
  565. })
  566. it('plugins should ignore silent mutations', function () {
  567. let initState
  568. const mutations = []
  569. const store = new Vuex.Store({
  570. state: {
  571. a: 1
  572. },
  573. mutations: {
  574. [TEST] (state, { n }) {
  575. state.a += n
  576. }
  577. },
  578. plugins: [
  579. store => {
  580. initState = store.state
  581. store.subscribe((mut, state) => {
  582. expect(state).to.equal(store.state)
  583. mutations.push(mut)
  584. })
  585. }
  586. ]
  587. })
  588. expect(initState).to.equal(store.state)
  589. store.commit(TEST, { n: 1 })
  590. store.commit({
  591. type: TEST,
  592. n: 2
  593. })
  594. store.commit({
  595. type: TEST,
  596. silent: true,
  597. n: 3
  598. })
  599. expect(mutations.length).to.equal(2)
  600. expect(mutations[0].type).to.equal(TEST)
  601. expect(mutations[1].type).to.equal(TEST)
  602. expect(mutations[0].payload.n).to.equal(1) // normal dispatch
  603. expect(mutations[1].n).to.equal(2) // object dispatch
  604. })
  605. it('strict mode: warn mutations outside of handlers', function () {
  606. const store = new Vuex.Store({
  607. state: {
  608. a: 1
  609. },
  610. strict: true
  611. })
  612. expect(() => {
  613. store.state.a++
  614. }).to.throw(/Do not mutate vuex store state outside mutation handlers/)
  615. })
  616. it('hot reload: mutations', function () {
  617. const mutations = {
  618. [TEST] (state, n) {
  619. state.a += n
  620. }
  621. }
  622. const store = new Vuex.Store({
  623. state: {
  624. a: 1
  625. },
  626. mutations,
  627. modules: {
  628. nested: {
  629. state: { a: 2 },
  630. mutations,
  631. modules: {
  632. one: {
  633. state: { a: 3 },
  634. mutations
  635. },
  636. nested: {
  637. modules: {
  638. two: {
  639. state: { a: 4 },
  640. mutations
  641. },
  642. three: {
  643. state: { a: 5 },
  644. mutations
  645. }
  646. }
  647. }
  648. }
  649. },
  650. four: {
  651. state: { a: 6 },
  652. mutations
  653. }
  654. }
  655. })
  656. store.commit(TEST, 1)
  657. expect(store.state.a).to.equal(2)
  658. expect(store.state.nested.a).to.equal(3)
  659. expect(store.state.nested.one.a).to.equal(4)
  660. expect(store.state.nested.nested.two.a).to.equal(5)
  661. expect(store.state.nested.nested.three.a).to.equal(6)
  662. expect(store.state.four.a).to.equal(7)
  663. // hot reload only root mutations
  664. store.hotUpdate({
  665. mutations: {
  666. [TEST] (state, n) {
  667. state.a = n
  668. }
  669. }
  670. })
  671. store.commit(TEST, 1)
  672. expect(store.state.a).to.equal(1) // only root mutation updated
  673. expect(store.state.nested.a).to.equal(4)
  674. expect(store.state.nested.one.a).to.equal(5)
  675. expect(store.state.nested.nested.two.a).to.equal(6)
  676. expect(store.state.nested.nested.three.a).to.equal(7)
  677. expect(store.state.four.a).to.equal(8)
  678. // hot reload modules
  679. store.hotUpdate({
  680. modules: {
  681. nested: {
  682. state: { a: 234 },
  683. mutations,
  684. modules: {
  685. one: {
  686. state: { a: 345 },
  687. mutations
  688. },
  689. nested: {
  690. modules: {
  691. two: {
  692. state: { a: 456 },
  693. mutations
  694. },
  695. three: {
  696. state: { a: 567 },
  697. mutations
  698. }
  699. }
  700. }
  701. }
  702. },
  703. four: {
  704. state: { a: 678 },
  705. mutations
  706. }
  707. }
  708. })
  709. store.commit(TEST, 2)
  710. expect(store.state.a).to.equal(2)
  711. expect(store.state.nested.a).to.equal(6) // should not reload initial state
  712. expect(store.state.nested.one.a).to.equal(7) // should not reload initial state
  713. expect(store.state.nested.nested.two.a).to.equal(8) // should not reload initial state
  714. expect(store.state.nested.nested.three.a).to.equal(9) // should not reload initial state
  715. expect(store.state.four.a).to.equal(10) // should not reload initial state
  716. // hot reload all
  717. store.hotUpdate({
  718. mutations: {
  719. [TEST] (state, n) {
  720. state.a -= n
  721. }
  722. },
  723. modules: {
  724. nested: {
  725. state: { a: 234 },
  726. mutations: {
  727. [TEST] (state, n) {
  728. state.a += n
  729. }
  730. },
  731. modules: {
  732. one: {
  733. state: { a: 345 },
  734. mutations: {
  735. [TEST] (state, n) {
  736. state.a += n
  737. }
  738. }
  739. },
  740. nested: {
  741. modules: {
  742. two: {
  743. state: { a: 456 },
  744. mutations: {
  745. [TEST] (state, n) {
  746. state.a += n
  747. }
  748. }
  749. },
  750. three: {
  751. state: { a: 567 },
  752. mutations: {
  753. [TEST] (state, n) {
  754. state.a -= n
  755. }
  756. }
  757. }
  758. }
  759. }
  760. }
  761. },
  762. four: {
  763. state: { a: 678 },
  764. mutations: {
  765. [TEST] (state, n) {
  766. state.a -= n
  767. }
  768. }
  769. }
  770. }
  771. })
  772. store.commit(TEST, 3)
  773. expect(store.state.a).to.equal(-1)
  774. expect(store.state.nested.a).to.equal(9)
  775. expect(store.state.nested.one.a).to.equal(10)
  776. expect(store.state.nested.nested.two.a).to.equal(11)
  777. expect(store.state.nested.nested.three.a).to.equal(6)
  778. expect(store.state.four.a).to.equal(7)
  779. })
  780. it('hot reload: actions', () => {
  781. const store = new Vuex.Store({
  782. state: {
  783. list: []
  784. },
  785. mutations: {
  786. [TEST] (state, n) {
  787. state.list.push(n)
  788. }
  789. },
  790. actions: {
  791. [TEST] ({ commit }) {
  792. commit(TEST, 1)
  793. }
  794. },
  795. modules: {
  796. a: {
  797. actions: {
  798. [TEST] ({ commit }) {
  799. commit(TEST, 2)
  800. }
  801. }
  802. }
  803. }
  804. })
  805. store.dispatch(TEST)
  806. expect(store.state.list.join()).to.equal('1,2')
  807. // update root
  808. store.hotUpdate({
  809. actions: {
  810. [TEST] ({ commit }) {
  811. commit(TEST, 3)
  812. }
  813. }
  814. })
  815. store.dispatch(TEST)
  816. expect(store.state.list.join()).to.equal('1,2,3,2')
  817. // update modules
  818. store.hotUpdate({
  819. actions: {
  820. [TEST] ({ commit }) {
  821. commit(TEST, 4)
  822. }
  823. },
  824. modules: {
  825. a: {
  826. actions: {
  827. [TEST] ({ commit }) {
  828. commit(TEST, 5)
  829. }
  830. }
  831. }
  832. }
  833. })
  834. store.dispatch(TEST)
  835. expect(store.state.list.join()).to.equal('1,2,3,2,4,5')
  836. })
  837. it('hot reload: getters', done => {
  838. const store = new Vuex.Store({
  839. state: {
  840. count: 0
  841. },
  842. mutations: {
  843. inc: state => state.count++
  844. },
  845. getters: {
  846. count: state => state.count
  847. },
  848. actions: {
  849. check ({ getters }, value) {
  850. expect(getters.count).to.equal(value)
  851. }
  852. }
  853. })
  854. const spy = sinon.spy()
  855. const vm = new Vue({
  856. computed: {
  857. a: () => store.getters.count
  858. },
  859. watch: {
  860. a: spy
  861. }
  862. })
  863. expect(vm.a).to.equal(0)
  864. store.dispatch('check', 0)
  865. store.commit('inc')
  866. expect(vm.a).to.equal(1)
  867. store.dispatch('check', 1)
  868. // update getters
  869. store.hotUpdate({
  870. getters: {
  871. count: state => state.count * 10
  872. }
  873. })
  874. expect(vm.a).to.equal(10)
  875. store.dispatch('check', 10)
  876. Vue.nextTick(() => {
  877. expect(spy).to.have.been.called
  878. done()
  879. })
  880. })
  881. })