2020-03-13 15:44:24 +05:30
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
import SnippetBlobView from '~/snippets/components/snippet_blob_view.vue';
|
|
|
|
import BlobHeader from '~/blob/components/blob_header.vue';
|
|
|
|
import BlobEmbeddable from '~/blob/components/blob_embeddable.vue';
|
|
|
|
import BlobContent from '~/blob/components/blob_content.vue';
|
2020-06-23 00:09:42 +05:30
|
|
|
import {
|
|
|
|
BLOB_RENDER_EVENT_LOAD,
|
|
|
|
BLOB_RENDER_EVENT_SHOW_SOURCE,
|
|
|
|
BLOB_RENDER_ERRORS,
|
|
|
|
} from '~/blob/components/constants';
|
2020-03-13 15:44:24 +05:30
|
|
|
import { RichViewer, SimpleViewer } from '~/vue_shared/components/blob_viewers';
|
|
|
|
import {
|
|
|
|
SNIPPET_VISIBILITY_PRIVATE,
|
|
|
|
SNIPPET_VISIBILITY_INTERNAL,
|
|
|
|
SNIPPET_VISIBILITY_PUBLIC,
|
|
|
|
} from '~/snippets/constants';
|
|
|
|
|
|
|
|
import { Blob as BlobMock, SimpleViewerMock, RichViewerMock } from 'jest/blob/components/mock_data';
|
|
|
|
|
|
|
|
describe('Blob Embeddable', () => {
|
|
|
|
let wrapper;
|
|
|
|
const snippet = {
|
|
|
|
id: 'gid://foo.bar/snippet',
|
|
|
|
webUrl: 'https://foo.bar',
|
|
|
|
visibilityLevel: SNIPPET_VISIBILITY_PUBLIC,
|
|
|
|
};
|
|
|
|
const dataMock = {
|
|
|
|
activeViewerType: SimpleViewerMock.type,
|
|
|
|
};
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
function createComponent({
|
|
|
|
snippetProps = {},
|
|
|
|
data = dataMock,
|
|
|
|
blob = BlobMock,
|
|
|
|
contentLoading = false,
|
|
|
|
} = {}) {
|
2020-03-13 15:44:24 +05:30
|
|
|
const $apollo = {
|
|
|
|
queries: {
|
|
|
|
blobContent: {
|
|
|
|
loading: contentLoading,
|
2020-05-24 23:13:21 +05:30
|
|
|
refetch: jest.fn(),
|
|
|
|
skip: true,
|
2020-03-13 15:44:24 +05:30
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
wrapper = mount(SnippetBlobView, {
|
|
|
|
propsData: {
|
|
|
|
snippet: {
|
|
|
|
...snippet,
|
2020-07-28 23:09:34 +05:30
|
|
|
...snippetProps,
|
2020-03-13 15:44:24 +05:30
|
|
|
},
|
2020-07-28 23:09:34 +05:30
|
|
|
blob,
|
2020-03-13 15:44:24 +05:30
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
...data,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
mocks: { $apollo },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('rendering', () => {
|
|
|
|
it('renders correct components', () => {
|
|
|
|
createComponent();
|
|
|
|
expect(wrapper.find(BlobHeader).exists()).toBe(true);
|
|
|
|
expect(wrapper.find(BlobContent).exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each([SNIPPET_VISIBILITY_INTERNAL, SNIPPET_VISIBILITY_PRIVATE, 'foo'])(
|
|
|
|
'does not render blob-embeddable by default',
|
|
|
|
visibilityLevel => {
|
|
|
|
createComponent({
|
2020-07-28 23:09:34 +05:30
|
|
|
snippetProps: {
|
|
|
|
visibilityLevel,
|
|
|
|
},
|
2020-03-13 15:44:24 +05:30
|
|
|
});
|
|
|
|
expect(wrapper.find(BlobEmbeddable).exists()).toBe(false);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
it('sets simple viewer correctly', () => {
|
|
|
|
createComponent();
|
|
|
|
expect(wrapper.find(SimpleViewer).exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets rich viewer correctly', () => {
|
2020-05-24 23:13:21 +05:30
|
|
|
const data = { ...dataMock, activeViewerType: RichViewerMock.type };
|
2020-07-28 23:09:34 +05:30
|
|
|
createComponent({
|
|
|
|
data,
|
|
|
|
});
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(wrapper.find(RichViewer).exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('correctly switches viewer type', () => {
|
|
|
|
createComponent();
|
|
|
|
expect(wrapper.find(SimpleViewer).exists()).toBe(true);
|
|
|
|
|
|
|
|
wrapper.vm.switchViewer(RichViewerMock.type);
|
|
|
|
|
|
|
|
return wrapper.vm
|
|
|
|
.$nextTick()
|
|
|
|
.then(() => {
|
|
|
|
expect(wrapper.find(RichViewer).exists()).toBe(true);
|
|
|
|
wrapper.vm.switchViewer(SimpleViewerMock.type);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
expect(wrapper.find(SimpleViewer).exists()).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
it('passes information about render error down to blob header', () => {
|
|
|
|
createComponent({
|
|
|
|
blob: {
|
|
|
|
...BlobMock,
|
|
|
|
simpleViewer: {
|
|
|
|
...SimpleViewerMock,
|
|
|
|
renderError: BLOB_RENDER_ERRORS.REASONS.COLLAPSED.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(wrapper.find(BlobHeader).props('hasRenderError')).toBe(true);
|
|
|
|
});
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
describe('URLS with hash', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
window.location.hash = '#LC2';
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
window.location.hash = '';
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders simple viewer by default if URL contains hash', () => {
|
2020-07-28 23:09:34 +05:30
|
|
|
createComponent({
|
|
|
|
data: {},
|
|
|
|
});
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
expect(wrapper.vm.activeViewerType).toBe(SimpleViewerMock.type);
|
|
|
|
expect(wrapper.find(SimpleViewer).exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('switchViewer()', () => {
|
2020-04-22 19:07:51 +05:30
|
|
|
it('switches to the passed viewer', () => {
|
2020-03-13 15:44:24 +05:30
|
|
|
createComponent();
|
|
|
|
|
|
|
|
wrapper.vm.switchViewer(RichViewerMock.type);
|
|
|
|
return wrapper.vm
|
|
|
|
.$nextTick()
|
|
|
|
.then(() => {
|
|
|
|
expect(wrapper.vm.activeViewerType).toBe(RichViewerMock.type);
|
|
|
|
expect(wrapper.find(RichViewer).exists()).toBe(true);
|
|
|
|
|
|
|
|
wrapper.vm.switchViewer(SimpleViewerMock.type);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
expect(wrapper.vm.activeViewerType).toBe(SimpleViewerMock.type);
|
|
|
|
expect(wrapper.find(SimpleViewer).exists()).toBe(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
describe('functionality', () => {
|
|
|
|
describe('render error', () => {
|
|
|
|
const findContentEl = () => wrapper.find(BlobContent);
|
|
|
|
|
|
|
|
it('correctly sets blob on the blob-content-error component', () => {
|
|
|
|
createComponent();
|
|
|
|
expect(findContentEl().props('blob')).toEqual(BlobMock);
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`refetches blob content on ${BLOB_RENDER_EVENT_LOAD} event`, () => {
|
|
|
|
createComponent();
|
|
|
|
|
|
|
|
expect(wrapper.vm.$apollo.queries.blobContent.refetch).not.toHaveBeenCalled();
|
|
|
|
findContentEl().vm.$emit(BLOB_RENDER_EVENT_LOAD);
|
|
|
|
expect(wrapper.vm.$apollo.queries.blobContent.refetch).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`sets '${SimpleViewerMock.type}' as active on ${BLOB_RENDER_EVENT_SHOW_SOURCE} event`, () => {
|
2020-07-28 23:09:34 +05:30
|
|
|
createComponent({
|
|
|
|
data: {
|
2020-05-24 23:13:21 +05:30
|
|
|
activeViewerType: RichViewerMock.type,
|
|
|
|
},
|
2020-07-28 23:09:34 +05:30
|
|
|
});
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
findContentEl().vm.$emit(BLOB_RENDER_EVENT_SHOW_SOURCE);
|
|
|
|
expect(wrapper.vm.activeViewerType).toEqual(SimpleViewerMock.type);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-03-13 15:44:24 +05:30
|
|
|
});
|