debian-mirror-gitlab/spec/frontend/static_site_editor/components/edit_meta_modal_spec.js

172 lines
5 KiB
JavaScript
Raw Normal View History

2021-01-03 14:25:43 +05:30
import { shallowMount } from '@vue/test-utils';
import { GlModal } from '@gitlab/ui';
2021-01-29 00:20:46 +05:30
import { useLocalStorageSpy } from 'helpers/local_storage_helper';
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import LocalStorageSync from '~/vue_shared/components/local_storage_sync.vue';
2021-01-03 14:25:43 +05:30
import EditMetaModal from '~/static_site_editor/components/edit_meta_modal.vue';
import EditMetaControls from '~/static_site_editor/components/edit_meta_controls.vue';
2021-01-29 00:20:46 +05:30
import { MR_META_LOCAL_STORAGE_KEY } from '~/static_site_editor/constants';
import {
sourcePath,
mergeRequestMeta,
mergeRequestTemplates,
project as namespaceProject,
} from '../mock_data';
2021-01-03 14:25:43 +05:30
describe('~/static_site_editor/components/edit_meta_modal.vue', () => {
2021-01-29 00:20:46 +05:30
useLocalStorageSpy();
2021-01-03 14:25:43 +05:30
let wrapper;
2021-01-29 00:20:46 +05:30
let mockAxios;
2021-01-03 14:25:43 +05:30
const { title, description } = mergeRequestMeta;
2021-01-29 00:20:46 +05:30
const [namespace, project] = namespaceProject.split('/');
2021-01-03 14:25:43 +05:30
2021-01-29 00:20:46 +05:30
const buildWrapper = (propsData = {}, data = {}) => {
2021-01-03 14:25:43 +05:30
wrapper = shallowMount(EditMetaModal, {
propsData: {
sourcePath,
2021-01-29 00:20:46 +05:30
namespace,
project,
2021-01-03 14:25:43 +05:30
...propsData,
},
2021-01-29 00:20:46 +05:30
data: () => data,
2021-01-03 14:25:43 +05:30
});
};
2021-01-29 00:20:46 +05:30
const buildMockAxios = () => {
mockAxios = new MockAdapter(axios);
const templatesMergeRequestsPath = `templates/merge_request`;
mockAxios
.onGet(`${namespace}/${project}/${templatesMergeRequestsPath}`)
.reply(200, mergeRequestTemplates);
};
const buildMockRefs = () => {
wrapper.vm.$refs.editMetaControls = { resetCachedEditable: jest.fn() };
2021-01-03 14:25:43 +05:30
};
const findGlModal = () => wrapper.find(GlModal);
const findEditMetaControls = () => wrapper.find(EditMetaControls);
2021-01-29 00:20:46 +05:30
const findLocalStorageSync = () => wrapper.find(LocalStorageSync);
2021-01-03 14:25:43 +05:30
beforeEach(() => {
2021-01-29 00:20:46 +05:30
localStorage.setItem(MR_META_LOCAL_STORAGE_KEY);
buildMockAxios();
2021-01-03 14:25:43 +05:30
buildWrapper();
2021-01-29 00:20:46 +05:30
buildMockRefs();
2021-01-03 14:25:43 +05:30
return wrapper.vm.$nextTick();
});
afterEach(() => {
2021-01-29 00:20:46 +05:30
mockAxios.restore();
2021-01-03 14:25:43 +05:30
wrapper.destroy();
wrapper = null;
});
2021-01-29 00:20:46 +05:30
it('initializes initial merge request meta with local storage data', async () => {
const localStorageMeta = {
title: 'stored title',
description: 'stored description',
templates: null,
currentTemplate: null,
};
findLocalStorageSync().vm.$emit('input', localStorageMeta);
await wrapper.vm.$nextTick();
expect(findEditMetaControls().props()).toEqual(localStorageMeta);
});
2021-01-03 14:25:43 +05:30
it('renders the modal', () => {
expect(findGlModal().exists()).toBe(true);
});
it('renders the edit meta controls', () => {
expect(findEditMetaControls().exists()).toBe(true);
});
it('contains the sourcePath in the title', () => {
expect(findEditMetaControls().props('title')).toContain(sourcePath);
});
it('forwards the title prop', () => {
expect(findEditMetaControls().props('title')).toBe(title);
});
it('forwards the description prop', () => {
expect(findEditMetaControls().props('description')).toBe(description);
});
2021-01-29 00:20:46 +05:30
it('forwards the templates prop', () => {
expect(findEditMetaControls().props('templates')).toBe(null);
});
it('forwards the currentTemplate prop', () => {
expect(findEditMetaControls().props('currentTemplate')).toBe(null);
});
describe('when save button is clicked', () => {
beforeEach(() => {
findGlModal().vm.$emit('primary', mergeRequestMeta);
});
it('removes merge request meta from local storage', () => {
expect(findLocalStorageSync().props().clear).toBe(true);
});
it('emits the primary event with mergeRequestMeta', () => {
expect(wrapper.emitted('primary')).toEqual([[mergeRequestMeta]]);
});
2021-01-03 14:25:43 +05:30
});
2021-01-29 00:20:46 +05:30
describe('when templates exist', () => {
const template1 = mergeRequestTemplates[0];
beforeEach(() => {
buildWrapper({}, { templates: mergeRequestTemplates, currentTemplate: null });
});
it('sets the currentTemplate on the changeTemplate event', async () => {
findEditMetaControls().vm.$emit('changeTemplate', template1);
await wrapper.vm.$nextTick();
expect(findEditMetaControls().props().currentTemplate).toBe(template1);
findEditMetaControls().vm.$emit('changeTemplate', null);
await wrapper.vm.$nextTick();
expect(findEditMetaControls().props().currentTemplate).toBe(null);
});
it('updates the description on the changeTemplate event', async () => {
findEditMetaControls().vm.$emit('changeTemplate', template1);
await wrapper.vm.$nextTick();
expect(findEditMetaControls().props().description).toEqual(template1.content);
});
2021-01-03 14:25:43 +05:30
});
it('emits the hide event', () => {
findGlModal().vm.$emit('hide');
expect(wrapper.emitted('hide')).toEqual([[]]);
});
2021-01-29 00:20:46 +05:30
it('stores merge request meta changes in local storage when changes happen', async () => {
const newMeta = { title: 'new title', description: 'new description' };
findEditMetaControls().vm.$emit('updateSettings', newMeta);
await wrapper.vm.$nextTick();
expect(findLocalStorageSync().props('value')).toEqual(newMeta);
});
2021-01-03 14:25:43 +05:30
});