debian-mirror-gitlab/spec/frontend/comment_templates/components/form_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

146 lines
4.9 KiB
JavaScript
Raw Normal View History

2023-05-27 22:25:52 +05:30
import Vue, { nextTick } from 'vue';
import { mount } from '@vue/test-utils';
import { GlAlert } from '@gitlab/ui';
import VueApollo from 'vue-apollo';
2023-06-20 00:43:36 +05:30
import createdSavedReplyResponse from 'test_fixtures/graphql/comment_templates/create_saved_reply.mutation.graphql.json';
import createdSavedReplyErrorResponse from 'test_fixtures/graphql/comment_templates/create_saved_reply_with_errors.mutation.graphql.json';
2023-05-27 22:25:52 +05:30
import createMockApollo from 'helpers/mock_apollo_helper';
import waitForPromises from 'helpers/wait_for_promises';
2023-06-20 00:43:36 +05:30
import Form from '~/comment_templates/components/form.vue';
import createSavedReplyMutation from '~/comment_templates/queries/create_saved_reply.mutation.graphql';
import updateSavedReplyMutation from '~/comment_templates/queries/update_saved_reply.mutation.graphql';
2023-05-27 22:25:52 +05:30
let wrapper;
let createSavedReplyResponseSpy;
let updateSavedReplyResponseSpy;
function createMockApolloProvider(response) {
Vue.use(VueApollo);
createSavedReplyResponseSpy = jest.fn().mockResolvedValue(response);
updateSavedReplyResponseSpy = jest.fn().mockResolvedValue(response);
const requestHandlers = [
[createSavedReplyMutation, createSavedReplyResponseSpy],
[updateSavedReplyMutation, updateSavedReplyResponseSpy],
];
return createMockApollo(requestHandlers);
}
function createComponent(id = null, response = createdSavedReplyResponse) {
const mockApollo = createMockApolloProvider(response);
return mount(Form, {
propsData: {
id,
},
apolloProvider: mockApollo,
});
}
2023-06-20 00:43:36 +05:30
const findSavedReplyNameInput = () => wrapper.find('[data-testid="comment-template-name-input"]');
2023-05-27 22:25:52 +05:30
const findSavedReplyNameFormGroup = () =>
2023-06-20 00:43:36 +05:30
wrapper.find('[data-testid="comment-template-name-form-group"]');
const findSavedReplyContentInput = () =>
wrapper.find('[data-testid="comment-template-content-input"]');
2023-05-27 22:25:52 +05:30
const findSavedReplyContentFormGroup = () =>
2023-06-20 00:43:36 +05:30
wrapper.find('[data-testid="comment-template-content-form-group"]');
const findSavedReplyFrom = () => wrapper.find('[data-testid="comment-template-form"]');
2023-05-27 22:25:52 +05:30
const findAlerts = () => wrapper.findAllComponents(GlAlert);
2023-06-20 00:43:36 +05:30
const findSubmitBtn = () => wrapper.find('[data-testid="comment-template-form-submit-btn"]');
2023-05-27 22:25:52 +05:30
2023-06-20 00:43:36 +05:30
describe('Comment templates form component', () => {
describe('creates comment template', () => {
2023-05-27 22:25:52 +05:30
it('calls apollo mutation', async () => {
wrapper = createComponent();
findSavedReplyNameInput().setValue('Test');
findSavedReplyContentInput().setValue('Test content');
findSavedReplyFrom().trigger('submit');
await waitForPromises();
expect(createSavedReplyResponseSpy).toHaveBeenCalledWith({
id: null,
content: 'Test content',
name: 'Test',
});
});
it('does not submit when form validation fails', async () => {
wrapper = createComponent();
findSavedReplyFrom().trigger('submit');
await waitForPromises();
expect(createSavedReplyResponseSpy).not.toHaveBeenCalled();
});
it.each`
findFormGroup | findInput | fieldName
${findSavedReplyNameFormGroup} | ${findSavedReplyContentInput} | ${'name'}
${findSavedReplyContentFormGroup} | ${findSavedReplyNameInput} | ${'content'}
`('shows errors for empty $fieldName input', async ({ findFormGroup, findInput }) => {
wrapper = createComponent(null, createdSavedReplyErrorResponse);
findInput().setValue('Test');
findSavedReplyFrom().trigger('submit');
await waitForPromises();
expect(findFormGroup().classes('is-invalid')).toBe(true);
});
it('displays errors when mutation fails', async () => {
wrapper = createComponent(null, createdSavedReplyErrorResponse);
findSavedReplyNameInput().setValue('Test');
findSavedReplyContentInput().setValue('Test content');
findSavedReplyFrom().trigger('submit');
await waitForPromises();
const { errors } = createdSavedReplyErrorResponse;
const alertMessages = findAlerts().wrappers.map((x) => x.text());
expect(alertMessages).toEqual(errors.map((x) => x.message));
});
it('shows loading state when saving', async () => {
wrapper = createComponent();
findSavedReplyNameInput().setValue('Test');
findSavedReplyContentInput().setValue('Test content');
findSavedReplyFrom().trigger('submit');
await nextTick();
expect(findSubmitBtn().props('loading')).toBe(true);
await waitForPromises();
expect(findSubmitBtn().props('loading')).toBe(false);
});
});
describe('updates saved reply', () => {
it('calls apollo mutation', async () => {
wrapper = createComponent('1');
findSavedReplyNameInput().setValue('Test');
findSavedReplyContentInput().setValue('Test content');
findSavedReplyFrom().trigger('submit');
await waitForPromises();
expect(updateSavedReplyResponseSpy).toHaveBeenCalledWith({
id: '1',
content: 'Test content',
name: 'Test',
});
});
});
});