helpers.spec.js 13 KB

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