|
| 1 | +import Vue from 'vue/dist/vue.common.js' |
| 2 | +import Vuex, { mapState, mapMutations, mapGetters, mapActions } from '../../dist/vuex.js' |
| 3 | + |
| 4 | +describe('Helpers', () => { |
| 5 | + it('mapState (array)', () => { |
| 6 | + const store = new Vuex.Store({ |
| 7 | + state: { |
| 8 | + a: 1 |
| 9 | + } |
| 10 | + }) |
| 11 | + const vm = new Vue({ |
| 12 | + store, |
| 13 | + computed: mapState(['a']) |
| 14 | + }) |
| 15 | + expect(vm.a).toBe(1) |
| 16 | + store.state.a++ |
| 17 | + expect(vm.a).toBe(2) |
| 18 | + }) |
| 19 | + |
| 20 | + it('mapState (object)', () => { |
| 21 | + const store = new Vuex.Store({ |
| 22 | + state: { |
| 23 | + a: 1 |
| 24 | + }, |
| 25 | + getters: { |
| 26 | + b: () => 2 |
| 27 | + } |
| 28 | + }) |
| 29 | + const vm = new Vue({ |
| 30 | + store, |
| 31 | + computed: mapState({ |
| 32 | + a: (state, getters) => { |
| 33 | + return state.a + getters.b |
| 34 | + } |
| 35 | + }) |
| 36 | + }) |
| 37 | + expect(vm.a).toBe(3) |
| 38 | + store.state.a++ |
| 39 | + expect(vm.a).toBe(4) |
| 40 | + }) |
| 41 | + |
| 42 | + it('mapMutations (array)', () => { |
| 43 | + const store = new Vuex.Store({ |
| 44 | + state: { count: 0 }, |
| 45 | + mutations: { |
| 46 | + inc: state => state.count++, |
| 47 | + dec: state => state.count-- |
| 48 | + } |
| 49 | + }) |
| 50 | + const vm = new Vue({ |
| 51 | + store, |
| 52 | + methods: mapMutations(['inc', 'dec']) |
| 53 | + }) |
| 54 | + vm.inc() |
| 55 | + expect(store.state.count).toBe(1) |
| 56 | + vm.dec() |
| 57 | + expect(store.state.count).toBe(0) |
| 58 | + }) |
| 59 | + |
| 60 | + it('mapMutations (object)', () => { |
| 61 | + const store = new Vuex.Store({ |
| 62 | + state: { count: 0 }, |
| 63 | + mutations: { |
| 64 | + inc: state => state.count++, |
| 65 | + dec: state => state.count-- |
| 66 | + } |
| 67 | + }) |
| 68 | + const vm = new Vue({ |
| 69 | + store, |
| 70 | + methods: mapMutations({ |
| 71 | + plus: 'inc', |
| 72 | + minus: 'dec' |
| 73 | + }) |
| 74 | + }) |
| 75 | + vm.plus() |
| 76 | + expect(store.state.count).toBe(1) |
| 77 | + vm.minus() |
| 78 | + expect(store.state.count).toBe(0) |
| 79 | + }) |
| 80 | + |
| 81 | + it('mapGetters (array)', () => { |
| 82 | + const store = new Vuex.Store({ |
| 83 | + state: { count: 0 }, |
| 84 | + mutations: { |
| 85 | + inc: state => state.count++, |
| 86 | + dec: state => state.count-- |
| 87 | + }, |
| 88 | + getters: { |
| 89 | + hasAny: ({ count }) => count > 0, |
| 90 | + negative: ({ count }) => count < 0 |
| 91 | + } |
| 92 | + }) |
| 93 | + const vm = new Vue({ |
| 94 | + store, |
| 95 | + computed: mapGetters(['hasAny', 'negative']) |
| 96 | + }) |
| 97 | + expect(vm.hasAny).toBe(false) |
| 98 | + expect(vm.negative).toBe(false) |
| 99 | + store.commit('inc') |
| 100 | + expect(vm.hasAny).toBe(true) |
| 101 | + expect(vm.negative).toBe(false) |
| 102 | + store.commit('dec') |
| 103 | + store.commit('dec') |
| 104 | + expect(vm.hasAny).toBe(false) |
| 105 | + expect(vm.negative).toBe(true) |
| 106 | + }) |
| 107 | + |
| 108 | + it('mapGetters (object)', () => { |
| 109 | + const store = new Vuex.Store({ |
| 110 | + state: { count: 0 }, |
| 111 | + mutations: { |
| 112 | + inc: state => state.count++, |
| 113 | + dec: state => state.count-- |
| 114 | + }, |
| 115 | + getters: { |
| 116 | + hasAny: ({ count }) => count > 0, |
| 117 | + negative: ({ count }) => count < 0 |
| 118 | + } |
| 119 | + }) |
| 120 | + const vm = new Vue({ |
| 121 | + store, |
| 122 | + computed: mapGetters({ |
| 123 | + a: 'hasAny', |
| 124 | + b: 'negative' |
| 125 | + }) |
| 126 | + }) |
| 127 | + expect(vm.a).toBe(false) |
| 128 | + expect(vm.b).toBe(false) |
| 129 | + store.commit('inc') |
| 130 | + expect(vm.a).toBe(true) |
| 131 | + expect(vm.b).toBe(false) |
| 132 | + store.commit('dec') |
| 133 | + store.commit('dec') |
| 134 | + expect(vm.a).toBe(false) |
| 135 | + expect(vm.b).toBe(true) |
| 136 | + }) |
| 137 | + |
| 138 | + it('mapActions (array)', () => { |
| 139 | + const a = jasmine.createSpy() |
| 140 | + const b = jasmine.createSpy() |
| 141 | + const store = new Vuex.Store({ |
| 142 | + actions: { |
| 143 | + a, |
| 144 | + b |
| 145 | + } |
| 146 | + }) |
| 147 | + const vm = new Vue({ |
| 148 | + store, |
| 149 | + methods: mapActions(['a', 'b']) |
| 150 | + }) |
| 151 | + vm.a() |
| 152 | + expect(a).toHaveBeenCalled() |
| 153 | + expect(b).not.toHaveBeenCalled() |
| 154 | + vm.b() |
| 155 | + expect(b).toHaveBeenCalled() |
| 156 | + }) |
| 157 | + |
| 158 | + it('mapActions (object)', () => { |
| 159 | + const a = jasmine.createSpy() |
| 160 | + const b = jasmine.createSpy() |
| 161 | + const store = new Vuex.Store({ |
| 162 | + actions: { |
| 163 | + a, |
| 164 | + b |
| 165 | + } |
| 166 | + }) |
| 167 | + const vm = new Vue({ |
| 168 | + store, |
| 169 | + methods: mapActions({ |
| 170 | + foo: 'a', |
| 171 | + bar: 'b' |
| 172 | + }) |
| 173 | + }) |
| 174 | + vm.foo() |
| 175 | + expect(a).toHaveBeenCalled() |
| 176 | + expect(b).not.toHaveBeenCalled() |
| 177 | + vm.bar() |
| 178 | + expect(b).toHaveBeenCalled() |
| 179 | + }) |
| 180 | +}) |
0 commit comments