Welcome to mirror list, hosted at ThFree Co, Russian Federation.

vuex_module_mappers_spec.js « utils « lib « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1821a15f677b389e47c2b02e06731a4e1000998e (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { mount } from '@vue/test-utils';
import Vue from 'vue';
import Vuex from 'vuex';
import {
  mapVuexModuleActions,
  mapVuexModuleGetters,
  mapVuexModuleState,
  REQUIRE_STRING_ERROR_MESSAGE,
} from '~/lib/utils/vuex_module_mappers';

const TEST_MODULE_NAME = 'testModuleName';

Vue.use(Vuex);

// setup test component and store ----------------------------------------------
//
// These are used to indirectly test `vuex_module_mappers`.
const TestComponent = {
  props: {
    vuexModule: {
      type: String,
      required: true,
    },
  },
  computed: {
    ...mapVuexModuleState((vm) => vm.vuexModule, { name: 'name', value: 'count' }),
    ...mapVuexModuleGetters((vm) => vm.vuexModule, ['hasValue', 'hasName']),
    stateJson() {
      return JSON.stringify({
        name: this.name,
        value: this.value,
      });
    },
    gettersJson() {
      return JSON.stringify({
        hasValue: this.hasValue,
        hasName: this.hasName,
      });
    },
  },
  methods: {
    ...mapVuexModuleActions((vm) => vm.vuexModule, ['increment']),
  },
  template: `
<div>
  <pre data-testid="state">{{ stateJson }}</pre>
  <pre data-testid="getters">{{ gettersJson }}</pre>
</div>`,
};

const createTestStore = () => {
  return new Vuex.Store({
    modules: {
      [TEST_MODULE_NAME]: {
        namespaced: true,
        state: {
          name: 'Lorem',
          count: 0,
        },
        mutations: {
          INCREMENT: (state, amount) => {
            state.count += amount;
          },
        },
        actions: {
          increment({ commit }, amount) {
            commit('INCREMENT', amount);
          },
        },
        getters: {
          hasValue: (state) => state.count > 0,
          hasName: (state) => Boolean(state.name.length),
        },
      },
    },
  });
};

describe('~/lib/utils/vuex_module_mappers', () => {
  let store;
  let wrapper;

  const getJsonInTemplate = (testId) =>
    JSON.parse(wrapper.find(`[data-testid="${testId}"]`).text());
  const getMappedState = () => getJsonInTemplate('state');
  const getMappedGetters = () => getJsonInTemplate('getters');

  beforeEach(() => {
    store = createTestStore();

    wrapper = mount(TestComponent, {
      propsData: {
        vuexModule: TEST_MODULE_NAME,
      },
      store,
    });
  });

  afterEach(() => {
    wrapper.destroy();
  });

  describe('from module defined by prop', () => {
    it('maps state', () => {
      expect(getMappedState()).toEqual({
        name: store.state[TEST_MODULE_NAME].name,
        value: store.state[TEST_MODULE_NAME].count,
      });
    });

    it('maps getters', () => {
      expect(getMappedGetters()).toEqual({
        hasName: true,
        hasValue: false,
      });
    });

    it('maps action', () => {
      jest.spyOn(store, 'dispatch');

      expect(store.dispatch).not.toHaveBeenCalled();

      wrapper.vm.increment(10);

      expect(store.dispatch).toHaveBeenCalledWith(`${TEST_MODULE_NAME}/increment`, 10);
    });
  });

  describe('with non-string object value', () => {
    it('throws helpful error', () => {
      expect(() => mapVuexModuleActions((vm) => vm.bogus, { foo: () => {} })).toThrowError(
        REQUIRE_STRING_ERROR_MESSAGE,
      );
    });
  });
});