@onewelcome/react-lib-components 1.1.0 → 1.3.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 (37) hide show
  1. package/dist/hooks/useDebouncedCallback.d.ts +1 -0
  2. package/dist/index.d.ts +2 -1
  3. package/dist/react-lib-components.cjs.development.js +107 -52
  4. package/dist/react-lib-components.cjs.development.js.map +1 -1
  5. package/dist/react-lib-components.cjs.production.min.js +1 -1
  6. package/dist/react-lib-components.cjs.production.min.js.map +1 -1
  7. package/dist/react-lib-components.esm.js +105 -53
  8. package/dist/react-lib-components.esm.js.map +1 -1
  9. package/dist/util/helper.d.ts +2 -0
  10. package/package.json +1 -1
  11. package/src/Button/Button.module.scss +9 -0
  12. package/src/Button/Button.tsx +1 -1
  13. package/src/Form/Input/Input.module.scss +5 -5
  14. package/src/Form/Select/Select.module.scss +3 -2
  15. package/src/Form/Textarea/Textarea.module.scss +1 -1
  16. package/src/Form/Toggle/Toggle.module.scss +4 -2
  17. package/src/Form/Wrapper/InputWrapper/InputWrapper.module.scss +1 -1
  18. package/src/Form/Wrapper/TextareaWrapper/TextareaWrapper.module.scss +1 -1
  19. package/src/Form/Wrapper/Wrapper/Wrapper.module.scss +3 -1
  20. package/src/Icon/Icon.module.scss +1 -0
  21. package/src/Notifications/BaseModal/BaseModal.module.scss +3 -1
  22. package/src/Notifications/SlideInModal/SlideInModal.module.scss +3 -1
  23. package/src/Notifications/Snackbar/SnackbarContainer/SnackbarContainer.module.scss +2 -1
  24. package/src/Notifications/Snackbar/SnackbarItem/SnackbarItem.module.scss +3 -3
  25. package/src/Popover/Popover.module.scss +7 -4
  26. package/src/Tabs/Tabs.module.scss +6 -3
  27. package/src/TextEllipsis/TextEllipsis.module.scss +1 -1
  28. package/src/Tiles/Tile.module.scss +1 -1
  29. package/src/Tooltip/Tooltip.module.scss +7 -4
  30. package/src/_BaseStyling_/BaseStyling.tsx +0 -3
  31. package/src/hooks/useDebouncedCallback.test.ts +140 -0
  32. package/src/hooks/useDebouncedCallback.tsx +32 -0
  33. package/src/index.ts +2 -1
  34. package/src/mixins.module.scss +21 -5
  35. package/src/readyclasses.module.scss +8 -0
  36. package/src/util/helper.test.tsx +128 -1
  37. package/src/util/helper.tsx +28 -0
@@ -14,7 +14,10 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- import { generateID, filterProps } from "./helper";
17
+ import React, { useCallback, useEffect, useState } from "react";
18
+ import { fireEvent, waitFor } from "@testing-library/dom";
19
+ import { generateID, filterProps, debounce, throttle } from "./helper";
20
+ import { render } from "@testing-library/react";
18
21
 
19
22
  /* Generate an ID of 20 characters with a string woven in */
20
23
 
@@ -66,3 +69,127 @@ describe("filterprops should return the correct object", () => {
66
69
  });
67
70
  });
68
71
  });
72
+
73
+ describe("debounce function", () => {
74
+ it("debounced the resizing of the window, it will only execute 1 time", async () => {
75
+ const debouncedFunction = jest.fn();
76
+
77
+ window.addEventListener("resize", debounce(debouncedFunction, 200));
78
+
79
+ await fireEvent.resize(window);
80
+ await fireEvent.resize(window);
81
+ await fireEvent.resize(window);
82
+ await fireEvent.resize(window);
83
+ await fireEvent.resize(window);
84
+
85
+ await waitFor(() => expect(debouncedFunction).toHaveBeenCalledTimes(1));
86
+ });
87
+
88
+ it("Works in a react component as well, it should only fire the exampleFunction once.", async () => {
89
+ const ExampleComponent = ({
90
+ debouncedFunction
91
+ }: {
92
+ debouncedFunction: (...args: unknown[]) => unknown;
93
+ }) => {
94
+ const [variable, setVariable] = useState(0);
95
+
96
+ useEffect(() => {
97
+ debouncedFunction(variable);
98
+ }, [variable]);
99
+
100
+ const incrementVariable = () => {
101
+ setVariable(Math.random());
102
+ };
103
+
104
+ useEffect(() => {
105
+ window.addEventListener("resize", debounce(incrementVariable, 200));
106
+ }, []);
107
+
108
+ return null;
109
+ };
110
+
111
+ const exampleFunction = jest.fn();
112
+
113
+ render(<ExampleComponent debouncedFunction={exampleFunction} />);
114
+
115
+ await fireEvent.resize(window);
116
+ await fireEvent.resize(window);
117
+ await fireEvent.resize(window);
118
+ await fireEvent.resize(window);
119
+ await fireEvent.resize(window);
120
+
121
+ expect(exampleFunction).toHaveBeenCalledTimes(1);
122
+ });
123
+ });
124
+
125
+ describe("throttling works", () => {
126
+ it("throttles the function", async () => {
127
+ const throttledFunction = jest.fn();
128
+
129
+ window.addEventListener("resize", throttle(throttledFunction, 1));
130
+
131
+ await fireEvent.resize(window);
132
+ await fireEvent.resize(window);
133
+ await fireEvent.resize(window);
134
+ await fireEvent.resize(window);
135
+ await fireEvent.resize(window);
136
+ await fireEvent.resize(window);
137
+ await fireEvent.resize(window);
138
+ await fireEvent.resize(window);
139
+ await fireEvent.resize(window);
140
+ await fireEvent.resize(window);
141
+ await fireEvent.resize(window);
142
+ await fireEvent.resize(window);
143
+ await fireEvent.resize(window);
144
+ await fireEvent.resize(window);
145
+ await fireEvent.resize(window);
146
+ await fireEvent.resize(window);
147
+ await fireEvent.resize(window);
148
+ await fireEvent.resize(window);
149
+
150
+ expect(throttledFunction).not.toHaveBeenCalledTimes(1);
151
+ expect(throttledFunction).not.toHaveBeenCalledTimes(10);
152
+ });
153
+
154
+ it("Works in a react component as well, it should only fire the exampleFunction once.", async () => {
155
+ const ExampleComponent = ({
156
+ throttledFunction
157
+ }: {
158
+ throttledFunction: (...args: unknown[]) => unknown;
159
+ }) => {
160
+ const [variable, setVariable] = useState(0);
161
+
162
+ useEffect(() => {
163
+ throttledFunction(variable);
164
+ }, [variable]);
165
+
166
+ const incrementVariable = useCallback(() => {
167
+ setVariable(Math.random());
168
+ }, []);
169
+
170
+ useEffect(() => {
171
+ window.addEventListener("resize", throttle(incrementVariable, 1));
172
+ }, []);
173
+
174
+ return null;
175
+ };
176
+
177
+ const exampleFunction = jest.fn();
178
+
179
+ render(<ExampleComponent throttledFunction={exampleFunction} />);
180
+
181
+ await fireEvent.resize(window);
182
+ await fireEvent.resize(window);
183
+ await fireEvent.resize(window);
184
+ await fireEvent.resize(window);
185
+ await fireEvent.resize(window);
186
+ await fireEvent.resize(window);
187
+ await fireEvent.resize(window);
188
+ await fireEvent.resize(window);
189
+ await fireEvent.resize(window);
190
+ await fireEvent.resize(window);
191
+
192
+ expect(exampleFunction).not.toHaveBeenCalledTimes(1);
193
+ expect(exampleFunction).not.toHaveBeenCalledTimes(10);
194
+ });
195
+ });
@@ -115,3 +115,31 @@ export const filterProps = (props: any, regexPattern: RegExp, returnFiltered: bo
115
115
  );
116
116
  }
117
117
  };
118
+
119
+ export const debounce = (fn: (...args: unknown[]) => unknown, delay: number) => {
120
+ let timeout: undefined | ReturnType<typeof setTimeout>;
121
+
122
+ return function executedFunction(...args: unknown[]) {
123
+ const later = () => {
124
+ clearTimeout(timeout);
125
+ fn(...args);
126
+ };
127
+
128
+ clearTimeout(timeout);
129
+
130
+ timeout = setTimeout(later, delay);
131
+ };
132
+ };
133
+
134
+ export const throttle = (fn: (...args: unknown[]) => unknown, delay: number) => {
135
+ let lastTime = 0;
136
+
137
+ return function () {
138
+ let now = Date.now();
139
+
140
+ if (now - lastTime >= delay) {
141
+ fn();
142
+ lastTime = now;
143
+ }
144
+ };
145
+ };