@onewelcome/react-lib-components 1.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onewelcome/react-lib-components",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "license": "Apache-2.0",
5
5
  "author": "OneWelcome B.V.",
6
6
  "main": "dist/index.js",
@@ -42,4 +42,13 @@
42
42
  padding-left: 1.25rem;
43
43
  padding-right: 1rem;
44
44
  }
45
+
46
+ i {
47
+ display: flex;
48
+ align-items: center;
49
+ justify-content: center;
50
+
51
+ width: 1.5rem;
52
+ margin-right: 0.25rem;
53
+ }
45
54
  }
@@ -62,7 +62,7 @@ export const Button = React.forwardRef<HTMLButtonElement, Props>(
62
62
  ref={ref}
63
63
  className={`${classes[color]} ${classes[variant]} ${additionalClasses.join(" ")}`}
64
64
  >
65
- {startIcon && <i>{startIcon}&nbsp;</i>}
65
+ {startIcon && <i>{startIcon}</i>}
66
66
  <span>{children}</span>
67
67
  {endIcon && <i>&nbsp;{endIcon}</i>}
68
68
  </BaseButton>
@@ -42,6 +42,7 @@
42
42
 
43
43
  .icon {
44
44
  line-height: 0;
45
+ font-size: 0.875rem;
45
46
  }
46
47
 
47
48
  .icon-image:before {
@@ -0,0 +1,140 @@
1
+ import { renderHook, act } from "@testing-library/react-hooks";
2
+ import { useDebouncedCallback } from "./useDebouncedCallback";
3
+
4
+ const BASE_DELAY = 300;
5
+ const EXTRA_DELAY = 200;
6
+
7
+ const sleep = (delay: number) => new Promise(resolve => setTimeout(resolve, delay));
8
+
9
+ describe("Testing the useDebouncedCallback hook", () => {
10
+ it("should run callback after specified delay", async () => {
11
+ const delay = BASE_DELAY;
12
+ const callback = jest.fn();
13
+
14
+ const { result } = renderHook(() => useDebouncedCallback(callback, delay));
15
+
16
+ expect(callback).toHaveBeenCalledTimes(0);
17
+ act(result.current);
18
+ await sleep(delay + EXTRA_DELAY);
19
+
20
+ expect(callback).toHaveBeenCalledTimes(1);
21
+
22
+ act(result.current);
23
+ await sleep(delay + EXTRA_DELAY);
24
+
25
+ expect(callback).toHaveBeenCalledTimes(2);
26
+ });
27
+
28
+ it("should pass args through to callback function", async () => {
29
+ let mutableValue = "";
30
+ const delay = BASE_DELAY;
31
+ const callback = jest.fn(arg => (mutableValue = `got arg ${arg}`));
32
+
33
+ const { result } = renderHook(() => useDebouncedCallback(callback, delay));
34
+
35
+ act(() => result.current("passthrough"));
36
+ await sleep(delay + EXTRA_DELAY);
37
+
38
+ expect(callback).toHaveBeenCalledTimes(1);
39
+ expect(mutableValue).toBe(`got arg passthrough`);
40
+ });
41
+
42
+ it("should cancel pending call if fn is called again", async () => {
43
+ const delay = BASE_DELAY;
44
+ const callback = jest.fn();
45
+
46
+ const { result } = renderHook(() => useDebouncedCallback(callback, delay));
47
+
48
+ expect(callback).toHaveBeenCalledTimes(0);
49
+ act(result.current);
50
+ await sleep(delay / 2);
51
+
52
+ act(result.current);
53
+ await sleep(delay + EXTRA_DELAY);
54
+
55
+ expect(callback).toHaveBeenCalledTimes(1);
56
+ });
57
+
58
+ it("should cancel pending call if callback changes", async () => {
59
+ const delay = BASE_DELAY;
60
+ const callback1 = jest.fn();
61
+ const callback2 = jest.fn();
62
+
63
+ const { result, rerender } = renderHook(
64
+ ({ callback, delay }) => useDebouncedCallback(callback, delay),
65
+ { initialProps: { callback: callback1, delay } }
66
+ );
67
+
68
+ act(result.current);
69
+ await sleep(delay / 2);
70
+
71
+ rerender({ callback: callback2, delay });
72
+ act(result.current);
73
+ await sleep(delay + EXTRA_DELAY);
74
+
75
+ expect(callback1).toHaveBeenCalledTimes(0);
76
+ expect(callback2).toHaveBeenCalledTimes(1);
77
+ });
78
+
79
+ it("should cancel pending call if delay changes", async () => {
80
+ const delay1 = BASE_DELAY;
81
+ const delay2 = BASE_DELAY * 2;
82
+ const callback = jest.fn();
83
+
84
+ const { result, rerender } = renderHook(
85
+ ({ callback, delay }) => useDebouncedCallback(callback, delay),
86
+ { initialProps: { callback, delay: delay1 } }
87
+ );
88
+
89
+ act(result.current);
90
+ await sleep(delay1 / 2);
91
+
92
+ rerender({ callback, delay: delay2 });
93
+ act(result.current);
94
+ await sleep(delay2 + EXTRA_DELAY);
95
+
96
+ expect(callback).toHaveBeenCalledTimes(1);
97
+ });
98
+
99
+ it("should cancel pending call if dependencies change", async () => {
100
+ const delay = BASE_DELAY;
101
+ const callback = jest.fn();
102
+
103
+ const { result, rerender } = renderHook(
104
+ ({ callback, delay, dependencies }) => useDebouncedCallback(callback, delay, dependencies),
105
+ { initialProps: { callback, delay, dependencies: ["a"] } }
106
+ );
107
+
108
+ act(result.current);
109
+ await sleep(delay / 2);
110
+
111
+ rerender({ callback, delay, dependencies: ["b"] });
112
+ act(result.current);
113
+ await sleep(delay + EXTRA_DELAY);
114
+
115
+ expect(callback).toHaveBeenCalledTimes(1);
116
+ });
117
+
118
+ it("should support async callbacks", async () => {
119
+ const makeAsyncCall = jest.fn(
120
+ () =>
121
+ new Promise(async resolve => {
122
+ await sleep(delay / 2);
123
+ resolve(null);
124
+ })
125
+ );
126
+
127
+ const delay = BASE_DELAY;
128
+ const callback = jest.fn(async () => {
129
+ await makeAsyncCall();
130
+ });
131
+
132
+ const { result } = renderHook(() => useDebouncedCallback(callback, delay));
133
+
134
+ await act(async () => result.current());
135
+ await sleep(delay + EXTRA_DELAY);
136
+
137
+ expect(callback).toHaveBeenCalledTimes(1);
138
+ expect(makeAsyncCall).toHaveBeenCalledTimes(1);
139
+ });
140
+ });
@@ -0,0 +1,32 @@
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 React from "react";
18
+
19
+ export const useDebouncedCallback = (callback: Function, delay: number, dependencies?: any[]) => {
20
+ const timeout = React.useRef<ReturnType<typeof setTimeout>>();
21
+ const comboDeps = dependencies ? [callback, delay, ...dependencies] : [callback, delay];
22
+
23
+ return React.useCallback((...args) => {
24
+ if (timeout.current != null) {
25
+ clearTimeout(timeout.current);
26
+ }
27
+
28
+ timeout.current = setTimeout(() => {
29
+ callback(...args);
30
+ }, delay);
31
+ }, comboDeps);
32
+ };
package/src/index.ts CHANGED
@@ -43,6 +43,7 @@ export {
43
43
 
44
44
  /* Utils */
45
45
  export { useRepeater } from "./hooks/useRepeater";
46
+ export { useDebouncedCallback } from "./hooks/useDebouncedCallback";
46
47
  export { generateID, debounce, throttle } from "./util/helper";
47
48
 
48
49
  /* Notifications */