@dr.pogodin/react-utils 1.25.6 → 1.26.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 (290) hide show
  1. package/babel.config.js +3 -1
  2. package/bin/build.js +18 -4
  3. package/build/development/client/getInj.js +9 -8
  4. package/build/development/client/getInj.js.map +1 -1
  5. package/build/development/client/index.js +6 -3
  6. package/build/development/client/index.js.map +1 -1
  7. package/build/development/client/init.js +4 -0
  8. package/build/development/client/init.js.map +1 -1
  9. package/build/development/index.js +166 -38
  10. package/build/development/index.js.map +1 -1
  11. package/build/development/server/Cache.js +13 -13
  12. package/build/development/server/Cache.js.map +1 -1
  13. package/build/development/server/index.js +16 -23
  14. package/build/development/server/index.js.map +1 -1
  15. package/build/development/server/renderer.js +56 -43
  16. package/build/development/server/renderer.js.map +1 -1
  17. package/build/development/server/server.js +9 -5
  18. package/build/development/server/server.js.map +1 -1
  19. package/build/development/server/utils/errors.js +12 -9
  20. package/build/development/server/utils/errors.js.map +1 -1
  21. package/build/development/server/utils/index.js +2 -2
  22. package/build/development/server/utils/index.js.map +1 -1
  23. package/build/development/shared/components/Button/index.js +7 -5
  24. package/build/development/shared/components/Button/index.js.map +1 -1
  25. package/build/development/shared/components/Checkbox/index.js +24 -26
  26. package/build/development/shared/components/Checkbox/index.js.map +1 -1
  27. package/build/development/shared/components/Dropdown/index.js +24 -17
  28. package/build/development/shared/components/Dropdown/index.js.map +1 -1
  29. package/build/development/shared/components/GenericLink/index.js +44 -37
  30. package/build/development/shared/components/GenericLink/index.js.map +1 -1
  31. package/build/development/shared/components/Input/index.js +7 -7
  32. package/build/development/shared/components/Input/index.js.map +1 -1
  33. package/build/development/shared/components/Link.js +10 -9
  34. package/build/development/shared/components/Link.js.map +1 -1
  35. package/build/development/shared/components/MetaTags.js +22 -18
  36. package/build/development/shared/components/MetaTags.js.map +1 -1
  37. package/build/development/shared/components/Modal/index.js +16 -16
  38. package/build/development/shared/components/Modal/index.js.map +1 -1
  39. package/build/development/shared/components/NavLink.js +10 -9
  40. package/build/development/shared/components/NavLink.js.map +1 -1
  41. package/build/development/shared/components/PageLayout/index.js +16 -18
  42. package/build/development/shared/components/PageLayout/index.js.map +1 -1
  43. package/build/development/shared/components/ScalableRect/index.js +27 -7
  44. package/build/development/shared/components/ScalableRect/index.js.map +1 -1
  45. package/build/development/shared/components/Throbber/index.js +14 -22
  46. package/build/development/shared/components/Throbber/index.js.map +1 -1
  47. package/build/development/shared/components/WithTooltip/Tooltip.js +19 -21
  48. package/build/development/shared/components/WithTooltip/Tooltip.js.map +1 -1
  49. package/build/development/shared/components/WithTooltip/index.js +11 -10
  50. package/build/development/shared/components/WithTooltip/index.js.map +1 -1
  51. package/build/development/shared/components/YouTubeVideo/index.js +15 -13
  52. package/build/development/shared/components/YouTubeVideo/index.js.map +1 -1
  53. package/build/development/shared/components/index.js +2 -2
  54. package/build/development/shared/components/index.js.map +1 -1
  55. package/build/development/shared/utils/config.js.map +1 -1
  56. package/build/development/shared/utils/globalState.js +15 -0
  57. package/build/development/shared/utils/globalState.js.map +1 -0
  58. package/build/development/shared/utils/index.js +13 -10
  59. package/build/development/shared/utils/index.js.map +1 -1
  60. package/build/development/shared/utils/isomorphy/buildInfo.js +7 -3
  61. package/build/development/shared/utils/isomorphy/buildInfo.js.map +1 -1
  62. package/build/development/shared/utils/isomorphy/environment-check.js.map +1 -1
  63. package/build/development/shared/utils/isomorphy/index.js.map +1 -1
  64. package/build/development/shared/utils/jest/E2eSsrEnv.js +35 -28
  65. package/build/development/shared/utils/jest/E2eSsrEnv.js.map +1 -1
  66. package/build/development/shared/utils/jest/global.js +17 -0
  67. package/build/development/shared/utils/jest/global.js.map +1 -0
  68. package/build/development/shared/utils/jest/index.js +17 -9
  69. package/build/development/shared/utils/jest/index.js.map +1 -1
  70. package/build/development/shared/utils/splitComponent.js +25 -34
  71. package/build/development/shared/utils/splitComponent.js.map +1 -1
  72. package/build/development/shared/utils/time.js +16 -12
  73. package/build/development/shared/utils/time.js.map +1 -1
  74. package/build/development/shared/utils/webpack.js +3 -3
  75. package/build/development/shared/utils/webpack.js.map +1 -1
  76. package/build/development/web.bundle.js +92 -82
  77. package/build/production/client/getInj.js +1 -1
  78. package/build/production/client/getInj.js.map +1 -1
  79. package/build/production/client/index.js +4 -4
  80. package/build/production/client/index.js.map +1 -1
  81. package/build/production/client/init.js +3 -1
  82. package/build/production/client/init.js.map +1 -1
  83. package/build/production/index.js +1 -1
  84. package/build/production/index.js.map +1 -1
  85. package/build/production/server/Cache.js +7 -8
  86. package/build/production/server/Cache.js.map +1 -1
  87. package/build/production/server/index.js +5 -4
  88. package/build/production/server/index.js.map +1 -1
  89. package/build/production/server/renderer.js +32 -30
  90. package/build/production/server/renderer.js.map +1 -1
  91. package/build/production/server/server.js +7 -5
  92. package/build/production/server/server.js.map +1 -1
  93. package/build/production/server/utils/errors.js +9 -10
  94. package/build/production/server/utils/errors.js.map +1 -1
  95. package/build/production/server/utils/index.js +1 -1
  96. package/build/production/server/utils/index.js.map +1 -1
  97. package/build/production/shared/components/Button/index.js +3 -3
  98. package/build/production/shared/components/Button/index.js.map +1 -1
  99. package/build/production/shared/components/Checkbox/index.js +11 -11
  100. package/build/production/shared/components/Checkbox/index.js.map +1 -1
  101. package/build/production/shared/components/Dropdown/index.js +11 -11
  102. package/build/production/shared/components/Dropdown/index.js.map +1 -1
  103. package/build/production/shared/components/GenericLink/index.js +25 -20
  104. package/build/production/shared/components/GenericLink/index.js.map +1 -1
  105. package/build/production/shared/components/Input/index.js +7 -7
  106. package/build/production/shared/components/Input/index.js.map +1 -1
  107. package/build/production/shared/components/Link.js +2 -2
  108. package/build/production/shared/components/Link.js.map +1 -1
  109. package/build/production/shared/components/MetaTags.js +10 -10
  110. package/build/production/shared/components/MetaTags.js.map +1 -1
  111. package/build/production/shared/components/Modal/index.js +2 -2
  112. package/build/production/shared/components/Modal/index.js.map +1 -1
  113. package/build/production/shared/components/NavLink.js +1 -1
  114. package/build/production/shared/components/NavLink.js.map +1 -1
  115. package/build/production/shared/components/PageLayout/index.js +2 -2
  116. package/build/production/shared/components/PageLayout/index.js.map +1 -1
  117. package/build/production/shared/components/ScalableRect/index.js +7 -3
  118. package/build/production/shared/components/ScalableRect/index.js.map +1 -1
  119. package/build/production/shared/components/Throbber/index.js +2 -2
  120. package/build/production/shared/components/Throbber/index.js.map +1 -1
  121. package/build/production/shared/components/WithTooltip/Tooltip.js +13 -13
  122. package/build/production/shared/components/WithTooltip/Tooltip.js.map +1 -1
  123. package/build/production/shared/components/WithTooltip/index.js +3 -3
  124. package/build/production/shared/components/WithTooltip/index.js.map +1 -1
  125. package/build/production/shared/components/YouTubeVideo/index.js +7 -7
  126. package/build/production/shared/components/YouTubeVideo/index.js.map +1 -1
  127. package/build/production/shared/components/index.js +1 -1
  128. package/build/production/shared/components/index.js.map +1 -1
  129. package/build/production/shared/utils/config.js.map +1 -1
  130. package/build/production/shared/utils/globalState.js +3 -0
  131. package/build/production/shared/utils/globalState.js.map +1 -0
  132. package/build/production/shared/utils/index.js +2 -2
  133. package/build/production/shared/utils/index.js.map +1 -1
  134. package/build/production/shared/utils/isomorphy/buildInfo.js +7 -4
  135. package/build/production/shared/utils/isomorphy/buildInfo.js.map +1 -1
  136. package/build/production/shared/utils/isomorphy/environment-check.js.map +1 -1
  137. package/build/production/shared/utils/isomorphy/index.js.map +1 -1
  138. package/build/production/shared/utils/jest/E2eSsrEnv.js +9 -9
  139. package/build/production/shared/utils/jest/E2eSsrEnv.js.map +1 -1
  140. package/build/production/shared/utils/jest/global.js +2 -0
  141. package/build/production/shared/utils/jest/global.js.map +1 -0
  142. package/build/production/shared/utils/jest/index.js +5 -5
  143. package/build/production/shared/utils/jest/index.js.map +1 -1
  144. package/build/production/shared/utils/splitComponent.js +16 -16
  145. package/build/production/shared/utils/splitComponent.js.map +1 -1
  146. package/build/production/shared/utils/time.js +12 -8
  147. package/build/production/shared/utils/time.js.map +1 -1
  148. package/build/production/shared/utils/webpack.js +3 -3
  149. package/build/production/shared/utils/webpack.js.map +1 -1
  150. package/build/production/web.bundle.js +1 -1
  151. package/build/production/web.bundle.js.map +1 -1
  152. package/build/types-code/client/getInj.d.ts +3 -0
  153. package/build/types-code/client/index.d.ts +11 -0
  154. package/build/types-code/client/init.d.ts +9 -0
  155. package/build/types-code/index.d.ts +10 -0
  156. package/build/types-code/server/Cache.d.ts +37 -0
  157. package/build/types-code/server/index.d.ts +145 -0
  158. package/build/types-code/server/renderer.d.ts +106 -0
  159. package/build/types-code/server/server.d.ts +41 -0
  160. package/build/types-code/server/utils/errors.d.ts +104 -0
  161. package/build/types-code/server/utils/index.d.ts +1 -0
  162. package/build/types-code/shared/components/Button/index.d.ts +27 -0
  163. package/build/types-code/shared/components/Checkbox/index.d.ts +21 -0
  164. package/build/types-code/shared/components/Dropdown/index.d.ts +23 -0
  165. package/build/types-code/shared/components/GenericLink/index.d.ts +61 -0
  166. package/build/types-code/shared/components/Input/index.d.ts +11 -0
  167. package/build/types-code/shared/components/Link.d.ts +12 -0
  168. package/build/types-code/shared/components/MetaTags.d.ts +68 -0
  169. package/build/types-code/shared/components/Modal/index.d.ts +26 -0
  170. package/build/types-code/shared/components/NavLink.d.ts +5 -0
  171. package/build/types-code/shared/components/PageLayout/index.d.ts +16 -0
  172. package/build/types-code/shared/components/ScalableRect/index.d.ts +19 -0
  173. package/build/types-code/shared/components/Throbber/index.d.ts +9 -0
  174. package/build/types-code/shared/components/WithTooltip/Tooltip.d.ts +23 -0
  175. package/build/types-code/shared/components/WithTooltip/index.d.ts +17 -0
  176. package/build/types-code/shared/components/YouTubeVideo/index.d.ts +13 -0
  177. package/build/types-code/shared/components/index.d.ts +16 -0
  178. package/build/types-code/shared/utils/config.d.ts +2 -0
  179. package/build/types-code/shared/utils/globalState.d.ts +20 -0
  180. package/build/types-code/shared/utils/index.d.ts +52 -0
  181. package/build/types-code/shared/utils/isomorphy/buildInfo.d.ts +23 -0
  182. package/build/types-code/shared/utils/isomorphy/environment-check.d.ts +11 -0
  183. package/build/types-code/shared/utils/isomorphy/index.d.ts +20 -0
  184. package/build/types-code/shared/utils/jest/E2eSsrEnv.d.ts +31 -0
  185. package/build/types-code/shared/utils/jest/global.d.ts +12 -0
  186. package/build/types-code/shared/utils/jest/index.d.ts +85 -0
  187. package/build/types-code/shared/utils/splitComponent.d.ts +41 -0
  188. package/build/types-code/shared/utils/time.d.ts +62 -0
  189. package/build/types-code/shared/utils/webpack.d.ts +18 -0
  190. package/build/types-scss/__tests__/js/config/publicPath support/__assets__/style.scss.d.ts +1 -0
  191. package/build/types-scss/__tests__/js/config/stylename-generation/__assets__/MockPackageA/TestComponent/style.scss.d.ts +1 -0
  192. package/build/types-scss/__tests__/js/config/stylename-generation/__assets__/MockPackageB/TestComponent/style.scss.d.ts +1 -0
  193. package/build/types-scss/__tests__/js/config/stylename-generation/__assets__/style.scss.d.ts +1 -0
  194. package/build/types-scss/__tests__/js/shared/components/NavLink/styles.scss.d.ts +1 -0
  195. package/build/types-scss/__tests__/js/shared/utils/splitComponent/__assets__/SampleScene/ComponentA/style.scss.d.ts +1 -0
  196. package/build/types-scss/__tests__/js/shared/utils/splitComponent/__assets__/SampleScene/ComponentB/style.scss.d.ts +1 -0
  197. package/build/types-scss/__tests__/js/shared/utils/splitComponent/__assets__/SampleScene/ComponentC/style.scss.d.ts +1 -0
  198. package/build/types-scss/__tests__/js/shared/utils/splitComponent/__assets__/SampleScene/style.scss.d.ts +1 -0
  199. package/build/types-scss/__tests__/ts/config/publicPath support/__assets__/style.scss.d.ts +1 -0
  200. package/build/types-scss/__tests__/ts/config/stylename-generation/__assets__/MockPackageA/TestComponent/style.scss.d.ts +1 -0
  201. package/build/types-scss/__tests__/ts/config/stylename-generation/__assets__/MockPackageB/TestComponent/style.scss.d.ts +1 -0
  202. package/build/types-scss/__tests__/ts/config/stylename-generation/__assets__/style.scss.d.ts +1 -0
  203. package/build/types-scss/__tests__/ts/shared/components/NavLink/styles.scss.d.ts +1 -0
  204. package/build/types-scss/__tests__/ts/shared/utils/splitComponent/__assets__/SampleScene/ComponentA/style.scss.d.ts +1 -0
  205. package/build/types-scss/__tests__/ts/shared/utils/splitComponent/__assets__/SampleScene/ComponentB/style.scss.d.ts +1 -0
  206. package/build/types-scss/__tests__/ts/shared/utils/splitComponent/__assets__/SampleScene/ComponentC/style.scss.d.ts +1 -0
  207. package/build/types-scss/__tests__/ts/shared/utils/splitComponent/__assets__/SampleScene/style.scss.d.ts +1 -0
  208. package/build/types-scss/src/shared/components/Button/style.scss.d.ts +6 -0
  209. package/build/types-scss/src/shared/components/Checkbox/theme.scss.d.ts +6 -0
  210. package/build/types-scss/src/shared/components/Dropdown/theme.scss.d.ts +9 -0
  211. package/build/types-scss/src/shared/components/GenericLink/style.scss.d.ts +1 -0
  212. package/build/types-scss/src/shared/components/Input/theme.scss.d.ts +6 -0
  213. package/build/types-scss/src/shared/components/Modal/base-theme.scss.d.ts +5 -0
  214. package/build/types-scss/src/shared/components/PageLayout/base-theme.scss.d.ts +6 -0
  215. package/build/types-scss/src/shared/components/ScalableRect/style.scss.d.ts +2 -0
  216. package/build/types-scss/src/shared/components/Throbber/theme.scss.d.ts +6 -0
  217. package/build/types-scss/src/shared/components/WithTooltip/default-theme.scss.d.ts +7 -0
  218. package/build/types-scss/src/shared/components/YouTubeVideo/base.scss.d.ts +5 -0
  219. package/build/types-scss/src/shared/components/YouTubeVideo/throbber.scss.d.ts +4 -0
  220. package/config/eslint/jest.json +3 -2
  221. package/config/eslint/typescript.js +34 -0
  222. package/config/jest/default.js +3 -3
  223. package/package.json +74 -32
  224. package/src/client/getInj.ts +43 -0
  225. package/src/client/index.tsx +40 -0
  226. package/src/client/init.ts +47 -0
  227. package/src/index.ts +58 -0
  228. package/src/server/Cache.ts +68 -0
  229. package/src/server/index.ts +230 -0
  230. package/src/server/renderer.tsx +604 -0
  231. package/src/server/server.ts +309 -0
  232. package/src/server/utils/errors.ts +135 -0
  233. package/src/server/utils/index.ts +3 -0
  234. package/src/shared/components/Button/index.tsx +146 -0
  235. package/src/shared/components/Button/style.scss +53 -0
  236. package/src/shared/components/Checkbox/index.tsx +71 -0
  237. package/src/shared/components/Checkbox/theme.scss +43 -0
  238. package/src/shared/components/Dropdown/index.tsx +144 -0
  239. package/src/shared/components/Dropdown/theme.scss +63 -0
  240. package/src/shared/components/GenericLink/index.tsx +157 -0
  241. package/src/shared/components/GenericLink/style.scss +3 -0
  242. package/src/shared/components/Input/index.tsx +59 -0
  243. package/src/shared/components/Input/theme.scss +27 -0
  244. package/src/shared/components/Link.tsx +21 -0
  245. package/src/shared/components/MetaTags.tsx +170 -0
  246. package/src/shared/components/Modal/base-theme.scss +38 -0
  247. package/src/shared/components/Modal/index.tsx +144 -0
  248. package/src/shared/components/Modal/styles.scss +5 -0
  249. package/src/shared/components/NavLink.tsx +13 -0
  250. package/src/shared/components/PageLayout/base-theme.scss +30 -0
  251. package/src/shared/components/PageLayout/index.tsx +76 -0
  252. package/src/shared/components/ScalableRect/index.tsx +84 -0
  253. package/src/shared/components/ScalableRect/style.scss +10 -0
  254. package/src/shared/components/Throbber/index.tsx +43 -0
  255. package/src/shared/components/Throbber/theme.scss +26 -0
  256. package/src/shared/components/WithTooltip/Tooltip.tsx +353 -0
  257. package/src/shared/components/WithTooltip/default-theme.scss +36 -0
  258. package/src/shared/components/WithTooltip/index.tsx +204 -0
  259. package/src/shared/components/YouTubeVideo/base.scss +13 -0
  260. package/src/shared/components/YouTubeVideo/index.tsx +96 -0
  261. package/src/shared/components/YouTubeVideo/throbber.scss +11 -0
  262. package/src/shared/components/index.ts +17 -0
  263. package/src/shared/utils/config.ts +21 -0
  264. package/src/shared/utils/globalState.ts +29 -0
  265. package/src/shared/utils/index.ts +105 -0
  266. package/src/shared/utils/isomorphy/buildInfo.ts +54 -0
  267. package/src/shared/utils/isomorphy/environment-check.ts +18 -0
  268. package/src/shared/utils/isomorphy/index.ts +38 -0
  269. package/src/shared/utils/jest/E2eSsrEnv.ts +250 -0
  270. package/src/shared/utils/jest/global.ts +19 -0
  271. package/src/shared/utils/jest/index.tsx +157 -0
  272. package/src/shared/utils/splitComponent.tsx +255 -0
  273. package/src/shared/utils/time.ts +118 -0
  274. package/src/shared/utils/webpack.ts +45 -0
  275. package/src/styles/_global/reset.css +52 -0
  276. package/src/styles/global.scss +11 -0
  277. package/tsconfig.configs.json +18 -0
  278. package/tsconfig.json +27 -0
  279. package/tsconfig.types.json +53 -0
  280. package/typed-scss-modules.config.ts +9 -0
  281. package/types.d.ts +37 -0
  282. package/{webpack.config.js → webpack.config.ts} +7 -3
  283. package/config/babel/node-ssr.js +0 -85
  284. package/config/babel/webpack.js +0 -123
  285. package/config/webpack/app-base.js +0 -330
  286. package/config/webpack/app-development.js +0 -80
  287. package/config/webpack/app-production.js +0 -60
  288. package/config/webpack/lib-base.js +0 -155
  289. package/config/webpack/lib-development.js +0 -45
  290. package/config/webpack/lib-production.js +0 -44
@@ -0,0 +1,157 @@
1
+ /* global expect, jest, document */
2
+ /* eslint-disable import/no-extraneous-dependencies */
3
+
4
+ import mockdate from 'mockdate';
5
+ import { type ReactElement, type ReactNode } from 'react';
6
+ import { type Root, createRoot } from 'react-dom/client';
7
+ import TU, { act } from 'react-dom/test-utils';
8
+
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 */
13
+
14
+ /**
15
+ * An alias for [act(..)](https://reactjs.org/docs/test-utils.html#act)
16
+ * from `react-dom/test-utils`.
17
+ * @param {function} action
18
+ */
19
+ export { act };
20
+
21
+ export { default as getGlobal } from './global';
22
+
23
+ declare global {
24
+ // eslint-disable-next-line no-var, vars-on-top
25
+ var IS_REACT_ACT_ENVIRONMENT: boolean | undefined;
26
+ }
27
+
28
+ global.IS_REACT_ACT_ENVIRONMENT = true;
29
+
30
+ const originalProcessVersions = process.versions;
31
+
32
+ /**
33
+ * Tricks **react-utils** into thinking the test is running within client-side
34
+ * (browser) environment.
35
+ */
36
+ export function mockClientSide() {
37
+ Object.defineProperty(process, 'versions', { value: null });
38
+ }
39
+
40
+ /**
41
+ * Reverts the effect of {@link module:JU.mockClientSide mockClientSide(..)}.
42
+ */
43
+ export function unmockClientSide() {
44
+ Object.defineProperty(process, 'versions', {
45
+ value: originalProcessVersions,
46
+ writable: false,
47
+ });
48
+ }
49
+
50
+ /**
51
+ * Generates a mock UUID, or better said it determenistically transforms given
52
+ * `seed` number into a UUID-formatted string.
53
+ * @param {number} seed
54
+ * @return {string}
55
+ */
56
+ export function getMockUuid(seed = 0) {
57
+ const x = seed.toString(16).padStart(32, '0');
58
+ return `${x.slice(0, 8)}-${x.slice(8, 12)}-${x.slice(12, 16)}-${x.slice(16, 20)}-${x.slice(20)}`;
59
+ }
60
+
61
+ /**
62
+ * Advances mock timers, and mock date by the specified time.
63
+ * @param {number} time Time step [ms].
64
+ * @returns {Promise} Wait for this to "jump after" any async code which should
65
+ * be executed because of the mock time movement.
66
+ */
67
+ export async function mockTimer(time: number) {
68
+ mockdate.set(time + Date.now());
69
+ jest.advanceTimersByTime(time);
70
+ }
71
+
72
+ export type MountedSceneT = HTMLElement & {
73
+ destroy: () => void;
74
+ };
75
+
76
+ /**
77
+ * Mounts `scene` to the DOM, and returns the root scene element.
78
+ * @param scene
79
+ * @return Created container DOM element with destroy() function
80
+ * attached.
81
+ */
82
+ export function mount(scene: ReactNode): MountedSceneT {
83
+ let root: Root;
84
+ const element = document.createElement('div');
85
+ document.body.appendChild(element);
86
+
87
+ const res: MountedSceneT = (element as unknown) as MountedSceneT;
88
+ res.destroy = () => {
89
+ act(() => root.unmount());
90
+ res.remove();
91
+ };
92
+
93
+ act(() => {
94
+ root = createRoot(res);
95
+ root.render(scene);
96
+ });
97
+ return res;
98
+ }
99
+
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);
153
+ expect(res).toMatchSnapshot();
154
+ return res;
155
+ }
156
+
157
+ export const simulate = TU.Simulate;
@@ -0,0 +1,255 @@
1
+ /* eslint-disable react/jsx-props-no-spreading */
2
+ /* global document */
3
+
4
+ import PT from 'prop-types';
5
+
6
+ import {
7
+ type ComponentType,
8
+ type ReactNode,
9
+ forwardRef,
10
+ lazy,
11
+ Suspense,
12
+ useInsertionEffect,
13
+ } from 'react';
14
+
15
+ import { Barrier } from '@dr.pogodin/js-utils';
16
+
17
+ import { type ChunkGroupsT, getSsrContext } from './globalState';
18
+
19
+ import {
20
+ IS_CLIENT_SIDE,
21
+ IS_SERVER_SIDE,
22
+ getBuildInfo,
23
+ } from './isomorphy';
24
+
25
+ // Note: At the client side we can get chunk groups immediately when loading
26
+ // the module; at the server-side we only can get them within React render flow.
27
+ // Thus, we set and use the following variable at the client-side, and then when
28
+ // needed on the server side, we'll fetch it differently.
29
+ let clientChunkGroups: ChunkGroupsT;
30
+
31
+ if (IS_CLIENT_SIDE) {
32
+ // eslint-disable-next-line global-require
33
+ clientChunkGroups = require('client/getInj').default().CHUNK_GROUPS || {};
34
+ }
35
+
36
+ const refCounts: { [path: string]: number } = {};
37
+
38
+ function getPublicPath() {
39
+ return getBuildInfo().publicPath;
40
+ }
41
+
42
+ /**
43
+ * Client-side only! Ensures the specified CSS stylesheet is loaded into
44
+ * the document; loads if it is missing; and does simple reference counting
45
+ * to facilitate future clean-up.
46
+ * @param name
47
+ * @param loadedSheets
48
+ * @param refCount
49
+ * @return
50
+ */
51
+ function bookStyleSheet(
52
+ name: string,
53
+ loadedSheets: Set<string>,
54
+ refCount: boolean,
55
+ ): Promise<void> | undefined {
56
+ let res: Barrier<void> | undefined;
57
+ const path = `${getPublicPath()}/${name}`;
58
+ const fullPath = `${document.location.origin}${path}`;
59
+
60
+ if (!loadedSheets.has(fullPath)) {
61
+ let link = document.querySelector(`link[href="${path}"]`);
62
+
63
+ if (!link) {
64
+ link = document.createElement('link');
65
+ link.setAttribute('rel', 'stylesheet');
66
+ link.setAttribute('href', path);
67
+ document.head.appendChild(link);
68
+ }
69
+
70
+ res = new Barrier<void>();
71
+ link.addEventListener('load', () => res!.resolve());
72
+ link.addEventListener('error', () => res!.resolve());
73
+ }
74
+
75
+ if (refCount) {
76
+ const current = refCounts[path] || 0;
77
+ refCounts[path] = 1 + current;
78
+ }
79
+
80
+ return res;
81
+ }
82
+
83
+ /**
84
+ * Generates the set of URLs for currently loaded, linked stylesheets.
85
+ * @return
86
+ */
87
+ function getLoadedStyleSheets(): Set<string> {
88
+ const res = new Set<string>();
89
+ const { styleSheets } = document;
90
+ for (let i = 0; i < styleSheets.length; ++i) {
91
+ const { href } = styleSheets[i];
92
+ if (href) res.add(href);
93
+ }
94
+ return res;
95
+ }
96
+
97
+ function assertChunkName(
98
+ chunkName: string,
99
+ chunkGroups: ChunkGroupsT,
100
+ ) {
101
+ if (chunkGroups[chunkName]) return;
102
+ throw Error(`Unknown chunk name "${chunkName}"`);
103
+ }
104
+
105
+ /**
106
+ * Client-side only! Ensures all CSS stylesheets required for the specified
107
+ * code chunk are loaded into the document; loads the missing ones; and does
108
+ * simple reference counting to facilitate future clean-up.
109
+ * @param chunkName Chunk name.
110
+ * @param refCount
111
+ * @return Resolves once all pending stylesheets, necessary for
112
+ * the chunk, are either loaded, or failed to load.
113
+ */
114
+ export function bookStyleSheets(
115
+ chunkName: string,
116
+ chunkGroups: ChunkGroupsT,
117
+ refCount: boolean,
118
+ ): Promise<void> {
119
+ const promises = [];
120
+ const assets = chunkGroups[chunkName];
121
+ const loadedSheets = getLoadedStyleSheets();
122
+
123
+ for (let i = 0; i < assets.length; ++i) {
124
+ const asset = assets[i];
125
+ if (asset.endsWith('.css')) {
126
+ const promise = bookStyleSheet(asset, loadedSheets, refCount);
127
+ if (promise) promises.push(promise);
128
+ }
129
+ }
130
+
131
+ return promises.length
132
+ ? Promise.allSettled(promises).then()
133
+ : Promise.resolve();
134
+ }
135
+
136
+ /**
137
+ * Client-side only! Frees from the document all CSS stylesheets that are
138
+ * required by the specified chunk, and have reference counter equal to one
139
+ * (for chunks with larger reference counter values, it just decrements
140
+ * the reference counter).
141
+ * @param {string} chunkName
142
+ */
143
+ export function freeStyleSheets(
144
+ chunkName: string,
145
+ chunkGroups: ChunkGroupsT,
146
+ ) {
147
+ const assets = chunkGroups[chunkName];
148
+ for (let i = 0; i < assets.length; ++i) {
149
+ const asset = assets[i];
150
+ if (asset.endsWith('.css')) {
151
+ const path = `${getPublicPath()}/${asset}`;
152
+ if (--refCounts[path] <= 0) {
153
+ document.head.querySelector(`link[href="${path}"]`)!.remove();
154
+ }
155
+ }
156
+ }
157
+ }
158
+
159
+ // Holds the set of chunk names already used for splitComponent() calls.
160
+ const usedChunkNames = new Set();
161
+
162
+ type ComponentOrModule<PropsT> = ComponentType<PropsT> | {
163
+ default: ComponentType<PropsT>,
164
+ };
165
+
166
+ /**
167
+ * Given an async component retrieval function `getComponent()` it creates
168
+ * a special "code split" component, which uses <Suspense> to asynchronously
169
+ * load on demand the code required by `getComponent()`.
170
+ * @param options
171
+ * @param options.chunkName
172
+ * @param {function} options.getComponent
173
+ * @param {React.Element} [options.placeholder]
174
+ * @return {React.ElementType}
175
+ */
176
+ export default function splitComponent<
177
+ ComponentPropsT extends { children?: ReactNode },
178
+ >({
179
+ chunkName,
180
+ getComponent,
181
+ placeholder,
182
+ }: {
183
+ chunkName: string;
184
+ getComponent: () => Promise<ComponentOrModule<ComponentPropsT>>,
185
+ placeholder?: ReactNode,
186
+ }) {
187
+ // On the client side we can check right away if the chunk name is known.
188
+ if (IS_CLIENT_SIDE) assertChunkName(chunkName, clientChunkGroups);
189
+
190
+ // The correct usage of splitComponent() assumes a single call per chunk.
191
+ if (usedChunkNames.has(chunkName)) {
192
+ throw Error(`Repeated splitComponent() call for the chunk "${chunkName}"`);
193
+ } else usedChunkNames.add(chunkName);
194
+
195
+ const LazyComponent = lazy(async () => {
196
+ const resolved = await getComponent();
197
+ const Component = 'default' in resolved ? resolved.default : resolved;
198
+
199
+ // This pre-loads necessary stylesheets prior to the first mount of
200
+ // the component (the lazy load function is executed by React one at
201
+ // the frist mount).
202
+ if (IS_CLIENT_SIDE) {
203
+ await bookStyleSheets(chunkName, clientChunkGroups, false);
204
+ }
205
+
206
+ const Wrapper = forwardRef((
207
+ { children, ...rest }: ComponentPropsT,
208
+ ref,
209
+ ) => {
210
+ // On the server side we'll assert the chunk name here,
211
+ // and also push it to the SSR chunks array.
212
+ if (IS_SERVER_SIDE) {
213
+ const { chunkGroups, chunks } = getSsrContext()!;
214
+ assertChunkName(chunkName, chunkGroups);
215
+ if (!chunks.includes(chunkName)) chunks.push(chunkName);
216
+ }
217
+
218
+ // This takes care about stylesheets management every time an instance of
219
+ // this component is mounted / unmounted.
220
+ useInsertionEffect(() => {
221
+ bookStyleSheets(chunkName, clientChunkGroups, true);
222
+ return () => freeStyleSheets(chunkName, clientChunkGroups);
223
+ }, []);
224
+
225
+ return (
226
+ <Component ref={ref} {...rest as ComponentPropsT}>
227
+ {children}
228
+ </Component>
229
+ );
230
+ });
231
+
232
+ return { default: Wrapper };
233
+ });
234
+
235
+ const CodeSplit: React.FunctionComponent<ComponentPropsT> = ({
236
+ children,
237
+ ...rest
238
+ }: ComponentPropsT) => (
239
+ <Suspense fallback={placeholder}>
240
+ <LazyComponent {...rest as Parameters<typeof LazyComponent>[0]}>
241
+ {children}
242
+ </LazyComponent>
243
+ </Suspense>
244
+ );
245
+
246
+ CodeSplit.propTypes = {
247
+ children: PT.node,
248
+ } as React.WeakValidationMap<ComponentPropsT>;
249
+
250
+ CodeSplit.defaultProps = {
251
+ children: undefined,
252
+ } as Partial<ComponentPropsT>;
253
+
254
+ return CodeSplit;
255
+ }
@@ -0,0 +1,118 @@
1
+ /* global document */
2
+
3
+ import Cookie from 'cookie';
4
+ import dayjs from 'dayjs';
5
+ import { useEffect } from 'react';
6
+
7
+ import {
8
+ DAY_MS,
9
+ HOUR_MS,
10
+ MIN_MS,
11
+ SEC_MS,
12
+ YEAR_MS,
13
+ timer,
14
+ } from '@dr.pogodin/js-utils';
15
+
16
+ import { type ForceT, useGlobalState } from '@dr.pogodin/react-global-state';
17
+
18
+ import { getSsrContext } from './globalState';
19
+
20
+ /**
21
+ * This react hook wraps Date.now() function in a SSR friendly way,
22
+ * ensuring that all calls to useCurrent() within the same render return
23
+ * exactly the same time (which is retrieved from Date.now() first, and
24
+ * then stored in the global state to be reused in all other calls), which
25
+ * is also passed and used in the first client side render, and then updated
26
+ * with a finite precision to avoid infinite re-rendering loops.
27
+ * @param [options] Optional settings.
28
+ * @param [options.globalStatePath="currentTime"] Global state path
29
+ * to keep the current time value.
30
+ * @param [options.precision= 5 * time.SEC_MS] Current time precision.
31
+ * The hook won't update the current time stored in the global state unless it
32
+ * is different from Date.now() result by this number (in milliseconds).
33
+ * Default to 5 seconds.
34
+ * @param [options.autorefresh=false] Set `true` to automatically
35
+ * refresh time stored in the global state with the given `precision` (and
36
+ * thus automatically re-rendering components dependent on this hook, or
37
+ * the global state with the period equal to the `precision`.
38
+ * @return Unix timestamp in milliseconds.
39
+ */
40
+ // TODO: Should we request the state type as generic parameter, to be able
41
+ // to verify the give globalStatePath is correct?
42
+ export function useCurrent({
43
+ autorefresh = false,
44
+ globalStatePath = 'currentTime',
45
+ precision = 5 * SEC_MS,
46
+ } = {}) {
47
+ const [now, setter] = useGlobalState<ForceT, number>(globalStatePath, Date.now);
48
+ useEffect(() => {
49
+ let timerId: NodeJS.Timeout;
50
+ const update = () => {
51
+ setter((old) => {
52
+ const neu = Date.now();
53
+ return Math.abs(neu - old) > precision ? neu : old;
54
+ });
55
+ if (autorefresh) timerId = setTimeout(update, precision);
56
+ };
57
+ update();
58
+ return () => {
59
+ if (timerId) clearTimeout(timerId);
60
+ };
61
+ }, [autorefresh, precision, setter]);
62
+ return now;
63
+ }
64
+
65
+ /**
66
+ * Wraps the standard Date.getTimezoneOffset() method in a SSR-friendly way.
67
+ * This hook retrieves the offset value at the client side and uses a cookie
68
+ * to pass it to the server in subsequent requests from that user. At the server
69
+ * side the value from cookie is used in renders and passed back to the client
70
+ * via the global state. Prior to the value being known (in the very first
71
+ * request from the user, when the cookie is still missing), zero value is used
72
+ * as the default value.
73
+ *
74
+ * @param {object} [options] Optional settings.
75
+ * @param {string} [options.cookieName="timezoneOffset"] Optional. The name of
76
+ * cookie to use to store the timezone offset. Defaults "timezoneOffset". Set
77
+ * to a falsy value to forbid using cookies altogether (in that case the hook
78
+ * will always return zero value at the server-side, and in the first render
79
+ * at the client-side).
80
+ * @param {string} [options.timezoneOffset="timezoneOffset"] Optional.
81
+ * The global state path to store the offset. Defaults "timezoneOffset".
82
+ * @return {number} Timezone offset.
83
+ */
84
+ // TODO: Should we request the state type as generic parameter, to be able
85
+ // to verify the give globalStatePath is correct?
86
+ export function useTimezoneOffset({
87
+ cookieName = 'timezoneOffset',
88
+ globalStatePath = 'timezoneOffset',
89
+ } = {}) {
90
+ const ssrContext = getSsrContext(false);
91
+ const [offset, setOffset] = useGlobalState<ForceT, number>(globalStatePath, () => {
92
+ const value = cookieName && ssrContext?.req?.cookies?.[cookieName];
93
+ return value ? parseInt(value, 10) : 0;
94
+ });
95
+ useEffect(() => {
96
+ const date = new Date();
97
+ const value = date.getTimezoneOffset();
98
+ setOffset(value);
99
+ if (cookieName) {
100
+ document.cookie = Cookie.serialize(cookieName, value.toString(), { path: '/' });
101
+ }
102
+ }, [cookieName, setOffset]);
103
+ return offset;
104
+ }
105
+
106
+ const time = {
107
+ DAY_MS,
108
+ HOUR_MS,
109
+ MIN_MS,
110
+ SEC_MS,
111
+ YEAR_MS,
112
+ now: Date.now,
113
+ timer,
114
+ useCurrent,
115
+ useTimezoneOffset,
116
+ };
117
+
118
+ export default Object.assign(dayjs, time);
@@ -0,0 +1,45 @@
1
+ import { IS_CLIENT_SIDE } from './isomorphy';
2
+
3
+ /**
4
+ * Requires the specified module without including it into the bundle during
5
+ * Webpack build.
6
+ * @param modulePath
7
+ * @param [basePath]
8
+ * @return Required module.
9
+ */
10
+ export function requireWeak(
11
+ modulePath: string,
12
+ basePath?: string,
13
+ ): NodeJS.Module | null {
14
+ if (IS_CLIENT_SIDE) return null;
15
+
16
+ try {
17
+ /* eslint-disable no-eval */
18
+ const { resolve } = eval('require')('path');
19
+ const path = basePath ? resolve(basePath, modulePath) : modulePath;
20
+ const { default: def, ...named } = eval('require')(path);
21
+ /* eslint-enable no-eval */
22
+
23
+ if (!def) return named;
24
+
25
+ Object.entries(named).forEach(([key, value]) => {
26
+ if (def[key]) throw Error('Conflict between default and named exports');
27
+ def[key] = value;
28
+ });
29
+ return def;
30
+ } catch {
31
+ return null;
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Resolves specified module path with help of Babel's module resolver.
37
+ * Yes, the function itself just returns its argument to the caller, but Babel
38
+ * is configured to resolve the first argument of resolveWeak(..) function, thus
39
+ * the result will be the resolved path.
40
+ * @param {string} modulePath
41
+ * @return {string} Absolute or relative path to the module.
42
+ */
43
+ export function resolveWeak(modulePath: string): string {
44
+ return modulePath;
45
+ }
@@ -0,0 +1,52 @@
1
+ /* Eric Meyer's "Reset CSS" 2.0 */
2
+
3
+ /* http://meyerweb.com/eric/tools/css/reset/
4
+ v2.0 | 20110126
5
+ License: none (public domain)
6
+ */
7
+
8
+ /* Having all selectors at individual lines is unreadable in the case of this
9
+ * style reset sheet. */
10
+ /* stylelint-disable selector-list-comma-newline-after */
11
+ a, abbr, acronym, address, applet, article, aside, audio, b, big, blockquote,
12
+ body, canvas, caption, center, cite, code, dd, del, details, dfn, div, dl, dt,
13
+ em, embed, fieldset, figcaption, figure, footer, form, h1, h2, h3, h4, h5, h6,
14
+ header, hgroup, html, i, iframe, img, ins, kbd, label, legend, li, mark, menu,
15
+ nav, object, ol, output, p, pre, q, ruby, s, samp, section, small, span,
16
+ strike, strong,sub, summary, sup, table, tbody, td, tfoot, th, thead, time, tr,
17
+ tt, u, ul, var, video {
18
+ margin: 0;
19
+ padding: 0;
20
+ border: 0;
21
+ font: inherit;
22
+ font-size: 100%;
23
+ vertical-align: baseline;
24
+ }
25
+
26
+ /* HTML5 display-role reset for older browsers */
27
+ article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav,
28
+ section {
29
+ display: block;
30
+ }
31
+
32
+ body {
33
+ line-height: 1;
34
+ }
35
+
36
+ ol, ul {
37
+ list-style: none;
38
+ }
39
+
40
+ blockquote, q {
41
+ quotes: none;
42
+ }
43
+
44
+ blockquote::before, blockquote::after, q::before, q::after {
45
+ content: "";
46
+ content: none;
47
+ }
48
+
49
+ table {
50
+ border-collapse: collapse;
51
+ border-spacing: 0;
52
+ }
@@ -0,0 +1,11 @@
1
+ /* Global styles. */
2
+
3
+ @import "_global/reset";
4
+
5
+ body {
6
+ text-rendering: auto;
7
+ }
8
+
9
+ * {
10
+ box-sizing: border-box;
11
+ }
@@ -0,0 +1,18 @@
1
+ // TS configuration for configs pre-building from TS sources.
2
+
3
+ {
4
+ "extends": "@tsconfig/recommended",
5
+ "include": [
6
+ "config-ts-src",
7
+ ],
8
+ "compilerOptions": {
9
+ "declaration": true,
10
+ "jsx": "react-jsx",
11
+ "rootDir": "config-ts-src",
12
+ "outDir": "config",
13
+ "paths": {
14
+ "*": ["./src/shared/*", "./src/*", "./*"]
15
+ },
16
+ "strict": true
17
+ }
18
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,27 @@
1
+ // TS config for ESLint, editor plugins, and other tools that look-up and use
2
+ // the default tsconfig.json file.
3
+ {
4
+ // Note: Full relative paths, as some tools don't support Node module
5
+ // resolution mechanics.
6
+ "extends": "./node_modules/@tsconfig/recommended/tsconfig.json",
7
+
8
+ "compilerOptions": {
9
+ "jsx": "react-jsx",
10
+
11
+ // BEWARE: Different settings are likely to transform dynamic import()
12
+ // statements in the code, thus breaking Webpack's code splitting.
13
+ // See: https://davidea.st/articles/webpack-typescript-code-split-wont-work
14
+ "module": "ESNext",
15
+ "moduleResolution": "Node",
16
+
17
+ "paths": {
18
+ "*": ["./src/shared/*", "./src/*", "./*"]
19
+ },
20
+ "rootDirs": [".", "build/types-scss"],
21
+ "strict": true,
22
+
23
+ // This ensures TypeScript does not remove unused imports, which may cause
24
+ // troubles when we rely on some side-effects from imported modules.
25
+ "verbatimModuleSyntax": true,
26
+ }
27
+ }