@tramvai/test-react 1.50.1 → 1.51.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/README.md CHANGED
@@ -14,6 +14,20 @@ If you are using jest for testing, consider to add a library [@testing-library/j
14
14
  npm i --save-dev @tramvai/test-react
15
15
  ```
16
16
 
17
+ ## Explanation
18
+
19
+ ### act
20
+
21
+ Based on the React [act test helper](https://reactjs.org/docs/test-utils.html#act) that is used to perform rerender component after start changes.
22
+
23
+ Put you actions that will update React state inside `act` function in order to check result render in the next code.
24
+
25
+ :::warning
26
+
27
+ Current wrapper should be awaited in tests to execute some additional internal steps
28
+
29
+ :::
30
+
17
31
  ## How to
18
32
 
19
33
  ### Test component
package/lib/index.es.js CHANGED
@@ -5,6 +5,7 @@ import { Provider } from '@tramvai/state';
5
5
  import { DIContext } from '@tramvai/react';
6
6
  import { Provider as Provider$1 } from '@tinkoff/router';
7
7
  import { createMockContext, createMockRouter } from '@tramvai/test-mocks';
8
+ import { waitRaf } from '@tramvai/test-jsdom';
8
9
  import { renderHook, act as act$1 } from '@testing-library/react-hooks';
9
10
 
10
11
  const testComponent = (element, { providers, initialState, store, stores, di, context = createMockContext({ initialState, providers, di, store, stores }), currentRoute, currentUrl, router = createMockRouter({ currentRoute, currentUrl }), renderOptions, } = {}) => {
@@ -15,7 +16,16 @@ const testComponent = (element, { providers, initialState, store, stores, di, co
15
16
  return {
16
17
  render: renderResults,
17
18
  rerender: (el) => renderResults.rerender(React.createElement(Wrapper, null, el)),
18
- act,
19
+ act: (callback) => {
20
+ return act(async () => {
21
+ await callback();
22
+ if (typeof window.requestAnimationFrame !== 'undefined') {
23
+ // as some of the state changes in browsers uses requestAnimationFrame to batch updates in single one
24
+ // but in tests requestAnimationFrame should mostly should be resolved manually
25
+ await waitRaf();
26
+ }
27
+ });
28
+ },
19
29
  fireEvent,
20
30
  context,
21
31
  router,
package/lib/index.js CHANGED
@@ -8,6 +8,7 @@ var state = require('@tramvai/state');
8
8
  var react$1 = require('@tramvai/react');
9
9
  var router = require('@tinkoff/router');
10
10
  var testMocks = require('@tramvai/test-mocks');
11
+ var testJsdom = require('@tramvai/test-jsdom');
11
12
  var reactHooks = require('@testing-library/react-hooks');
12
13
 
13
14
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
@@ -22,7 +23,16 @@ const testComponent = (element, { providers, initialState, store, stores, di, co
22
23
  return {
23
24
  render: renderResults,
24
25
  rerender: (el) => renderResults.rerender(React__default["default"].createElement(Wrapper, null, el)),
25
- act: react.act,
26
+ act: (callback) => {
27
+ return react.act(async () => {
28
+ await callback();
29
+ if (typeof window.requestAnimationFrame !== 'undefined') {
30
+ // as some of the state changes in browsers uses requestAnimationFrame to batch updates in single one
31
+ // but in tests requestAnimationFrame should mostly should be resolved manually
32
+ await testJsdom.waitRaf();
33
+ }
34
+ });
35
+ },
26
36
  fireEvent: react.fireEvent,
27
37
  context,
28
38
  router: router$1,
@@ -15,7 +15,7 @@ declare type Options = OptionsDi & OptionsContext & OptionsRouter & {
15
15
  export declare const testComponent: (element: React.ReactElement, { providers, initialState, store, stores, di, context, currentRoute, currentUrl, router, renderOptions, }?: Options) => {
16
16
  render: import("@testing-library/react").RenderResult<typeof import("@testing-library/dom/types/queries"), HTMLElement>;
17
17
  rerender: (el: React.ReactElement) => void;
18
- act: typeof import("react-dom/test-utils").act;
18
+ act: (callback: () => Promise<void> | void) => Promise<undefined>;
19
19
  fireEvent: import("@testing-library/react").FireFunction & import("@testing-library/react").FireObject;
20
20
  context: import("@tramvai/tokens-common").ConsumerContext;
21
21
  router: AbstractRouter;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/test-react",
3
- "version": "1.50.1",
3
+ "version": "1.51.0",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -20,11 +20,12 @@
20
20
  "dependencies": {
21
21
  "@testing-library/react": "^11.2.6",
22
22
  "@testing-library/react-hooks": "^5.1.2",
23
- "@tinkoff/router": "0.1.66",
24
- "@tramvai/react": "1.50.1",
25
- "@tramvai/state": "1.50.1",
26
- "@tramvai/test-mocks": "1.50.1",
27
- "@tramvai/tokens-common": "1.50.1",
23
+ "@tinkoff/router": "0.1.67",
24
+ "@tramvai/react": "1.51.0",
25
+ "@tramvai/state": "1.51.0",
26
+ "@tramvai/test-jsdom": "1.51.0",
27
+ "@tramvai/test-mocks": "1.51.0",
28
+ "@tramvai/tokens-common": "1.51.0",
28
29
  "react-test-renderer": "^17.0.2",
29
30
  "tslib": "^2.0.3"
30
31
  },