123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- import Vue from 'vue/dist/vue.common.js'
- import Vuex, { mapState, mapMutations, mapGetters, mapActions } from '../../dist/vuex.js'
- describe('Helpers', () => {
- it('mapState (array)', () => {
- const store = new Vuex.Store({
- state: {
- a: 1
- }
- })
- const vm = new Vue({
- store,
- computed: mapState(['a'])
- })
- expect(vm.a).toBe(1)
- store.state.a++
- expect(vm.a).toBe(2)
- })
- it('mapState (object)', () => {
- const store = new Vuex.Store({
- state: {
- a: 1
- },
- getters: {
- b: () => 2
- }
- })
- const vm = new Vue({
- store,
- computed: mapState({
- a: (state, getters) => {
- return state.a + getters.b
- }
- })
- })
- expect(vm.a).toBe(3)
- store.state.a++
- expect(vm.a).toBe(4)
- })
- it('mapState (with namespace)', () => {
- const store = new Vuex.Store({
- modules: {
- foo: {
- namespaced: true,
- state: { a: 1 },
- getters: {
- b: state => state.a + 1
- }
- }
- }
- })
- const vm = new Vue({
- store,
- computed: mapState('foo', {
- a: (state, getters) => {
- return state.a + getters.b
- }
- })
- })
- expect(vm.a).toBe(3)
- store.state.foo.a++
- expect(vm.a).toBe(5)
- store.replaceState({
- foo: { a: 3 }
- })
- expect(vm.a).toBe(7)
- })
- // #708
- it('mapState (with namespace and a nested module)', () => {
- const store = new Vuex.Store({
- modules: {
- foo: {
- namespaced: true,
- state: { a: 1 },
- modules: {
- bar: {
- state: { b: 2 }
- }
- }
- }
- }
- })
- const vm = new Vue({
- store,
- computed: mapState('foo', {
- value: state => state
- })
- })
- expect(vm.value.a).toBe(1)
- expect(vm.value.bar.b).toBe(2)
- expect(vm.value.b).toBeUndefined()
- })
- it('mapMutations (array)', () => {
- const store = new Vuex.Store({
- state: { count: 0 },
- mutations: {
- inc: state => state.count++,
- dec: state => state.count--
- }
- })
- const vm = new Vue({
- store,
- methods: mapMutations(['inc', 'dec'])
- })
- vm.inc()
- expect(store.state.count).toBe(1)
- vm.dec()
- expect(store.state.count).toBe(0)
- })
- it('mapMutations (object)', () => {
- const store = new Vuex.Store({
- state: { count: 0 },
- mutations: {
- inc: state => state.count++,
- dec: state => state.count--
- }
- })
- const vm = new Vue({
- store,
- methods: mapMutations({
- plus: 'inc',
- minus: 'dec'
- })
- })
- vm.plus()
- expect(store.state.count).toBe(1)
- vm.minus()
- expect(store.state.count).toBe(0)
- })
- it('mapMutations (with namespace)', () => {
- const store = new Vuex.Store({
- modules: {
- foo: {
- namespaced: true,
- state: { count: 0 },
- mutations: {
- inc: state => state.count++,
- dec: state => state.count--
- }
- }
- }
- })
- const vm = new Vue({
- store,
- methods: mapMutations('foo', {
- plus: 'inc',
- minus: 'dec'
- })
- })
- vm.plus()
- expect(store.state.foo.count).toBe(1)
- vm.minus()
- expect(store.state.foo.count).toBe(0)
- })
- it('mapGetters (array)', () => {
- const store = new Vuex.Store({
- state: { count: 0 },
- mutations: {
- inc: state => state.count++,
- dec: state => state.count--
- },
- getters: {
- hasAny: ({ count }) => count > 0,
- negative: ({ count }) => count < 0
- }
- })
- const vm = new Vue({
- store,
- computed: mapGetters(['hasAny', 'negative'])
- })
- expect(vm.hasAny).toBe(false)
- expect(vm.negative).toBe(false)
- store.commit('inc')
- expect(vm.hasAny).toBe(true)
- expect(vm.negative).toBe(false)
- store.commit('dec')
- store.commit('dec')
- expect(vm.hasAny).toBe(false)
- expect(vm.negative).toBe(true)
- })
- it('mapGetters (object)', () => {
- const store = new Vuex.Store({
- state: { count: 0 },
- mutations: {
- inc: state => state.count++,
- dec: state => state.count--
- },
- getters: {
- hasAny: ({ count }) => count > 0,
- negative: ({ count }) => count < 0
- }
- })
- const vm = new Vue({
- store,
- computed: mapGetters({
- a: 'hasAny',
- b: 'negative'
- })
- })
- expect(vm.a).toBe(false)
- expect(vm.b).toBe(false)
- store.commit('inc')
- expect(vm.a).toBe(true)
- expect(vm.b).toBe(false)
- store.commit('dec')
- store.commit('dec')
- expect(vm.a).toBe(false)
- expect(vm.b).toBe(true)
- })
- it('mapGetters (with namespace)', () => {
- const store = new Vuex.Store({
- modules: {
- foo: {
- namespaced: true,
- state: { count: 0 },
- mutations: {
- inc: state => state.count++,
- dec: state => state.count--
- },
- getters: {
- hasAny: ({ count }) => count > 0,
- negative: ({ count }) => count < 0
- }
- }
- }
- })
- const vm = new Vue({
- store,
- computed: mapGetters('foo', {
- a: 'hasAny',
- b: 'negative'
- })
- })
- expect(vm.a).toBe(false)
- expect(vm.b).toBe(false)
- store.commit('foo/inc')
- expect(vm.a).toBe(true)
- expect(vm.b).toBe(false)
- store.commit('foo/dec')
- store.commit('foo/dec')
- expect(vm.a).toBe(false)
- expect(vm.b).toBe(true)
- })
- it('mapActions (array)', () => {
- const a = jasmine.createSpy()
- const b = jasmine.createSpy()
- const store = new Vuex.Store({
- actions: {
- a,
- b
- }
- })
- const vm = new Vue({
- store,
- methods: mapActions(['a', 'b'])
- })
- vm.a()
- expect(a).toHaveBeenCalled()
- expect(b).not.toHaveBeenCalled()
- vm.b()
- expect(b).toHaveBeenCalled()
- })
- it('mapActions (object)', () => {
- const a = jasmine.createSpy()
- const b = jasmine.createSpy()
- const store = new Vuex.Store({
- actions: {
- a,
- b
- }
- })
- const vm = new Vue({
- store,
- methods: mapActions({
- foo: 'a',
- bar: 'b'
- })
- })
- vm.foo()
- expect(a).toHaveBeenCalled()
- expect(b).not.toHaveBeenCalled()
- vm.bar()
- expect(b).toHaveBeenCalled()
- })
- it('mapActions (with namespace)', () => {
- const a = jasmine.createSpy()
- const b = jasmine.createSpy()
- const store = new Vuex.Store({
- modules: {
- foo: {
- namespaced: true,
- actions: {
- a,
- b
- }
- }
- }
- })
- const vm = new Vue({
- store,
- methods: mapActions('foo/', {
- foo: 'a',
- bar: 'b'
- })
- })
- vm.foo()
- expect(a).toHaveBeenCalled()
- expect(b).not.toHaveBeenCalled()
- vm.bar()
- expect(b).toHaveBeenCalled()
- })
- })
|