test.js 21 KB

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