1
0

helpers.spec.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 (with namespace)', () => {
  130. const store = new Vuex.Store({
  131. modules: {
  132. foo: {
  133. namespaced: true,
  134. state: { count: 0 },
  135. mutations: {
  136. inc: state => state.count++,
  137. dec: state => state.count--
  138. }
  139. }
  140. }
  141. })
  142. const vm = new Vue({
  143. store,
  144. methods: mapMutations('foo', {
  145. plus: 'inc',
  146. minus: 'dec'
  147. })
  148. })
  149. vm.plus()
  150. expect(store.state.foo.count).toBe(1)
  151. vm.minus()
  152. expect(store.state.foo.count).toBe(0)
  153. })
  154. it('mapGetters (array)', () => {
  155. const store = new Vuex.Store({
  156. state: { count: 0 },
  157. mutations: {
  158. inc: state => state.count++,
  159. dec: state => state.count--
  160. },
  161. getters: {
  162. hasAny: ({ count }) => count > 0,
  163. negative: ({ count }) => count < 0
  164. }
  165. })
  166. const vm = new Vue({
  167. store,
  168. computed: mapGetters(['hasAny', 'negative'])
  169. })
  170. expect(vm.hasAny).toBe(false)
  171. expect(vm.negative).toBe(false)
  172. store.commit('inc')
  173. expect(vm.hasAny).toBe(true)
  174. expect(vm.negative).toBe(false)
  175. store.commit('dec')
  176. store.commit('dec')
  177. expect(vm.hasAny).toBe(false)
  178. expect(vm.negative).toBe(true)
  179. })
  180. it('mapGetters (object)', () => {
  181. const store = new Vuex.Store({
  182. state: { count: 0 },
  183. mutations: {
  184. inc: state => state.count++,
  185. dec: state => state.count--
  186. },
  187. getters: {
  188. hasAny: ({ count }) => count > 0,
  189. negative: ({ count }) => count < 0
  190. }
  191. })
  192. const vm = new Vue({
  193. store,
  194. computed: mapGetters({
  195. a: 'hasAny',
  196. b: 'negative'
  197. })
  198. })
  199. expect(vm.a).toBe(false)
  200. expect(vm.b).toBe(false)
  201. store.commit('inc')
  202. expect(vm.a).toBe(true)
  203. expect(vm.b).toBe(false)
  204. store.commit('dec')
  205. store.commit('dec')
  206. expect(vm.a).toBe(false)
  207. expect(vm.b).toBe(true)
  208. })
  209. it('mapGetters (with namespace)', () => {
  210. const store = new Vuex.Store({
  211. modules: {
  212. foo: {
  213. namespaced: true,
  214. state: { count: 0 },
  215. mutations: {
  216. inc: state => state.count++,
  217. dec: state => state.count--
  218. },
  219. getters: {
  220. hasAny: ({ count }) => count > 0,
  221. negative: ({ count }) => count < 0
  222. }
  223. }
  224. }
  225. })
  226. const vm = new Vue({
  227. store,
  228. computed: mapGetters('foo', {
  229. a: 'hasAny',
  230. b: 'negative'
  231. })
  232. })
  233. expect(vm.a).toBe(false)
  234. expect(vm.b).toBe(false)
  235. store.commit('foo/inc')
  236. expect(vm.a).toBe(true)
  237. expect(vm.b).toBe(false)
  238. store.commit('foo/dec')
  239. store.commit('foo/dec')
  240. expect(vm.a).toBe(false)
  241. expect(vm.b).toBe(true)
  242. })
  243. it('mapGetters (with namespace and nested module)', () => {
  244. const store = new Vuex.Store({
  245. modules: {
  246. foo: {
  247. namespaced: true,
  248. modules: {
  249. bar: {
  250. namespaced: true,
  251. state: { count: 0 },
  252. mutations: {
  253. inc: state => state.count++,
  254. dec: state => state.count--
  255. },
  256. getters: {
  257. hasAny: ({ count }) => count > 0,
  258. negative: ({ count }) => count < 0
  259. }
  260. },
  261. cat: {
  262. state: { count: 9 },
  263. getters: {
  264. count: ({ count }) => count
  265. }
  266. }
  267. }
  268. }
  269. }
  270. })
  271. const vm = new Vue({
  272. store,
  273. computed: {
  274. ...mapGetters('foo/bar', [
  275. 'hasAny',
  276. 'negative'
  277. ]),
  278. ...mapGetters('foo', [
  279. 'count'
  280. ])
  281. }
  282. })
  283. expect(vm.hasAny).toBe(false)
  284. expect(vm.negative).toBe(false)
  285. store.commit('foo/bar/inc')
  286. expect(vm.hasAny).toBe(true)
  287. expect(vm.negative).toBe(false)
  288. store.commit('foo/bar/dec')
  289. store.commit('foo/bar/dec')
  290. expect(vm.hasAny).toBe(false)
  291. expect(vm.negative).toBe(true)
  292. expect(vm.count).toBe(9)
  293. })
  294. it('mapActions (array)', () => {
  295. const a = jasmine.createSpy()
  296. const b = jasmine.createSpy()
  297. const store = new Vuex.Store({
  298. actions: {
  299. a,
  300. b
  301. }
  302. })
  303. const vm = new Vue({
  304. store,
  305. methods: mapActions(['a', 'b'])
  306. })
  307. vm.a()
  308. expect(a).toHaveBeenCalled()
  309. expect(b).not.toHaveBeenCalled()
  310. vm.b()
  311. expect(b).toHaveBeenCalled()
  312. })
  313. it('mapActions (object)', () => {
  314. const a = jasmine.createSpy()
  315. const b = jasmine.createSpy()
  316. const store = new Vuex.Store({
  317. actions: {
  318. a,
  319. b
  320. }
  321. })
  322. const vm = new Vue({
  323. store,
  324. methods: mapActions({
  325. foo: 'a',
  326. bar: 'b'
  327. })
  328. })
  329. vm.foo()
  330. expect(a).toHaveBeenCalled()
  331. expect(b).not.toHaveBeenCalled()
  332. vm.bar()
  333. expect(b).toHaveBeenCalled()
  334. })
  335. it('mapActions (with namespace)', () => {
  336. const a = jasmine.createSpy()
  337. const b = jasmine.createSpy()
  338. const store = new Vuex.Store({
  339. modules: {
  340. foo: {
  341. namespaced: true,
  342. actions: {
  343. a,
  344. b
  345. }
  346. }
  347. }
  348. })
  349. const vm = new Vue({
  350. store,
  351. methods: mapActions('foo/', {
  352. foo: 'a',
  353. bar: 'b'
  354. })
  355. })
  356. vm.foo()
  357. expect(a).toHaveBeenCalled()
  358. expect(b).not.toHaveBeenCalled()
  359. vm.bar()
  360. expect(b).toHaveBeenCalled()
  361. })
  362. it('createNamespacedHelpers', () => {
  363. const actionA = jasmine.createSpy()
  364. const actionB = jasmine.createSpy()
  365. const store = new Vuex.Store({
  366. modules: {
  367. foo: {
  368. namespaced: true,
  369. state: { count: 0 },
  370. getters: {
  371. isEven: state => state.count % 2 === 0
  372. },
  373. mutations: {
  374. inc: state => state.count++,
  375. dec: state => state.count--
  376. },
  377. actions: {
  378. actionA,
  379. actionB
  380. }
  381. }
  382. }
  383. })
  384. const {
  385. mapState,
  386. mapGetters,
  387. mapMutations,
  388. mapActions
  389. } = createNamespacedHelpers('foo/')
  390. const vm = new Vue({
  391. store,
  392. computed: {
  393. ...mapState(['count']),
  394. ...mapGetters(['isEven'])
  395. },
  396. methods: {
  397. ...mapMutations(['inc', 'dec']),
  398. ...mapActions(['actionA', 'actionB'])
  399. }
  400. })
  401. expect(vm.count).toBe(0)
  402. expect(vm.isEven).toBe(true)
  403. store.state.foo.count++
  404. expect(vm.count).toBe(1)
  405. expect(vm.isEven).toBe(false)
  406. vm.inc()
  407. expect(store.state.foo.count).toBe(2)
  408. expect(store.getters['foo/isEven']).toBe(true)
  409. vm.dec()
  410. expect(store.state.foo.count).toBe(1)
  411. expect(store.getters['foo/isEven']).toBe(false)
  412. vm.actionA()
  413. expect(actionA).toHaveBeenCalled()
  414. expect(actionB).not.toHaveBeenCalled()
  415. vm.actionB()
  416. expect(actionB).toHaveBeenCalled()
  417. })
  418. })