summaryrefslogtreecommitdiff
path: root/spec/frontend/vuex_shared/bindings_spec.js
blob: 0f91a09018f7fb81774a6038500761e12b4382e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { shallowMount } from '@vue/test-utils';
import { mapComputed } from '~/vuex_shared/bindings';

describe('Binding utils', () => {
  describe('mapComputed', () => {
    const defaultArgs = [['baz'], 'bar', 'foo'];

    const createDummy = (mapComputedArgs = defaultArgs) => ({
      computed: {
        ...mapComputed(...mapComputedArgs),
      },
      render() {
        return null;
      },
    });

    const mocks = {
      $store: {
        state: {
          baz: 2,
          foo: {
            baz: 1,
          },
        },
        getters: {
          getBaz: 'foo',
        },
        dispatch: jest.fn(),
      },
    };

    it('returns an object with keys equal to the first fn parameter ', () => {
      const keyList = ['foo1', 'foo2'];
      const result = mapComputed(keyList, 'foo', 'bar');
      expect(Object.keys(result)).toEqual(keyList);
    });

    it('returned object has set and get function', () => {
      const result = mapComputed(['baz'], 'foo', 'bar');
      expect(result.baz.set).toBeDefined();
      expect(result.baz.get).toBeDefined();
    });

    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' });
      });
    });

    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);
      });
    });
  });
});