|
| 1 | +import Vuex from 'vuex'; |
| 2 | +import { createLocalVue, shallowMount } from '@vue/test-utils'; |
| 3 | + |
| 4 | +import { createHelpers, getField, updateField } from './package/src'; |
| 5 | + |
| 6 | +const localVue = createLocalVue(); |
| 7 | + |
| 8 | +localVue.use(Vuex); |
| 9 | + |
| 10 | +const { mapFields } = createHelpers({ |
| 11 | + getterType: `getField`, |
| 12 | + mutationType: `updateField`, |
| 13 | +}); |
| 14 | + |
| 15 | +describe(`Component initialized with namespaced Vuex module.`, () => { |
| 16 | + let Component; |
| 17 | + let store; |
| 18 | + let wrapper; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + Component = { |
| 22 | + template: `<input id="foo" v-model="foo">`, |
| 23 | + computed: { |
| 24 | + ...mapFields(`fooModule`, [ |
| 25 | + `foo`, |
| 26 | + ]), |
| 27 | + }, |
| 28 | + }; |
| 29 | + |
| 30 | + store = new Vuex.Store({ |
| 31 | + modules: { |
| 32 | + fooModule: { |
| 33 | + namespaced: true, |
| 34 | + state: { |
| 35 | + foo: ``, |
| 36 | + }, |
| 37 | + getters: { |
| 38 | + getField, |
| 39 | + }, |
| 40 | + mutations: { |
| 41 | + updateField, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + wrapper = shallowMount(Component, { localVue, store }); |
| 48 | + }); |
| 49 | + |
| 50 | + test(`It should render the component.`, () => { |
| 51 | + expect(wrapper.exists()).toBe(true); |
| 52 | + }); |
| 53 | + |
| 54 | + test(`It should update field values when the store is updated.`, () => { |
| 55 | + store.state.fooModule.foo = `foo`; |
| 56 | + |
| 57 | + expect(wrapper.element.value).toBe(`foo`); |
| 58 | + }); |
| 59 | + |
| 60 | + test(`It should update the store when the field values are updated.`, () => { |
| 61 | + wrapper.element.value = `foo`; |
| 62 | + wrapper.trigger(`input`); |
| 63 | + |
| 64 | + expect(store.state.fooModule.foo).toBe(`foo`); |
| 65 | + }); |
| 66 | +}); |
0 commit comments