test.js 21 KB

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