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

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

101 lines
3 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';
2022-05-07 20:08:51 +05:30
import Papa from 'papaparse';
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;
2022-05-07 20:08:51 +05:30
const createComponent = ({
csv = validCsv,
remoteFile = false,
mountFunction = shallowMount,
} = {}) => {
2021-10-27 15:23:28 +05:30
wrapper = mountFunction(CsvViewer, {
2021-09-30 23:02:18 +05:30
propsData: {
csv,
2022-05-07 20:08:51 +05:30
remoteFile,
2021-09-30 23:02:18 +05:30
},
});
};
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 });
2022-07-23 23:45:48 +05:30
await nextTick();
2021-09-30 23:02:18 +05:30
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();
2022-07-23 23:45:48 +05:30
await nextTick();
2021-09-30 23:02:18 +05:30
expect(findAlert().exists()).toBe(false);
});
it('renders the CSV table with the correct attributes', async () => {
createComponent();
2022-07-23 23:45:48 +05:30
await nextTick();
2021-09-30 23:02:18 +05:30
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 });
2022-07-23 23:45:48 +05:30
await nextTick();
2021-09-30 23:02:18 +05:30
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);
});
});
2022-05-07 20:08:51 +05:30
describe('when csv prop is path and indicates a remote file', () => {
it('should render call parse with download flag true', async () => {
const path = 'path/to/remote/file.csv';
jest.spyOn(Papa, 'parse').mockImplementation((_, { complete }) => {
complete({ data: validCsv.split(','), errors: [] });
});
createComponent({ csv: path, remoteFile: true });
expect(Papa.parse).toHaveBeenCalledWith(path, {
download: true,
skipEmptyLines: true,
complete: expect.any(Function),
});
2022-07-23 23:45:48 +05:30
await nextTick();
2022-05-07 20:08:51 +05:30
expect(wrapper.vm.items).toEqual(validCsv.split(','));
});
});
2021-09-30 23:02:18 +05:30
});