Skip to content

Commit

Permalink
build(deps): Upgrade Papaparse to 5.0.0 (#1330)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conrad Chan authored Feb 24, 2021
1 parent df692c4 commit 9e66b55
Show file tree
Hide file tree
Showing 6 changed files with 652 additions and 266 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
fixture: false,
hljs: false,
jest: false,
Papa: false,
pdfjsLib: false,
pdfjsViewer: false,
remarkable: false,
Expand Down
53 changes: 22 additions & 31 deletions src/lib/viewers/text/CSVViewer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import PreviewError from '../../PreviewError';
import TextBaseViewer from './TextBaseViewer';
import { createAssetUrlCreator } from '../../util';
import { ERROR_CODE, VIEWER_EVENT } from '../../events';
import { TEXT_STATIC_ASSETS_VERSION } from '../../constants';
import './CSV.scss';
import { ERROR_CODE, VIEWER_EVENT } from '../../events';
import PreviewError from '../../PreviewError';

const JS = [`third-party/text/${TEXT_STATIC_ASSETS_VERSION}/papaparse.min.js`, 'csv.js'];
const PAPAPARSE_ERROR_TYPES = {
Expand Down Expand Up @@ -53,38 +52,30 @@ class CSVViewer extends TextBaseViewer {
load() {
super.load();

const { representation, location } = this.options;
const { representation } = this.options;
const template = representation.content.url_template;
const assetUrlCreator = createAssetUrlCreator(location);
const papaWorkerUrl = assetUrlCreator(JS[0]);

return Promise.all([this.loadAssets(JS), this.getRepStatus().getPromise()])
.then(() => {
this.api.get(papaWorkerUrl, { type: 'blob' }).then(papaWorkerBlob => {
/* global Papa */
const workerSrc = URL.createObjectURL(papaWorkerBlob);
Papa.SCRIPT_PATH = workerSrc;

this.startLoadTimer();
const urlWithAuth = this.createContentUrlWithAuthParams(template);
Papa.parse(urlWithAuth, {
download: true,
error: (err, file, inputElem, reason) => {
const error = new PreviewError(ERROR_CODE.LOAD_CSV, __('error_refresh'), { reason });
this.handleDownloadError(error, urlWithAuth);
},
complete: results => {
if (this.isDestroyed() || !results) {
return;
}

this.checkForParseErrors(results);

this.data = results.data;
this.finishLoading();
URL.revokeObjectURL(workerSrc);
},
});
this.startLoadTimer();
const urlWithAuth = this.createContentUrlWithAuthParams(template);
Papa.parse(urlWithAuth, {
download: true,
error: (err, file, inputElem, reason) => {
const error = new PreviewError(ERROR_CODE.LOAD_CSV, __('error_refresh'), { reason });
this.handleDownloadError(error, urlWithAuth);
},
complete: results => {
if (this.isDestroyed() || !results) {
return;
}

this.checkForParseErrors(results);

this.data = results.data;
this.finishLoading();
},
worker: true,
});
})
.catch(this.handleAssetError);
Expand Down
27 changes: 4 additions & 23 deletions src/lib/viewers/text/__tests__/CSVViewer-test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* eslint-disable no-unused-expressions */
import React from 'react'; // eslint-disable-line no-unused-vars
import createReactClass from 'create-react-class';
import Papa from '../../../../third-party/text/0.114.0/papaparse.min.js';
import Papa from '../../../../third-party/text/2.65.0/papaparse.min.js';
import Api from '../../../api';
import BaseViewer from '../../BaseViewer';
import CSVViewer from '../CSVViewer';
import TextBaseViewer from '../TextBaseViewer';
import BaseViewer from '../../BaseViewer';
import * as util from '../../../util';
import { VIEWER_EVENT } from '../../../events';

let containerEl;
Expand Down Expand Up @@ -67,7 +66,7 @@ describe('lib/viewers/text/CSVViewer', () => {

beforeEach(() => {
jest.spyOn(URL, 'createObjectURL');
jest.spyOn(window.Papa, 'parse');
jest.spyOn(window.Papa, 'parse').mockImplementation(() => {});
jest.spyOn(csv, 'setup');
jest.spyOn(csv, 'loadAssets').mockResolvedValue(undefined);
jest.spyOn(csv, 'getRepStatus').mockReturnValue({ getPromise: () => Promise.resolve() });
Expand All @@ -78,31 +77,14 @@ describe('lib/viewers/text/CSVViewer', () => {
Object.defineProperty(TextBaseViewer.prototype, 'load', { value: loadFunc });
});

test('should load papaparse worker and call parent load()', () => {
const blob = {};
const workerUrl = 'workerUrl';
jest.spyOn(stubs.api, 'get').mockResolvedValue(blob);
jest.spyOn(util, 'createAssetUrlCreator').mockReturnValue(jest.fn().mockReturnValue(workerUrl));

Object.defineProperty(TextBaseViewer.prototype, 'load', { value: jest.fn() });

return csv.load().then(() => {
expect(URL.createObjectURL).toBeCalledWith(blob);
expect(stubs.api.get).toBeCalledWith(workerUrl, { type: 'blob' });
});
});

/* eslint-disable no-undef */
test('should parse with Papaparse', () => {
jest.spyOn(util, 'createAssetUrlCreator').mockReturnValue(jest.fn().mockReturnValue('someUrl'));
Object.defineProperty(TextBaseViewer.prototype, 'load', { value: jest.fn() });

csv.options.token = 'token';
csv.options.sharedLink = 'sharedLink';
csv.options.sharedLinkPassword = 'sharedLinkPassword';

jest.spyOn(stubs.api, 'get').mockResolvedValue(undefined);

const csvUrlWithAuth = `csvUrl/?access_token=token&shared_link=sharedLink&shared_link_password=sharedLinkPassword&box_client_name=${__NAME__}&box_client_version=${__VERSION__}`;

return csv.load().then(() => {
Expand All @@ -112,19 +94,18 @@ describe('lib/viewers/text/CSVViewer', () => {
download: true,
error: expect.any(Function),
complete: expect.any(Function),
worker: true,
}),
);
});
});
/* eslint-enable no-undef */

test('should invoke startLoadTimer()', () => {
jest.spyOn(util, 'createAssetUrlCreator').mockReturnValue(jest.fn().mockReturnValue('someUrl'));
Object.defineProperty(TextBaseViewer.prototype, 'load', { value: jest.fn() });
csv.options.token = 'token';
csv.options.sharedLink = 'sharedLink';
csv.options.sharedLinkPassword = 'sharedLinkPassword';
jest.spyOn(stubs.api, 'get').mockResolvedValue(undefined);
jest.spyOn(csv, 'startLoadTimer');

return csv.load().then(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/third-party/text/2.65.0/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "2.65.0",
"dependencies": {
"highlightjs": "9.18.1",
"papaparse": "4.1.2",
"papaparse": "5.0.0",
"remarkable": "2.0.1"
}
}
Loading

0 comments on commit 9e66b55

Please sign in to comment.