helpers.spec.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. import Vue from 'vue/dist/vue.common.js'
  2. import Vuex, { mapState, mapMutations, mapGetters, mapActions } from '../../dist/vuex.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('mapActions (array)', () => {
  244. const a = jasmine.createSpy()
  245. const b = jasmine.createSpy()
  246. const store = new Vuex.Store({
  247. actions: {
  248. a,
  249. b
  250. }
  251. })
  252. const vm = new Vue({
  253. store,
  254. methods: mapActions(['a', 'b'])
  255. })
  256. vm.a()
  257. expect(a).toHaveBeenCalled()
  258. expect(b).not.toHaveBeenCalled()
  259. vm.b()
  260. expect(b).toHaveBeenCalled()
  261. })
  262. it('mapActions (object)', () => {
  263. const a = jasmine.createSpy()
  264. const b = jasmine.createSpy()
  265. const store = new Vuex.Store({
  266. actions: {
  267. a,
  268. b
  269. }
  270. })
  271. const vm = new Vue({
  272. store,
  273. methods: mapActions({
  274. foo: 'a',
  275. bar: 'b'
  276. })
  277. })
  278. vm.foo()
  279. expect(a).toHaveBeenCalled()
  280. expect(b).not.toHaveBeenCalled()
  281. vm.bar()
  282. expect(b).toHaveBeenCalled()
  283. })
  284. it('mapActions (with namespace)', () => {
  285. const a = jasmine.createSpy()
  286. const b = jasmine.createSpy()
  287. const store = new Vuex.Store({
  288. modules: {
  289. foo: {
  290. namespaced: true,
  291. actions: {
  292. a,
  293. b
  294. }
  295. }
  296. }
  297. })
  298. const vm = new Vue({
  299. store,
  300. methods: mapActions('foo/', {
  301. foo: 'a',
  302. bar: 'b'
  303. })
  304. })
  305. vm.foo()
  306. expect(a).toHaveBeenCalled()
  307. expect(b).not.toHaveBeenCalled()
  308. vm.bar()
  309. expect(b).toHaveBeenCalled()
  310. })
  311. })