debian-mirror-gitlab/spec/frontend/static_site_editor/pages/home_spec.js

305 lines
7.9 KiB
JavaScript
Raw Normal View History

2020-05-24 23:13:21 +05:30
import { shallowMount, createLocalVue } from '@vue/test-utils';
2020-10-24 23:57:45 +05:30
import { mockTracking, unmockTracking } from 'helpers/tracking_helper';
2020-05-24 23:13:21 +05:30
import EditArea from '~/static_site_editor/components/edit_area.vue';
2021-01-03 14:25:43 +05:30
import EditMetaModal from '~/static_site_editor/components/edit_meta_modal.vue';
2020-05-24 23:13:21 +05:30
import InvalidContentMessage from '~/static_site_editor/components/invalid_content_message.vue';
2021-03-11 19:13:27 +05:30
import SkeletonLoader from '~/static_site_editor/components/skeleton_loader.vue';
2020-05-24 23:13:21 +05:30
import SubmitChangesError from '~/static_site_editor/components/submit_changes_error.vue';
2021-03-11 19:13:27 +05:30
import { TRACKING_ACTION_INITIALIZE_EDITOR } from '~/static_site_editor/constants';
2021-01-03 14:25:43 +05:30
import hasSubmittedChangesMutation from '~/static_site_editor/graphql/mutations/has_submitted_changes.mutation.graphql';
2021-03-11 19:13:27 +05:30
import submitContentChangesMutation from '~/static_site_editor/graphql/mutations/submit_content_changes.mutation.graphql';
import Home from '~/static_site_editor/pages/home.vue';
2020-05-24 23:13:21 +05:30
import { SUCCESS_ROUTE } from '~/static_site_editor/router/constants';
import {
2021-01-29 00:20:46 +05:30
project,
2020-05-24 23:13:21 +05:30
returnUrl,
2020-11-24 15:15:51 +05:30
sourceContentYAML as content,
2020-05-24 23:13:21 +05:30
sourceContentTitle as title,
sourcePath,
username,
2021-01-03 14:25:43 +05:30
mergeRequestMeta,
2020-05-24 23:13:21 +05:30
savedContentMeta,
submitChangesError,
2020-06-23 00:09:42 +05:30
trackingCategory,
2021-01-03 14:25:43 +05:30
images,
2021-01-29 00:20:46 +05:30
mounts,
branch,
baseUrl,
imageRoot,
2020-05-24 23:13:21 +05:30
} from '../mock_data';
const localVue = createLocalVue();
describe('static_site_editor/pages/home', () => {
let wrapper;
let store;
let $apollo;
let $router;
let mutateMock;
2020-06-23 00:09:42 +05:30
let trackingSpy;
2021-01-03 14:25:43 +05:30
const defaultAppData = {
isSupportedContent: true,
hasSubmittedChanges: false,
returnUrl,
project,
username,
sourcePath,
2021-01-29 00:20:46 +05:30
mounts,
branch,
baseUrl,
imageUploadPath: imageRoot,
2021-01-03 14:25:43 +05:30
};
const hasSubmittedChangesMutationPayload = {
data: {
appData: { ...defaultAppData, hasSubmittedChanges: true },
},
};
2020-05-24 23:13:21 +05:30
const buildApollo = (queries = {}) => {
mutateMock = jest.fn();
$apollo = {
queries: {
sourceContent: {
loading: false,
},
...queries,
},
mutate: mutateMock,
};
};
const buildRouter = () => {
$router = {
push: jest.fn(),
};
};
const buildWrapper = (data = {}) => {
wrapper = shallowMount(Home, {
localVue,
store,
mocks: {
$apollo,
$router,
},
data() {
return {
2021-01-03 14:25:43 +05:30
appData: { ...defaultAppData },
2020-05-24 23:13:21 +05:30
sourceContent: { title, content },
...data,
};
},
});
};
const findEditArea = () => wrapper.find(EditArea);
2021-01-03 14:25:43 +05:30
const findEditMetaModal = () => wrapper.find(EditMetaModal);
2020-05-24 23:13:21 +05:30
const findInvalidContentMessage = () => wrapper.find(InvalidContentMessage);
const findSkeletonLoader = () => wrapper.find(SkeletonLoader);
const findSubmitChangesError = () => wrapper.find(SubmitChangesError);
beforeEach(() => {
buildApollo();
buildRouter();
2020-06-23 00:09:42 +05:30
document.body.dataset.page = trackingCategory;
trackingSpy = mockTracking(document.body.dataset.page, undefined, jest.spyOn);
2020-05-24 23:13:21 +05:30
});
afterEach(() => {
wrapper.destroy();
2020-06-23 00:09:42 +05:30
unmockTracking();
2020-05-24 23:13:21 +05:30
wrapper = null;
$apollo = null;
});
describe('when content is loaded', () => {
beforeEach(() => {
buildWrapper();
});
it('renders edit area', () => {
expect(findEditArea().exists()).toBe(true);
});
it('provides source content, returnUrl, and isSavingChanges to the edit area', () => {
expect(findEditArea().props()).toMatchObject({
title,
2021-01-29 00:20:46 +05:30
mounts,
2020-05-24 23:13:21 +05:30
content,
returnUrl,
savingChanges: false,
});
});
});
it('does not render edit area when content is not loaded', () => {
buildWrapper({ sourceContent: null });
expect(findEditArea().exists()).toBe(false);
});
it('renders skeleton loader when content is not loading', () => {
buildApollo({
sourceContent: {
loading: true,
},
});
buildWrapper();
expect(findSkeletonLoader().exists()).toBe(true);
});
it('does not render skeleton loader when content is not loading', () => {
buildApollo({
sourceContent: {
loading: false,
},
});
buildWrapper();
expect(findSkeletonLoader().exists()).toBe(false);
});
it('displays invalid content message when content is not supported', () => {
2021-01-03 14:25:43 +05:30
buildWrapper({ appData: { ...defaultAppData, isSupportedContent: false } });
2020-05-24 23:13:21 +05:30
expect(findInvalidContentMessage().exists()).toBe(true);
});
it('does not display invalid content message when content is supported', () => {
2021-01-03 14:25:43 +05:30
buildWrapper();
2020-05-24 23:13:21 +05:30
expect(findInvalidContentMessage().exists()).toBe(false);
});
2021-01-03 14:25:43 +05:30
it('renders an EditMetaModal component', () => {
buildWrapper();
expect(findEditMetaModal().exists()).toBe(true);
});
2020-05-24 23:13:21 +05:30
2021-01-03 14:25:43 +05:30
describe('when preparing submission', () => {
it('calls the show method when the edit-area submit event is emitted', () => {
2020-05-24 23:13:21 +05:30
buildWrapper();
2021-01-03 14:25:43 +05:30
const mockInstance = { show: jest.fn() };
wrapper.vm.$refs.editMetaModal = mockInstance;
2020-05-24 23:13:21 +05:30
findEditArea().vm.$emit('submit', { content });
2021-01-03 14:25:43 +05:30
return wrapper.vm.$nextTick().then(() => {
expect(mockInstance.show).toHaveBeenCalled();
});
});
});
describe('when submitting changes fails', () => {
const setupMutateMock = () => {
mutateMock
.mockResolvedValueOnce(hasSubmittedChangesMutationPayload)
.mockRejectedValueOnce(new Error(submitChangesError));
};
beforeEach(() => {
setupMutateMock();
buildWrapper({ content });
findEditMetaModal().vm.$emit('primary', mergeRequestMeta);
2020-05-24 23:13:21 +05:30
return wrapper.vm.$nextTick();
});
it('displays submit changes error message', () => {
expect(findSubmitChangesError().exists()).toBe(true);
});
it('retries submitting changes when retry button is clicked', () => {
2021-01-03 14:25:43 +05:30
setupMutateMock();
2020-05-24 23:13:21 +05:30
findSubmitChangesError().vm.$emit('retry');
expect(mutateMock).toHaveBeenCalled();
});
it('hides submit changes error message when dismiss button is clicked', () => {
findSubmitChangesError().vm.$emit('dismiss');
return wrapper.vm.$nextTick().then(() => {
expect(findSubmitChangesError().exists()).toBe(false);
});
});
});
describe('when submitting changes succeeds', () => {
const newContent = `new ${content}`;
2021-02-22 17:27:13 +05:30
const formattedMarkdown = `formatted ${content}`;
2020-05-24 23:13:21 +05:30
beforeEach(() => {
2021-01-03 14:25:43 +05:30
mutateMock.mockResolvedValueOnce(hasSubmittedChangesMutationPayload).mockResolvedValueOnce({
data: {
submitContentChanges: savedContentMeta,
},
});
2020-05-24 23:13:21 +05:30
2021-02-22 17:27:13 +05:30
buildWrapper();
findEditMetaModal().vm.show = jest.fn();
findEditArea().vm.$emit('submit', { content: newContent, images, formattedMarkdown });
2021-01-03 14:25:43 +05:30
findEditMetaModal().vm.$emit('primary', mergeRequestMeta);
2020-05-24 23:13:21 +05:30
return wrapper.vm.$nextTick();
});
2021-01-03 14:25:43 +05:30
it('dispatches hasSubmittedChanges mutation', () => {
expect(mutateMock).toHaveBeenNthCalledWith(1, {
mutation: hasSubmittedChangesMutation,
variables: {
input: {
hasSubmittedChanges: true,
},
},
});
});
2020-05-24 23:13:21 +05:30
it('dispatches submitContentChanges mutation', () => {
2021-01-03 14:25:43 +05:30
expect(mutateMock).toHaveBeenNthCalledWith(2, {
2020-05-24 23:13:21 +05:30
mutation: submitContentChangesMutation,
variables: {
input: {
content: newContent,
2021-02-22 17:27:13 +05:30
formattedMarkdown,
2020-05-24 23:13:21 +05:30
project,
sourcePath,
username,
2021-01-03 14:25:43 +05:30
images,
mergeRequestMeta,
2020-05-24 23:13:21 +05:30
},
},
});
});
it('transitions to the SUCCESS route', () => {
expect($router.push).toHaveBeenCalledWith(SUCCESS_ROUTE);
});
});
2020-06-23 00:09:42 +05:30
2021-01-03 14:25:43 +05:30
it('does not display submit changes error when an error does not exist', () => {
buildWrapper();
expect(findSubmitChangesError().exists()).toBe(false);
});
2020-06-23 00:09:42 +05:30
it('tracks when editor is initialized on the mounted lifecycle hook', () => {
buildWrapper();
expect(trackingSpy).toHaveBeenCalledWith(
document.body.dataset.page,
TRACKING_ACTION_INITIALIZE_EDITOR,
);
});
2020-05-24 23:13:21 +05:30
});