test.js 20 KB

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