2019-12-19 07:07:35 -05:00
|
|
|
import { shallowMount } from '@vue/test-utils';
|
|
|
|
import { mapComputed } from '~/vuex_shared/bindings';
|
|
|
|
|
|
|
|
describe('Binding utils', () => {
|
|
|
|
describe('mapComputed', () => {
|
2020-01-13 10:07:53 -05:00
|
|
|
const defaultArgs = [['baz'], 'bar', 'foo'];
|
|
|
|
|
|
|
|
const createDummy = (mapComputedArgs = defaultArgs) => ({
|
2019-12-19 07:07:35 -05:00
|
|
|
computed: {
|
2020-01-13 10:07:53 -05:00
|
|
|
...mapComputed(...mapComputedArgs),
|
2019-12-19 07:07:35 -05:00
|
|
|
},
|
|
|
|
render() {
|
|
|
|
return null;
|
|
|
|
},
|
2020-01-13 10:07:53 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
const mocks = {
|
|
|
|
$store: {
|
|
|
|
state: {
|
|
|
|
baz: 2,
|
|
|
|
foo: {
|
|
|
|
baz: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
getters: {
|
|
|
|
getBaz: 'foo',
|
|
|
|
},
|
|
|
|
dispatch: jest.fn(),
|
|
|
|
},
|
2019-12-19 07:07:35 -05:00
|
|
|
};
|
2020-01-13 10:07:53 -05:00
|
|
|
|
|
|
|
it('returns an object with keys equal to the first fn parameter ', () => {
|
2019-12-19 07:07:35 -05:00
|
|
|
const keyList = ['foo1', 'foo2'];
|
2020-01-13 10:07:53 -05:00
|
|
|
const result = mapComputed(keyList, 'foo', 'bar');
|
2019-12-19 07:07:35 -05:00
|
|
|
expect(Object.keys(result)).toEqual(keyList);
|
|
|
|
});
|
2020-01-13 10:07:53 -05:00
|
|
|
|
2019-12-19 07:07:35 -05:00
|
|
|
it('returned object has set and get function', () => {
|
2020-01-13 10:07:53 -05:00
|
|
|
const result = mapComputed(['baz'], 'foo', 'bar');
|
2019-12-19 07:07:35 -05:00
|
|
|
expect(result.baz.set).toBeDefined();
|
|
|
|
expect(result.baz.get).toBeDefined();
|
|
|
|
});
|
|
|
|
|
2020-01-13 10:07:53 -05:00
|
|
|
describe('set function', () => {
|
|
|
|
it('invokes $store.dispatch', () => {
|
|
|
|
const context = shallowMount(createDummy(), { mocks });
|
|
|
|
context.vm.baz = 'a';
|
|
|
|
expect(context.vm.$store.dispatch).toHaveBeenCalledWith('bar', { baz: 'a' });
|
|
|
|
});
|
|
|
|
it('uses updateFn in list object mode if updateFn exists', () => {
|
|
|
|
const context = shallowMount(createDummy([[{ key: 'foo', updateFn: 'baz' }]]), { mocks });
|
|
|
|
context.vm.foo = 'b';
|
|
|
|
expect(context.vm.$store.dispatch).toHaveBeenCalledWith('baz', { foo: 'b' });
|
|
|
|
});
|
|
|
|
it('in list object mode defaults to defaultUpdateFn if updateFn do not exists', () => {
|
|
|
|
const context = shallowMount(createDummy([[{ key: 'foo' }], 'defaultFn']), { mocks });
|
|
|
|
context.vm.foo = 'c';
|
|
|
|
expect(context.vm.$store.dispatch).toHaveBeenCalledWith('defaultFn', { foo: 'c' });
|
2019-12-19 07:07:35 -05:00
|
|
|
});
|
|
|
|
});
|
2020-01-13 10:07:53 -05:00
|
|
|
|
|
|
|
describe('get function', () => {
|
|
|
|
it('if root is set returns $store.state[root][key]', () => {
|
|
|
|
const context = shallowMount(createDummy(), { mocks });
|
|
|
|
expect(context.vm.baz).toBe(mocks.$store.state.foo.baz);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('if root is not set returns $store.state[key]', () => {
|
|
|
|
const context = shallowMount(createDummy([['baz'], 'bar']), { mocks });
|
|
|
|
expect(context.vm.baz).toBe(mocks.$store.state.baz);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('when using getters it invoke the appropriate getter', () => {
|
|
|
|
const context = shallowMount(createDummy([[{ getter: 'getBaz', key: 'baz' }]]), { mocks });
|
|
|
|
expect(context.vm.baz).toBe(mocks.$store.getters.getBaz);
|
2019-12-19 07:07:35 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|