Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue on download CSV file #53

Merged
merged 1 commit into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/components/Client.vue
Original file line number Diff line number Diff line change
Expand Up @@ -708,15 +708,10 @@ export default {
return name;
},
prepareValueForCsv(value = '') {
let preparedValue = value;
if (preparedValue) {
preparedValue = preparedValue.toString();
if (!value && value !== 0) {
return '';
}
preparedValue = preparedValue.replace(/"/g, '""');
if (preparedValue.search(/("|,|\n)/g) >= 0) {
preparedValue = `"${preparedValue}"`;
}
return preparedValue;
return `"${value.toString().replace(/"/g, '\\"').replace(/(\r\n|\n|\r)/gm, '')}"`;
},
getCsvData({ headers, columns, top }) {
const rows = [];
Expand Down
27 changes: 26 additions & 1 deletion tests/unit/Client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { shallowMount } from '@vue/test-utils';
import Client from '@/components/Client.vue';

describe('Client.vue', () => {
it('renders a table with 1 row', () => {
it('Renders a table with 1 row', () => {
const wrapper = shallowMount(Client, {
propsData: {
data: [{ first_name: '1', id: 1 }],
Expand Down Expand Up @@ -153,3 +153,28 @@ describe('Client.vue', () => {
expect(rows).to.have.lengthOf(3);
});
});

describe('Client.vue methods', () => {
const wrapper = shallowMount(Client, {});
it('prepareValueForCsv escapes quotes', () => {
expect(wrapper.vm.prepareValueForCsv('test with quote "')).to.equal('"test with quote \\""');
});
it('prepareValueForCsv returns an empty string when input is null', () => {
expect(wrapper.vm.prepareValueForCsv(null)).to.equal('');
});
it('prepareValueForCsv returns an empty string when input is undefined', () => {
expect(wrapper.vm.prepareValueForCsv(undefined)).to.equal('');
});
it('prepareValueForCsv returns "0" when input is 0', () => {
expect(wrapper.vm.prepareValueForCsv(0)).to.equal('"0"');
});
it('prepareValueForCsv removes line feeds', () => {
expect(wrapper.vm.prepareValueForCsv('test with line feed \n')).to.equal('"test with line feed "');
});
it('prepareValueForCsv removes carriage returns', () => {
expect(wrapper.vm.prepareValueForCsv('test with carriage return \r')).to.equal('"test with carriage return "');
});
it('getFileNameWithExtension returns a filename with .csv extension when input does not have it included', () => {
expect(wrapper.vm.getFileNameWithExtension('filename')).to.equal('filename.csv');
});
});