test.js 22 KB

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