debian-mirror-gitlab/spec/frontend/blob/components/blob_edit_content_spec.js

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

106 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-04-08 14:13:33 +05:30
import { shallowMount } from '@vue/test-utils';
import { nextTick } from 'vue';
2020-10-24 23:57:45 +05:30
import BlobEditContent from '~/blob/components/blob_edit_content.vue';
import * as utils from '~/blob/utils';
2020-04-08 14:13:33 +05:30
2021-09-30 23:02:18 +05:30
jest.mock('~/editor/source_editor');
2020-04-08 14:13:33 +05:30
describe('Blob Header Editing', () => {
let wrapper;
2020-11-24 15:15:51 +05:30
const onDidChangeModelContent = jest.fn();
const updateModelLanguage = jest.fn();
const getValue = jest.fn();
2020-04-08 14:13:33 +05:30
const value = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
const fileName = 'lorem.txt';
2020-10-24 23:57:45 +05:30
const fileGlobalId = 'snippet_777';
2020-04-08 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
function createComponent(props = {}) {
2020-04-08 14:13:33 +05:30
wrapper = shallowMount(BlobEditContent, {
propsData: {
value,
fileName,
2020-10-24 23:57:45 +05:30
fileGlobalId,
2020-04-22 19:07:51 +05:30
...props,
2020-04-08 14:13:33 +05:30
},
});
}
beforeEach(() => {
2021-09-30 23:02:18 +05:30
jest.spyOn(utils, 'initSourceEditor').mockImplementation(() => ({
2020-11-24 15:15:51 +05:30
onDidChangeModelContent,
updateModelLanguage,
getValue,
dispose: jest.fn(),
}));
2020-10-24 23:57:45 +05:30
2020-04-08 14:13:33 +05:30
createComponent();
});
afterEach(() => {
wrapper.destroy();
});
2021-03-08 18:12:59 +05:30
const triggerChangeContent = (val) => {
2020-11-24 15:15:51 +05:30
getValue.mockReturnValue(val);
const [cb] = onDidChangeModelContent.mock.calls[0];
2020-10-24 23:57:45 +05:30
cb();
jest.runOnlyPendingTimers();
};
2020-04-08 14:13:33 +05:30
describe('rendering', () => {
it('matches the snapshot', () => {
expect(wrapper.element).toMatchSnapshot();
});
it('renders content', () => {
expect(wrapper.text()).toContain(value);
});
});
describe('functionality', () => {
2020-04-22 19:07:51 +05:30
it('does not fail without content', () => {
const spy = jest.spyOn(global.console, 'error');
createComponent({ value: undefined });
expect(spy).not.toHaveBeenCalled();
2020-11-24 15:15:51 +05:30
expect(wrapper.find('#editor').exists()).toBe(true);
2020-04-22 19:07:51 +05:30
});
2021-09-30 23:02:18 +05:30
it('initialises Source Editor', () => {
2020-04-08 14:13:33 +05:30
const el = wrapper.find({ ref: 'editor' }).element;
2021-09-30 23:02:18 +05:30
expect(utils.initSourceEditor).toHaveBeenCalledWith({
2020-04-08 14:13:33 +05:30
el,
blobPath: fileName,
2020-10-24 23:57:45 +05:30
blobGlobalId: fileGlobalId,
2020-04-08 14:13:33 +05:30
blobContent: value,
});
});
it('reacts to the changes in fileName', () => {
const newFileName = 'ipsum.txt';
wrapper.setProps({
fileName: newFileName,
});
return nextTick().then(() => {
2020-11-24 15:15:51 +05:30
expect(updateModelLanguage).toHaveBeenCalledWith(newFileName);
2020-04-08 14:13:33 +05:30
});
});
2020-10-24 23:57:45 +05:30
it('registers callback with editor onChangeContent', () => {
2020-11-24 15:15:51 +05:30
expect(onDidChangeModelContent).toHaveBeenCalledWith(expect.any(Function));
2020-10-24 23:57:45 +05:30
});
2020-04-08 14:13:33 +05:30
it('emits input event when the blob content is changed', () => {
2020-10-24 23:57:45 +05:30
expect(wrapper.emitted().input).toBeUndefined();
2020-04-08 14:13:33 +05:30
2020-10-24 23:57:45 +05:30
triggerChangeContent(value);
2020-04-08 14:13:33 +05:30
2020-10-24 23:57:45 +05:30
expect(wrapper.emitted().input).toEqual([[value]]);
2020-04-08 14:13:33 +05:30
});
});
});