helpers.spec.js 11 KB

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