2021-03-29 12:09:14 +00:00
|
|
|
import { GlLoadingIcon } from '@gitlab/ui';
|
2021-07-02 12:08:31 +00:00
|
|
|
import { shallowMount, mount, createLocalVue } from '@vue/test-utils';
|
|
|
|
import axios from 'axios';
|
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2021-05-08 15:10:37 +00:00
|
|
|
import { nextTick } from 'vue';
|
2021-07-02 12:08:31 +00:00
|
|
|
import VueApollo from 'vue-apollo';
|
|
|
|
import createMockApollo from 'helpers/mock_apollo_helper';
|
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2021-03-29 12:09:14 +00:00
|
|
|
import BlobContent from '~/blob/components/blob_content.vue';
|
|
|
|
import BlobHeader from '~/blob/components/blob_header.vue';
|
2021-06-25 09:07:34 +00:00
|
|
|
import BlobButtonGroup from '~/repository/components/blob_button_group.vue';
|
2021-03-29 12:09:14 +00:00
|
|
|
import BlobContentViewer from '~/repository/components/blob_content_viewer.vue';
|
2021-06-17 18:10:22 +00:00
|
|
|
import BlobEdit from '~/repository/components/blob_edit.vue';
|
2021-07-08 09:09:33 +00:00
|
|
|
import { loadViewer, viewerProps } from '~/repository/components/blob_viewers';
|
2021-07-13 15:08:38 +00:00
|
|
|
import DownloadViewer from '~/repository/components/blob_viewers/download_viewer.vue';
|
2021-07-14 09:08:33 +00:00
|
|
|
import EmptyViewer from '~/repository/components/blob_viewers/empty_viewer.vue';
|
2021-07-08 09:09:33 +00:00
|
|
|
import TextViewer from '~/repository/components/blob_viewers/text_viewer.vue';
|
2021-07-02 12:08:31 +00:00
|
|
|
import blobInfoQuery from '~/repository/queries/blob_info.query.graphql';
|
2021-03-29 12:09:14 +00:00
|
|
|
|
2021-07-05 06:08:01 +00:00
|
|
|
jest.mock('~/repository/components/blob_viewers');
|
|
|
|
|
2021-03-29 12:09:14 +00:00
|
|
|
let wrapper;
|
2021-05-06 21:10:07 +00:00
|
|
|
const simpleMockData = {
|
2021-03-29 12:09:14 +00:00
|
|
|
name: 'some_file.js',
|
|
|
|
size: 123,
|
2021-05-06 21:10:07 +00:00
|
|
|
rawSize: 123,
|
2021-05-03 06:10:30 +00:00
|
|
|
rawTextBlob: 'raw content',
|
2021-03-29 12:09:14 +00:00
|
|
|
type: 'text',
|
|
|
|
fileType: 'text',
|
|
|
|
tooLarge: false,
|
|
|
|
path: 'some_file.js',
|
2021-07-02 12:08:31 +00:00
|
|
|
webPath: 'some_file.js',
|
2021-03-29 12:09:14 +00:00
|
|
|
editBlobPath: 'some_file.js/edit',
|
|
|
|
ideEditPath: 'some_file.js/ide/edit',
|
|
|
|
storedExternally: false,
|
|
|
|
rawPath: 'some_file.js',
|
|
|
|
externalStorageUrl: 'some_file.js',
|
|
|
|
replacePath: 'some_file.js/replace',
|
|
|
|
deletePath: 'some_file.js/delete',
|
|
|
|
canLock: true,
|
|
|
|
isLocked: false,
|
|
|
|
lockLink: 'some_file.js/lock',
|
|
|
|
forkPath: 'some_file.js/fork',
|
2021-05-03 06:10:30 +00:00
|
|
|
simpleViewer: {
|
|
|
|
fileType: 'text',
|
|
|
|
tooLarge: false,
|
|
|
|
type: 'simple',
|
2021-05-06 21:10:07 +00:00
|
|
|
renderError: null,
|
2021-05-03 06:10:30 +00:00
|
|
|
},
|
|
|
|
richViewer: null,
|
2021-03-29 12:09:14 +00:00
|
|
|
};
|
2021-05-06 21:10:07 +00:00
|
|
|
const richMockData = {
|
|
|
|
...simpleMockData,
|
|
|
|
richViewer: {
|
|
|
|
fileType: 'markup',
|
|
|
|
tooLarge: false,
|
|
|
|
type: 'rich',
|
|
|
|
renderError: null,
|
|
|
|
},
|
|
|
|
};
|
2021-07-14 12:09:23 +00:00
|
|
|
|
|
|
|
const projectMockData = {
|
2021-07-06 09:07:05 +00:00
|
|
|
userPermissions: {
|
|
|
|
pushCode: true,
|
|
|
|
},
|
2021-07-14 12:09:23 +00:00
|
|
|
repository: {
|
|
|
|
empty: false,
|
|
|
|
},
|
2021-07-06 09:07:05 +00:00
|
|
|
};
|
2021-03-29 12:09:14 +00:00
|
|
|
|
2021-07-02 12:08:31 +00:00
|
|
|
const localVue = createLocalVue();
|
|
|
|
const mockAxios = new MockAdapter(axios);
|
|
|
|
|
2021-07-14 12:09:23 +00:00
|
|
|
const createComponentWithApollo = (mockData = {}) => {
|
2021-07-02 12:08:31 +00:00
|
|
|
localVue.use(VueApollo);
|
|
|
|
|
2021-07-14 12:09:23 +00:00
|
|
|
const defaultPushCode = projectMockData.userPermissions.pushCode;
|
|
|
|
const defaultEmptyRepo = projectMockData.repository.empty;
|
|
|
|
const { blobs, emptyRepo = defaultEmptyRepo, canPushCode = defaultPushCode } = mockData;
|
|
|
|
|
2021-07-06 09:07:05 +00:00
|
|
|
const mockResolver = jest.fn().mockResolvedValue({
|
|
|
|
data: {
|
|
|
|
project: {
|
2021-07-14 12:09:23 +00:00
|
|
|
userPermissions: { pushCode: canPushCode },
|
|
|
|
repository: {
|
|
|
|
empty: emptyRepo,
|
|
|
|
blobs: {
|
|
|
|
nodes: [blobs],
|
|
|
|
},
|
|
|
|
},
|
2021-07-06 09:07:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2021-07-02 12:08:31 +00:00
|
|
|
|
|
|
|
const fakeApollo = createMockApollo([[blobInfoQuery, mockResolver]]);
|
|
|
|
|
|
|
|
wrapper = shallowMount(BlobContentViewer, {
|
|
|
|
localVue,
|
|
|
|
apolloProvider: fakeApollo,
|
|
|
|
propsData: {
|
|
|
|
path: 'some_file.js',
|
|
|
|
projectPath: 'some/path',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-05-08 15:10:37 +00:00
|
|
|
const createFactory = (mountFn) => (
|
|
|
|
{ props = {}, mockData = {}, stubs = {} } = {},
|
|
|
|
loading = false,
|
|
|
|
) => {
|
|
|
|
wrapper = mountFn(BlobContentViewer, {
|
2021-03-29 12:09:14 +00:00
|
|
|
propsData: {
|
2021-05-06 21:10:07 +00:00
|
|
|
path: 'some_file.js',
|
2021-05-03 06:10:30 +00:00
|
|
|
projectPath: 'some/path',
|
2021-05-06 21:10:07 +00:00
|
|
|
...props,
|
2021-03-29 12:09:14 +00:00
|
|
|
},
|
|
|
|
mocks: {
|
|
|
|
$apollo: {
|
|
|
|
queries: {
|
2021-05-03 06:10:30 +00:00
|
|
|
project: {
|
2021-03-29 12:09:14 +00:00
|
|
|
loading,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-05-08 15:10:37 +00:00
|
|
|
stubs,
|
2021-03-29 12:09:14 +00:00
|
|
|
});
|
|
|
|
|
2021-05-06 21:10:07 +00:00
|
|
|
wrapper.setData(mockData);
|
2021-05-08 15:10:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const factory = createFactory(shallowMount);
|
|
|
|
const fullFactory = createFactory(mount);
|
2021-03-29 12:09:14 +00:00
|
|
|
|
|
|
|
describe('Blob content viewer component', () => {
|
2021-06-15 09:10:21 +00:00
|
|
|
const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
|
|
|
|
const findBlobHeader = () => wrapper.findComponent(BlobHeader);
|
2021-06-17 18:10:22 +00:00
|
|
|
const findBlobEdit = () => wrapper.findComponent(BlobEdit);
|
2021-06-15 09:10:21 +00:00
|
|
|
const findBlobContent = () => wrapper.findComponent(BlobContent);
|
2021-06-25 09:07:34 +00:00
|
|
|
const findBlobButtonGroup = () => wrapper.findComponent(BlobButtonGroup);
|
2021-03-29 12:09:14 +00:00
|
|
|
|
2021-05-06 21:10:07 +00:00
|
|
|
afterEach(() => {
|
|
|
|
wrapper.destroy();
|
2021-03-29 12:09:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('renders a GlLoadingIcon component', () => {
|
2021-05-06 21:10:07 +00:00
|
|
|
factory({ mockData: { blobInfo: simpleMockData } }, true);
|
2021-03-29 12:09:14 +00:00
|
|
|
|
|
|
|
expect(findLoadingIcon().exists()).toBe(true);
|
|
|
|
});
|
|
|
|
|
2021-05-06 21:10:07 +00:00
|
|
|
describe('simple viewer', () => {
|
2021-05-08 15:10:37 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
factory({ mockData: { blobInfo: simpleMockData } });
|
|
|
|
});
|
|
|
|
|
2021-05-06 21:10:07 +00:00
|
|
|
it('renders a BlobHeader component', () => {
|
|
|
|
expect(findBlobHeader().props('activeViewerType')).toEqual('simple');
|
|
|
|
expect(findBlobHeader().props('hasRenderError')).toEqual(false);
|
|
|
|
expect(findBlobHeader().props('hideViewerSwitcher')).toEqual(true);
|
|
|
|
expect(findBlobHeader().props('blob')).toEqual(simpleMockData);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders a BlobContent component', () => {
|
|
|
|
expect(findBlobContent().props('loading')).toEqual(false);
|
|
|
|
expect(findBlobContent().props('content')).toEqual('raw content');
|
|
|
|
expect(findBlobContent().props('isRawContent')).toBe(true);
|
|
|
|
expect(findBlobContent().props('activeViewer')).toEqual({
|
|
|
|
fileType: 'text',
|
|
|
|
tooLarge: false,
|
|
|
|
type: 'simple',
|
|
|
|
renderError: null,
|
|
|
|
});
|
|
|
|
});
|
2021-03-29 12:09:14 +00:00
|
|
|
});
|
|
|
|
|
2021-05-06 21:10:07 +00:00
|
|
|
describe('rich viewer', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
factory({
|
|
|
|
mockData: { blobInfo: richMockData, activeViewerType: 'rich' },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders a BlobHeader component', () => {
|
|
|
|
expect(findBlobHeader().props('activeViewerType')).toEqual('rich');
|
|
|
|
expect(findBlobHeader().props('hasRenderError')).toEqual(false);
|
|
|
|
expect(findBlobHeader().props('hideViewerSwitcher')).toEqual(false);
|
|
|
|
expect(findBlobHeader().props('blob')).toEqual(richMockData);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders a BlobContent component', () => {
|
|
|
|
expect(findBlobContent().props('loading')).toEqual(false);
|
|
|
|
expect(findBlobContent().props('content')).toEqual('raw content');
|
|
|
|
expect(findBlobContent().props('isRawContent')).toBe(true);
|
|
|
|
expect(findBlobContent().props('activeViewer')).toEqual({
|
|
|
|
fileType: 'markup',
|
|
|
|
tooLarge: false,
|
|
|
|
type: 'rich',
|
|
|
|
renderError: null,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates viewer type when viewer changed is clicked', async () => {
|
|
|
|
expect(findBlobContent().props('activeViewer')).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
type: 'rich',
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
expect(findBlobHeader().props('activeViewerType')).toEqual('rich');
|
|
|
|
|
|
|
|
findBlobHeader().vm.$emit('viewer-changed', 'simple');
|
2021-05-08 15:10:37 +00:00
|
|
|
await nextTick();
|
2021-03-29 12:09:14 +00:00
|
|
|
|
2021-05-06 21:10:07 +00:00
|
|
|
expect(findBlobHeader().props('activeViewerType')).toEqual('simple');
|
|
|
|
expect(findBlobContent().props('activeViewer')).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
type: 'simple',
|
|
|
|
}),
|
|
|
|
);
|
2021-03-29 12:09:14 +00:00
|
|
|
});
|
|
|
|
});
|
2021-05-08 15:10:37 +00:00
|
|
|
|
2021-07-02 12:08:31 +00:00
|
|
|
describe('legacy viewers', () => {
|
|
|
|
it('does not load a legacy viewer when a rich viewer is not available', async () => {
|
2021-07-14 12:09:23 +00:00
|
|
|
createComponentWithApollo({ blobs: simpleMockData });
|
2021-07-02 12:08:31 +00:00
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(mockAxios.history.get).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('loads a legacy viewer when a rich viewer is available', async () => {
|
2021-07-14 12:09:23 +00:00
|
|
|
createComponentWithApollo({ blobs: richMockData });
|
2021-07-02 12:08:31 +00:00
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(mockAxios.history.get).toHaveLength(1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-07-05 06:08:01 +00:00
|
|
|
describe('Blob viewer', () => {
|
2021-07-14 09:08:33 +00:00
|
|
|
afterEach(() => {
|
|
|
|
loadViewer.mockRestore();
|
|
|
|
viewerProps.mockRestore();
|
2021-07-05 06:08:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does not render a BlobContent component if a Blob viewer is available', () => {
|
|
|
|
loadViewer.mockReturnValueOnce(() => true);
|
|
|
|
factory({ mockData: { blobInfo: richMockData } });
|
|
|
|
|
|
|
|
expect(findBlobContent().exists()).toBe(false);
|
|
|
|
});
|
2021-07-08 09:09:33 +00:00
|
|
|
|
2021-07-14 09:08:33 +00:00
|
|
|
it.each`
|
|
|
|
viewer | loadViewerReturnValue | viewerPropsReturnValue
|
|
|
|
${'empty'} | ${EmptyViewer} | ${{}}
|
|
|
|
${'download'} | ${DownloadViewer} | ${{ filePath: '/some/file/path', fileName: 'test.js', fileSize: 100 }}
|
|
|
|
${'text'} | ${TextViewer} | ${{ content: 'test', fileName: 'test.js', readOnly: true }}
|
|
|
|
`(
|
|
|
|
'renders viewer component for $viewer files',
|
|
|
|
async ({ viewer, loadViewerReturnValue, viewerPropsReturnValue }) => {
|
|
|
|
loadViewer.mockReturnValue(loadViewerReturnValue);
|
|
|
|
viewerProps.mockReturnValue(viewerPropsReturnValue);
|
|
|
|
|
|
|
|
factory({
|
|
|
|
mockData: {
|
|
|
|
blobInfo: {
|
|
|
|
...simpleMockData,
|
|
|
|
fileType: null,
|
|
|
|
simpleViewer: {
|
|
|
|
...simpleMockData.simpleViewer,
|
|
|
|
fileType: viewer,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2021-07-13 15:08:38 +00:00
|
|
|
|
2021-07-14 09:08:33 +00:00
|
|
|
await nextTick();
|
2021-07-13 15:08:38 +00:00
|
|
|
|
2021-07-14 09:08:33 +00:00
|
|
|
expect(loadViewer).toHaveBeenCalledWith(viewer);
|
|
|
|
expect(wrapper.findComponent(loadViewerReturnValue).exists()).toBe(true);
|
|
|
|
},
|
|
|
|
);
|
2021-07-05 06:08:01 +00:00
|
|
|
});
|
|
|
|
|
2021-05-08 15:10:37 +00:00
|
|
|
describe('BlobHeader action slot', () => {
|
2021-06-03 15:10:01 +00:00
|
|
|
const { ideEditPath, editBlobPath } = simpleMockData;
|
|
|
|
|
|
|
|
it('renders BlobHeaderEdit buttons in simple viewer', async () => {
|
2021-05-08 15:10:37 +00:00
|
|
|
fullFactory({
|
|
|
|
mockData: { blobInfo: simpleMockData },
|
|
|
|
stubs: {
|
|
|
|
BlobContent: true,
|
2021-06-15 09:10:21 +00:00
|
|
|
BlobReplace: true,
|
2021-05-08 15:10:37 +00:00
|
|
|
},
|
|
|
|
});
|
2021-06-03 15:10:01 +00:00
|
|
|
|
2021-05-08 15:10:37 +00:00
|
|
|
await nextTick();
|
2021-06-03 15:10:01 +00:00
|
|
|
|
2021-06-17 18:10:22 +00:00
|
|
|
expect(findBlobEdit().props()).toMatchObject({
|
2021-06-03 15:10:01 +00:00
|
|
|
editPath: editBlobPath,
|
|
|
|
webIdePath: ideEditPath,
|
|
|
|
});
|
2021-05-08 15:10:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('renders BlobHeaderEdit button in rich viewer', async () => {
|
|
|
|
fullFactory({
|
|
|
|
mockData: { blobInfo: richMockData },
|
|
|
|
stubs: {
|
|
|
|
BlobContent: true,
|
2021-06-15 09:10:21 +00:00
|
|
|
BlobReplace: true,
|
2021-05-08 15:10:37 +00:00
|
|
|
},
|
|
|
|
});
|
2021-06-03 15:10:01 +00:00
|
|
|
|
2021-05-08 15:10:37 +00:00
|
|
|
await nextTick();
|
2021-06-03 15:10:01 +00:00
|
|
|
|
2021-06-17 18:10:22 +00:00
|
|
|
expect(findBlobEdit().props()).toMatchObject({
|
2021-06-03 15:10:01 +00:00
|
|
|
editPath: editBlobPath,
|
|
|
|
webIdePath: ideEditPath,
|
|
|
|
});
|
2021-05-08 15:10:37 +00:00
|
|
|
});
|
2021-06-15 09:10:21 +00:00
|
|
|
|
2021-07-02 12:08:31 +00:00
|
|
|
it('does not render BlobHeaderEdit button when viewing a binary file', async () => {
|
|
|
|
fullFactory({
|
|
|
|
mockData: { blobInfo: richMockData, isBinary: true },
|
|
|
|
stubs: {
|
|
|
|
BlobContent: true,
|
|
|
|
BlobReplace: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await nextTick();
|
|
|
|
|
|
|
|
expect(findBlobEdit().exists()).toBe(false);
|
|
|
|
});
|
|
|
|
|
2021-06-25 09:07:34 +00:00
|
|
|
describe('BlobButtonGroup', () => {
|
2021-07-14 12:09:23 +00:00
|
|
|
const { name, path, replacePath, webPath } = simpleMockData;
|
2021-07-06 09:07:05 +00:00
|
|
|
const {
|
|
|
|
userPermissions: { pushCode },
|
2021-07-14 12:09:23 +00:00
|
|
|
repository: { empty },
|
|
|
|
} = projectMockData;
|
2021-06-15 09:10:21 +00:00
|
|
|
|
|
|
|
it('renders component', async () => {
|
|
|
|
window.gon.current_user_id = 1;
|
|
|
|
|
|
|
|
fullFactory({
|
2021-07-14 12:09:23 +00:00
|
|
|
mockData: {
|
|
|
|
blobInfo: simpleMockData,
|
|
|
|
project: { userPermissions: { pushCode }, repository: { empty } },
|
|
|
|
},
|
2021-06-15 09:10:21 +00:00
|
|
|
stubs: {
|
|
|
|
BlobContent: true,
|
2021-06-25 09:07:34 +00:00
|
|
|
BlobButtonGroup: true,
|
2021-06-15 09:10:21 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await nextTick();
|
|
|
|
|
2021-06-25 09:07:34 +00:00
|
|
|
expect(findBlobButtonGroup().props()).toMatchObject({
|
2021-06-15 09:10:21 +00:00
|
|
|
name,
|
|
|
|
path,
|
2021-07-06 09:07:05 +00:00
|
|
|
replacePath,
|
2021-07-14 12:09:23 +00:00
|
|
|
deletePath: webPath,
|
2021-07-06 09:07:05 +00:00
|
|
|
canPushCode: pushCode,
|
2021-07-14 12:09:23 +00:00
|
|
|
emptyRepo: empty,
|
2021-06-15 09:10:21 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not render if not logged in', async () => {
|
|
|
|
window.gon.current_user_id = null;
|
|
|
|
|
|
|
|
fullFactory({
|
|
|
|
mockData: { blobInfo: simpleMockData },
|
|
|
|
stubs: {
|
|
|
|
BlobContent: true,
|
|
|
|
BlobReplace: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await nextTick();
|
|
|
|
|
2021-06-25 09:07:34 +00:00
|
|
|
expect(findBlobButtonGroup().exists()).toBe(false);
|
2021-06-15 09:10:21 +00:00
|
|
|
});
|
|
|
|
});
|
2021-05-08 15:10:37 +00:00
|
|
|
});
|
2021-03-29 12:09:14 +00:00
|
|
|
});
|