debian-mirror-gitlab/spec/frontend/snippets/components/snippet_blob_edit_spec.js

166 lines
5 KiB
JavaScript
Raw Normal View History

2020-04-08 14:13:33 +05:30
import SnippetBlobEdit from '~/snippets/components/snippet_blob_edit.vue';
import BlobHeaderEdit from '~/blob/components/blob_edit_header.vue';
import BlobContentEdit from '~/blob/components/blob_edit_content.vue';
2020-04-22 19:07:51 +05:30
import { GlLoadingIcon } from '@gitlab/ui';
2020-04-08 14:13:33 +05:30
import { shallowMount } from '@vue/test-utils';
2020-04-22 19:07:51 +05:30
import { nextTick } from 'vue';
2020-07-28 23:09:34 +05:30
import AxiosMockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
import { joinPaths } from '~/lib/utils/url_utility';
import waitForPromises from 'helpers/wait_for_promises';
2020-04-08 14:13:33 +05:30
jest.mock('~/blob/utils', () => jest.fn());
2020-07-28 23:09:34 +05:30
jest.mock('~/lib/utils/url_utility', () => ({
getBaseURL: jest.fn().mockReturnValue('foo/'),
joinPaths: jest
.fn()
.mockName('joinPaths')
.mockReturnValue('contentApiURL'),
}));
jest.mock('~/flash');
let flashSpy;
2020-04-08 14:13:33 +05:30
describe('Snippet Blob Edit component', () => {
let wrapper;
2020-07-28 23:09:34 +05:30
let axiosMock;
const contentMock = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
const pathMock = 'lorem.txt';
const rawPathMock = 'foo/bar';
const blob = {
path: pathMock,
content: contentMock,
rawPath: rawPathMock,
};
const findComponent = component => wrapper.find(component);
2020-04-08 14:13:33 +05:30
2020-07-28 23:09:34 +05:30
function createComponent(props = {}, data = { isContentLoading: false }) {
2020-04-08 14:13:33 +05:30
wrapper = shallowMount(SnippetBlobEdit, {
propsData: {
2020-04-22 19:07:51 +05:30
...props,
2020-04-08 14:13:33 +05:30
},
2020-07-28 23:09:34 +05:30
data() {
return {
...data,
};
},
2020-04-08 14:13:33 +05:30
});
2020-07-28 23:09:34 +05:30
flashSpy = jest.spyOn(wrapper.vm, 'flashAPIFailure');
2020-04-08 14:13:33 +05:30
}
beforeEach(() => {
2020-07-28 23:09:34 +05:30
axiosMock = new AxiosMockAdapter(axios);
2020-04-08 14:13:33 +05:30
createComponent();
});
afterEach(() => {
2020-07-28 23:09:34 +05:30
axiosMock.restore();
2020-04-08 14:13:33 +05:30
wrapper.destroy();
});
describe('rendering', () => {
it('matches the snapshot', () => {
2020-07-28 23:09:34 +05:30
createComponent({ blob });
2020-04-08 14:13:33 +05:30
expect(wrapper.element).toMatchSnapshot();
});
it('renders required components', () => {
2020-07-28 23:09:34 +05:30
expect(findComponent(BlobHeaderEdit).exists()).toBe(true);
expect(findComponent(BlobContentEdit).exists()).toBe(true);
2020-04-22 19:07:51 +05:30
});
2020-07-28 23:09:34 +05:30
it('renders loader if existing blob is supplied but no content is fetched yet', () => {
createComponent({ blob }, { isContentLoading: true });
2020-04-22 19:07:51 +05:30
expect(wrapper.contains(GlLoadingIcon)).toBe(true);
2020-07-28 23:09:34 +05:30
expect(findComponent(BlobContentEdit).exists()).toBe(false);
});
it('does not render loader if when blob is not supplied', () => {
createComponent();
expect(wrapper.contains(GlLoadingIcon)).toBe(false);
expect(findComponent(BlobContentEdit).exists()).toBe(true);
2020-04-22 19:07:51 +05:30
});
});
describe('functionality', () => {
2020-07-28 23:09:34 +05:30
it('does not fail without blob', () => {
2020-04-22 19:07:51 +05:30
const spy = jest.spyOn(global.console, 'error');
2020-07-28 23:09:34 +05:30
createComponent({ blob: undefined });
2020-04-22 19:07:51 +05:30
expect(spy).not.toHaveBeenCalled();
2020-07-28 23:09:34 +05:30
expect(findComponent(BlobContentEdit).exists()).toBe(true);
2020-04-22 19:07:51 +05:30
});
2020-07-28 23:09:34 +05:30
it.each`
emitter | prop
${BlobHeaderEdit} | ${'filePath'}
${BlobContentEdit} | ${'content'}
`('emits "blob-updated" event when the $prop gets changed', ({ emitter, prop }) => {
expect(wrapper.emitted('blob-updated')).toBeUndefined();
const newValue = 'foo.bar';
findComponent(emitter).vm.$emit('input', newValue);
2020-04-22 19:07:51 +05:30
return nextTick().then(() => {
2020-07-28 23:09:34 +05:30
expect(wrapper.emitted('blob-updated')[0]).toEqual([
expect.objectContaining({
[prop]: newValue,
}),
]);
2020-04-22 19:07:51 +05:30
});
});
2020-07-28 23:09:34 +05:30
describe('fetching blob content', () => {
const bootstrapForExistingSnippet = resp => {
createComponent({
blob: {
...blob,
content: '',
},
});
2020-04-22 19:07:51 +05:30
2020-07-28 23:09:34 +05:30
if (resp === 500) {
axiosMock.onGet('contentApiURL').reply(500);
} else {
axiosMock.onGet('contentApiURL').reply(200, contentMock);
}
};
const bootstrapForNewSnippet = () => {
createComponent();
};
it('fetches blob content with the additional query', () => {
bootstrapForExistingSnippet();
return waitForPromises().then(() => {
expect(joinPaths).toHaveBeenCalledWith('foo/', rawPathMock);
expect(findComponent(BlobHeaderEdit).props('value')).toBe(pathMock);
expect(findComponent(BlobContentEdit).props('value')).toBe(contentMock);
});
});
it('flashes the error message if fetching content fails', () => {
bootstrapForExistingSnippet(500);
return waitForPromises().then(() => {
expect(flashSpy).toHaveBeenCalled();
expect(findComponent(BlobContentEdit).props('value')).toBe('');
});
});
it('does not fetch content for new snippet', () => {
bootstrapForNewSnippet();
return waitForPromises().then(() => {
// we keep using waitForPromises to make sure we do not run failed test
expect(findComponent(BlobHeaderEdit).props('value')).toBe('');
expect(findComponent(BlobContentEdit).props('value')).toBe('');
expect(joinPaths).not.toHaveBeenCalled();
});
2020-04-22 19:07:51 +05:30
});
2020-04-08 14:13:33 +05:30
});
});
});