@dr.pogodin/react-utils 1.33.3 → 1.34.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/build/development/shared/components/Button/index.js +0 -38
- package/build/development/shared/components/Button/index.js.map +1 -1
- package/build/development/shared/components/Checkbox/index.js +0 -5
- package/build/development/shared/components/Checkbox/index.js.map +1 -1
- package/build/development/shared/components/GenericLink/index.js +0 -12
- package/build/development/shared/components/GenericLink/index.js.map +1 -1
- package/build/development/shared/components/Input/index.js +0 -3
- package/build/development/shared/components/Input/index.js.map +1 -1
- package/build/development/shared/components/MetaTags.js +0 -8
- package/build/development/shared/components/MetaTags.js.map +1 -1
- package/build/development/shared/components/Modal/index.js +0 -8
- package/build/development/shared/components/Modal/index.js.map +1 -1
- package/build/development/shared/components/PageLayout/index.js +0 -5
- package/build/development/shared/components/PageLayout/index.js.map +1 -1
- package/build/development/shared/components/TextArea/index.js +0 -7
- package/build/development/shared/components/TextArea/index.js.map +1 -1
- package/build/development/shared/components/WithTooltip/Tooltip.js +0 -3
- package/build/development/shared/components/WithTooltip/Tooltip.js.map +1 -1
- package/build/development/shared/components/WithTooltip/index.js +1 -6
- package/build/development/shared/components/WithTooltip/index.js.map +1 -1
- package/build/development/shared/components/YouTubeVideo/index.js +0 -4
- package/build/development/shared/components/YouTubeVideo/index.js.map +1 -1
- package/build/development/shared/components/selectors/CustomDropdown/Options/index.js +0 -4
- package/build/development/shared/components/selectors/CustomDropdown/Options/index.js.map +1 -1
- package/build/development/shared/components/selectors/CustomDropdown/index.js +1 -8
- package/build/development/shared/components/selectors/CustomDropdown/index.js.map +1 -1
- package/build/development/shared/components/selectors/NativeDropdown/index.js +1 -8
- package/build/development/shared/components/selectors/NativeDropdown/index.js.map +1 -1
- package/build/development/shared/components/selectors/Switch/index.js +1 -7
- package/build/development/shared/components/selectors/Switch/index.js.map +1 -1
- package/build/development/shared/components/selectors/common.js +11 -6
- package/build/development/shared/components/selectors/common.js.map +1 -1
- package/build/development/shared/utils/jest/index.js +16 -75
- package/build/development/shared/utils/jest/index.js.map +1 -1
- package/build/development/shared/utils/splitComponent.js +0 -3
- package/build/development/shared/utils/splitComponent.js.map +1 -1
- package/build/development/web.bundle.js +23 -33
- package/build/production/shared/components/Button/index.js +1 -26
- package/build/production/shared/components/Button/index.js.map +1 -1
- package/build/production/shared/components/Checkbox/index.js +1 -1
- package/build/production/shared/components/Checkbox/index.js.map +1 -1
- package/build/production/shared/components/GenericLink/index.js +1 -1
- package/build/production/shared/components/GenericLink/index.js.map +1 -1
- package/build/production/shared/components/Input/index.js +1 -1
- package/build/production/shared/components/Input/index.js.map +1 -1
- package/build/production/shared/components/MetaTags.js +1 -1
- package/build/production/shared/components/MetaTags.js.map +1 -1
- package/build/production/shared/components/Modal/index.js +2 -2
- package/build/production/shared/components/Modal/index.js.map +1 -1
- package/build/production/shared/components/PageLayout/index.js +1 -1
- package/build/production/shared/components/PageLayout/index.js.map +1 -1
- package/build/production/shared/components/TextArea/index.js +1 -1
- package/build/production/shared/components/TextArea/index.js.map +1 -1
- package/build/production/shared/components/WithTooltip/Tooltip.js +1 -1
- package/build/production/shared/components/WithTooltip/Tooltip.js.map +1 -1
- package/build/production/shared/components/WithTooltip/index.js +2 -2
- package/build/production/shared/components/WithTooltip/index.js.map +1 -1
- package/build/production/shared/components/YouTubeVideo/index.js +1 -1
- package/build/production/shared/components/YouTubeVideo/index.js.map +1 -1
- package/build/production/shared/components/selectors/CustomDropdown/Options/index.js +1 -1
- package/build/production/shared/components/selectors/CustomDropdown/Options/index.js.map +1 -1
- package/build/production/shared/components/selectors/CustomDropdown/index.js +1 -1
- package/build/production/shared/components/selectors/CustomDropdown/index.js.map +1 -1
- package/build/production/shared/components/selectors/NativeDropdown/index.js +1 -1
- package/build/production/shared/components/selectors/NativeDropdown/index.js.map +1 -1
- package/build/production/shared/components/selectors/Switch/index.js +1 -1
- package/build/production/shared/components/selectors/Switch/index.js.map +1 -1
- package/build/production/shared/components/selectors/common.js +2 -2
- package/build/production/shared/components/selectors/common.js.map +1 -1
- package/build/production/shared/utils/jest/index.js +7 -33
- package/build/production/shared/utils/jest/index.js.map +1 -1
- package/build/production/shared/utils/splitComponent.js +1 -1
- package/build/production/shared/utils/splitComponent.js.map +1 -1
- package/build/production/web.bundle.js +1 -1
- package/build/production/web.bundle.js.map +1 -1
- package/build/types-code/shared/components/selectors/CustomDropdown/Options/index.d.ts +3 -3
- package/build/types-code/shared/components/selectors/CustomDropdown/index.d.ts +2 -2
- package/build/types-code/shared/components/selectors/Switch/index.d.ts +3 -3
- package/build/types-code/shared/components/selectors/common.d.ts +11 -9
- package/build/types-code/shared/utils/jest/index.d.ts +3 -43
- package/config/eslint/default.json +2 -1
- package/config/eslint/jest.json +2 -1
- package/config/eslint/typescript.js +5 -0
- package/package.json +40 -39
- package/src/shared/components/Button/index.tsx +0 -38
- package/src/shared/components/Checkbox/index.tsx +0 -6
- package/src/shared/components/GenericLink/index.tsx +0 -13
- package/src/shared/components/Input/index.tsx +0 -4
- package/src/shared/components/MetaTags.tsx +0 -9
- package/src/shared/components/Modal/index.tsx +0 -10
- package/src/shared/components/PageLayout/index.tsx +0 -6
- package/src/shared/components/TextArea/index.tsx +0 -8
- package/src/shared/components/WithTooltip/Tooltip.tsx +0 -4
- package/src/shared/components/WithTooltip/index.tsx +1 -7
- package/src/shared/components/YouTubeVideo/index.tsx +0 -5
- package/src/shared/components/selectors/CustomDropdown/Options/index.tsx +3 -7
- package/src/shared/components/selectors/CustomDropdown/index.tsx +4 -10
- package/src/shared/components/selectors/NativeDropdown/index.tsx +2 -9
- package/src/shared/components/selectors/Switch/index.tsx +10 -11
- package/src/shared/components/selectors/common.ts +25 -11
- package/src/shared/utils/jest/index.tsx +14 -64
- package/src/shared/utils/splitComponent.tsx +1 -5
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import PT from 'prop-types';
|
|
2
2
|
import themed, { type Theme } from '@dr.pogodin/react-themes';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
type OptionsT,
|
|
6
|
+
type ValueT,
|
|
7
|
+
optionsValidator,
|
|
8
|
+
optionValueName,
|
|
9
|
+
valueValidator,
|
|
10
|
+
} from '../common';
|
|
5
11
|
|
|
6
12
|
import defaultTheme from './theme.scss';
|
|
7
13
|
|
|
@@ -15,10 +21,10 @@ const validThemeKeys = [
|
|
|
15
21
|
|
|
16
22
|
type PropsT = {
|
|
17
23
|
label?: React.ReactNode;
|
|
18
|
-
onChange?: (value:
|
|
24
|
+
onChange?: (value: ValueT) => void;
|
|
19
25
|
options?: Readonly<OptionsT<React.ReactNode>>;
|
|
20
26
|
theme: Theme<typeof validThemeKeys>;
|
|
21
|
-
value?:
|
|
27
|
+
value?: ValueT;
|
|
22
28
|
};
|
|
23
29
|
|
|
24
30
|
const BaseSwitch: React.FunctionComponent<PropsT> = ({
|
|
@@ -81,14 +87,7 @@ BaseSwitch.propTypes = {
|
|
|
81
87
|
onChange: PT.func,
|
|
82
88
|
options: optionsValidator,
|
|
83
89
|
theme: ThemedSwitch.themeType.isRequired,
|
|
84
|
-
value:
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
BaseSwitch.defaultProps = {
|
|
88
|
-
label: undefined,
|
|
89
|
-
onChange: undefined,
|
|
90
|
-
options: [],
|
|
91
|
-
value: undefined,
|
|
90
|
+
value: valueValidator,
|
|
92
91
|
};
|
|
93
92
|
|
|
94
93
|
export default ThemedSwitch;
|
|
@@ -19,52 +19,66 @@ export const validThemeKeys = [
|
|
|
19
19
|
'upward',
|
|
20
20
|
] as const;
|
|
21
21
|
|
|
22
|
+
export type ValueT = number | string;
|
|
23
|
+
|
|
24
|
+
export const valueValidator: PT.Requireable<ValueT> = PT.oneOfType([
|
|
25
|
+
PT.number.isRequired,
|
|
26
|
+
PT.string.isRequired,
|
|
27
|
+
]);
|
|
28
|
+
|
|
22
29
|
export type OptionT<NameT> = {
|
|
23
30
|
name?: NameT | null;
|
|
24
|
-
value:
|
|
31
|
+
value: ValueT;
|
|
25
32
|
};
|
|
26
33
|
|
|
27
|
-
export type OptionsT<NameT> = Array<OptionT<NameT> |
|
|
34
|
+
export type OptionsT<NameT> = Array<OptionT<NameT> | ValueT>;
|
|
28
35
|
|
|
29
36
|
export type PropsT<
|
|
30
37
|
NameT,
|
|
31
38
|
OnChangeT = React.ChangeEventHandler<HTMLSelectElement>,
|
|
32
39
|
> = {
|
|
33
|
-
filter?: (item: OptionT<NameT> |
|
|
40
|
+
filter?: (item: OptionT<NameT> | ValueT) => boolean;
|
|
34
41
|
label?: React.ReactNode;
|
|
35
42
|
onChange?: OnChangeT;
|
|
36
43
|
options?: OptionsT<NameT>;
|
|
37
44
|
theme: Theme<typeof validThemeKeys>;
|
|
38
|
-
value?:
|
|
45
|
+
value?: ValueT;
|
|
39
46
|
};
|
|
40
47
|
|
|
41
48
|
export const optionValidator:
|
|
42
|
-
PT.Requireable<OptionT<React.ReactNode> |
|
|
49
|
+
PT.Requireable<OptionT<React.ReactNode> | ValueT> = PT.oneOfType([
|
|
43
50
|
PT.shape({
|
|
44
51
|
name: PT.node,
|
|
45
|
-
value:
|
|
52
|
+
value: valueValidator.isRequired,
|
|
46
53
|
}).isRequired,
|
|
54
|
+
PT.number.isRequired,
|
|
47
55
|
PT.string.isRequired,
|
|
48
56
|
]);
|
|
49
57
|
|
|
50
58
|
export const optionsValidator = PT.arrayOf(optionValidator.isRequired);
|
|
51
59
|
|
|
52
60
|
export const stringOptionValidator:
|
|
53
|
-
PT.Requireable<OptionT<string> |
|
|
61
|
+
PT.Requireable<OptionT<string> | ValueT> = PT.oneOfType([
|
|
54
62
|
PT.shape({
|
|
55
63
|
name: PT.string,
|
|
56
|
-
value:
|
|
64
|
+
value: valueValidator.isRequired,
|
|
57
65
|
}).isRequired,
|
|
66
|
+
PT.number.isRequired,
|
|
58
67
|
PT.string.isRequired,
|
|
59
68
|
]);
|
|
60
69
|
|
|
61
70
|
export const stringOptionsValidator = PT.arrayOf(stringOptionValidator.isRequired);
|
|
62
71
|
|
|
72
|
+
function isValue<T>(x: OptionT<T> | ValueT): x is ValueT {
|
|
73
|
+
const type = typeof x;
|
|
74
|
+
return type === 'number' || type === 'string';
|
|
75
|
+
}
|
|
76
|
+
|
|
63
77
|
/** Returns option value and name as a tuple. */
|
|
64
78
|
export function optionValueName<NameT>(
|
|
65
|
-
option: OptionT<NameT> |
|
|
66
|
-
): [
|
|
67
|
-
return
|
|
79
|
+
option: OptionT<NameT> | ValueT,
|
|
80
|
+
): [ValueT, NameT | ValueT] {
|
|
81
|
+
return isValue(option)
|
|
68
82
|
? [option, option]
|
|
69
83
|
: [option.value, option.name ?? option.value];
|
|
70
84
|
}
|
|
@@ -1,20 +1,15 @@
|
|
|
1
|
-
/* global
|
|
1
|
+
/* global jest, document */
|
|
2
2
|
/* eslint-disable import/no-extraneous-dependencies */
|
|
3
3
|
|
|
4
4
|
import mockdate from 'mockdate';
|
|
5
|
-
import { type
|
|
5
|
+
import { type ReactNode, act } from 'react';
|
|
6
6
|
import { type Root, createRoot } from 'react-dom/client';
|
|
7
|
-
import TU, { act } from 'react-dom/test-utils';
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
import Renderer from 'react-test-renderer';
|
|
11
|
-
import { type ShallowRenderer, createRenderer } from 'react-test-renderer/shallow';
|
|
12
|
-
/* eslint-enable import/no-extraneous-dependencies */
|
|
8
|
+
import { render } from '@testing-library/react';
|
|
13
9
|
|
|
14
10
|
/**
|
|
15
11
|
* An alias for [act(..)](https://reactjs.org/docs/test-utils.html#act)
|
|
16
|
-
* from `react
|
|
17
|
-
* @param {function} action
|
|
12
|
+
* from `react`.
|
|
18
13
|
*/
|
|
19
14
|
export { act };
|
|
20
15
|
|
|
@@ -86,10 +81,18 @@ export function mount(scene: ReactNode): MountedSceneT {
|
|
|
86
81
|
|
|
87
82
|
const res: MountedSceneT = (element as unknown) as MountedSceneT;
|
|
88
83
|
res.destroy = () => {
|
|
84
|
+
// NOTE: As it seems @testing-library may reset this flag to false
|
|
85
|
+
// when it is simulating user events.
|
|
86
|
+
global.IS_REACT_ACT_ENVIRONMENT = true;
|
|
87
|
+
|
|
89
88
|
act(() => root.unmount());
|
|
90
89
|
res.remove();
|
|
91
90
|
};
|
|
92
91
|
|
|
92
|
+
// NOTE: As it seems @testing-library may reset this flag to false
|
|
93
|
+
// when it is simulating user events.
|
|
94
|
+
global.IS_REACT_ACT_ENVIRONMENT = true;
|
|
95
|
+
|
|
93
96
|
act(() => {
|
|
94
97
|
root = createRoot(res);
|
|
95
98
|
root.render(scene);
|
|
@@ -97,61 +100,8 @@ export function mount(scene: ReactNode): MountedSceneT {
|
|
|
97
100
|
return res;
|
|
98
101
|
}
|
|
99
102
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Renders provided ReactJS component into JSON representation of the component
|
|
104
|
-
* tree, using [`react-test-renderer`](https://www.npmjs.com/package/react-test-renderer).
|
|
105
|
-
* @param {object} component ReactJS component to render.
|
|
106
|
-
* @return {object} JSON representation of the rendered tree.
|
|
107
|
-
* @example
|
|
108
|
-
* import { JU } from '@dr.pogodin/react-utils';
|
|
109
|
-
* console.log(JU.render(<div>Example</div>));
|
|
110
|
-
*/
|
|
111
|
-
export function render(component: ReactElement) {
|
|
112
|
-
return Renderer.create(component).toJSON();
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Generates a shallow render of given ReactJS component, using
|
|
117
|
-
* [react-test-renderer/shallow](https://reactjs.org/docs/shallow-renderer.html)
|
|
118
|
-
* and returns the result.
|
|
119
|
-
* @param {object} component ReactJS component to render.
|
|
120
|
-
* @return {object} JSON representation of the shallow component's render tree.
|
|
121
|
-
*/
|
|
122
|
-
export function shallowRender(component: ReactElement) {
|
|
123
|
-
const renderer: ShallowRenderer = createRenderer();
|
|
124
|
-
renderer.render(component);
|
|
125
|
-
return renderer.getRenderOutput();
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Makes a shallow snapshot test of the given ReactJS component, and also
|
|
130
|
-
* returns JSON representation of the rendered component tree. Under the hood
|
|
131
|
-
* it uses {@link module:JU.shallowRender shallowRender(..)} to generate
|
|
132
|
-
* the render, then executes `expect(RENDER_RESULT).toMatchSnapshot()`,
|
|
133
|
-
* and finally returns the `RENDER_RESULT` to the caller.
|
|
134
|
-
* @param {object} component ReactJS component to render.
|
|
135
|
-
* @return {object} JSON representation of shallow render.
|
|
136
|
-
*/
|
|
137
|
-
export function shallowSnapshot(component: ReactElement) {
|
|
138
|
-
const res = shallowRender(component);
|
|
139
|
-
expect(res).toMatchSnapshot();
|
|
140
|
-
return res;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Makes snapshot test of the given ReactJS component, and also returns JSON
|
|
145
|
-
* representation of the rendered component tree. Under the hood, it uses
|
|
146
|
-
* {@link module:JU.render render(..)} to render it, then executes
|
|
147
|
-
* `expect(RENDER_RESULT).toMatchSnapshot()`, and then returns `RENDER_RESULT`.
|
|
148
|
-
* @param {object} component ReactJS component to render.
|
|
149
|
-
* @return {object} JSON render of the component.
|
|
150
|
-
*/
|
|
151
|
-
export function snapshot(component: ReactElement) {
|
|
152
|
-
const res = render(component);
|
|
103
|
+
export function snapshot(element: React.ReactElement) {
|
|
104
|
+
const res = render(element).asFragment().firstChild;
|
|
153
105
|
expect(res).toMatchSnapshot();
|
|
154
106
|
return res;
|
|
155
107
|
}
|
|
156
|
-
|
|
157
|
-
export const simulate = TU.Simulate;
|
|
@@ -245,11 +245,7 @@ export default function splitComponent<
|
|
|
245
245
|
|
|
246
246
|
CodeSplit.propTypes = {
|
|
247
247
|
children: PT.node,
|
|
248
|
-
} as
|
|
249
|
-
|
|
250
|
-
CodeSplit.defaultProps = {
|
|
251
|
-
children: undefined,
|
|
252
|
-
} as Partial<ComponentPropsT>;
|
|
248
|
+
} as PT.WeakValidationMap<ComponentPropsT>;
|
|
253
249
|
|
|
254
250
|
return CodeSplit;
|
|
255
251
|
}
|