summaryrefslogtreecommitdiff
path: root/spec/frontend/ci_variable_list/components/ci_group_variables_spec.js
blob: e45656acfd8ae2b101efd0a326048a7e5e0f003c (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import Vue, { nextTick } from 'vue';
import VueApollo from 'vue-apollo';
import { GlLoadingIcon, GlTable } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
import createFlash from '~/flash';
import { resolvers } from '~/ci_variable_list/graphql/resolvers';
import { convertToGraphQLId } from '~/graphql_shared/utils';

import ciGroupVariables from '~/ci_variable_list/components/ci_group_variables.vue';
import ciVariableSettings from '~/ci_variable_list/components/ci_variable_settings.vue';
import ciVariableTable from '~/ci_variable_list/components/ci_variable_table.vue';
import getGroupVariables from '~/ci_variable_list/graphql/queries/group_variables.query.graphql';

import addGroupVariable from '~/ci_variable_list/graphql/mutations/group_add_variable.mutation.graphql';
import deleteGroupVariable from '~/ci_variable_list/graphql/mutations/group_delete_variable.mutation.graphql';
import updateGroupVariable from '~/ci_variable_list/graphql/mutations/group_update_variable.mutation.graphql';

import { genericMutationErrorText, variableFetchErrorText } from '~/ci_variable_list/constants';

import { mockGroupVariables, newVariable } from '../mocks';

jest.mock('~/flash');

Vue.use(VueApollo);

const mockProvide = {
  endpoint: '/variables',
  groupPath: '/namespace/group',
  groupId: 1,
};

describe('Ci Group Variable list', () => {
  let wrapper;

  let mockApollo;
  let mockVariables;

  const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
  const findCiTable = () => wrapper.findComponent(GlTable);
  const findCiSettings = () => wrapper.findComponent(ciVariableSettings);

  // eslint-disable-next-line consistent-return
  const createComponentWithApollo = async ({ isLoading = false } = {}) => {
    const handlers = [[getGroupVariables, mockVariables]];

    mockApollo = createMockApollo(handlers, resolvers);

    wrapper = shallowMount(ciGroupVariables, {
      provide: mockProvide,
      apolloProvider: mockApollo,
      stubs: { ciVariableSettings, ciVariableTable },
    });

    if (!isLoading) {
      return waitForPromises();
    }
  };

  beforeEach(() => {
    mockVariables = jest.fn();
  });

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

  describe('while queries are being fetch', () => {
    beforeEach(() => {
      createComponentWithApollo({ isLoading: true });
    });

    it('shows a loading icon', () => {
      expect(findLoadingIcon().exists()).toBe(true);
      expect(findCiTable().exists()).toBe(false);
    });
  });

  describe('when queries are resolved', () => {
    describe('successfuly', () => {
      beforeEach(async () => {
        mockVariables.mockResolvedValue(mockGroupVariables);

        await createComponentWithApollo();
      });

      it('passes down the expected environments as props', () => {
        expect(findCiSettings().props('environments')).toEqual([]);
      });

      it('passes down the expected variables as props', () => {
        expect(findCiSettings().props('variables')).toEqual(
          mockGroupVariables.data.group.ciVariables.nodes,
        );
      });

      it('createFlash was not called', () => {
        expect(createFlash).not.toHaveBeenCalled();
      });
    });

    describe('with an error for variables', () => {
      beforeEach(async () => {
        mockVariables.mockRejectedValue();

        await createComponentWithApollo();
      });

      it('calls createFlash with the expected error message', () => {
        expect(createFlash).toHaveBeenCalledWith({ message: variableFetchErrorText });
      });
    });
  });

  describe('mutations', () => {
    beforeEach(async () => {
      mockVariables.mockResolvedValue(mockGroupVariables);

      await createComponentWithApollo();
    });
    it.each`
      actionName  | mutation               | event
      ${'add'}    | ${addGroupVariable}    | ${'add-variable'}
      ${'update'} | ${updateGroupVariable} | ${'update-variable'}
      ${'delete'} | ${deleteGroupVariable} | ${'delete-variable'}
    `(
      'calls the right mutation when user performs $actionName variable',
      async ({ event, mutation }) => {
        jest.spyOn(wrapper.vm.$apollo, 'mutate').mockResolvedValue();
        await findCiSettings().vm.$emit(event, newVariable);

        expect(wrapper.vm.$apollo.mutate).toHaveBeenCalledWith({
          mutation,
          variables: {
            endpoint: mockProvide.endpoint,
            fullPath: mockProvide.groupPath,
            groupId: convertToGraphQLId('Group', mockProvide.groupId),
            variable: newVariable,
          },
        });
      },
    );

    it.each`
      actionName  | event                | mutationName
      ${'add'}    | ${'add-variable'}    | ${'addGroupVariable'}
      ${'update'} | ${'update-variable'} | ${'updateGroupVariable'}
      ${'delete'} | ${'delete-variable'} | ${'deleteGroupVariable'}
    `(
      'throws with the specific graphql error if present when user performs $actionName variable',
      async ({ event, mutationName }) => {
        const graphQLErrorMessage = 'There is a problem with this graphQL action';
        jest
          .spyOn(wrapper.vm.$apollo, 'mutate')
          .mockResolvedValue({ data: { [mutationName]: { errors: [graphQLErrorMessage] } } });
        await findCiSettings().vm.$emit(event, newVariable);
        await nextTick();

        expect(wrapper.vm.$apollo.mutate).toHaveBeenCalled();
        expect(createFlash).toHaveBeenCalledWith({ message: graphQLErrorMessage });
      },
    );

    it.each`
      actionName  | event
      ${'add'}    | ${'add-variable'}
      ${'update'} | ${'update-variable'}
      ${'delete'} | ${'delete-variable'}
    `(
      'throws generic error when the mutation fails with no graphql errors and user performs $actionName variable',
      async ({ event }) => {
        jest.spyOn(wrapper.vm.$apollo, 'mutate').mockImplementationOnce(() => {
          throw new Error();
        });
        await findCiSettings().vm.$emit(event, newVariable);

        expect(wrapper.vm.$apollo.mutate).toHaveBeenCalled();
        expect(createFlash).toHaveBeenCalledWith({ message: genericMutationErrorText });
      },
    );
  });
});