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

179 lines
4.8 KiB
JavaScript
Raw Normal View History

2020-03-13 15:44:24 +05:30
import { shallowMount, mount } from '@vue/test-utils';
2022-04-04 11:22:00 +05:30
import { nextTick } from 'vue';
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';
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;
function createComponent(blobProps = {}, options = {}, propsData = {}, shouldMount = false) {
const method = shouldMount ? mount : shallowMount;
2021-01-29 00:20:46 +05:30
const blobHash = 'foo-bar';
2020-03-13 15:44:24 +05:30
wrapper = method.call(this, BlobHeader, {
2021-01-29 00:20:46 +05:30
provide: {
blobHash,
},
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,
});
}
afterEach(() => {
wrapper.destroy();
});
describe('rendering', () => {
2021-10-27 15:23:28 +05:30
const findDefaultActions = () => wrapper.find(DefaultActions);
2020-03-13 15:44:24 +05:30
const slots = {
prepend: 'Foo Prepend',
actions: 'Actions Bar',
};
it('matches the snapshot', () => {
createComponent();
expect(wrapper.element).toMatchSnapshot();
});
it('renders all components', () => {
createComponent();
2021-12-11 22:18:48 +05:30
expect(wrapper.find(TableContents).exists()).toBe(true);
2020-03-13 15:44:24 +05:30
expect(wrapper.find(ViewerSwitcher).exists()).toBe(true);
2021-10-27 15:23:28 +05:30
expect(findDefaultActions().exists()).toBe(true);
2020-03-13 15:44:24 +05:30
expect(wrapper.find(BlobFilepath).exists()).toBe(true);
});
it('does not render viewer switcher if the blob has only the simple viewer', () => {
createComponent({
richViewer: null,
});
expect(wrapper.find(ViewerSwitcher).exists()).toBe(false);
});
it('does not render viewer switcher if a corresponding prop is passed', () => {
createComponent(
{},
{},
{
hideViewerSwitcher: true,
},
);
expect(wrapper.find(ViewerSwitcher).exists()).toBe(false);
});
it('does not render default actions is corresponding prop is passed', () => {
createComponent(
{},
{},
{
hideDefaultActions: true,
},
);
expect(wrapper.find(DefaultActions).exists()).toBe(false);
});
2021-03-08 18:12:59 +05:30
Object.keys(slots).forEach((slot) => {
2020-03-13 15:44:24 +05:30
it('renders the slots', () => {
const slotContent = slots[slot];
createComponent(
{},
{
scopedSlots: {
[slot]: `<span>${slotContent}</span>`,
},
},
{},
true,
);
expect(wrapper.text()).toContain(slotContent);
});
});
2020-06-23 00:09:42 +05:30
it('passes information about render error down to default actions', () => {
createComponent(
{},
{},
{
hasRenderError: true,
},
);
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', () => {
createComponent({}, {}, { isBinary: true });
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 newViewer = 'Foo Bar';
const activeViewerType = 'Alpha Beta';
const factory = (hideViewerSwitcher = false) => {
createComponent(
{},
{},
{
activeViewerType,
hideViewerSwitcher,
},
);
};
it('by default sets viewer data based on activeViewerType', () => {
factory();
expect(wrapper.vm.viewer).toBe(activeViewerType);
});
it('sets viewer to null if the viewer switcher should be hidden', () => {
factory(true);
expect(wrapper.vm.viewer).toBe(null);
});
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();
jest.spyOn(wrapper.vm, '$emit');
wrapper.vm.viewer = newViewer;
2022-04-04 11:22:00 +05:30
await nextTick();
expect(wrapper.vm.$emit).toHaveBeenCalledWith('viewer-changed', newViewer);
2020-03-13 15:44:24 +05:30
});
2022-04-04 11:22:00 +05:30
it('does not emit event if the switcher is not rendered', async () => {
2020-03-13 15:44:24 +05:30
factory(true);
expect(wrapper.vm.showViewerSwitcher).toBe(false);
jest.spyOn(wrapper.vm, '$emit');
wrapper.vm.viewer = newViewer;
2022-04-04 11:22:00 +05:30
await nextTick();
expect(wrapper.vm.$emit).not.toHaveBeenCalled();
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();
expect(wrapper.vm.blobSwitcherDocIcon).toBe('document');
await wrapper.setProps({
blob: {
...Blob,
richViewer: {
...Blob.richViewer,
fileType: 'csv',
},
},
});
expect(wrapper.vm.blobSwitcherDocIcon).toBe('table');
});
2020-03-13 15:44:24 +05:30
});
});