debian-mirror-gitlab/spec/frontend/blob/csv/csv_viewer_spec.js

77 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-10-27 15:23:28 +05:30
import { GlLoadingIcon, GlTable } from '@gitlab/ui';
2021-09-30 23:02:18 +05:30
import { getAllByRole } from '@testing-library/dom';
import { shallowMount, mount } from '@vue/test-utils';
import { nextTick } from 'vue';
2021-10-27 15:23:28 +05:30
import CsvViewer from '~/blob/csv/csv_viewer.vue';
import PapaParseAlert from '~/vue_shared/components/papa_parse_alert.vue';
2021-09-30 23:02:18 +05:30
const validCsv = 'one,two,three';
const brokenCsv = '{\n "json": 1,\n "key": [1, 2, 3]\n}';
describe('app/assets/javascripts/blob/csv/csv_viewer.vue', () => {
let wrapper;
const createComponent = ({ csv = validCsv, mountFunction = shallowMount } = {}) => {
2021-10-27 15:23:28 +05:30
wrapper = mountFunction(CsvViewer, {
2021-09-30 23:02:18 +05:30
propsData: {
csv,
},
});
};
const findCsvTable = () => wrapper.findComponent(GlTable);
const findLoadingIcon = () => wrapper.findComponent(GlLoadingIcon);
2021-10-27 15:23:28 +05:30
const findAlert = () => wrapper.findComponent(PapaParseAlert);
2021-09-30 23:02:18 +05:30
afterEach(() => {
wrapper.destroy();
});
it('should render loading spinner', () => {
createComponent();
expect(findLoadingIcon().props()).toMatchObject({
size: 'lg',
});
});
describe('when the CSV contains errors', () => {
2021-10-27 15:23:28 +05:30
it('should render alert with correct props', async () => {
2021-09-30 23:02:18 +05:30
createComponent({ csv: brokenCsv });
await nextTick;
expect(findAlert().props()).toMatchObject({
2021-10-27 15:23:28 +05:30
papaParseErrors: [{ code: 'UndetectableDelimiter' }],
2021-09-30 23:02:18 +05:30
});
});
});
describe('when the CSV contains no errors', () => {
it('should not render alert', async () => {
createComponent();
await nextTick;
expect(findAlert().exists()).toBe(false);
});
it('renders the CSV table with the correct attributes', async () => {
createComponent();
await nextTick;
expect(findCsvTable().attributes()).toMatchObject({
'empty-text': 'No CSV data to display.',
items: validCsv,
});
});
it('renders the CSV table with the correct content', async () => {
createComponent({ mountFunction: mount });
await nextTick;
expect(getAllByRole(wrapper.element, 'row', { name: /One/i })).toHaveLength(1);
expect(getAllByRole(wrapper.element, 'row', { name: /Two/i })).toHaveLength(1);
expect(getAllByRole(wrapper.element, 'row', { name: /Three/i })).toHaveLength(1);
});
});
});