@rstest/browser-react 0.9.1 → 0.9.2

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/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { beforeEach } from "@rstest/core";
2
- import { cleanup } from "./879.js";
2
+ import { cleanup } from "./pure.js";
3
3
  beforeEach(async ()=>{
4
4
  await cleanup();
5
5
  });
6
- export { act, cleanup, configure, render, renderHook } from "./879.js";
6
+ export { act, cleanup, configure, render, renderHook } from "./pure.js";
package/dist/pure.js CHANGED
@@ -1 +1,111 @@
1
- export { act, cleanup, configure, render, renderHook } from "./879.js";
1
+ import { StrictMode, act, createElement, useEffect } from "react";
2
+ import { createRoot } from "react-dom/client";
3
+ let activeActs = 0;
4
+ function setActEnvironment(value) {
5
+ globalThis.IS_REACT_ACT_ENVIRONMENT = value;
6
+ }
7
+ function updateActEnvironment() {
8
+ setActEnvironment(activeActs > 0);
9
+ }
10
+ const _act = act;
11
+ const act_act = 'function' != typeof _act ? async (callback)=>{
12
+ await callback();
13
+ } : async (callback)=>{
14
+ activeActs++;
15
+ updateActEnvironment();
16
+ try {
17
+ await _act(callback);
18
+ } finally{
19
+ activeActs--;
20
+ updateActEnvironment();
21
+ }
22
+ };
23
+ const mountedContainers = new Set();
24
+ const mountedRootEntries = [];
25
+ const config = {
26
+ reactStrictMode: false
27
+ };
28
+ function pure_createRoot(container) {
29
+ const root = createRoot(container);
30
+ return {
31
+ render: (element)=>root.render(element),
32
+ unmount: ()=>root.unmount()
33
+ };
34
+ }
35
+ function strictModeIfNeeded(ui) {
36
+ return config.reactStrictMode ? /*#__PURE__*/ createElement(StrictMode, null, ui) : ui;
37
+ }
38
+ function wrapUiIfNeeded(ui, wrapper) {
39
+ return wrapper ? /*#__PURE__*/ createElement(wrapper, null, ui) : ui;
40
+ }
41
+ async function render(ui, options = {}) {
42
+ const { wrapper } = options;
43
+ let { container, baseElement } = options;
44
+ if (!baseElement) baseElement = document.body;
45
+ if (!container) container = baseElement.appendChild(document.createElement('div'));
46
+ let root;
47
+ if (mountedContainers.has(container)) {
48
+ const entry = mountedRootEntries.find((e)=>e.container === container);
49
+ if (!entry) throw new Error('Container is tracked but root entry not found');
50
+ root = entry.root;
51
+ } else {
52
+ root = pure_createRoot(container);
53
+ mountedRootEntries.push({
54
+ container,
55
+ root
56
+ });
57
+ mountedContainers.add(container);
58
+ }
59
+ const wrappedUi = wrapUiIfNeeded(strictModeIfNeeded(ui), wrapper);
60
+ await act_act(()=>root.render(wrappedUi));
61
+ return {
62
+ container,
63
+ baseElement,
64
+ unmount: async ()=>{
65
+ await act_act(()=>root.unmount());
66
+ },
67
+ rerender: async (newUi)=>{
68
+ const wrapped = wrapUiIfNeeded(strictModeIfNeeded(newUi), wrapper);
69
+ await act_act(()=>root.render(wrapped));
70
+ },
71
+ asFragment: ()=>document.createRange().createContextualFragment(container.innerHTML)
72
+ };
73
+ }
74
+ async function renderHook(renderCallback, options = {}) {
75
+ const { initialProps, ...renderOptions } = options;
76
+ const result = {
77
+ current: void 0
78
+ };
79
+ function TestComponent({ hookProps }) {
80
+ const value = renderCallback(hookProps);
81
+ useEffect(()=>{
82
+ result.current = value;
83
+ });
84
+ return null;
85
+ }
86
+ const { rerender: baseRerender, unmount } = await render(/*#__PURE__*/ createElement(TestComponent, {
87
+ hookProps: initialProps
88
+ }), renderOptions);
89
+ return {
90
+ result,
91
+ rerender: async (props)=>{
92
+ await baseRerender(/*#__PURE__*/ createElement(TestComponent, {
93
+ hookProps: props
94
+ }));
95
+ },
96
+ unmount,
97
+ act: act_act
98
+ };
99
+ }
100
+ async function cleanup() {
101
+ for (const { root, container } of mountedRootEntries){
102
+ await act_act(()=>root.unmount());
103
+ if (container.parentNode === document.body) document.body.removeChild(container);
104
+ }
105
+ mountedRootEntries.length = 0;
106
+ mountedContainers.clear();
107
+ }
108
+ function configure(customConfig) {
109
+ Object.assign(config, customConfig);
110
+ }
111
+ export { act_act as act, cleanup, configure, render, renderHook };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rstest/browser-react",
3
- "version": "0.9.1",
3
+ "version": "0.9.2",
4
4
  "type": "module",
5
5
  "description": "React component testing support for Rstest browser mode",
6
6
  "license": "MIT",
@@ -25,16 +25,16 @@
25
25
  "peerDependencies": {
26
26
  "react": "^17.0.0 || ^18.0.0 || ^19.0.0",
27
27
  "react-dom": "^17.0.0 || ^18.0.0 || ^19.0.0",
28
- "@rstest/core": "^0.9.1"
28
+ "@rstest/core": "^0.9.2"
29
29
  },
30
30
  "devDependencies": {
31
- "@rslib/core": "^0.19.6",
31
+ "@rslib/core": "0.20.0",
32
32
  "@types/react": "^19.2.14",
33
33
  "@types/react-dom": "^19.2.3",
34
34
  "react": "^19.2.4",
35
35
  "react-dom": "^19.2.4",
36
36
  "typescript": "^5.9.3",
37
- "@rstest/core": "0.9.1",
37
+ "@rstest/core": "0.9.2",
38
38
  "@rstest/tsconfig": "0.0.1"
39
39
  },
40
40
  "keywords": [
package/dist/879.js DELETED
@@ -1,111 +0,0 @@
1
- import { StrictMode, act, createElement, useEffect } from "react";
2
- import { createRoot } from "react-dom/client";
3
- let activeActs = 0;
4
- function setActEnvironment(value) {
5
- globalThis.IS_REACT_ACT_ENVIRONMENT = value;
6
- }
7
- function updateActEnvironment() {
8
- setActEnvironment(activeActs > 0);
9
- }
10
- const _act = act;
11
- const act_act = 'function' != typeof _act ? async (callback)=>{
12
- await callback();
13
- } : async (callback)=>{
14
- activeActs++;
15
- updateActEnvironment();
16
- try {
17
- await _act(callback);
18
- } finally{
19
- activeActs--;
20
- updateActEnvironment();
21
- }
22
- };
23
- const mountedContainers = new Set();
24
- const mountedRootEntries = [];
25
- const config = {
26
- reactStrictMode: false
27
- };
28
- function pure_createRoot(container) {
29
- const root = createRoot(container);
30
- return {
31
- render: (element)=>root.render(element),
32
- unmount: ()=>root.unmount()
33
- };
34
- }
35
- function strictModeIfNeeded(ui) {
36
- return config.reactStrictMode ? /*#__PURE__*/ createElement(StrictMode, null, ui) : ui;
37
- }
38
- function wrapUiIfNeeded(ui, wrapper) {
39
- return wrapper ? /*#__PURE__*/ createElement(wrapper, null, ui) : ui;
40
- }
41
- async function render(ui, options = {}) {
42
- const { wrapper } = options;
43
- let { container, baseElement } = options;
44
- if (!baseElement) baseElement = document.body;
45
- if (!container) container = baseElement.appendChild(document.createElement('div'));
46
- let root;
47
- if (mountedContainers.has(container)) {
48
- const entry = mountedRootEntries.find((e)=>e.container === container);
49
- if (!entry) throw new Error('Container is tracked but root entry not found');
50
- root = entry.root;
51
- } else {
52
- root = pure_createRoot(container);
53
- mountedRootEntries.push({
54
- container,
55
- root
56
- });
57
- mountedContainers.add(container);
58
- }
59
- const wrappedUi = wrapUiIfNeeded(strictModeIfNeeded(ui), wrapper);
60
- await act_act(()=>root.render(wrappedUi));
61
- return {
62
- container,
63
- baseElement,
64
- unmount: async ()=>{
65
- await act_act(()=>root.unmount());
66
- },
67
- rerender: async (newUi)=>{
68
- const wrapped = wrapUiIfNeeded(strictModeIfNeeded(newUi), wrapper);
69
- await act_act(()=>root.render(wrapped));
70
- },
71
- asFragment: ()=>document.createRange().createContextualFragment(container.innerHTML)
72
- };
73
- }
74
- async function renderHook(renderCallback, options = {}) {
75
- const { initialProps, ...renderOptions } = options;
76
- const result = {
77
- current: void 0
78
- };
79
- function TestComponent({ hookProps }) {
80
- const value = renderCallback(hookProps);
81
- useEffect(()=>{
82
- result.current = value;
83
- });
84
- return null;
85
- }
86
- const { rerender: baseRerender, unmount } = await render(/*#__PURE__*/ createElement(TestComponent, {
87
- hookProps: initialProps
88
- }), renderOptions);
89
- return {
90
- result,
91
- rerender: async (props)=>{
92
- await baseRerender(/*#__PURE__*/ createElement(TestComponent, {
93
- hookProps: props
94
- }));
95
- },
96
- unmount,
97
- act: act_act
98
- };
99
- }
100
- async function cleanup() {
101
- for (const { root, container } of mountedRootEntries){
102
- await act_act(()=>root.unmount());
103
- if (container.parentNode === document.body) document.body.removeChild(container);
104
- }
105
- mountedRootEntries.length = 0;
106
- mountedContainers.clear();
107
- }
108
- function configure(customConfig) {
109
- Object.assign(config, customConfig);
110
- }
111
- export { act_act as act, cleanup, configure, render, renderHook };