helpers.spec.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. import Vue from 'vue'
  2. import Vuex, { mapState, mapMutations, mapGetters, mapActions, createNamespacedHelpers } from '../../src/index'
  3. describe('Helpers', () => {
  4. it('mapState (array)', () => {
  5. const store = new Vuex.Store({
  6. state: {
  7. a: 1
  8. }
  9. })
  10. const vm = new Vue({
  11. store,
  12. computed: mapState(['a'])
  13. })
  14. expect(vm.a).toBe(1)
  15. store.state.a++
  16. expect(vm.a).toBe(2)
  17. })
  18. it('mapState (object)', () => {
  19. const store = new Vuex.Store({
  20. state: {
  21. a: 1
  22. },
  23. getters: {
  24. b: () => 2
  25. }
  26. })
  27. const vm = new Vue({
  28. store,
  29. computed: mapState({
  30. a: (state, getters) => {
  31. return state.a + getters.b
  32. }
  33. })
  34. })
  35. expect(vm.a).toBe(3)
  36. store.state.a++
  37. expect(vm.a).toBe(4)
  38. })
  39. it('mapState (with namespace)', () => {
  40. const store = new Vuex.Store({
  41. modules: {
  42. foo: {
  43. namespaced: true,
  44. state: { a: 1 },
  45. getters: {
  46. b: state => state.a + 1
  47. }
  48. }
  49. }
  50. })
  51. const vm = new Vue({
  52. store,
  53. computed: mapState('foo', {
  54. a: (state, getters) => {
  55. return state.a + getters.b
  56. }
  57. })
  58. })
  59. expect(vm.a).toBe(3)
  60. store.state.foo.a++
  61. expect(vm.a).toBe(5)
  62. store.replaceState({
  63. foo: { a: 3 }
  64. })
  65. expect(vm.a).toBe(7)
  66. })
  67. // #708
  68. it('mapState (with namespace and a nested module)', () => {
  69. const store = new Vuex.Store({
  70. modules: {
  71. foo: {
  72. namespaced: true,
  73. state: { a: 1 },
  74. modules: {
  75. bar: {
  76. state: { b: 2 }
  77. }
  78. }
  79. }
  80. }
  81. })
  82. const vm = new Vue({
  83. store,
  84. computed: mapState('foo', {
  85. value: state => state
  86. })
  87. })
  88. expect(vm.value.a).toBe(1)
  89. expect(vm.value.bar.b).toBe(2)
  90. expect(vm.value.b).toBeUndefined()
  91. })
  92. it('mapState (with undefined states)', () => {
  93. jest.spyOn(console, 'error').mockImplementation()
  94. const store = new Vuex.Store({
  95. modules: {
  96. foo: {
  97. namespaced: true,
  98. state: { a: 1 }
  99. }
  100. }
  101. })
  102. const vm = new Vue({
  103. store,
  104. computed: mapState('foo')
  105. })
  106. expect(vm.a).toBeUndefined()
  107. expect(console.error).toHaveBeenCalledWith('[vuex] mapState: mapper parameter must be either an Array or an Object')
  108. })
  109. it('mapMutations (array)', () => {
  110. const store = new Vuex.Store({
  111. state: { count: 0 },
  112. mutations: {
  113. inc: state => state.count++,
  114. dec: state => state.count--
  115. }
  116. })
  117. const vm = new Vue({
  118. store,
  119. methods: mapMutations(['inc', 'dec'])
  120. })
  121. vm.inc()
  122. expect(store.state.count).toBe(1)
  123. vm.dec()
  124. expect(store.state.count).toBe(0)
  125. })
  126. it('mapMutations (object)', () => {
  127. const store = new Vuex.Store({
  128. state: { count: 0 },
  129. mutations: {
  130. inc: state => state.count++,
  131. dec: state => state.count--
  132. }
  133. })
  134. const vm = new Vue({
  135. store,
  136. methods: mapMutations({
  137. plus: 'inc',
  138. minus: 'dec'
  139. })
  140. })
  141. vm.plus()
  142. expect(store.state.count).toBe(1)
  143. vm.minus()
  144. expect(store.state.count).toBe(0)
  145. })
  146. it('mapMutations (function)', () => {
  147. const store = new Vuex.Store({
  148. state: { count: 0 },
  149. mutations: {
  150. inc (state, amount) {
  151. state.count += amount
  152. }
  153. }
  154. })
  155. const vm = new Vue({
  156. store,
  157. methods: mapMutations({
  158. plus (commit, amount) {
  159. commit('inc', amount + 1)
  160. }
  161. })
  162. })
  163. vm.plus(42)
  164. expect(store.state.count).toBe(43)
  165. })
  166. it('mapMutations (with namespace)', () => {
  167. const store = new Vuex.Store({
  168. modules: {
  169. foo: {
  170. namespaced: true,
  171. state: { count: 0 },
  172. mutations: {
  173. inc: state => state.count++,
  174. dec: state => state.count--
  175. }
  176. }
  177. }
  178. })
  179. const vm = new Vue({
  180. store,
  181. methods: mapMutations('foo', {
  182. plus: 'inc',
  183. minus: 'dec'
  184. })
  185. })
  186. vm.plus()
  187. expect(store.state.foo.count).toBe(1)
  188. vm.minus()
  189. expect(store.state.foo.count).toBe(0)
  190. })
  191. it('mapMutations (function with namepsace)', () => {
  192. const store = new Vuex.Store({
  193. modules: {
  194. foo: {
  195. namespaced: true,
  196. state: { count: 0 },
  197. mutations: {
  198. inc (state, amount) {
  199. state.count += amount
  200. }
  201. }
  202. }
  203. }
  204. })
  205. const vm = new Vue({
  206. store,
  207. methods: mapMutations('foo', {
  208. plus (commit, amount) {
  209. commit('inc', amount + 1)
  210. }
  211. })
  212. })
  213. vm.plus(42)
  214. expect(store.state.foo.count).toBe(43)
  215. })
  216. it('mapMutations (with undefined mutations)', () => {
  217. jest.spyOn(console, 'error').mockImplementation()
  218. const store = new Vuex.Store({
  219. modules: {
  220. foo: {
  221. namespaced: true,
  222. state: { count: 0 },
  223. mutations: {
  224. inc: state => state.count++,
  225. dec: state => state.count--
  226. }
  227. }
  228. }
  229. })
  230. const vm = new Vue({
  231. store,
  232. methods: mapMutations('foo')
  233. })
  234. expect(vm.inc).toBeUndefined()
  235. expect(vm.dec).toBeUndefined()
  236. expect(console.error).toHaveBeenCalledWith('[vuex] mapMutations: mapper parameter must be either an Array or an Object')
  237. })
  238. it('mapGetters (array)', () => {
  239. const store = new Vuex.Store({
  240. state: { count: 0 },
  241. mutations: {
  242. inc: state => state.count++,
  243. dec: state => state.count--
  244. },
  245. getters: {
  246. hasAny: ({ count }) => count > 0,
  247. negative: ({ count }) => count < 0
  248. }
  249. })
  250. const vm = new Vue({
  251. store,
  252. computed: mapGetters(['hasAny', 'negative'])
  253. })
  254. expect(vm.hasAny).toBe(false)
  255. expect(vm.negative).toBe(false)
  256. store.commit('inc')
  257. expect(vm.hasAny).toBe(true)
  258. expect(vm.negative).toBe(false)
  259. store.commit('dec')
  260. store.commit('dec')
  261. expect(vm.hasAny).toBe(false)
  262. expect(vm.negative).toBe(true)
  263. })
  264. it('mapGetters (object)', () => {
  265. const store = new Vuex.Store({
  266. state: { count: 0 },
  267. mutations: {
  268. inc: state => state.count++,
  269. dec: state => state.count--
  270. },
  271. getters: {
  272. hasAny: ({ count }) => count > 0,
  273. negative: ({ count }) => count < 0
  274. }
  275. })
  276. const vm = new Vue({
  277. store,
  278. computed: mapGetters({
  279. a: 'hasAny',
  280. b: 'negative'
  281. })
  282. })
  283. expect(vm.a).toBe(false)
  284. expect(vm.b).toBe(false)
  285. store.commit('inc')
  286. expect(vm.a).toBe(true)
  287. expect(vm.b).toBe(false)
  288. store.commit('dec')
  289. store.commit('dec')
  290. expect(vm.a).toBe(false)
  291. expect(vm.b).toBe(true)
  292. })
  293. it('mapGetters (with namespace)', () => {
  294. const store = new Vuex.Store({
  295. modules: {
  296. foo: {
  297. namespaced: true,
  298. state: { count: 0 },
  299. mutations: {
  300. inc: state => state.count++,
  301. dec: state => state.count--
  302. },
  303. getters: {
  304. hasAny: ({ count }) => count > 0,
  305. negative: ({ count }) => count < 0
  306. }
  307. }
  308. }
  309. })
  310. const vm = new Vue({
  311. store,
  312. computed: mapGetters('foo', {
  313. a: 'hasAny',
  314. b: 'negative'
  315. })
  316. })
  317. expect(vm.a).toBe(false)
  318. expect(vm.b).toBe(false)
  319. store.commit('foo/inc')
  320. expect(vm.a).toBe(true)
  321. expect(vm.b).toBe(false)
  322. store.commit('foo/dec')
  323. store.commit('foo/dec')
  324. expect(vm.a).toBe(false)
  325. expect(vm.b).toBe(true)
  326. })
  327. it('mapGetters (with namespace and nested module)', () => {
  328. const store = new Vuex.Store({
  329. modules: {
  330. foo: {
  331. namespaced: true,
  332. modules: {
  333. bar: {
  334. namespaced: true,
  335. state: { count: 0 },
  336. mutations: {
  337. inc: state => state.count++,
  338. dec: state => state.count--
  339. },
  340. getters: {
  341. hasAny: ({ count }) => count > 0,
  342. negative: ({ count }) => count < 0
  343. }
  344. },
  345. cat: {
  346. state: { count: 9 },
  347. getters: {
  348. count: ({ count }) => count
  349. }
  350. }
  351. }
  352. }
  353. }
  354. })
  355. const vm = new Vue({
  356. store,
  357. computed: {
  358. ...mapGetters('foo/bar', [
  359. 'hasAny',
  360. 'negative'
  361. ]),
  362. ...mapGetters('foo', [
  363. 'count'
  364. ])
  365. }
  366. })
  367. expect(vm.hasAny).toBe(false)
  368. expect(vm.negative).toBe(false)
  369. store.commit('foo/bar/inc')
  370. expect(vm.hasAny).toBe(true)
  371. expect(vm.negative).toBe(false)
  372. store.commit('foo/bar/dec')
  373. store.commit('foo/bar/dec')
  374. expect(vm.hasAny).toBe(false)
  375. expect(vm.negative).toBe(true)
  376. expect(vm.count).toBe(9)
  377. })
  378. it('mapGetters (with undefined getters)', () => {
  379. jest.spyOn(console, 'error').mockImplementation()
  380. const store = new Vuex.Store({
  381. modules: {
  382. foo: {
  383. namespaced: true,
  384. state: { count: 0 },
  385. mutations: {
  386. inc: state => state.count++,
  387. dec: state => state.count--
  388. },
  389. getters: {
  390. hasAny: ({ count }) => count > 0,
  391. negative: ({ count }) => count < 0
  392. }
  393. }
  394. }
  395. })
  396. const vm = new Vue({
  397. store,
  398. computed: mapGetters('foo')
  399. })
  400. expect(vm.a).toBeUndefined()
  401. expect(vm.b).toBeUndefined()
  402. expect(console.error).toHaveBeenCalledWith('[vuex] mapGetters: mapper parameter must be either an Array or an Object')
  403. })
  404. it('mapActions (array)', () => {
  405. const a = jest.fn()
  406. const b = jest.fn()
  407. const store = new Vuex.Store({
  408. actions: {
  409. a,
  410. b
  411. }
  412. })
  413. const vm = new Vue({
  414. store,
  415. methods: mapActions(['a', 'b'])
  416. })
  417. vm.a()
  418. expect(a).toHaveBeenCalled()
  419. expect(b).not.toHaveBeenCalled()
  420. vm.b()
  421. expect(b).toHaveBeenCalled()
  422. })
  423. it('mapActions (object)', () => {
  424. const a = jest.fn()
  425. const b = jest.fn()
  426. const store = new Vuex.Store({
  427. actions: {
  428. a,
  429. b
  430. }
  431. })
  432. const vm = new Vue({
  433. store,
  434. methods: mapActions({
  435. foo: 'a',
  436. bar: 'b'
  437. })
  438. })
  439. vm.foo()
  440. expect(a).toHaveBeenCalled()
  441. expect(b).not.toHaveBeenCalled()
  442. vm.bar()
  443. expect(b).toHaveBeenCalled()
  444. })
  445. it('mapActions (function)', () => {
  446. const a = jest.fn()
  447. const store = new Vuex.Store({
  448. actions: { a }
  449. })
  450. const vm = new Vue({
  451. store,
  452. methods: mapActions({
  453. foo (dispatch, arg) {
  454. dispatch('a', arg + 'bar')
  455. }
  456. })
  457. })
  458. vm.foo('foo')
  459. expect(a.mock.calls[0][1]).toBe('foobar')
  460. })
  461. it('mapActions (with namespace)', () => {
  462. const a = jest.fn()
  463. const b = jest.fn()
  464. const store = new Vuex.Store({
  465. modules: {
  466. foo: {
  467. namespaced: true,
  468. actions: {
  469. a,
  470. b
  471. }
  472. }
  473. }
  474. })
  475. const vm = new Vue({
  476. store,
  477. methods: mapActions('foo/', {
  478. foo: 'a',
  479. bar: 'b'
  480. })
  481. })
  482. vm.foo()
  483. expect(a).toHaveBeenCalled()
  484. expect(b).not.toHaveBeenCalled()
  485. vm.bar()
  486. expect(b).toHaveBeenCalled()
  487. })
  488. it('mapActions (function with namespace)', () => {
  489. const a = jest.fn()
  490. const store = new Vuex.Store({
  491. modules: {
  492. foo: {
  493. namespaced: true,
  494. actions: { a }
  495. }
  496. }
  497. })
  498. const vm = new Vue({
  499. store,
  500. methods: mapActions('foo/', {
  501. foo (dispatch, arg) {
  502. dispatch('a', arg + 'bar')
  503. }
  504. })
  505. })
  506. vm.foo('foo')
  507. expect(a.mock.calls[0][1]).toBe('foobar')
  508. })
  509. it('mapActions (with undefined actions)', () => {
  510. jest.spyOn(console, 'error').mockImplementation()
  511. const a = jest.fn()
  512. const store = new Vuex.Store({
  513. modules: {
  514. foo: {
  515. namespaced: true,
  516. actions: {
  517. a
  518. }
  519. }
  520. }
  521. })
  522. const vm = new Vue({
  523. store,
  524. methods: mapActions('foo/')
  525. })
  526. expect(vm.a).toBeUndefined()
  527. expect(a).not.toHaveBeenCalled()
  528. expect(console.error).toHaveBeenCalledWith('[vuex] mapActions: mapper parameter must be either an Array or an Object')
  529. })
  530. it('createNamespacedHelpers', () => {
  531. const actionA = jest.fn()
  532. const actionB = jest.fn()
  533. const store = new Vuex.Store({
  534. modules: {
  535. foo: {
  536. namespaced: true,
  537. state: { count: 0 },
  538. getters: {
  539. isEven: state => state.count % 2 === 0
  540. },
  541. mutations: {
  542. inc: state => state.count++,
  543. dec: state => state.count--
  544. },
  545. actions: {
  546. actionA,
  547. actionB
  548. }
  549. }
  550. }
  551. })
  552. const {
  553. mapState,
  554. mapGetters,
  555. mapMutations,
  556. mapActions
  557. } = createNamespacedHelpers('foo/')
  558. const vm = new Vue({
  559. store,
  560. computed: {
  561. ...mapState(['count']),
  562. ...mapGetters(['isEven'])
  563. },
  564. methods: {
  565. ...mapMutations(['inc', 'dec']),
  566. ...mapActions(['actionA', 'actionB'])
  567. }
  568. })
  569. expect(vm.count).toBe(0)
  570. expect(vm.isEven).toBe(true)
  571. store.state.foo.count++
  572. expect(vm.count).toBe(1)
  573. expect(vm.isEven).toBe(false)
  574. vm.inc()
  575. expect(store.state.foo.count).toBe(2)
  576. expect(store.getters['foo/isEven']).toBe(true)
  577. vm.dec()
  578. expect(store.state.foo.count).toBe(1)
  579. expect(store.getters['foo/isEven']).toBe(false)
  580. vm.actionA()
  581. expect(actionA).toHaveBeenCalled()
  582. expect(actionB).not.toHaveBeenCalled()
  583. vm.actionB()
  584. expect(actionB).toHaveBeenCalled()
  585. })
  586. })