|
| 1 | +const Vue = require('vue') |
| 2 | + |
| 3 | +const { createStore } = require('./utils') |
| 4 | +const { StoreonVue } = require('../index') |
| 5 | +const { mapState, mapDispatch } = require('../helpers') |
| 6 | + |
| 7 | +it('mapState (array)', () => { |
| 8 | + let store = createStore() |
| 9 | + Vue.use(StoreonVue) |
| 10 | + let vm = new Vue({ |
| 11 | + store, |
| 12 | + computed: mapState(['count']) |
| 13 | + }) |
| 14 | + expect(vm.count).toBe(0) |
| 15 | + store.dispatch('inc') |
| 16 | + expect(vm.count).toBe(1) |
| 17 | +}) |
| 18 | + |
| 19 | +it('mapState (object)', () => { |
| 20 | + let store = createStore() |
| 21 | + Vue.use(StoreonVue) |
| 22 | + let vm = new Vue({ |
| 23 | + store, |
| 24 | + computed: mapState({ |
| 25 | + a: state => { |
| 26 | + return state.count + 1 |
| 27 | + } |
| 28 | + }) |
| 29 | + }) |
| 30 | + expect(vm.a).toBe(1) |
| 31 | + store.dispatch('inc') |
| 32 | + expect(vm.a).toBe(2) |
| 33 | +}) |
| 34 | + |
| 35 | +it('mapState (with undefined states)', () => { |
| 36 | + jest.spyOn(console, 'error') |
| 37 | + let store = createStore() |
| 38 | + Vue.use(StoreonVue) |
| 39 | + let vm = new Vue({ |
| 40 | + store, |
| 41 | + computed: mapState('foo') |
| 42 | + }) |
| 43 | + expect(vm.count).toBeUndefined() |
| 44 | + expect(console.error).toHaveBeenCalledWith( |
| 45 | + 'Mapper parameter must be either an Array or an Object' |
| 46 | + ) |
| 47 | +}) |
| 48 | + |
| 49 | +it('mapDispatch (array)', () => { |
| 50 | + let store = createStore() |
| 51 | + jest.spyOn(store, 'dispatch') |
| 52 | + Vue.use(StoreonVue) |
| 53 | + let vm = new Vue({ |
| 54 | + store, |
| 55 | + methods: mapDispatch(['inc', 'foo/set']) |
| 56 | + }) |
| 57 | + vm.inc() |
| 58 | + expect(store.dispatch).toHaveBeenCalledWith('inc') |
| 59 | + expect(store.dispatch).not.toHaveBeenCalledWith('foo/set') |
| 60 | + vm['foo/set']() |
| 61 | + expect(store.dispatch).toHaveBeenCalledWith('foo/set') |
| 62 | +}) |
| 63 | + |
| 64 | +it('mapDispatch (object)', () => { |
| 65 | + let store = createStore() |
| 66 | + jest.spyOn(store, 'dispatch') |
| 67 | + Vue.use(StoreonVue) |
| 68 | + let vm = new Vue({ |
| 69 | + store, |
| 70 | + methods: mapDispatch({ |
| 71 | + foo: 'inc', |
| 72 | + bar: 'foo/set' |
| 73 | + }) |
| 74 | + }) |
| 75 | + vm.bar('bar') |
| 76 | + expect(store.dispatch).toHaveBeenCalledWith('foo/set', 'bar') |
| 77 | + expect(store.dispatch).not.toHaveBeenCalledWith('inc') |
| 78 | + expect(vm.$storeon.state.foo).toBe('bar') |
| 79 | + vm.foo() |
| 80 | + expect(store.dispatch).toHaveBeenCalledWith('inc') |
| 81 | +}) |
| 82 | + |
| 83 | +it('mapDispatch (function)', () => { |
| 84 | + let store = createStore() |
| 85 | + jest.spyOn(store, 'dispatch') |
| 86 | + Vue.use(StoreonVue) |
| 87 | + let vm = new Vue({ |
| 88 | + store, |
| 89 | + methods: mapDispatch({ |
| 90 | + foo (dispatch, arg) { |
| 91 | + dispatch('a', arg + 'bar') |
| 92 | + } |
| 93 | + }) |
| 94 | + }) |
| 95 | + vm.foo('foo') |
| 96 | + expect(store.dispatch.mock.calls[0][1]).toBe('foobar') |
| 97 | +}) |
| 98 | + |
| 99 | +it('mapDispatch (with undefined actions)', () => { |
| 100 | + jest.spyOn(console, 'error') |
| 101 | + let store = createStore() |
| 102 | + jest.spyOn(store, 'dispatch') |
| 103 | + Vue.use(StoreonVue) |
| 104 | + let vm = new Vue({ |
| 105 | + store, |
| 106 | + methods: mapDispatch('inc') |
| 107 | + }) |
| 108 | + expect(vm.count).toBeUndefined() |
| 109 | + expect(store.dispatch).not.toHaveBeenCalled() |
| 110 | + expect(console.error).toHaveBeenCalledWith( |
| 111 | + 'Mapper parameter must be either an Array or an Object' |
| 112 | + ) |
| 113 | +}) |
0 commit comments