@pingux/astro 1.0.0-alpha.1 → 1.0.0-alpha.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,280 @@
1
+ import _Object$defineProperty from "@babel/runtime-corejs3/core-js-stable/object/define-property";
2
+ import _Object$defineProperties from "@babel/runtime-corejs3/core-js-stable/object/define-properties";
3
+ import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
4
+ import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each";
5
+ import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
6
+ import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
7
+ import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
8
+ import _Object$values from "@babel/runtime-corejs3/core-js-stable/object/values";
9
+ import _extends from "@babel/runtime-corejs3/helpers/esm/extends";
10
+ import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
11
+ import _concatInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/concat";
12
+ import _URL from "@babel/runtime-corejs3/core-js-stable/url";
13
+ import _mapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/map";
14
+ import _sliceInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/slice";
15
+ import _Array$from from "@babel/runtime-corejs3/core-js-stable/array/from";
16
+ import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
17
+ import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
18
+ import _objectWithoutProperties from "@babel/runtime-corejs3/helpers/esm/objectWithoutProperties";
19
+
20
+ function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); if (enumerableOnly) symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
21
+
22
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { var _context2; _forEachInstanceProperty(_context2 = ownKeys(Object(source), true)).call(_context2, function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors) { _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)); } else { var _context3; _forEachInstanceProperty(_context3 = ownKeys(Object(source))).call(_context3, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } } return target; }
23
+
24
+ import React, { forwardRef, useCallback, useImperativeHandle, useMemo, useRef, useState } from 'react';
25
+ import { useVisuallyHidden } from '@react-aria/visually-hidden';
26
+ import { useDropzone } from 'react-dropzone';
27
+ import PropTypes from 'prop-types';
28
+ import { mergeProps } from '@react-aria/utils';
29
+ import { v4 as uuidv4 } from 'uuid';
30
+ import { Box, Input, FieldHelperText, Label, Loader } from '../../index';
31
+ import FileItem from './FileItem';
32
+ import FileSelect from './FileSelect';
33
+ import useStatusClasses from '../../hooks/useStatusClasses';
34
+ import useField from '../../hooks/useField';
35
+ import statuses from '../../utils/devUtils/constants/statuses';
36
+ /**
37
+ * The FileInputField component allows users to upload one or more files by
38
+ * clicking the button or via drag and drop.
39
+ *
40
+ * We utilize [react-dropzone](https://react-dropzone.js.org/) for the drag and drop functionality.
41
+ * */
42
+
43
+ import { jsx as ___EmotionJSX } from "@emotion/react";
44
+ var FileInputField = /*#__PURE__*/forwardRef(function (props, ref) {
45
+ var defaultButtonText = props.defaultButtonText,
46
+ defaultFileList = props.defaultFileList,
47
+ uploadedFilesImperative = props.fileList,
48
+ helperText = props.helperText,
49
+ isDisabled = props.isDisabled,
50
+ isLoading = props.isLoading,
51
+ isMultiple = props.isMultiple,
52
+ onFileSelect = props.onFileSelect,
53
+ onRemove = props.onRemove,
54
+ status = props.status,
55
+ textProps = props.textProps,
56
+ others = _objectWithoutProperties(props, ["defaultButtonText", "defaultFileList", "fileList", "helperText", "isDisabled", "isLoading", "isMultiple", "onFileSelect", "onRemove", "status", "textProps"]);
57
+
58
+ var _useState = useState(defaultFileList || []),
59
+ _useState2 = _slicedToArray(_useState, 2),
60
+ uploadedFiles = _useState2[0],
61
+ setUploadedFiles = _useState2[1];
62
+
63
+ var inputRef = useRef();
64
+ /* istanbul ignore next */
65
+
66
+ useImperativeHandle(ref, function () {
67
+ return inputRef.current;
68
+ });
69
+
70
+ var _useField = useField(_objectSpread({
71
+ status: status,
72
+ isDisabled: isDisabled
73
+ }, others)),
74
+ fieldContainerProps = _useField.fieldContainerProps,
75
+ fieldControlProps = _useField.fieldControlProps,
76
+ fieldLabelProps = _useField.fieldLabelProps;
77
+
78
+ var _useVisuallyHidden = useVisuallyHidden(),
79
+ visuallyHiddenProps = _useVisuallyHidden.visuallyHiddenProps;
80
+
81
+ var handleFileSelect = useCallback(function () {
82
+ inputRef.current.value = null;
83
+ inputRef.current.click();
84
+ }, [inputRef]);
85
+ var handleFileUpload = useCallback(function (event, newFiles) {
86
+ if (onFileSelect) {
87
+ onFileSelect(event, newFiles);
88
+ }
89
+
90
+ var arrayWithNewFiles = _Array$from(newFiles);
91
+
92
+ if (!isMultiple) {
93
+ arrayWithNewFiles = _sliceInstanceProperty(arrayWithNewFiles).call(arrayWithNewFiles, 0, 1);
94
+ }
95
+
96
+ var newFilesWithIdAndLink = _mapInstanceProperty(arrayWithNewFiles).call(arrayWithNewFiles, function (newFile) {
97
+ return {
98
+ fileObj: newFile,
99
+ name: newFile.name,
100
+ id: uuidv4(),
101
+ downloadLink: _URL.createObjectURL(newFile),
102
+ status: statuses.DEFAULT
103
+ };
104
+ });
105
+
106
+ if (isMultiple) {
107
+ setUploadedFiles(function (prevFiles) {
108
+ var _context;
109
+
110
+ return _concatInstanceProperty(_context = []).call(_context, prevFiles, newFilesWithIdAndLink);
111
+ });
112
+ } else {
113
+ setUploadedFiles(newFilesWithIdAndLink);
114
+ }
115
+ }, [isMultiple, onFileSelect]);
116
+ var onDrop = useCallback(function (acceptedFiles, fileRejections, event) {
117
+ handleFileUpload(event, acceptedFiles);
118
+ }, [handleFileUpload]);
119
+
120
+ var _useDropzone = useDropzone({
121
+ onDrop: onDrop,
122
+ noKeyboard: true,
123
+ disabled: isDisabled || isLoading
124
+ }),
125
+ getRootProps = _useDropzone.getRootProps,
126
+ getInputProps = _useDropzone.getInputProps,
127
+ isDragActive = _useDropzone.isDragActive;
128
+
129
+ var handleOnChange = useCallback(function (event) {
130
+ handleFileUpload(event, event.target.files);
131
+ }, [handleFileUpload]);
132
+ var handleFileDelete = useCallback(function (e, fileId) {
133
+ if (onRemove) {
134
+ onRemove(e, fileId);
135
+ }
136
+
137
+ setUploadedFiles(function (prevFiles) {
138
+ return _filterInstanceProperty(prevFiles).call(prevFiles, function (_ref) {
139
+ var id = _ref.id;
140
+ return id !== fileId;
141
+ });
142
+ });
143
+ }, [onRemove]);
144
+ var filesListNode = useMemo(function () {
145
+ var filesToRender = uploadedFilesImperative || uploadedFiles;
146
+
147
+ if (!(filesToRender === null || filesToRender === void 0 ? void 0 : filesToRender.length)) {
148
+ return null;
149
+ }
150
+
151
+ return _mapInstanceProperty(filesToRender).call(filesToRender, function (fileProps) {
152
+ return ___EmotionJSX(FileItem, _extends({
153
+ handleFileDelete: handleFileDelete,
154
+ isDisabled: isDisabled || isLoading,
155
+ key: fileProps.id,
156
+ textProps: textProps
157
+ }, fileProps));
158
+ });
159
+ }, [uploadedFilesImperative, uploadedFiles, handleFileDelete, isDisabled, isLoading, textProps]);
160
+
161
+ var _useStatusClasses = useStatusClasses('fileInputFieldWrapper', _defineProperty({
162
+ isDragActive: isDragActive,
163
+ isLoading: isLoading
164
+ }, "is-".concat(status), true)),
165
+ classNames = _useStatusClasses.classNames;
166
+
167
+ var shouldFileSelectRender = useCallback(function () {
168
+ var isFileUploaded;
169
+
170
+ if (uploadedFilesImperative) {
171
+ isFileUploaded = Boolean(uploadedFilesImperative === null || uploadedFilesImperative === void 0 ? void 0 : uploadedFilesImperative.length);
172
+ } else {
173
+ isFileUploaded = Boolean(uploadedFiles === null || uploadedFiles === void 0 ? void 0 : uploadedFiles.length);
174
+ }
175
+
176
+ return isMultiple || !isFileUploaded;
177
+ }, [uploadedFiles, uploadedFilesImperative, isMultiple]);
178
+ return ___EmotionJSX(React.Fragment, null, ___EmotionJSX(Label, fieldLabelProps), ___EmotionJSX(Box, _extends({
179
+ variant: "boxes.fileInputFieldWrapper"
180
+ }, mergeProps(fieldContainerProps, others), {
181
+ className: classNames
182
+ }, getRootProps()), ___EmotionJSX(Input, _extends({}, mergeProps(visuallyHiddenProps, fieldControlProps, getInputProps()), {
183
+ "aria-label": "File Input",
184
+ multiple: isMultiple,
185
+ onChange: handleOnChange,
186
+ ref: inputRef,
187
+ type: "file"
188
+ })), filesListNode, shouldFileSelectRender() && ___EmotionJSX(FileSelect, {
189
+ buttonText: defaultButtonText,
190
+ handleFileSelect: handleFileSelect,
191
+ isDisabled: isDisabled || isLoading,
192
+ textProps: textProps
193
+ }), isLoading && ___EmotionJSX(Loader, {
194
+ color: "active",
195
+ sx: {
196
+ position: 'absolute'
197
+ },
198
+ "data-testid": "file-input-field__loader"
199
+ })), helperText && ___EmotionJSX(FieldHelperText, {
200
+ status: status
201
+ }, helperText));
202
+ });
203
+ FileInputField.displayName = 'FileInputField';
204
+ FileInputField.propTypes = {
205
+ /** The rendered label for the field. */
206
+ label: PropTypes.node,
207
+
208
+ /** Text rendered below the input. */
209
+ helperText: PropTypes.node,
210
+
211
+ /** Determines the component border status color indicator and helper text styling. */
212
+ status: PropTypes.oneOf(_Object$values(statuses)),
213
+
214
+ /** Default button text that will be changed on the file name once file is uploaded */
215
+ defaultButtonText: PropTypes.string,
216
+
217
+ /** Defines whether input can accept multiple files or not */
218
+ isMultiple: PropTypes.bool,
219
+
220
+ /** Determines whether the loading indicator is shown. */
221
+ isLoading: PropTypes.bool,
222
+
223
+ /** Whether the field is disabled. */
224
+ isDisabled: PropTypes.bool,
225
+
226
+ /** Array of objects for uploaded files.
227
+ * Objects should have the following structure:
228
+ *
229
+ * {
230
+ *
231
+ * fileObj: File object (optional),
232
+ *
233
+ * id: file id (required - will be returned on delete),
234
+ *
235
+ * name: file name that will be displayed (required - will be displayed),
236
+ *
237
+ * downloadLink: link for the file download (optional),
238
+ *
239
+ * status: file status (if error - icon of the
240
+ * component will be red icon and warning sign)(optional),
241
+ *
242
+ * }
243
+ * */
244
+ fileList: PropTypes.arrayOf(PropTypes.shape({
245
+ fileObj: PropTypes.shape({}),
246
+ id: PropTypes.string.required,
247
+ name: PropTypes.string.required,
248
+ downloadLink: PropTypes.string,
249
+ status: PropTypes.oneOf(_Object$values(statuses))
250
+ })),
251
+
252
+ /** Default array of objects for uploaded files. */
253
+ defaultFileList: PropTypes.arrayOf(PropTypes.shape({
254
+ fileObj: PropTypes.shape({}),
255
+ id: PropTypes.string.required,
256
+ name: PropTypes.string.required,
257
+ downloadLink: PropTypes.string,
258
+ status: PropTypes.oneOf(_Object$values(statuses))
259
+ })),
260
+
261
+ /** The handler that is called when the input files is uploaded.
262
+ *
263
+ * `(event, files) => void`
264
+ * */
265
+ onFileSelect: PropTypes.func,
266
+
267
+ /** The handler that is called when an uploaded file got removed.
268
+ *
269
+ * `(fileId) => void`
270
+ * */
271
+ onRemove: PropTypes.func,
272
+
273
+ /** These props will be spread to the field text component. */
274
+ textProps: PropTypes.shape({})
275
+ };
276
+ FileInputField.defaultProps = {
277
+ defaultButtonText: 'Select a file',
278
+ status: statuses.DEFAULT
279
+ };
280
+ export default FileInputField;
@@ -0,0 +1,206 @@
1
+ import _slicedToArray from "@babel/runtime-corejs3/helpers/esm/slicedToArray";
2
+ import _extends from "@babel/runtime-corejs3/helpers/esm/extends";
3
+ import React, { useState } from 'react';
4
+ import { v4 as uuidv4 } from 'uuid';
5
+ import FileInputField from './';
6
+ import { Box } from '../../index';
7
+ import statuses from '../../utils/devUtils/constants/statuses';
8
+ import { jsx as ___EmotionJSX } from "@emotion/react";
9
+ export default {
10
+ title: 'Form/FileInputField',
11
+ component: FileInputField,
12
+ parameters: {
13
+ docs: {
14
+ source: {
15
+ type: 'code'
16
+ }
17
+ }
18
+ },
19
+ argTypes: {
20
+ label: {
21
+ control: {
22
+ type: 'text'
23
+ },
24
+ defaultValue: 'Field Label'
25
+ },
26
+ helperText: {
27
+ control: {
28
+ type: 'text'
29
+ }
30
+ },
31
+ status: {
32
+ control: {
33
+ type: 'select',
34
+ options: statuses
35
+ },
36
+ defaultValue: statuses.DEFAULT
37
+ },
38
+ defaultButtonText: {
39
+ control: {
40
+ type: 'text'
41
+ }
42
+ },
43
+ isMultiple: {
44
+ control: {
45
+ type: 'boolean'
46
+ }
47
+ },
48
+ isLoading: {
49
+ control: {
50
+ type: 'boolean'
51
+ }
52
+ },
53
+ isDisabled: {
54
+ control: {
55
+ type: 'boolean'
56
+ }
57
+ },
58
+ fileList: {
59
+ control: {
60
+ type: 'none'
61
+ }
62
+ },
63
+ defaultFileList: {
64
+ control: {
65
+ type: 'none'
66
+ }
67
+ },
68
+ textProps: {
69
+ control: {
70
+ type: 'none'
71
+ }
72
+ }
73
+ }
74
+ };
75
+ var fitContentWidthSx = {
76
+ width: 'fit-content'
77
+ };
78
+ export var Default = function Default(args) {
79
+ return ___EmotionJSX(FileInputField, _extends({
80
+ sx: fitContentWidthSx
81
+ }, args));
82
+ };
83
+ export var CustomButtonText = function CustomButtonText() {
84
+ return ___EmotionJSX(FileInputField, {
85
+ defaultButtonText: "Original Button Name",
86
+ label: "Custom Button Text",
87
+ sx: fitContentWidthSx
88
+ });
89
+ };
90
+ export var ControlledState = function ControlledState() {
91
+ var _useState = useState([]),
92
+ _useState2 = _slicedToArray(_useState, 2),
93
+ userFiles = _useState2[0],
94
+ setUserFiles = _useState2[1];
95
+
96
+ var _useState3 = useState(),
97
+ _useState4 = _slicedToArray(_useState3, 2),
98
+ error = _useState4[0],
99
+ setError = _useState4[1];
100
+
101
+ var handleFileSelect = function handleFileSelect(event, files) {
102
+ // this is approximate conversion just for an example
103
+ var uploadedFileSizeMb = files[0].size / 1e6;
104
+
105
+ if (uploadedFileSizeMb > 2) {
106
+ setError('Only files under 2 MB can be uploaded.');
107
+ } else {
108
+ setUserFiles([{
109
+ id: uuidv4(),
110
+ name: files[0].name,
111
+ downloadLink: 'link for testing purposes'
112
+ }]);
113
+ setError(null);
114
+ }
115
+ }; // you would want to handle this differently in case you have multiple files
116
+
117
+
118
+ var handleFileRemove = function handleFileRemove() {
119
+ setUserFiles([]);
120
+ setError(null);
121
+ };
122
+
123
+ return ___EmotionJSX(FileInputField, {
124
+ label: "Controlled State With limit on file size upload below 2 mb",
125
+ onFileSelect: handleFileSelect,
126
+ onRemove: handleFileRemove,
127
+ fileList: userFiles,
128
+ sx: fitContentWidthSx,
129
+ status: error && statuses.ERROR,
130
+ helperText: error
131
+ });
132
+ };
133
+ export var DefaultFileListUncontrolled = function DefaultFileListUncontrolled() {
134
+ return ___EmotionJSX(FileInputField, {
135
+ defaultFileList: [{
136
+ id: 'test-id1',
137
+ name: 'PingId Mobile (Android)',
138
+ downloadLink: 'https://download.pingidentity.com/public/PingID/android/PingID.apk'
139
+ }, {
140
+ id: 'test-id2',
141
+ name: 'PingId Desktop (macOS)',
142
+ downloadLink: 'https://downloads.pingidentity.com/pingid/mac-client/PingID.pkg'
143
+ }, {
144
+ id: 'test-id3',
145
+ name: 'PingId Desktop (Windows)',
146
+ downloadLink: 'https://download.pingidentity.com/public/PingID/PingID_1.7.2.zip'
147
+ }],
148
+ isMultiple: true,
149
+ label: "Uncontrolled File List with default files",
150
+ sx: fitContentWidthSx
151
+ });
152
+ };
153
+ export var ErrorStatusSingleFile = function ErrorStatusSingleFile() {
154
+ return ___EmotionJSX(FileInputField, {
155
+ defaultFileList: [{
156
+ id: 'test-id3',
157
+ name: 'PingId Desktop (Windows)',
158
+ downloadLink: 'https://download.pingidentity.com/public/PingID/PingID_1.7.2.zip',
159
+ status: statuses.ERROR
160
+ }],
161
+ label: "Error Status Single File",
162
+ sx: fitContentWidthSx,
163
+ helperText: "There is an error",
164
+ status: statuses.ERROR
165
+ });
166
+ };
167
+ export var ErrorWithMultipleFiles = function ErrorWithMultipleFiles() {
168
+ return ___EmotionJSX(FileInputField, {
169
+ defaultFileList: [{
170
+ id: 'test-id3',
171
+ name: 'PingId Desktop (Windows)',
172
+ downloadLink: 'https://download.pingidentity.com/public/PingID/PingID_1.7.2.zip',
173
+ status: statuses.ERROR
174
+ }, {
175
+ id: 'test-id2',
176
+ name: 'PingId Desktop (macOS)',
177
+ downloadLink: 'https://downloads.pingidentity.com/pingid/mac-client/PingID.pkg'
178
+ }],
179
+ label: "Error Status With Multiple Files but without a red border",
180
+ sx: fitContentWidthSx,
181
+ helperText: "There is an error but helperText text will be default since no status passed",
182
+ isMultiple: true
183
+ });
184
+ };
185
+ export var WithCustomWidth = function WithCustomWidth() {
186
+ var textSx = {
187
+ textOverflow: 'ellipsis',
188
+ overflow: 'hidden',
189
+ whiteSpace: 'nowrap'
190
+ };
191
+ return ___EmotionJSX(Box, {
192
+ width: 200
193
+ }, ___EmotionJSX(FileInputField, {
194
+ defaultButtonText: "Long Long Button Text With A Lot of Words In It",
195
+ defaultFileList: [{
196
+ id: 'test-id1',
197
+ name: 'Really Long File Name That Cant Fit',
198
+ downloadLink: 'https://download.pingidentity.com/public/PingID/android/PingID.apk'
199
+ }],
200
+ label: "Custom Width",
201
+ textProps: {
202
+ sx: textSx
203
+ },
204
+ isMultiple: true
205
+ }));
206
+ };
@@ -0,0 +1,187 @@
1
+ import _Object$defineProperty from "@babel/runtime-corejs3/core-js-stable/object/define-property";
2
+ import _Object$defineProperties from "@babel/runtime-corejs3/core-js-stable/object/define-properties";
3
+ import _Object$getOwnPropertyDescriptors from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors";
4
+ import _forEachInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/for-each";
5
+ import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor";
6
+ import _filterInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/filter";
7
+ import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols";
8
+ import _Object$keys from "@babel/runtime-corejs3/core-js-stable/object/keys";
9
+ import _defineProperty from "@babel/runtime-corejs3/helpers/esm/defineProperty";
10
+ import _extends from "@babel/runtime-corejs3/helpers/esm/extends";
11
+
12
+ function ownKeys(object, enumerableOnly) { var keys = _Object$keys(object); if (_Object$getOwnPropertySymbols) { var symbols = _Object$getOwnPropertySymbols(object); if (enumerableOnly) symbols = _filterInstanceProperty(symbols).call(symbols, function (sym) { return _Object$getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
13
+
14
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { var _context; _forEachInstanceProperty(_context = ownKeys(Object(source), true)).call(_context, function (key) { _defineProperty(target, key, source[key]); }); } else if (_Object$getOwnPropertyDescriptors) { _Object$defineProperties(target, _Object$getOwnPropertyDescriptors(source)); } else { var _context2; _forEachInstanceProperty(_context2 = ownKeys(Object(source))).call(_context2, function (key) { _Object$defineProperty(target, key, _Object$getOwnPropertyDescriptor(source, key)); }); } } return target; }
15
+
16
+ import React from 'react';
17
+ import userEvent from '@testing-library/user-event';
18
+ import { fireEvent, render, screen } from '@testing-library/react';
19
+ import axeTest from '../../utils/testUtils/testAxe';
20
+ import FileInputField from './FileInputField';
21
+ import statuses from '../../utils/devUtils/constants/statuses';
22
+ import { jsx as ___EmotionJSX } from "@emotion/react";
23
+ var fileInputFieldTestId = 'file-input-test-id';
24
+ var testLabel = 'file-input-test-label';
25
+ var fileSelectTestIdButton = 'file-input-field__file-select';
26
+ var fileUploadedDeleteIconTestId = 'file-uploaded__delete-file-button';
27
+ var fileUploadedFileIconErrorTestId = 'file-uploaded__file-icon--is-error';
28
+ var fileUploadedDownloadTestId = 'file-uploaded__download-file-button';
29
+ var fileUploadedDownloadLinkTestId = 'file-uploaded__download-link';
30
+ var loaderTestId = 'file-input-field__loader';
31
+ var testFileURL = 'test-file-url';
32
+ var testFileId = 'test-file-id-1';
33
+ var testFileName = 'chucknorris.png';
34
+ var testFileName2 = 'chucknorris222.png';
35
+ var testFile = new File(['(⌐□_□)'], testFileName, {
36
+ type: 'image/png'
37
+ });
38
+ var testFile2 = new File(['(⌐□_□)'], testFileName2, {
39
+ type: 'image/png'
40
+ });
41
+ var testFileObject = {
42
+ id: testFileId,
43
+ name: testFileName,
44
+ downloadLink: testFileURL
45
+ };
46
+ var originalValue = global.URL.createObjectURL;
47
+ var defaultProps = {
48
+ 'data-testid': fileInputFieldTestId,
49
+ label: testLabel
50
+ };
51
+ beforeAll(function () {
52
+ global.URL.createObjectURL = jest.fn(function () {
53
+ return testFileURL;
54
+ });
55
+ });
56
+ afterAll(function () {
57
+ global.URL.createObjectURL = originalValue;
58
+ });
59
+
60
+ var getComponent = function getComponent(props) {
61
+ return render(___EmotionJSX(FileInputField, _extends({}, defaultProps, props)));
62
+ };
63
+
64
+ axeTest(getComponent);
65
+ test('should render file input field component by default', function () {
66
+ getComponent();
67
+ var fileUploadField = screen.getByTestId(fileInputFieldTestId);
68
+ expect(fileUploadField).toBeInstanceOf(HTMLDivElement);
69
+ expect(fileUploadField).toBeInTheDocument();
70
+ });
71
+ test('should render files if they are passed as default', function () {
72
+ getComponent({
73
+ defaultFileList: [testFileObject]
74
+ });
75
+ expect(screen.getByText(testFileName)).toBeInTheDocument();
76
+ });
77
+ test('should render files if they are passed as controlled prop', function () {
78
+ getComponent({
79
+ fileList: [testFileObject]
80
+ });
81
+ expect(screen.getByText(testFileName)).toBeInTheDocument();
82
+ });
83
+ test('should be able to display uploaded file', function () {
84
+ getComponent();
85
+ var fileUploadFieldInput = screen.getByLabelText(testLabel);
86
+ userEvent.click(screen.getByTestId(fileSelectTestIdButton));
87
+ fireEvent.change(fileUploadFieldInput, {
88
+ target: {
89
+ files: [testFile]
90
+ }
91
+ });
92
+ expect(screen.getByText(testFileName)).toBeInTheDocument();
93
+ });
94
+ test('should be able to add uploaded file if isMultiple true and file select always present', function () {
95
+ var testCustomButtonName = 'test Custom Button Name';
96
+ getComponent({
97
+ defaultFileList: [testFileObject],
98
+ defaultButtonText: testCustomButtonName,
99
+ isMultiple: true
100
+ });
101
+ var fileUploadFieldInput = screen.getByLabelText(testLabel);
102
+ userEvent.click(screen.getByTestId(fileSelectTestIdButton));
103
+ fireEvent.change(fileUploadFieldInput, {
104
+ target: {
105
+ files: [testFile2]
106
+ }
107
+ });
108
+ expect(screen.getByText(testFileName)).toBeInTheDocument();
109
+ expect(screen.getByText(testFileName2)).toBeInTheDocument();
110
+ expect(screen.getByText(testCustomButtonName)).toBeInTheDocument();
111
+ });
112
+ test('should call onFileSelect if file uploaded', function () {
113
+ var mockOnFileSelect = jest.fn();
114
+ getComponent({
115
+ onFileSelect: mockOnFileSelect
116
+ });
117
+ var fileUploadFieldInput = screen.getByLabelText(testLabel);
118
+ userEvent.click(screen.getByTestId(fileSelectTestIdButton));
119
+ fireEvent.change(fileUploadFieldInput, {
120
+ target: {
121
+ files: [testFile]
122
+ }
123
+ });
124
+ expect(mockOnFileSelect).toHaveBeenCalledTimes(1);
125
+ });
126
+ test('file should have download link as attribute', function () {
127
+ getComponent({
128
+ defaultFileList: [testFileObject]
129
+ });
130
+ userEvent.click(screen.getByTestId(fileUploadedDownloadTestId));
131
+ expect(screen.getByTestId(fileUploadedDownloadLinkTestId)).toHaveAttribute('href', testFileURL);
132
+ });
133
+ test('should remove file if trash icon clicked', function () {
134
+ getComponent({
135
+ defaultFileList: [testFileObject]
136
+ });
137
+ expect(screen.getByText(testFileName)).toBeInTheDocument();
138
+ userEvent.click(screen.getByTestId(fileUploadedDeleteIconTestId));
139
+ expect(screen.queryByText(testFileName)).not.toBeInTheDocument();
140
+ });
141
+ test('should call onRemove if trash icon clicked', function () {
142
+ var mockOnRemove = jest.fn();
143
+ getComponent({
144
+ defaultFileList: [{
145
+ id: testFileId,
146
+ name: 'test'
147
+ }],
148
+ onRemove: mockOnRemove
149
+ });
150
+ userEvent.click(screen.getByTestId(fileUploadedDeleteIconTestId));
151
+ expect(mockOnRemove).toHaveBeenCalledTimes(1);
152
+ expect(mockOnRemove).toHaveBeenCalledWith(expect.anything(), testFileId);
153
+ });
154
+ test('file select will have custom text if from props if provided', function () {
155
+ var mockTitle = 'test-title';
156
+ getComponent({
157
+ defaultButtonText: mockTitle
158
+ });
159
+ expect(screen.getByText(mockTitle)).toBeInTheDocument();
160
+ });
161
+ test('should render error border if appropriate state passed', function () {
162
+ getComponent({
163
+ status: statuses.ERROR
164
+ });
165
+ expect(screen.getByTestId(fileInputFieldTestId)).toHaveClass('is-error');
166
+ });
167
+ test('file uploaded should render red error icon', function () {
168
+ getComponent({
169
+ defaultFileList: [_objectSpread(_objectSpread({}, testFileObject), {}, {
170
+ status: statuses.ERROR
171
+ })]
172
+ });
173
+ expect(screen.getByTestId(fileUploadedFileIconErrorTestId)).toBeInTheDocument();
174
+ });
175
+ test('should display loader if appropriate prop is passed', function () {
176
+ getComponent({
177
+ isLoading: true
178
+ });
179
+ expect(screen.getByTestId(loaderTestId)).toBeInTheDocument();
180
+ });
181
+ test('should display helper text if passed', function () {
182
+ var testHelperText = 'testHelperText';
183
+ getComponent({
184
+ helperText: testHelperText
185
+ });
186
+ expect(screen.getByText(testHelperText)).toBeInTheDocument();
187
+ });