@onewelcome/react-lib-components 1.5.0 → 1.7.0

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.
Files changed (101) hide show
  1. package/README.md +4 -4
  2. package/dist/Button/Button.d.ts +0 -1
  3. package/dist/DataGrid/datagrid.interfaces.d.ts +1 -0
  4. package/dist/Form/Checkbox/Checkbox.d.ts +1 -1
  5. package/dist/Form/FileUpload/FileItem/FileItem.d.ts +17 -0
  6. package/dist/Form/FileUpload/FileUpload.d.ts +26 -0
  7. package/dist/Form/FormHelperText/FormHelperText.d.ts +1 -1
  8. package/dist/Form/FormSelectorWrapper/FormSelectorWrapper.d.ts +1 -1
  9. package/dist/Form/Input/Input.d.ts +2 -2
  10. package/dist/Form/Radio/Radio.d.ts +1 -1
  11. package/dist/Form/Select/Select.d.ts +1 -1
  12. package/dist/Form/Textarea/Textarea.d.ts +1 -6
  13. package/dist/Form/Toggle/Toggle.d.ts +1 -1
  14. package/dist/Form/Wrapper/CheckboxWrapper/CheckboxWrapper.d.ts +1 -1
  15. package/dist/Form/Wrapper/InputWrapper/InputWrapper.d.ts +1 -1
  16. package/dist/Form/Wrapper/RadioWrapper/RadioWrapper.d.ts +1 -1
  17. package/dist/Form/Wrapper/SelectWrapper/SelectWrapper.d.ts +1 -1
  18. package/dist/Form/Wrapper/TextareaWrapper/TextareaWrapper.d.ts +1 -1
  19. package/dist/Form/form.interfaces.d.ts +1 -0
  20. package/dist/Icon/Icon.d.ts +4 -1
  21. package/dist/Link/Link.d.ts +1 -2
  22. package/dist/Notifications/Banner/Banner.d.ts +11 -0
  23. package/dist/ProgressBar/ProgressBar.d.ts +2 -1
  24. package/dist/Tabs/TabButton.d.ts +0 -1
  25. package/dist/_BaseStyling_/BaseStyling.d.ts +5 -0
  26. package/dist/hooks/useDetermineStatusIcon.d.ts +3 -0
  27. package/dist/hooks/useUploadFile.d.ts +22 -0
  28. package/dist/index.d.ts +1 -0
  29. package/dist/react-lib-components.cjs.development.js +431 -326
  30. package/dist/react-lib-components.cjs.development.js.map +1 -1
  31. package/dist/react-lib-components.cjs.production.min.js +1 -1
  32. package/dist/react-lib-components.cjs.production.min.js.map +1 -1
  33. package/dist/react-lib-components.esm.js +431 -327
  34. package/dist/react-lib-components.esm.js.map +1 -1
  35. package/dist/util/helper.d.ts +5 -0
  36. package/package.json +28 -25
  37. package/src/Button/BaseButton.module.scss +2 -2
  38. package/src/Button/Button.module.scss +4 -5
  39. package/src/Button/Button.tsx +0 -1
  40. package/src/Button/IconButton.module.scss +4 -5
  41. package/src/DataGrid/DataGrid.tsx +3 -2
  42. package/src/DataGrid/DataGridActions/DataGridActions.tsx +16 -9
  43. package/src/DataGrid/DataGridBody/DataGridCell.module.scss +2 -2
  44. package/src/DataGrid/DataGridHeader/DataGridHeader.test.tsx +8 -3
  45. package/src/DataGrid/DataGridHeader/DataGridHeader.tsx +3 -1
  46. package/src/DataGrid/datagrid.interfaces.ts +1 -0
  47. package/src/Form/FileUpload/FileItem/FileItem.modules.scss +75 -0
  48. package/src/Form/FileUpload/FileItem/FileItem.test.tsx +103 -0
  49. package/src/Form/FileUpload/FileItem/FileItem.tsx +141 -0
  50. package/src/Form/FileUpload/FileUpload.module.scss +106 -0
  51. package/src/Form/FileUpload/FileUpload.test.tsx +374 -0
  52. package/src/Form/FileUpload/FileUpload.tsx +251 -0
  53. package/src/Form/Input/Input.module.scss +36 -26
  54. package/src/Form/Input/Input.test.tsx +10 -0
  55. package/src/Form/Input/Input.tsx +7 -5
  56. package/src/Form/Select/Select.module.scss +9 -6
  57. package/src/Form/Select/Select.test.tsx +11 -0
  58. package/src/Form/Select/Select.tsx +5 -9
  59. package/src/Form/Select/SelectService.ts +2 -2
  60. package/src/Form/Textarea/Textarea.module.scss +21 -13
  61. package/src/Form/Textarea/Textarea.test.tsx +8 -0
  62. package/src/Form/Textarea/Textarea.tsx +6 -12
  63. package/src/Form/Toggle/Toggle.module.scss +3 -3
  64. package/src/Form/Wrapper/InputWrapper/InputWrapper.module.scss +7 -3
  65. package/src/Form/Wrapper/InputWrapper/InputWrapper.tsx +2 -0
  66. package/src/Form/Wrapper/SelectWrapper/SelectWrapper.tsx +12 -1
  67. package/src/Form/Wrapper/TextareaWrapper/TextareaWrapper.module.scss +15 -14
  68. package/src/Form/Wrapper/TextareaWrapper/TextareaWrapper.tsx +2 -1
  69. package/src/Form/Wrapper/Wrapper/Wrapper.module.scss +2 -2
  70. package/src/Form/form.interfaces.ts +1 -0
  71. package/src/Icon/Icon.module.scss +12 -0
  72. package/src/Icon/Icon.tsx +4 -1
  73. package/src/Link/Link.module.scss +5 -5
  74. package/src/Link/Link.tsx +14 -13
  75. package/src/Notifications/Banner/Banner.module.scss +76 -0
  76. package/src/Notifications/Banner/Banner.test.tsx +84 -0
  77. package/src/Notifications/Banner/Banner.tsx +78 -0
  78. package/src/Notifications/BaseModal/BaseModal.module.scss +2 -2
  79. package/src/Notifications/Snackbar/SnackbarContainer/SnackbarContainer.module.scss +2 -2
  80. package/src/Notifications/Snackbar/SnackbarItem/SnackbarItem.module.scss +4 -4
  81. package/src/Notifications/Snackbar/SnackbarItem/SnackbarItem.tsx +3 -2
  82. package/src/Popover/Popover.module.scss +2 -2
  83. package/src/ProgressBar/ProgressBar.module.scss +11 -9
  84. package/src/ProgressBar/ProgressBar.test.tsx +21 -0
  85. package/src/ProgressBar/ProgressBar.tsx +7 -2
  86. package/src/Skeleton/Skeleton.module.scss +2 -2
  87. package/src/Tabs/TabButton.tsx +1 -2
  88. package/src/Tabs/Tabs.module.scss +2 -2
  89. package/src/Tabs/Tabs.tsx +13 -10
  90. package/src/Tiles/Tile.module.scss +4 -4
  91. package/src/Tooltip/Tooltip.module.scss +3 -3
  92. package/src/Typography/Typography.module.scss +2 -2
  93. package/src/_BaseStyling_/BaseStyling.tsx +13 -3
  94. package/src/hooks/useDetermineStatusIcon.test.ts +28 -0
  95. package/src/hooks/useDetermineStatusIcon.tsx +35 -0
  96. package/src/hooks/useUploadFile.test.ts +211 -0
  97. package/src/hooks/useUploadFile.tsx +136 -0
  98. package/src/index.ts +1 -0
  99. package/src/mixins.module.scss +24 -5
  100. package/src/util/helper.test.tsx +156 -1
  101. package/src/util/helper.tsx +33 -0
@@ -96,9 +96,10 @@ export const SnackbarItem = ({
96
96
  onClose(id);
97
97
  actionProp.onClick && actionProp.onClick(e);
98
98
  }}
99
- children={actionProp.label}
100
99
  className={classes["action-button"]}
101
- ></button>
100
+ >
101
+ {actionProp.label}
102
+ </button>
102
103
  ));
103
104
 
104
105
  return (
@@ -14,7 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- @import "../mixins.module.scss";
17
+ @use "../mixins.module.scss";
18
18
 
19
19
  .popover {
20
20
  $transition-property: transform, opacity;
@@ -28,7 +28,7 @@
28
28
  opacity: 0;
29
29
  transform: scale(0.5);
30
30
 
31
- @include transition($transition-property, 0.2s, ease-in-out);
31
+ @include mixins.transition($transition-property, 0.2s, ease-in-out);
32
32
 
33
33
  &.show {
34
34
  transform: scale(1);
@@ -13,6 +13,7 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
+ @import "../mixins.module.scss";
16
17
 
17
18
  .progress-bar {
18
19
  position: relative;
@@ -30,18 +31,19 @@
30
31
  .bar {
31
32
  position: absolute;
32
33
  height: 0.5rem;
33
- width: 50%;
34
34
  border-radius: 0.25rem;
35
35
  background-color: var(--color-primary);
36
- animation: cubic-bezier(0.23, 0.78, 0.78, 0.23) 1s slide-in infinite;
36
+ &.loading-state {
37
+ width: 50%;
38
+ animation: cubic-bezier(0.23, 0.78, 0.78, 0.23) 1s slide-in infinite;
39
+ @keyframes slide-in {
40
+ 0% {
41
+ left: -50%;
42
+ }
37
43
 
38
- @keyframes slide-in {
39
- 0% {
40
- left: -50%;
41
- }
42
-
43
- 100% {
44
- left: 100%;
44
+ 100% {
45
+ left: 100%;
46
+ }
45
47
  }
46
48
  }
47
49
  }
@@ -56,3 +56,24 @@ describe("ref should work", () => {
56
56
  render(<ExampleComponent propagateRef={refCheck} />, { container });
57
57
  });
58
58
  });
59
+
60
+ describe("ProgressBar should change styles depending on props", () => {
61
+ it("should show a progress when 'completed' prop is provided", () => {
62
+ const { ProgressBarComponent } = createProgressBar(defaultParams => ({
63
+ ...defaultParams,
64
+ completed: 40
65
+ }));
66
+
67
+ const bar = ProgressBarComponent.querySelector(".bar");
68
+ expect(bar).toHaveClass("w-40");
69
+ });
70
+
71
+ it("should show a loading effect when 'completed' prop is not provided", () => {
72
+ const { ProgressBarComponent } = createProgressBar(defaultParams => ({
73
+ ...defaultParams
74
+ }));
75
+
76
+ const bar = ProgressBarComponent.querySelector(".bar");
77
+ expect(bar).toHaveClass("loading-state");
78
+ });
79
+ });
@@ -20,16 +20,21 @@ import classes from "./ProgressBar.module.scss";
20
20
 
21
21
  export interface Props extends Omit<ComponentPropsWithRef<"span">, "children"> {
22
22
  placeholderText: string;
23
+ completed?: number;
23
24
  }
24
25
 
25
26
  const ProgressBarComponent: ForwardRefRenderFunction<HTMLSpanElement, Props> = (
26
- { placeholderText, ...rest }: Props,
27
+ { placeholderText, completed, ...rest }: Props,
27
28
  ref
28
29
  ) => {
29
30
  return (
30
31
  <span {...rest} ref={ref} role="progressbar">
31
32
  <span className={classes["progress-bar"]}>
32
- <span className={classes["bar"]} />
33
+ <span
34
+ className={`${classes["bar"]} ${
35
+ completed ? classes[`w-${5 * Math.round(completed / 5)}`] : classes["loading-state"]
36
+ }`}
37
+ />
33
38
  </span>
34
39
  <Typography className={classes["placeholder"]} spacing={{ marginBottom: 0 }} variant="body">
35
40
  {placeholderText}
@@ -14,12 +14,12 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- @import "../mixins.module.scss";
17
+ @use "../mixins.module.scss";
18
18
 
19
19
  .skeleton {
20
20
  display: block;
21
21
  height: auto;
22
- @include skeletonLoading();
22
+ @include mixins.skeletonLoading();
23
23
  }
24
24
 
25
25
  .no-height::before {
@@ -24,13 +24,12 @@ import React, {
24
24
  import classes from "./TabButton.module.scss";
25
25
 
26
26
  export interface Props extends ComponentPropsWithRef<"button"> {
27
- children?: string;
28
27
  tabActive?: boolean;
29
28
  focused?: boolean;
30
29
  }
31
30
 
32
31
  const TabButtonComponent: ForwardRefRenderFunction<HTMLButtonElement, Props> = (
33
- { children, tabActive, focused, ...rest }: Props,
32
+ { children, tabActive, focused, title, ...rest }: Props,
34
33
  ref
35
34
  ) => {
36
35
  let buttonRef = (ref as RefObject<HTMLButtonElement>) || createRef<HTMLButtonElement>();
@@ -14,7 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- @import "../mixins.module.scss";
17
+ @use "../mixins.module.scss";
18
18
 
19
19
  $focus-width: 5px;
20
20
 
@@ -45,7 +45,7 @@ $focus-width: 5px;
45
45
  height: var(--tab-active-border-height);
46
46
  background-color: var(--tab-active-border-color);
47
47
 
48
- @include transition($transition-property, 0.2s, ease-in-out);
48
+ @include mixins.transition($transition-property, 0.2s, ease-in-out);
49
49
  }
50
50
 
51
51
  .tabdivider {
package/src/Tabs/Tabs.tsx CHANGED
@@ -111,16 +111,19 @@ const TabsComponent: ForwardRefRenderFunction<HTMLDivElement, Props> = (
111
111
  useEffect(() => {
112
112
  const buttons = React.Children.map(children, (child, index) => {
113
113
  if (Object.prototype.hasOwnProperty.call(child.props, "title")) {
114
- return React.createElement(TabButton, {
115
- key: `${child.props.title.toLowerCase().replace(/\s/, "_")}_button`,
116
- tabIndex: activeTabIndex === index ? 0 : -1,
117
- "aria-selected": activeTabIndex === index,
118
- focused: usingKeyboardNavigation && activeTabIndex === index,
119
- tabActive: activeTabIndex === index,
120
- "aria-controls": `tab_${index}`,
121
- onClick: () => setActiveTabIndex(index),
122
- children: child.props.title
123
- });
114
+ return React.createElement(
115
+ TabButton,
116
+ {
117
+ key: `${child.props.title.toLowerCase().replace(/\s/, "_")}_button`,
118
+ tabIndex: activeTabIndex === index ? 0 : -1,
119
+ "aria-selected": activeTabIndex === index,
120
+ focused: usingKeyboardNavigation && activeTabIndex === index,
121
+ tabActive: activeTabIndex === index,
122
+ "aria-controls": `tab_${index}`,
123
+ onClick: () => setActiveTabIndex(index)
124
+ },
125
+ child.props.title
126
+ );
124
127
  }
125
128
  return null;
126
129
  });
@@ -14,7 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- @import "../mixins.module.scss";
17
+ @use "../mixins.module.scss";
18
18
 
19
19
  .tile {
20
20
  border: 1px solid var(--light-grey-border);
@@ -22,7 +22,7 @@
22
22
  box-sizing: border-box;
23
23
  padding: 0 0 2rem;
24
24
  background-color: var(--light);
25
- @include transition(box-shadow, 0.2s, ease-in-out);
25
+ @include mixins.transition(box-shadow, 0.2s, ease-in-out);
26
26
  font-family: var(--font-family);
27
27
 
28
28
  &:hover {
@@ -71,14 +71,14 @@
71
71
 
72
72
  .image {
73
73
  margin-top: 2.5rem;
74
- @include skeletonLoading();
74
+ @include mixins.skeletonLoading();
75
75
  border-radius: 0.5rem;
76
76
  width: 3rem;
77
77
  height: 3rem;
78
78
  }
79
79
 
80
80
  .title {
81
- @include skeletonLoading();
81
+ @include mixins.skeletonLoading();
82
82
  color: transparent;
83
83
  display: inline-block;
84
84
  width: 70%;
@@ -14,7 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- @import "../mixins.module.scss";
17
+ @use "../mixins.module.scss";
18
18
 
19
19
  .wrapper {
20
20
  display: flex;
@@ -24,7 +24,7 @@
24
24
  .tooltip-wrapper {
25
25
  .icon {
26
26
  margin-left: 0.5rem;
27
- @include transition(all, 0.2s, ease-in-out);
27
+ @include mixins.transition(all, 0.2s, ease-in-out);
28
28
  color: var(--greyed-out);
29
29
 
30
30
  &:hover {
@@ -46,7 +46,7 @@
46
46
  transform-origin: center left;
47
47
  pointer-events: none;
48
48
 
49
- @include transition($transition-property, 0.2s, ease-in-out);
49
+ @include mixins.transition($transition-property, 0.2s, ease-in-out);
50
50
 
51
51
  &.visible {
52
52
  opacity: 1;
@@ -37,11 +37,11 @@
37
37
  }
38
38
 
39
39
  &h3 {
40
- @include fontProperties(var(--font-family), var(--font-size-h3), 400, 1.16666);
40
+ @include fontProperties(var(--font-family), var(--font-size-h3), 500, 1.16666);
41
41
  }
42
42
 
43
43
  &h4 {
44
- @include fontProperties(var(--font-family), var(--font-size-h4), 700, 1.2);
44
+ @include fontProperties(var(--font-family), var(--font-size-h4), 500, 1.2);
45
45
  }
46
46
 
47
47
  &body {
@@ -26,6 +26,8 @@ interface CSSProperties {
26
26
  colorPrimary?: string;
27
27
  colorSecondary?: string;
28
28
  colorTertiary?: string;
29
+ lightPink?: string;
30
+ vividViolet?: string;
29
31
  defaultLineHeight?: string;
30
32
  buttonBorderRadius?: string;
31
33
  buttonBorderWidth?: string;
@@ -39,6 +41,7 @@ interface CSSProperties {
39
41
  inputBorderWidthFocus?: string;
40
42
  inputBorderStyle?: string;
41
43
  inputBackgroundColor?: string;
44
+ dragBorderStyle?: string;
42
45
  modalShadowColor?: string;
43
46
  modalBackgroundColor?: string;
44
47
  modalHeaderBackgroundColor?: string;
@@ -49,6 +52,7 @@ interface CSSProperties {
49
52
  snackbarSuccessBackgroundColor?: string;
50
53
  snackbarErrorBackgroundColor?: string;
51
54
  snackbarBorderRadius?: string;
55
+ bannerBorderRadius?: string;
52
56
  dataGridRowBackgroundColor?: string;
53
57
  dataGridRowHoverBackgroundColor?: string;
54
58
  tabsBackgroundColor?: string;
@@ -61,6 +65,7 @@ interface CSSProperties {
61
65
  default?: string;
62
66
  success?: string;
63
67
  error?: string;
68
+ info?: string;
64
69
  disabled?: string;
65
70
  greyedOut?: string;
66
71
  lightGreyBorder?: string;
@@ -89,6 +94,8 @@ export const BaseStyling = ({ children, properties = {} }: Props) => {
89
94
  colorPrimary: "#9e006b",
90
95
  colorSecondary: "#003b5e",
91
96
  colorTertiary: "#ff1e4e",
97
+ lightPink: "#9E006B1A",
98
+ vividViolet: "#9E006B",
92
99
  defaultLineHeight: "26px",
93
100
  buttonBorderRadius: "20px",
94
101
  buttonBorderWidth: "2px",
@@ -112,6 +119,7 @@ export const BaseStyling = ({ children, properties = {} }: Props) => {
112
119
  snackbarSuccessBackgroundColor: "#008a28",
113
120
  snackbarErrorBackgroundColor: "#d9291c",
114
121
  snackbarBorderRadius: "8px",
122
+ bannerBorderRadius: "8px",
115
123
  dataGridRowBackgroundColor: "transparent",
116
124
  dataGridRowHoverBackgroundColor: "#f5e6f0",
117
125
  tabsBackgroundColor: "var(--light)",
@@ -124,21 +132,23 @@ export const BaseStyling = ({ children, properties = {} }: Props) => {
124
132
  default: "#0f0f1e",
125
133
  success: "#008a28",
126
134
  error: "#d9291c",
135
+ info: "var(--color-secondary)",
127
136
  disabled: "#e9e9eb",
128
137
  greyedOut: "#6f6f76",
129
138
  lightGreyBorder: "#e9e9eb",
130
139
  warning: "#ff6105",
131
140
  light: "#FFF",
132
141
  grey: "#c3c3c7",
133
- fontFamily: "'Red Hat Display', sans-serif",
134
- fontFamilyCode: "'Red Hat Mono', monospace",
142
+ fontFamily: "Roboto, sans-serif",
143
+ fontFamilyCode: "'Roboto Mono', monospace",
135
144
  fontSize: "1rem",
136
145
  fontSizeH1: "2.5rem",
137
146
  fontSizeH2: "1.625rem",
138
147
  fontSizeH3: "1.5rem",
139
148
  fontSizeH4: "1.25rem",
140
149
  fontSizeSub: ".75rem",
141
- fontSizeCode: "1rem"
150
+ fontSizeCode: "1rem",
151
+ dragBorderStyle: "dashed"
142
152
  };
143
153
 
144
154
  /** We need a loading state, because otherwise you see the colors flash from the default to the possible overridden ones. */
@@ -0,0 +1,28 @@
1
+ import { renderHook } from "@testing-library/react-hooks";
2
+ import { useDetermineStatusIcon } from "./useDetermineStatusIcon";
3
+
4
+ describe("it should return the correct icon", () => {
5
+ it("should return an icon of type success", () => {
6
+ const { result } = renderHook(useDetermineStatusIcon, {
7
+ initialProps: { success: true, error: false }
8
+ });
9
+
10
+ expect(result.current).toBeDefined();
11
+ expect(result.current?.props?.["data-icon-status"]).toEqual("success");
12
+ });
13
+
14
+ it("should return an icon of type error", () => {
15
+ const { result } = renderHook(useDetermineStatusIcon, {
16
+ initialProps: { success: true, error: true }
17
+ });
18
+
19
+ expect(result.current).toBeDefined();
20
+ expect(result.current?.props?.["data-icon-status"]).toEqual("error");
21
+ });
22
+
23
+ it("should return null", () => {
24
+ const { result } = renderHook(useDetermineStatusIcon);
25
+
26
+ expect(result.current).toBeNull();
27
+ });
28
+ });
@@ -0,0 +1,35 @@
1
+ /*
2
+ * Copyright 2022 OneWelcome B.V.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { FormElement } from "../Form/form.interfaces";
18
+ import React, { ReactElement, useRef } from "react";
19
+ import { Icon, Icons } from "../Icon/Icon";
20
+ export const useDetermineStatusIcon = (params: Partial<FormElement>): ReactElement | null => {
21
+ const { error, success } = params || false;
22
+ let icon = null;
23
+ const errorRef = useRef(null);
24
+ const successRef = useRef(null);
25
+
26
+ if (error) {
27
+ icon = <Icon ref={errorRef} data-icon-status="error" icon={Icons.Error} />;
28
+ } else if (success) {
29
+ icon = (
30
+ <Icon ref={successRef} data-icon-status="success" icon={Icons.CheckmarkCircleBreakout} />
31
+ );
32
+ }
33
+
34
+ return icon;
35
+ };
@@ -0,0 +1,211 @@
1
+ import { useUploadFile } from "./useUploadFile";
2
+ import { renderHook } from "@testing-library/react-hooks";
3
+ import { waitFor } from "@testing-library/react";
4
+ import { FileType } from "../Form/FileUpload/FileUpload";
5
+
6
+ const DONE = 4;
7
+
8
+ const mockXhrRequest = (status: number, readyState?: number, response?: Object) => {
9
+ return {
10
+ open: jest.fn(),
11
+ send: jest.fn(),
12
+ addEventListener: jest.fn(),
13
+ onprogress: jest.fn(),
14
+ responseText: response,
15
+ onreadystatechange: jest.fn(),
16
+ getResponseHeader: jest.fn(),
17
+ upload: {
18
+ addEventListener: jest.fn()
19
+ },
20
+ DONE,
21
+ setRequestHeader: jest.fn(),
22
+ readyState: readyState || 4,
23
+ status
24
+ };
25
+ };
26
+
27
+ type mockRequestParams = Parameters<typeof mockXhrRequest>;
28
+
29
+ const progressData = {
30
+ loaded: 12,
31
+ total: 100
32
+ };
33
+
34
+ const requestInfo = { url: "https://www.test.io", withCredentials: true };
35
+
36
+ const file = {
37
+ name: "test.txt",
38
+ data: new File([""], "test.txt"),
39
+ size: 5,
40
+ type: ""
41
+ };
42
+
43
+ const setupXhrEnvironment = (mockParams: mockRequestParams) => {
44
+ const mock = mockXhrRequest(...mockParams);
45
+ const mockClass = () => mock;
46
+
47
+ // @ts-ignore
48
+ window.XMLHttpRequest = jest.fn().mockImplementation(mockClass);
49
+
50
+ return mock;
51
+ };
52
+
53
+ const expectReadyStateInfo = async (
54
+ files: FileType[],
55
+ mock: ReturnType<typeof mockXhrRequest>,
56
+ response: { code: number; body: { message: string } }
57
+ ) => {
58
+ const { result } = renderHook(() =>
59
+ useUploadFile(files, { ...requestInfo, responseErrorPath: "body.message" })
60
+ );
61
+
62
+ expect(mock.addEventListener).toHaveBeenCalled();
63
+ const [[, readystatechange]] = mock.addEventListener.mock.calls;
64
+ await waitFor(() => readystatechange());
65
+ const currentFile = result.current.updatedFiles[0];
66
+ expect(currentFile.status).toEqual("error");
67
+ expect(currentFile.error).toEqual(response.body.message);
68
+ };
69
+
70
+ describe("it should perform upload", () => {
71
+ it("should register the correct progress", async () => {
72
+ const mock = setupXhrEnvironment([200]);
73
+ const { result } = renderHook(() => useUploadFile([file], requestInfo));
74
+ expect(result.current).toBeDefined();
75
+ expect(mock.upload.addEventListener).toHaveBeenCalled();
76
+ const [[, progress]] = mock.upload.addEventListener.mock.calls;
77
+ await waitFor(() => progress(progressData));
78
+
79
+ const targetFile = result.current.updatedFiles[0];
80
+ expect(targetFile.name).toEqual(file.name);
81
+ expect(targetFile.progress).toEqual(12);
82
+ });
83
+
84
+ it("should contain a file with status of retry", async () => {
85
+ const mock = setupXhrEnvironment([0, DONE]);
86
+ const files2 = [
87
+ {
88
+ name: "test2.txt",
89
+ data: new File([""], "test2.txt"),
90
+ size: 5,
91
+ type: ""
92
+ }
93
+ ];
94
+ const { result } = renderHook(() => useUploadFile(files2, requestInfo));
95
+ expect(result.current).toBeDefined();
96
+ expect(mock.addEventListener).toHaveBeenCalled();
97
+ const [[, readystatechange]] = mock.addEventListener.mock.calls;
98
+ await waitFor(() => readystatechange());
99
+ expect(result.current.updatedFiles[0].status).toEqual("retry");
100
+ });
101
+
102
+ it("should contain a file with status of error", async () => {
103
+ const response = { code: 404, body: { message: "Error test" } };
104
+ const mock = setupXhrEnvironment([404, DONE, response]);
105
+ const files3 = [
106
+ {
107
+ name: "test3.txt",
108
+ data: new File([""], "test3.txt"),
109
+ size: 5,
110
+ type: ""
111
+ }
112
+ ];
113
+ await expectReadyStateInfo(files3, mock, response);
114
+ });
115
+
116
+ it("should contain a file with status of server error", async () => {
117
+ const response = { code: 500, body: { message: "Error test" } };
118
+ const mock = setupXhrEnvironment([500, DONE, response]);
119
+ const files6 = [
120
+ {
121
+ name: "test6.txt",
122
+ data: new File([""], "test3.txt"),
123
+ size: 5,
124
+ type: ""
125
+ }
126
+ ];
127
+ await expectReadyStateInfo(files6, mock, response);
128
+ });
129
+
130
+ it("should contain a file with status of success", async () => {
131
+ const mock = setupXhrEnvironment([200, DONE]);
132
+ const files3 = [
133
+ {
134
+ name: "test3.txt",
135
+ data: new File([""], "test3.txt"),
136
+ size: 5,
137
+ type: ""
138
+ }
139
+ ];
140
+ const { result } = renderHook(() => useUploadFile(files3, requestInfo));
141
+ expect(result.current).toBeDefined();
142
+ expect(mock.addEventListener).toHaveBeenCalled();
143
+ const [[, readystatechange]] = mock.addEventListener.mock.calls;
144
+ await waitFor(() => readystatechange());
145
+ const currentFile = result.current.updatedFiles[0];
146
+ expect(currentFile.status).toEqual("completed");
147
+ });
148
+ });
149
+
150
+ describe("should return data according to the parameters", () => {
151
+ it("should call custom callbacks", async () => {
152
+ const onComplete = jest.fn();
153
+ const onProgress = jest.fn();
154
+ const mock = setupXhrEnvironment([200, DONE]);
155
+ const files4 = [
156
+ {
157
+ name: "test4.txt",
158
+ data: new File([""], "test3.txt"),
159
+ size: 5,
160
+ type: ""
161
+ }
162
+ ];
163
+ renderHook(() => useUploadFile(files4, requestInfo, { onProgress, onComplete }));
164
+
165
+ const [[, progress]] = mock.upload.addEventListener.mock.calls;
166
+ await waitFor(() => progress(progressData));
167
+
168
+ const [[, readystatechange]] = mock.addEventListener.mock.calls;
169
+ await waitFor(() => readystatechange());
170
+
171
+ expect(mock.addEventListener).toHaveBeenCalled();
172
+ expect(onComplete).toHaveBeenCalled();
173
+ expect(onComplete).toHaveBeenCalled();
174
+ });
175
+
176
+ it("should add the correct error message", async () => {
177
+ const mock = setupXhrEnvironment([0, DONE]);
178
+ const files5 = [
179
+ {
180
+ name: "test5.txt",
181
+ data: new File([""], "test5.txt"),
182
+ size: 5,
183
+ type: ""
184
+ }
185
+ ];
186
+ const error = "Network error";
187
+ const { result } = renderHook(() =>
188
+ useUploadFile(files5, { ...requestInfo, networkErrorText: error })
189
+ );
190
+ const [[, readystatechange]] = mock.addEventListener.mock.calls;
191
+ await waitFor(() => readystatechange());
192
+
193
+ expect(mock.addEventListener).toHaveBeenCalled();
194
+ const file = result.current.updatedFiles[0];
195
+ expect(file.error).toEqual(error);
196
+ });
197
+ });
198
+
199
+ describe("useFileUpload hook should not fire when url and array of files is not present", () => {
200
+ it("should return an empty array if url is empty", () => {
201
+ const { result } = renderHook(() => useUploadFile([], { url: "https://yoohoo.co" }));
202
+
203
+ expect(result.current.uploadingFiles.length).toBe(0);
204
+ });
205
+
206
+ it("should return an empty array if url is empty", () => {
207
+ const { result } = renderHook(() => useUploadFile([file], { url: "" }));
208
+
209
+ expect(result.current.uploadingFiles.length).toBe(0);
210
+ });
211
+ });