summaryrefslogtreecommitdiff
path: root/spec/frontend/clusters_list/components/agent_options_spec.js
blob: 05bab247816d65611351e337e158560b704311ed (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { GlDropdown, GlDropdownItem, GlModal, GlFormInput } from '@gitlab/ui';
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import { ENTER_KEY } from '~/lib/utils/keys';
import getAgentsQuery from '~/clusters_list/graphql/queries/get_agents.query.graphql';
import deleteAgentMutation from '~/clusters_list/graphql/mutations/delete_agent.mutation.graphql';
import createMockApollo from 'helpers/mock_apollo_helper';
import AgentOptions from '~/clusters_list/components/agent_options.vue';
import { MAX_LIST_COUNT } from '~/clusters_list/constants';
import { getAgentResponse, mockDeleteResponse, mockErrorDeleteResponse } from '../mocks/apollo';

Vue.use(VueApollo);

const projectPath = 'path/to/project';
const defaultBranchName = 'default';
const maxAgents = MAX_LIST_COUNT;
const agent = {
  id: 'agent-id',
  name: 'agent-name',
  webPath: 'agent-webPath',
};

describe('AgentOptions', () => {
  let wrapper;
  let toast;
  let apolloProvider;
  let deleteResponse;

  const findModal = () => wrapper.findComponent(GlModal);
  const findDropdown = () => wrapper.findComponent(GlDropdown);
  const findDeleteBtn = () => wrapper.findComponent(GlDropdownItem);
  const findInput = () => wrapper.findComponent(GlFormInput);
  const findPrimaryAction = () => findModal().props('actionPrimary');
  const findPrimaryActionAttributes = (attr) => findPrimaryAction().attributes[0][attr];

  const createMockApolloProvider = ({ mutationResponse }) => {
    deleteResponse = jest.fn().mockResolvedValue(mutationResponse);

    return createMockApollo([[deleteAgentMutation, deleteResponse]]);
  };

  const writeQuery = () => {
    apolloProvider.clients.defaultClient.cache.writeQuery({
      query: getAgentsQuery,
      variables: {
        projectPath,
        defaultBranchName,
        first: maxAgents,
        last: null,
      },
      data: getAgentResponse.data,
    });
  };

  const createWrapper = ({ mutationResponse = mockDeleteResponse } = {}) => {
    apolloProvider = createMockApolloProvider({ mutationResponse });
    const provide = {
      projectPath,
    };
    const propsData = {
      defaultBranchName,
      maxAgents,
      agent,
    };

    toast = jest.fn();

    wrapper = shallowMountExtended(AgentOptions, {
      apolloProvider,
      provide,
      propsData,
      mocks: { $toast: { show: toast } },
      stubs: { GlModal },
    });
    wrapper.vm.$refs.modal.hide = jest.fn();

    writeQuery();
    return wrapper.vm.$nextTick();
  };

  const submitAgentToDelete = async () => {
    findDeleteBtn().vm.$emit('click');
    findInput().vm.$emit('input', agent.name);
    await findModal().vm.$emit('primary');
  };

  beforeEach(() => {
    return createWrapper({});
  });

  afterEach(() => {
    wrapper.destroy();
    apolloProvider = null;
    deleteResponse = null;
    toast = null;
  });

  describe('delete agent action', () => {
    it('displays a delete button', () => {
      expect(findDeleteBtn().text()).toBe('Delete agent');
    });

    describe('when clicking the delete button', () => {
      beforeEach(() => {
        findDeleteBtn().vm.$emit('click');
      });

      it('displays a delete confirmation modal', () => {
        expect(findModal().isVisible()).toBe(true);
      });
    });

    describe.each`
      condition                                   | agentName       | isDisabled | mutationCalled
      ${'the input with agent name is missing'}   | ${''}           | ${true}    | ${false}
      ${'the input with agent name is incorrect'} | ${'wrong-name'} | ${true}    | ${false}
      ${'the input with agent name is correct'}   | ${agent.name}   | ${false}   | ${true}
    `('when $condition', ({ agentName, isDisabled, mutationCalled }) => {
      beforeEach(() => {
        findDeleteBtn().vm.$emit('click');
        findInput().vm.$emit('input', agentName);
      });

      it(`${isDisabled ? 'disables' : 'enables'} the modal primary button`, () => {
        expect(findPrimaryActionAttributes('disabled')).toBe(isDisabled);
      });

      describe('when user clicks the modal primary button', () => {
        beforeEach(async () => {
          await findModal().vm.$emit('primary');
        });

        if (mutationCalled) {
          it('calls the delete mutation', () => {
            expect(deleteResponse).toHaveBeenCalledWith({ input: { id: agent.id } });
          });
        } else {
          it("doesn't call the delete mutation", () => {
            expect(deleteResponse).not.toHaveBeenCalled();
          });
        }
      });

      describe('when user presses the enter button', () => {
        beforeEach(async () => {
          await findInput().vm.$emit('keydown', new KeyboardEvent({ key: ENTER_KEY }));
        });

        if (mutationCalled) {
          it('calls the delete mutation', () => {
            expect(deleteResponse).toHaveBeenCalledWith({ input: { id: agent.id } });
          });
        } else {
          it("doesn't call the delete mutation", () => {
            expect(deleteResponse).not.toHaveBeenCalled();
          });
        }
      });
    });

    describe('when agent was deleted successfully', () => {
      beforeEach(async () => {
        await submitAgentToDelete();
      });

      it('calls the toast action', () => {
        expect(toast).toHaveBeenCalledWith(`${agent.name} successfully deleted`);
      });
    });
  });

  describe('when getting an error deleting agent', () => {
    beforeEach(async () => {
      await createWrapper({ mutationResponse: mockErrorDeleteResponse });

      submitAgentToDelete();
    });

    it('displays the error message', () => {
      expect(toast).toHaveBeenCalledWith('could not delete agent');
    });
  });

  describe('when the delete modal was closed', () => {
    beforeEach(async () => {
      const loadingResponse = new Promise(() => {});
      await createWrapper({ mutationResponse: loadingResponse });

      submitAgentToDelete();
    });

    it('reenables the options dropdown', async () => {
      expect(findPrimaryActionAttributes('loading')).toBe(true);
      expect(findDropdown().attributes('disabled')).toBe('true');

      await findModal().vm.$emit('hide');

      expect(findPrimaryActionAttributes('loading')).toBe(false);
      expect(findDropdown().attributes('disabled')).toBeUndefined();
    });

    it('clears the agent name input', async () => {
      expect(findInput().attributes('value')).toBe(agent.name);

      await findModal().vm.$emit('hide');

      expect(findInput().attributes('value')).toBeUndefined();
    });
  });
});