@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.
Files changed (102) hide show
  1. package/build/development/shared/components/Button/index.js +0 -38
  2. package/build/development/shared/components/Button/index.js.map +1 -1
  3. package/build/development/shared/components/Checkbox/index.js +0 -5
  4. package/build/development/shared/components/Checkbox/index.js.map +1 -1
  5. package/build/development/shared/components/GenericLink/index.js +0 -12
  6. package/build/development/shared/components/GenericLink/index.js.map +1 -1
  7. package/build/development/shared/components/Input/index.js +0 -3
  8. package/build/development/shared/components/Input/index.js.map +1 -1
  9. package/build/development/shared/components/MetaTags.js +0 -8
  10. package/build/development/shared/components/MetaTags.js.map +1 -1
  11. package/build/development/shared/components/Modal/index.js +0 -8
  12. package/build/development/shared/components/Modal/index.js.map +1 -1
  13. package/build/development/shared/components/PageLayout/index.js +0 -5
  14. package/build/development/shared/components/PageLayout/index.js.map +1 -1
  15. package/build/development/shared/components/TextArea/index.js +0 -7
  16. package/build/development/shared/components/TextArea/index.js.map +1 -1
  17. package/build/development/shared/components/WithTooltip/Tooltip.js +0 -3
  18. package/build/development/shared/components/WithTooltip/Tooltip.js.map +1 -1
  19. package/build/development/shared/components/WithTooltip/index.js +1 -6
  20. package/build/development/shared/components/WithTooltip/index.js.map +1 -1
  21. package/build/development/shared/components/YouTubeVideo/index.js +0 -4
  22. package/build/development/shared/components/YouTubeVideo/index.js.map +1 -1
  23. package/build/development/shared/components/selectors/CustomDropdown/Options/index.js +0 -4
  24. package/build/development/shared/components/selectors/CustomDropdown/Options/index.js.map +1 -1
  25. package/build/development/shared/components/selectors/CustomDropdown/index.js +1 -8
  26. package/build/development/shared/components/selectors/CustomDropdown/index.js.map +1 -1
  27. package/build/development/shared/components/selectors/NativeDropdown/index.js +1 -8
  28. package/build/development/shared/components/selectors/NativeDropdown/index.js.map +1 -1
  29. package/build/development/shared/components/selectors/Switch/index.js +1 -7
  30. package/build/development/shared/components/selectors/Switch/index.js.map +1 -1
  31. package/build/development/shared/components/selectors/common.js +11 -6
  32. package/build/development/shared/components/selectors/common.js.map +1 -1
  33. package/build/development/shared/utils/jest/index.js +16 -75
  34. package/build/development/shared/utils/jest/index.js.map +1 -1
  35. package/build/development/shared/utils/splitComponent.js +0 -3
  36. package/build/development/shared/utils/splitComponent.js.map +1 -1
  37. package/build/development/web.bundle.js +23 -33
  38. package/build/production/shared/components/Button/index.js +1 -26
  39. package/build/production/shared/components/Button/index.js.map +1 -1
  40. package/build/production/shared/components/Checkbox/index.js +1 -1
  41. package/build/production/shared/components/Checkbox/index.js.map +1 -1
  42. package/build/production/shared/components/GenericLink/index.js +1 -1
  43. package/build/production/shared/components/GenericLink/index.js.map +1 -1
  44. package/build/production/shared/components/Input/index.js +1 -1
  45. package/build/production/shared/components/Input/index.js.map +1 -1
  46. package/build/production/shared/components/MetaTags.js +1 -1
  47. package/build/production/shared/components/MetaTags.js.map +1 -1
  48. package/build/production/shared/components/Modal/index.js +2 -2
  49. package/build/production/shared/components/Modal/index.js.map +1 -1
  50. package/build/production/shared/components/PageLayout/index.js +1 -1
  51. package/build/production/shared/components/PageLayout/index.js.map +1 -1
  52. package/build/production/shared/components/TextArea/index.js +1 -1
  53. package/build/production/shared/components/TextArea/index.js.map +1 -1
  54. package/build/production/shared/components/WithTooltip/Tooltip.js +1 -1
  55. package/build/production/shared/components/WithTooltip/Tooltip.js.map +1 -1
  56. package/build/production/shared/components/WithTooltip/index.js +2 -2
  57. package/build/production/shared/components/WithTooltip/index.js.map +1 -1
  58. package/build/production/shared/components/YouTubeVideo/index.js +1 -1
  59. package/build/production/shared/components/YouTubeVideo/index.js.map +1 -1
  60. package/build/production/shared/components/selectors/CustomDropdown/Options/index.js +1 -1
  61. package/build/production/shared/components/selectors/CustomDropdown/Options/index.js.map +1 -1
  62. package/build/production/shared/components/selectors/CustomDropdown/index.js +1 -1
  63. package/build/production/shared/components/selectors/CustomDropdown/index.js.map +1 -1
  64. package/build/production/shared/components/selectors/NativeDropdown/index.js +1 -1
  65. package/build/production/shared/components/selectors/NativeDropdown/index.js.map +1 -1
  66. package/build/production/shared/components/selectors/Switch/index.js +1 -1
  67. package/build/production/shared/components/selectors/Switch/index.js.map +1 -1
  68. package/build/production/shared/components/selectors/common.js +2 -2
  69. package/build/production/shared/components/selectors/common.js.map +1 -1
  70. package/build/production/shared/utils/jest/index.js +7 -33
  71. package/build/production/shared/utils/jest/index.js.map +1 -1
  72. package/build/production/shared/utils/splitComponent.js +1 -1
  73. package/build/production/shared/utils/splitComponent.js.map +1 -1
  74. package/build/production/web.bundle.js +1 -1
  75. package/build/production/web.bundle.js.map +1 -1
  76. package/build/types-code/shared/components/selectors/CustomDropdown/Options/index.d.ts +3 -3
  77. package/build/types-code/shared/components/selectors/CustomDropdown/index.d.ts +2 -2
  78. package/build/types-code/shared/components/selectors/Switch/index.d.ts +3 -3
  79. package/build/types-code/shared/components/selectors/common.d.ts +11 -9
  80. package/build/types-code/shared/utils/jest/index.d.ts +3 -43
  81. package/config/eslint/default.json +2 -1
  82. package/config/eslint/jest.json +2 -1
  83. package/config/eslint/typescript.js +5 -0
  84. package/package.json +40 -39
  85. package/src/shared/components/Button/index.tsx +0 -38
  86. package/src/shared/components/Checkbox/index.tsx +0 -6
  87. package/src/shared/components/GenericLink/index.tsx +0 -13
  88. package/src/shared/components/Input/index.tsx +0 -4
  89. package/src/shared/components/MetaTags.tsx +0 -9
  90. package/src/shared/components/Modal/index.tsx +0 -10
  91. package/src/shared/components/PageLayout/index.tsx +0 -6
  92. package/src/shared/components/TextArea/index.tsx +0 -8
  93. package/src/shared/components/WithTooltip/Tooltip.tsx +0 -4
  94. package/src/shared/components/WithTooltip/index.tsx +1 -7
  95. package/src/shared/components/YouTubeVideo/index.tsx +0 -5
  96. package/src/shared/components/selectors/CustomDropdown/Options/index.tsx +3 -7
  97. package/src/shared/components/selectors/CustomDropdown/index.tsx +4 -10
  98. package/src/shared/components/selectors/NativeDropdown/index.tsx +2 -9
  99. package/src/shared/components/selectors/Switch/index.tsx +10 -11
  100. package/src/shared/components/selectors/common.ts +25 -11
  101. package/src/shared/utils/jest/index.tsx +14 -64
  102. 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 { type OptionsT, optionsValidator, optionValueName } from '../common';
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: string) => void;
24
+ onChange?: (value: ValueT) => void;
19
25
  options?: Readonly<OptionsT<React.ReactNode>>;
20
26
  theme: Theme<typeof validThemeKeys>;
21
- value?: string;
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: PT.string,
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: string;
31
+ value: ValueT;
25
32
  };
26
33
 
27
- export type OptionsT<NameT> = Array<OptionT<NameT> | string>;
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> | string) => boolean;
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?: string;
45
+ value?: ValueT;
39
46
  };
40
47
 
41
48
  export const optionValidator:
42
- PT.Requireable<OptionT<React.ReactNode> | string> = PT.oneOfType([
49
+ PT.Requireable<OptionT<React.ReactNode> | ValueT> = PT.oneOfType([
43
50
  PT.shape({
44
51
  name: PT.node,
45
- value: PT.string.isRequired,
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> | string> = PT.oneOfType([
61
+ PT.Requireable<OptionT<string> | ValueT> = PT.oneOfType([
54
62
  PT.shape({
55
63
  name: PT.string,
56
- value: PT.string.isRequired,
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> | string,
66
- ): [string, NameT | string] {
67
- return typeof option === 'string'
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 expect, jest, document */
1
+ /* global jest, document */
2
2
  /* eslint-disable import/no-extraneous-dependencies */
3
3
 
4
4
  import mockdate from 'mockdate';
5
- import { type ReactElement, type ReactNode } from 'react';
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
- /* eslint-disable import/no-extraneous-dependencies */
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-dom/test-utils`.
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
- /* OLD STUFF BELOW THIS MARK */
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 React.WeakValidationMap<ComponentPropsT>;
249
-
250
- CodeSplit.defaultProps = {
251
- children: undefined,
252
- } as Partial<ComponentPropsT>;
248
+ } as PT.WeakValidationMap<ComponentPropsT>;
253
249
 
254
250
  return CodeSplit;
255
251
  }