Skip to content

Commit 2859170

Browse files
committed
Remove dependency on react-pdf internals in test suite
1 parent 6fb4810 commit 2859170

File tree

3 files changed

+99
-5
lines changed

3 files changed

+99
-5
lines changed

test/PassingOptions.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
2-
import { isDataURI } from 'react-pdf/src/shared/utils';
2+
3+
import { isDataURI } from './shared/utils';
34

45
import type { File, PassMethod } from './shared/types';
56

test/Test.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { pdfjs, Document, Outline, Page, Thumbnail } from 'react-pdf/src';
44
import 'react-pdf/src/Page/AnnotationLayer.css';
55
import 'react-pdf/src/Page/TextLayer.css';
66

7-
import { isArrayBuffer, isBlob, isBrowser, loadFromFile } from 'react-pdf/src/shared/utils';
8-
97
import './Test.css';
108

119
import AnnotationOptions from './AnnotationOptions';
@@ -15,7 +13,7 @@ import PassingOptions from './PassingOptions';
1513
import ViewOptions from './ViewOptions';
1614
import CustomRenderer from './CustomRenderer';
1715

18-
import { dataURItoBlob } from './shared/utils';
16+
import { isArrayBuffer, isBlob, isBrowser, loadFromFile, dataURItoBlob } from './shared/utils';
1917

2018
import type { PDFDocumentProxy, PDFPageProxy } from 'pdfjs-dist';
2119
import type { ExternalLinkTarget, File, PassMethod, RenderMode } from './shared/types';

test/shared/utils.ts

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,60 @@
1-
import { dataURItoByteString } from 'react-pdf/src/shared/utils';
1+
import invariant from 'tiny-invariant';
2+
3+
/**
4+
* Checks if we're running in a browser environment.
5+
*/
6+
export const isBrowser = typeof document !== 'undefined';
7+
8+
/**
9+
* Checks whether a variable provided is a string.
10+
*
11+
* @param {*} variable Variable to check
12+
*/
13+
export function isString(variable: unknown): variable is string {
14+
return typeof variable === 'string';
15+
}
16+
17+
/**
18+
* Checks whether a variable provided is an ArrayBuffer.
19+
*
20+
* @param {*} variable Variable to check
21+
*/
22+
export function isArrayBuffer(variable: unknown): variable is ArrayBuffer {
23+
return variable instanceof ArrayBuffer;
24+
}
25+
26+
/**
27+
* Checks whether a variable provided is a Blob.
28+
*
29+
* @param {*} variable Variable to check
30+
*/
31+
export function isBlob(variable: unknown): variable is Blob {
32+
invariant(isBrowser, 'isBlob can only be used in a browser environment');
33+
34+
return variable instanceof Blob;
35+
}
36+
37+
/**
38+
* Checks whether a variable provided is a data URI.
39+
*
40+
* @param {*} variable String to check
41+
*/
42+
export function isDataURI(variable: unknown): variable is `data:${string}` {
43+
return isString(variable) && /^data:/.test(variable);
44+
}
45+
46+
export function dataURItoByteString(dataURI: unknown): string {
47+
invariant(isDataURI(dataURI), 'Invalid data URI.');
48+
49+
const [headersString = '', dataString = ''] = dataURI.split(',');
50+
const headers = headersString.split(';');
51+
52+
if (headers.indexOf('base64') !== -1) {
53+
return atob(dataString);
54+
}
55+
56+
return unescape(dataString);
57+
}
258

359
function dataURItoUint8Array(dataURI: string): Uint8Array {
460
const byteString = dataURItoByteString(dataURI);
@@ -22,3 +78,42 @@ export function dataURItoBlob(dataURI: string): Blob {
2278
const mimeString = header.split(':')[1];
2379
return new Blob([ia], { type: mimeString });
2480
}
81+
82+
export function loadFromFile(file: Blob): Promise<ArrayBuffer> {
83+
return new Promise((resolve, reject) => {
84+
const reader = new FileReader();
85+
86+
reader.onload = () => {
87+
if (!reader.result) {
88+
return reject(new Error('Error while reading a file.'));
89+
}
90+
91+
resolve(reader.result as ArrayBuffer);
92+
};
93+
94+
reader.onerror = (event) => {
95+
if (!event.target) {
96+
return reject(new Error('Error while reading a file.'));
97+
}
98+
99+
const { error } = event.target;
100+
101+
if (!error) {
102+
return reject(new Error('Error while reading a file.'));
103+
}
104+
105+
switch (error.code) {
106+
case error.NOT_FOUND_ERR:
107+
return reject(new Error('Error while reading a file: File not found.'));
108+
case error.SECURITY_ERR:
109+
return reject(new Error('Error while reading a file: Security error.'));
110+
case error.ABORT_ERR:
111+
return reject(new Error('Error while reading a file: Aborted.'));
112+
default:
113+
return reject(new Error('Error while reading a file.'));
114+
}
115+
};
116+
117+
reader.readAsArrayBuffer(file);
118+
});
119+
}

0 commit comments

Comments
 (0)