@etsoo/react 1.8.20 → 1.8.22
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/__tests__/EventWatcher.tsx +2 -1
- package/__tests__/States.tsx +27 -29
- package/__tests__/tsconfig.json +5 -4
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/uses/useRequiredContext.d.ts +7 -0
- package/lib/uses/useRequiredContext.js +13 -0
- package/package.json +12 -21
- package/src/index.ts +1 -0
- package/src/uses/useRequiredContext.ts +16 -0
- package/tsconfig.json +1 -1
- package/vite.config.ts +11 -0
|
@@ -13,11 +13,12 @@ function App(props: { callback: () => void }) {
|
|
|
13
13
|
once: true
|
|
14
14
|
});
|
|
15
15
|
}, []);
|
|
16
|
+
|
|
16
17
|
return <button onClick={(event) => watcher.do(event)}></button>;
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
test("Tests for EventWatcher", () => {
|
|
20
|
-
const callback =
|
|
21
|
+
const callback = vi.fn();
|
|
21
22
|
render(<App callback={callback} />);
|
|
22
23
|
const button = screen.getByRole<HTMLButtonElement>("button");
|
|
23
24
|
button.click();
|
package/__tests__/States.tsx
CHANGED
|
@@ -1,39 +1,37 @@
|
|
|
1
|
-
import React from
|
|
2
|
-
import { render, screen } from
|
|
3
|
-
import { useAsyncState } from
|
|
4
|
-
import { act } from
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { render, screen } from "@testing-library/react";
|
|
3
|
+
import { useAsyncState } from "../src/uses/useAsyncState";
|
|
4
|
+
import { act } from "react";
|
|
5
5
|
|
|
6
6
|
function App(props: { callback: (state: number) => void }) {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
const { callback } = props;
|
|
8
|
+
const [state, setState] = useAsyncState(0);
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
const [state1] = useAsyncState<number>();
|
|
11
|
+
expect(state1).toBeUndefined();
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
const click = async () => {
|
|
14
|
+
const currentState = await setState((prev) => prev + 1);
|
|
15
|
+
callback(currentState + 1);
|
|
16
|
+
};
|
|
17
|
+
callback(state);
|
|
18
|
+
return <button onClick={click}>State: {state}</button>;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
test(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
test("Tests for useAsyncState", async () => {
|
|
22
|
+
const callback = vi.fn();
|
|
23
|
+
render(<App callback={callback} />);
|
|
24
|
+
const button = screen.getByRole<HTMLButtonElement>("button");
|
|
25
|
+
expect(button.innerHTML).toBe("State: 0");
|
|
26
|
+
act(() => {
|
|
27
|
+
button.click();
|
|
28
|
+
});
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
expect(button.innerHTML).toBe("State: 1");
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
expect(callback).toHaveBeenLastCalledWith(2);
|
|
32
|
+
// Wait for the state to be updated
|
|
33
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
35
34
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}, 100);
|
|
35
|
+
// Expect to happen
|
|
36
|
+
expect(callback).toHaveBeenLastCalledWith(2);
|
|
39
37
|
});
|
package/__tests__/tsconfig.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
|
-
"target": "
|
|
4
|
-
"module": "
|
|
5
|
-
"moduleResolution": "
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ES2022",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
6
|
"allowJs": false,
|
|
7
7
|
"skipLibCheck": true,
|
|
8
8
|
"esModuleInterop": true,
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
"isolatedModules": true,
|
|
14
14
|
"noEmit": true,
|
|
15
15
|
"jsx": "react",
|
|
16
|
-
"declaration": true
|
|
16
|
+
"declaration": true,
|
|
17
|
+
"types": ["vitest/globals"]
|
|
17
18
|
},
|
|
18
19
|
"include": [".."]
|
|
19
20
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export * from "./uses/useDelayedExecutor";
|
|
|
26
26
|
export * from "./uses/useDimensions";
|
|
27
27
|
export * from "./uses/useParamsEx";
|
|
28
28
|
export * from "./uses/useRefs";
|
|
29
|
+
export * from "./uses/useRequiredContext";
|
|
29
30
|
export * from "./uses/useSearchParamsEx";
|
|
30
31
|
export * from "./uses/useTimeout";
|
|
31
32
|
export * from "./uses/useWindowScroll";
|
package/lib/index.js
CHANGED
|
@@ -31,6 +31,7 @@ export * from "./uses/useDelayedExecutor";
|
|
|
31
31
|
export * from "./uses/useDimensions";
|
|
32
32
|
export * from "./uses/useParamsEx";
|
|
33
33
|
export * from "./uses/useRefs";
|
|
34
|
+
export * from "./uses/useRequiredContext";
|
|
34
35
|
export * from "./uses/useSearchParamsEx";
|
|
35
36
|
export * from "./uses/useTimeout";
|
|
36
37
|
export * from "./uses/useWindowScroll";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Required context
|
|
4
|
+
* @param context Context
|
|
5
|
+
* @returns Value
|
|
6
|
+
*/
|
|
7
|
+
export function useRequiredContext(context) {
|
|
8
|
+
const value = React.useContext(context);
|
|
9
|
+
if (value == null) {
|
|
10
|
+
throw new Error(`useRequiredContext: ${context.displayName} is required`);
|
|
11
|
+
}
|
|
12
|
+
return value;
|
|
13
|
+
}
|
package/package.json
CHANGED
|
@@ -1,27 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/react",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.22",
|
|
4
4
|
"description": "TypeScript ReactJs UI Independent Framework",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"types": "lib/index.d.ts",
|
|
7
8
|
"scripts": {
|
|
8
9
|
"build": "tsc",
|
|
9
|
-
"test": "
|
|
10
|
-
"test:debug": "node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand"
|
|
11
|
-
},
|
|
12
|
-
"jest": {
|
|
13
|
-
"automock": false,
|
|
14
|
-
"testMatch": [
|
|
15
|
-
"<rootDir>/__tests__/**/*.{ts,tsx}"
|
|
16
|
-
],
|
|
17
|
-
"testEnvironment": "jsdom",
|
|
18
|
-
"transform": {
|
|
19
|
-
".+\\.jsx?$": "babel-jest",
|
|
20
|
-
".+\\.tsx?$": "ts-jest"
|
|
21
|
-
},
|
|
22
|
-
"transformIgnorePatterns": [
|
|
23
|
-
"/node_modules/(?!@etsoo/).+\\.js$"
|
|
24
|
-
]
|
|
10
|
+
"test": "vitest"
|
|
25
11
|
},
|
|
26
12
|
"repository": {
|
|
27
13
|
"type": "git",
|
|
@@ -56,6 +42,11 @@
|
|
|
56
42
|
"react-router-dom": "^7.1.1",
|
|
57
43
|
"react-window": "^1.8.10"
|
|
58
44
|
},
|
|
45
|
+
"overrides": {
|
|
46
|
+
"react": "$react",
|
|
47
|
+
"react-dom": "$react-dom",
|
|
48
|
+
"@emotion/react": "$@emotion/react"
|
|
49
|
+
},
|
|
59
50
|
"devDependencies": {
|
|
60
51
|
"@babel/cli": "^7.25.9",
|
|
61
52
|
"@babel/core": "^7.26.0",
|
|
@@ -68,9 +59,9 @@
|
|
|
68
59
|
"@types/react": "^18.3.18",
|
|
69
60
|
"@types/react-dom": "^18.3.1",
|
|
70
61
|
"@types/react-window": "^1.8.8",
|
|
71
|
-
"
|
|
72
|
-
"
|
|
73
|
-
"
|
|
74
|
-
"
|
|
62
|
+
"@vitejs/plugin-react": "^4.3.4",
|
|
63
|
+
"jsdom": "^26.0.0",
|
|
64
|
+
"typescript": "^5.7.3",
|
|
65
|
+
"vitest": "^2.1.8"
|
|
75
66
|
}
|
|
76
67
|
}
|
package/src/index.ts
CHANGED
|
@@ -41,6 +41,7 @@ export * from "./uses/useDelayedExecutor";
|
|
|
41
41
|
export * from "./uses/useDimensions";
|
|
42
42
|
export * from "./uses/useParamsEx";
|
|
43
43
|
export * from "./uses/useRefs";
|
|
44
|
+
export * from "./uses/useRequiredContext";
|
|
44
45
|
export * from "./uses/useSearchParamsEx";
|
|
45
46
|
export * from "./uses/useTimeout";
|
|
46
47
|
export * from "./uses/useWindowScroll";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Required context
|
|
5
|
+
* @param context Context
|
|
6
|
+
* @returns Value
|
|
7
|
+
*/
|
|
8
|
+
export function useRequiredContext<T>(context: React.Context<T>) {
|
|
9
|
+
const value = React.useContext(context);
|
|
10
|
+
|
|
11
|
+
if (value == null) {
|
|
12
|
+
throw new Error(`useRequiredContext: ${context.displayName} is required`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return value;
|
|
16
|
+
}
|
package/tsconfig.json
CHANGED
package/vite.config.ts
ADDED