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

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

179 lines
5.1 KiB
JavaScript
Raw Normal View History

2020-03-13 15:44:24 +05:30
import { shallowMount, mount } from '@vue/test-utils';
2023-05-27 22:25:52 +05:30
import { mountExtended } from 'helpers/vue_test_utils_helper';
2020-03-13 15:44:24 +05:30
import BlobHeader from '~/blob/components/blob_header.vue';
import DefaultActions from '~/blob/components/blob_header_default_actions.vue';
import BlobFilepath from '~/blob/components/blob_header_filepath.vue';
2021-03-11 19:13:27 +05:30
import ViewerSwitcher from '~/blob/components/blob_header_viewer_switcher.vue';
2023-05-27 22:25:52 +05:30
import {
RICH_BLOB_VIEWER_TITLE,
SIMPLE_BLOB_VIEWER,
SIMPLE_BLOB_VIEWER_TITLE,
} from '~/blob/components/constants';
2021-12-11 22:18:48 +05:30
import TableContents from '~/blob/components/table_contents.vue';
2020-03-13 15:44:24 +05:30
import { Blob } from './mock_data';
describe('Blob Header Default Actions', () => {
let wrapper;
2023-05-27 22:25:52 +05:30
const defaultProvide = {
blobHash: 'foo-bar',
};
const findDefaultActions = () => wrapper.findComponent(DefaultActions);
const findTableContents = () => wrapper.findComponent(TableContents);
const findViewSwitcher = () => wrapper.findComponent(ViewerSwitcher);
const findBlobFilePath = () => wrapper.findComponent(BlobFilepath);
const findRichTextEditorBtn = () => wrapper.findByLabelText(RICH_BLOB_VIEWER_TITLE);
const findSimpleTextEditorBtn = () => wrapper.findByLabelText(SIMPLE_BLOB_VIEWER_TITLE);
function createComponent({
blobProps = {},
options = {},
propsData = {},
mountFn = shallowMount,
} = {}) {
wrapper = mountFn(BlobHeader, {
2021-01-29 00:20:46 +05:30
provide: {
2023-05-27 22:25:52 +05:30
...defaultProvide,
2021-01-29 00:20:46 +05:30
},
2020-03-13 15:44:24 +05:30
propsData: {
2020-05-24 23:13:21 +05:30
blob: { ...Blob, ...blobProps },
2020-03-13 15:44:24 +05:30
...propsData,
},
...options,
});
}
describe('rendering', () => {
it('matches the snapshot', () => {
createComponent();
expect(wrapper.element).toMatchSnapshot();
});
2023-05-27 22:25:52 +05:30
describe('default render', () => {
it.each`
findComponent | componentName
${findTableContents} | ${'TableContents'}
${findViewSwitcher} | ${'ViewSwitcher'}
${findDefaultActions} | ${'DefaultActions'}
${findBlobFilePath} | ${'BlobFilePath'}
`('renders $componentName component by default', ({ findComponent }) => {
createComponent();
expect(findComponent().exists()).toBe(true);
});
2020-03-13 15:44:24 +05:30
});
it('does not render viewer switcher if the blob has only the simple viewer', () => {
createComponent({
2023-05-27 22:25:52 +05:30
blobProps: {
richViewer: null,
},
2020-03-13 15:44:24 +05:30
});
2023-05-27 22:25:52 +05:30
expect(findViewSwitcher().exists()).toBe(false);
2020-03-13 15:44:24 +05:30
});
it('does not render viewer switcher if a corresponding prop is passed', () => {
2023-05-27 22:25:52 +05:30
createComponent({
propsData: {
2020-03-13 15:44:24 +05:30
hideViewerSwitcher: true,
},
2023-05-27 22:25:52 +05:30
});
expect(findViewSwitcher().exists()).toBe(false);
2020-03-13 15:44:24 +05:30
});
it('does not render default actions is corresponding prop is passed', () => {
2023-05-27 22:25:52 +05:30
createComponent({
propsData: {
2020-03-13 15:44:24 +05:30
hideDefaultActions: true,
},
2023-05-27 22:25:52 +05:30
});
expect(findDefaultActions().exists()).toBe(false);
2020-03-13 15:44:24 +05:30
});
2023-05-27 22:25:52 +05:30
it.each`
slotContent | key
${'Foo Prepend'} | ${'prepend'}
${'Actions Bar'} | ${'actions'}
`('renders the slot $key', ({ key, slotContent }) => {
createComponent({
options: {
scopedSlots: {
[key]: `<span>${slotContent}</span>`,
2020-03-13 15:44:24 +05:30
},
2023-05-27 22:25:52 +05:30
},
mountFn: mount,
2020-03-13 15:44:24 +05:30
});
2023-05-27 22:25:52 +05:30
expect(wrapper.text()).toContain(slotContent);
2020-03-13 15:44:24 +05:30
});
2020-06-23 00:09:42 +05:30
it('passes information about render error down to default actions', () => {
2023-05-27 22:25:52 +05:30
createComponent({
propsData: {
2020-06-23 00:09:42 +05:30
hasRenderError: true,
},
2023-05-27 22:25:52 +05:30
});
2021-10-27 15:23:28 +05:30
expect(findDefaultActions().props('hasRenderError')).toBe(true);
});
it('passes the correct isBinary value to default actions when viewing a binary file', () => {
2023-05-27 22:25:52 +05:30
createComponent({ propsData: { isBinary: true } });
2021-10-27 15:23:28 +05:30
expect(findDefaultActions().props('isBinary')).toBe(true);
2020-06-23 00:09:42 +05:30
});
2020-03-13 15:44:24 +05:30
});
describe('functionality', () => {
const factory = (hideViewerSwitcher = false) => {
2023-05-27 22:25:52 +05:30
createComponent({
propsData: {
activeViewerType: SIMPLE_BLOB_VIEWER,
2020-03-13 15:44:24 +05:30
hideViewerSwitcher,
},
2023-05-27 22:25:52 +05:30
mountFn: mountExtended,
});
2020-03-13 15:44:24 +05:30
};
2023-05-27 22:25:52 +05:30
it('shows the correctly selected view by default', () => {
2020-03-13 15:44:24 +05:30
factory();
2023-05-27 22:25:52 +05:30
expect(findViewSwitcher().exists()).toBe(true);
expect(findRichTextEditorBtn().props().selected).toBe(false);
expect(findSimpleTextEditorBtn().props().selected).toBe(true);
2020-03-13 15:44:24 +05:30
});
2023-05-27 22:25:52 +05:30
it('Does not show the viewer switcher should be hidden', () => {
2020-03-13 15:44:24 +05:30
factory(true);
2023-05-27 22:25:52 +05:30
expect(findViewSwitcher().exists()).toBe(false);
2020-03-13 15:44:24 +05:30
});
2022-04-04 11:22:00 +05:30
it('watches the changes in viewer data and emits event when the change is registered', async () => {
2020-03-13 15:44:24 +05:30
factory();
2023-05-27 22:25:52 +05:30
await findRichTextEditorBtn().trigger('click');
2020-03-13 15:44:24 +05:30
2023-05-27 22:25:52 +05:30
expect(wrapper.emitted('viewer-changed')).toBeDefined();
2020-03-13 15:44:24 +05:30
});
2022-05-07 20:08:51 +05:30
it('sets different icons depending on the blob file type', async () => {
factory();
2023-05-27 22:25:52 +05:30
expect(findViewSwitcher().props('docIcon')).toBe('document');
2022-05-07 20:08:51 +05:30
await wrapper.setProps({
blob: {
...Blob,
richViewer: {
...Blob.richViewer,
fileType: 'csv',
},
},
});
2023-05-27 22:25:52 +05:30
expect(findViewSwitcher().props('docIcon')).toBe('table');
2022-05-07 20:08:51 +05:30
});
2020-03-13 15:44:24 +05:30
});
});