Commit 18711a65 authored by anna_vovchenko's avatar anna_vovchenko

Implemented suggestions after the FE review

parent 6b5b5e8f
...@@ -61,6 +61,7 @@ export default { ...@@ -61,6 +61,7 @@ export default {
loading: false, loading: false,
error: null, error: null,
deleteConfirmText: null, deleteConfirmText: null,
agentName: this.agent.name,
}; };
}, },
computed: { computed: {
...@@ -98,15 +99,17 @@ export default { ...@@ -98,15 +99,17 @@ export default {
}, },
methods: { methods: {
async deleteAgent() { async deleteAgent() {
if (this.disableModalSubmit || this.loading) {
return;
}
this.loading = true; this.loading = true;
this.error = null; this.error = null;
const successMessage = sprintf(this.$options.i18n.successMessage, { name: this.agent.name });
try { try {
const { errors } = await this.deleteAgentMutation(); const { errors } = await this.deleteAgentMutation();
if (errors?.length > 0) { if (errors.length) {
throw new Error(errors[0]); throw new Error(errors[0]);
} }
} catch (error) { } catch (error) {
...@@ -117,6 +120,7 @@ export default { ...@@ -117,6 +120,7 @@ export default {
} }
} finally { } finally {
this.loading = false; this.loading = false;
const successMessage = sprintf(this.$options.i18n.successMessage, { name: this.agentName });
if (!this.error) { if (!this.error) {
this.$toast.show(successMessage); this.$toast.show(successMessage);
...@@ -196,7 +200,7 @@ export default { ...@@ -196,7 +200,7 @@ export default {
</template> </template>
</gl-sprintf> </gl-sprintf>
</template> </template>
<gl-form-input v-model="deleteConfirmText" @keyup.enter="deleteAgent" /> <gl-form-input v-model="deleteConfirmText" @keydown.enter="deleteAgent" />
</gl-form-group> </gl-form-group>
</gl-modal> </gl-modal>
</div> </div>
......
import { GlDropdownItem, GlModal, GlFormInput } from '@gitlab/ui'; import { GlDropdown, GlDropdownItem, GlModal, GlFormInput } from '@gitlab/ui';
import Vue from 'vue'; import Vue from 'vue';
import VueApollo from 'vue-apollo'; import VueApollo from 'vue-apollo';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper'; 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 getAgentsQuery from '~/clusters_list/graphql/queries/get_agents.query.graphql';
import deleteAgentMutation from '~/clusters_list/graphql/mutations/delete_agent.mutation.graphql'; import deleteAgentMutation from '~/clusters_list/graphql/mutations/delete_agent.mutation.graphql';
import createMockApollo from 'helpers/mock_apollo_helper'; import createMockApollo from 'helpers/mock_apollo_helper';
...@@ -27,6 +28,7 @@ describe('AgentActions', () => { ...@@ -27,6 +28,7 @@ describe('AgentActions', () => {
let deleteResponse; let deleteResponse;
const findModal = () => wrapper.findComponent(GlModal); const findModal = () => wrapper.findComponent(GlModal);
const findDropdown = () => wrapper.findComponent(GlDropdown);
const findDeleteBtn = () => wrapper.findComponent(GlDropdownItem); const findDeleteBtn = () => wrapper.findComponent(GlDropdownItem);
const findInput = () => wrapper.findComponent(GlFormInput); const findInput = () => wrapper.findComponent(GlFormInput);
const findPrimaryAction = () => findModal().props('actionPrimary'); const findPrimaryAction = () => findModal().props('actionPrimary');
...@@ -77,6 +79,12 @@ describe('AgentActions', () => { ...@@ -77,6 +79,12 @@ describe('AgentActions', () => {
return wrapper.vm.$nextTick(); return wrapper.vm.$nextTick();
}; };
const submitAgentToDelete = async () => {
findDeleteBtn().vm.$emit('click');
findInput().vm.$emit('input', agent.name);
await findModal().vm.$emit('primary');
};
beforeEach(() => { beforeEach(() => {
return createWrapper({}); return createWrapper({});
}); });
...@@ -103,28 +111,58 @@ describe('AgentActions', () => { ...@@ -103,28 +111,58 @@ describe('AgentActions', () => {
}); });
}); });
describe('when the input with agent name is missing', () => { 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(() => { beforeEach(() => {
findDeleteBtn().vm.$emit('click'); findDeleteBtn().vm.$emit('click');
findInput().vm.$emit('input', agentName);
}); });
it('disables the modal primary button', () => { it(`${isDisabled ? 'disables' : 'enables'} the modal primary button`, () => {
expect(findPrimaryActionAttributes('disabled')).toBe(true); expect(findPrimaryActionAttributes('disabled')).toBe(isDisabled);
}); });
describe('when user clicks the modal primary button', () => {
beforeEach(async () => {
await findModal().vm.$emit('primary');
}); });
describe('when submitting the delete modal and the input with agent name is correct ', () => { if (mutationCalled) {
beforeEach(() => { it('calls the delete mutation', () => {
findDeleteBtn().vm.$emit('click'); expect(deleteResponse).toHaveBeenCalledWith({ input: { id: agent.id } });
findInput().vm.$emit('input', agent.name); });
findModal().vm.$emit('primary'); } else {
it("doesn't call the delete mutation", () => {
expect(deleteResponse).not.toHaveBeenCalled();
});
}
});
return wrapper.vm.$nextTick(); 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', () => { it('calls the delete mutation', () => {
expect(deleteResponse).toHaveBeenCalledWith({ input: { id: agent.id } }); 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', () => { it('calls the toast action', () => {
expect(toast).toHaveBeenCalledWith(`${agent.name} successfully deleted`); expect(toast).toHaveBeenCalledWith(`${agent.name} successfully deleted`);
...@@ -136,12 +174,38 @@ describe('AgentActions', () => { ...@@ -136,12 +174,38 @@ describe('AgentActions', () => {
beforeEach(async () => { beforeEach(async () => {
await createWrapper({ mutationResponse: mockErrorDeleteResponse }); await createWrapper({ mutationResponse: mockErrorDeleteResponse });
findDeleteBtn().vm.$emit('click'); submitAgentToDelete();
findModal().vm.$emit('primary');
}); });
it('displays the error message', () => { it('displays the error message', () => {
expect(toast).toHaveBeenCalledWith('could not delete agent'); 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 actions 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();
});
});
}); });
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment