@wandelbots/wandelbots-js-react-components 2.26.0-pr.feature-update-to-react-19.361.b99883a → 2.26.0-pr.feature-update-to-react-19.361.3244daa

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.
@@ -0,0 +1,2 @@
1
+ import '@testing-library/jest-dom';
2
+ //# sourceMappingURL=setup.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setup.d.ts","sourceRoot":"","sources":["../../src/test/setup.ts"],"names":[],"mappings":"AAAA,OAAO,2BAA2B,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wandelbots/wandelbots-js-react-components",
3
- "version": "2.26.0-pr.feature-update-to-react-19.361.b99883a",
3
+ "version": "2.26.0-pr.feature-update-to-react-19.361.3244daa",
4
4
  "description": "React UI toolkit for building applications on top of the Wandelbots platform",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -25,6 +25,8 @@
25
25
  "dev:wbjs": "nodemon --watch $WBJS_PATH -e tgz --exec 'npm install file:$WBJS_PATH && storybook dev -p 6006 --no-open'",
26
26
  "tsc": "tsc --pretty --noEmit -p stories/tsconfig.json",
27
27
  "test": "test-storybook --url http://127.0.0.1:6006 --index-json --browsers chromium",
28
+ "test:unit": "vitest",
29
+ "test:unit:run": "vitest run",
28
30
  "ci:test": "run-s ci:test:build ci:test:built",
29
31
  "ci:test:build": "storybook build --quiet --test",
30
32
  "ci:test:built": "run-p --race ci:test:server ci:test:runner",
@@ -62,6 +64,9 @@
62
64
  "@storybook/react-vite": "^9.0.8",
63
65
  "@storybook/test-runner": "^0.23.0",
64
66
  "@svgr/rollup": "^8.1.0",
67
+ "@testing-library/jest-dom": "^6.6.3",
68
+ "@testing-library/react": "^16.3.0",
69
+ "@testing-library/user-event": "^14.6.1",
65
70
  "@types/lodash-es": "^4.17.12",
66
71
  "@types/react": "^19.1.8",
67
72
  "@types/three": "^0.174.0",
@@ -75,6 +80,7 @@
75
80
  "i18next": "^25.2.1",
76
81
  "jest-simple-dot-reporter": "^1.0.5",
77
82
  "jest-summary-reporter": "^0.0.2",
83
+ "jsdom": "^26.1.0",
78
84
  "monaco-editor": "^0.52.2",
79
85
  "nodemon": "^3.1.9",
80
86
  "npm-run-all": "^4.1.5",
@@ -4,7 +4,7 @@ import type {
4
4
  DHParameter,
5
5
  MotionGroupStateResponse,
6
6
  } from "@wandelbots/nova-api/v1"
7
- import React, { useRef } from "react"
7
+ import React, { useEffect, useRef, useState, useCallback } from "react"
8
8
  import type { Group, Object3D } from "three"
9
9
  import { useAutorun } from "../utils/hooks"
10
10
  import { collectJoints } from "./robotModelLogic"
@@ -25,24 +25,57 @@ export default function RobotAnimator({
25
25
  Globals.assign({ frameLoop: "always" })
26
26
  const jointValues = useRef<number[]>([])
27
27
  const jointObjects = useRef<Object3D[]>([])
28
+ const [isInitialized, setIsInitialized] = useState(false)
28
29
  const { invalidate } = useThree()
29
30
 
30
31
  function setGroupRef(group: Group | null) {
31
- if (!group) return
32
+ if (!group) {
33
+ setIsInitialized(false)
34
+ return
35
+ }
32
36
 
33
37
  jointObjects.current = collectJoints(group)
34
38
 
35
- // Set initial position
36
- setRotation()
37
- invalidate()
39
+ // Only mark as initialized if we have valid joint objects
40
+ if (
41
+ jointObjects.current.length > 0 &&
42
+ jointObjects.current.every((obj) => obj != null)
43
+ ) {
44
+ setIsInitialized(true)
45
+ } else {
46
+ setIsInitialized(false)
47
+ }
38
48
  }
39
49
 
40
50
  function updateJoints(newJointValues: number[]) {
51
+ if (!isInitialized) return
52
+
41
53
  jointValues.current = newJointValues
42
54
  setSpring.start(Object.assign({}, jointValues.current) as any)
43
55
  }
44
56
 
45
- function setRotation() {
57
+ // Initialize spring only after joints are ready
58
+ const [axisValues, setSpring] = useSpring(() => ({
59
+ ...Object.assign(
60
+ {},
61
+ rapidlyChangingMotionState.state.joint_position.joints,
62
+ ),
63
+ onChange: () => {
64
+ if (isInitialized) {
65
+ setRotation()
66
+ invalidate()
67
+ }
68
+ },
69
+ onResolve: () => {
70
+ if (isInitialized) {
71
+ setRotation()
72
+ }
73
+ },
74
+ }))
75
+
76
+ const setRotation = useCallback(() => {
77
+ if (!isInitialized) return
78
+
46
79
  const updatedJointValues = jointObjects.current.map((_, objectIndex) =>
47
80
  (axisValues as any)[objectIndex].get(),
48
81
  )
@@ -59,9 +92,19 @@ export default function RobotAnimator({
59
92
  rotationSign * updatedJointValues[index]! + rotationOffset
60
93
  }
61
94
  }
62
- }
95
+ }, [isInitialized, axisValues, onRotationChanged, dhParameters])
96
+
97
+ // Effect to handle initial rotation when component becomes initialized
98
+ useEffect(() => {
99
+ if (isInitialized) {
100
+ setRotation()
101
+ invalidate()
102
+ }
103
+ }, [isInitialized, setRotation, invalidate])
63
104
 
64
105
  useAutorun(() => {
106
+ if (!isInitialized) return
107
+
65
108
  const newJointValues =
66
109
  rapidlyChangingMotionState.state.joint_position.joints.filter(
67
110
  (item) => item !== undefined,
@@ -70,19 +113,5 @@ export default function RobotAnimator({
70
113
  requestAnimationFrame(() => updateJoints(newJointValues))
71
114
  })
72
115
 
73
- const [axisValues, setSpring] = useSpring(() => ({
74
- ...Object.assign(
75
- {},
76
- rapidlyChangingMotionState.state.joint_position.joints,
77
- ),
78
- onChange: () => {
79
- setRotation()
80
- invalidate()
81
- },
82
- onResolve: () => {
83
- setRotation()
84
- },
85
- }))
86
-
87
116
  return <group ref={setGroupRef}>{children}</group>
88
117
  }
@@ -0,0 +1,287 @@
1
+ import { vi, describe, it, expect, beforeEach, afterEach } from "vitest"
2
+ import { Object3D } from "three"
3
+ import type { DHParameter, MotionGroupStateResponse } from "@wandelbots/nova-api/v1"
4
+
5
+ // Import the component for testing
6
+ import RobotAnimator from "./RobotAnimator"
7
+
8
+ // Mock the dependencies
9
+ vi.mock("../utils/hooks", () => ({
10
+ useAutorun: vi.fn(),
11
+ }))
12
+
13
+ vi.mock("./robotModelLogic", () => ({
14
+ collectJoints: vi.fn(),
15
+ }))
16
+
17
+ vi.mock("@react-spring/three", () => ({
18
+ Globals: {
19
+ assign: vi.fn(),
20
+ },
21
+ useSpring: vi.fn(),
22
+ }))
23
+
24
+ vi.mock("@react-three/fiber", () => ({
25
+ useThree: () => ({
26
+ invalidate: vi.fn(),
27
+ }),
28
+ }))
29
+
30
+ import { useAutorun } from "../utils/hooks"
31
+ import { collectJoints } from "./robotModelLogic"
32
+ import { useSpring } from "@react-spring/three"
33
+
34
+ const mockUseAutorun = useAutorun as any
35
+ const mockCollectJoints = collectJoints as any
36
+ const mockUseSpring = useSpring as any
37
+
38
+ describe("RobotAnimator Logic Tests", () => {
39
+ const mockMotionState: MotionGroupStateResponse = {
40
+ state: {
41
+ joint_position: {
42
+ joints: [0, 0.5, 1, 1.5, 2, 2.5],
43
+ },
44
+ },
45
+ } as any
46
+
47
+ const mockDhParameters: DHParameter[] = [
48
+ { theta: 0, reverse_rotation_direction: false },
49
+ { theta: 0.1, reverse_rotation_direction: true },
50
+ { theta: 0.2, reverse_rotation_direction: false },
51
+ { theta: 0.3, reverse_rotation_direction: true },
52
+ { theta: 0.4, reverse_rotation_direction: false },
53
+ { theta: 0.5, reverse_rotation_direction: true },
54
+ ]
55
+
56
+ let mockJointObjects: Object3D[]
57
+ let mockAxisValues: any
58
+ let mockSetSpring: any
59
+ let mockSpringConfig: any
60
+ let mockAutorunCallback: any
61
+
62
+ beforeEach(() => {
63
+ // Create mock joint objects
64
+ mockJointObjects = Array.from({ length: 6 }, (_, i) => {
65
+ const obj = new Object3D()
66
+ obj.name = `Joint_J${i + 1}`
67
+ obj.rotation.y = 0
68
+ return obj
69
+ })
70
+
71
+ // Mock axis values object that acts like spring values
72
+ mockAxisValues = {}
73
+ for (let i = 0; i < 6; i++) {
74
+ mockAxisValues[i] = {
75
+ get: vi.fn().mockReturnValue(mockMotionState.state.joint_position.joints[i]),
76
+ }
77
+ }
78
+
79
+ mockSetSpring = {
80
+ start: vi.fn(),
81
+ }
82
+
83
+ // Mock spring config that will be returned by useSpring
84
+ mockSpringConfig = {
85
+ onChange: vi.fn(),
86
+ onResolve: vi.fn(),
87
+ }
88
+
89
+ // Mock collectJoints to return our mock joint objects
90
+ mockCollectJoints.mockReturnValue(mockJointObjects)
91
+
92
+ // Mock useSpring to return our mock values and accept a factory function
93
+ mockUseSpring.mockImplementation((factory: any) => {
94
+ const config = factory()
95
+ mockSpringConfig.onChange = config.onChange
96
+ mockSpringConfig.onResolve = config.onResolve
97
+ return [mockAxisValues, mockSetSpring]
98
+ })
99
+
100
+ // Mock useAutorun to store the callback
101
+ mockUseAutorun.mockImplementation((callback: any) => {
102
+ mockAutorunCallback = callback
103
+ })
104
+ })
105
+
106
+ afterEach(() => {
107
+ vi.clearAllMocks()
108
+ })
109
+
110
+ it("should have correct component structure", () => {
111
+ expect(RobotAnimator).toBeDefined()
112
+ expect(typeof RobotAnimator).toBe("function")
113
+ })
114
+
115
+ it("should test joint rotation calculation logic", () => {
116
+ // Test the joint rotation calculation logic separately
117
+ const joints = mockJointObjects
118
+ const jointValues = [0, 0.5, 1, 1.5, 2, 2.5]
119
+ const dhParameters = mockDhParameters
120
+
121
+ // Simulate the rotation calculation that would happen in the component
122
+ joints.forEach((joint, index) => {
123
+ const dhParam = dhParameters[index]
124
+ const rotationOffset = dhParam.theta || 0
125
+ const rotationSign = dhParam.reverse_rotation_direction ? -1 : 1
126
+
127
+ joint.rotation.y = rotationSign * jointValues[index]! + rotationOffset
128
+ })
129
+
130
+ // Verify rotations are set correctly
131
+ expect(joints[0].rotation.y).toBe(0 * 1 + 0) // 0
132
+ expect(joints[1].rotation.y).toBe(0.5 * -1 + 0.1) // -0.4
133
+ expect(joints[2].rotation.y).toBe(1 * 1 + 0.2) // 1.2
134
+ expect(joints[3].rotation.y).toBe(1.5 * -1 + 0.3) // -1.2
135
+ expect(joints[4].rotation.y).toBe(2 * 1 + 0.4) // 2.4
136
+ expect(joints[5].rotation.y).toBe(2.5 * -1 + 0.5) // -2.0
137
+ })
138
+
139
+ it("should test joint value filtering logic", () => {
140
+ // Test the filtering logic that removes undefined values
141
+ const inputJoints = [1, undefined, 3, undefined, 5, 6]
142
+ const filteredJoints = inputJoints.filter((item) => item !== undefined)
143
+
144
+ expect(filteredJoints).toEqual([1, 3, 5, 6])
145
+ expect(filteredJoints.length).toBe(4)
146
+ })
147
+
148
+ it("should test DH parameter application", () => {
149
+ // Test various DH parameter combinations
150
+ const testCases = [
151
+ {
152
+ jointValue: 1.0,
153
+ dhParam: { theta: 0, reverse_rotation_direction: false },
154
+ expected: 1.0
155
+ },
156
+ {
157
+ jointValue: 1.0,
158
+ dhParam: { theta: 0.5, reverse_rotation_direction: false },
159
+ expected: 1.5
160
+ },
161
+ {
162
+ jointValue: 1.0,
163
+ dhParam: { theta: 0, reverse_rotation_direction: true },
164
+ expected: -1.0
165
+ },
166
+ {
167
+ jointValue: 1.0,
168
+ dhParam: { theta: 0.5, reverse_rotation_direction: true },
169
+ expected: -0.5
170
+ },
171
+ ]
172
+
173
+ testCases.forEach(({ jointValue, dhParam, expected }) => {
174
+ const joint = new Object3D()
175
+ const rotationOffset = dhParam.theta || 0
176
+ const rotationSign = dhParam.reverse_rotation_direction ? -1 : 1
177
+
178
+ joint.rotation.y = rotationSign * jointValue + rotationOffset
179
+
180
+ expect(joint.rotation.y).toBeCloseTo(expected, 5)
181
+ })
182
+ })
183
+
184
+ it("should test component props validation", () => {
185
+ // Test that the component has the correct prop structure
186
+ // We can't call the component directly without React context,
187
+ // so we'll validate the prop types instead
188
+ const validProps = {
189
+ rapidlyChangingMotionState: mockMotionState,
190
+ dhParameters: mockDhParameters,
191
+ onRotationChanged: vi.fn(),
192
+ children: null,
193
+ }
194
+
195
+ // Verify the prop structure matches expected types
196
+ expect(validProps.rapidlyChangingMotionState).toBeDefined()
197
+ expect(validProps.dhParameters).toBeDefined()
198
+ expect(Array.isArray(validProps.dhParameters)).toBe(true)
199
+ expect(typeof validProps.onRotationChanged).toBe("function")
200
+ expect(validProps.children).toBeDefined()
201
+ })
202
+
203
+ it("should test empty joint array handling", () => {
204
+ // Test behavior when no joints are available
205
+ const emptyJoints: Object3D[] = []
206
+
207
+ // This simulates the isInitialized check in the component
208
+ const isInitialized = emptyJoints.length > 0 && emptyJoints.every((obj) => obj != null)
209
+
210
+ expect(isInitialized).toBe(false)
211
+ })
212
+
213
+ it("should test joint object validation", () => {
214
+ // Test the joint object validation logic
215
+ const validJoints = [new Object3D(), new Object3D(), new Object3D()]
216
+ const invalidJoints = [new Object3D(), null, new Object3D()]
217
+
218
+ const isValidJointsInitialized = validJoints.length > 0 && validJoints.every((obj) => obj != null)
219
+ const isInvalidJointsInitialized = invalidJoints.length > 0 && invalidJoints.every((obj) => obj != null)
220
+
221
+ expect(isValidJointsInitialized).toBe(true)
222
+ expect(isInvalidJointsInitialized).toBe(false)
223
+ })
224
+
225
+ it("should test motion state data structure", () => {
226
+ // Test that motion state has the expected structure
227
+ expect(mockMotionState.state).toBeDefined()
228
+ expect(mockMotionState.state.joint_position).toBeDefined()
229
+ expect(mockMotionState.state.joint_position.joints).toBeDefined()
230
+ expect(Array.isArray(mockMotionState.state.joint_position.joints)).toBe(true)
231
+ expect(mockMotionState.state.joint_position.joints.length).toBe(6)
232
+ })
233
+
234
+ it("should test DH parameters structure", () => {
235
+ // Test that DH parameters have the expected structure
236
+ mockDhParameters.forEach((param, index) => {
237
+ expect(param).toBeDefined()
238
+ expect(typeof param.theta).toBe("number")
239
+ expect(typeof param.reverse_rotation_direction).toBe("boolean")
240
+ })
241
+ })
242
+
243
+ it("should test onRotationChanged callback structure", () => {
244
+ // Test callback function signature
245
+ const mockCallback = vi.fn()
246
+ const testJoints = [new Object3D(), new Object3D()]
247
+ const testValues = [1.0, 2.0]
248
+
249
+ // Simulate calling the callback
250
+ mockCallback(testJoints, testValues)
251
+
252
+ expect(mockCallback).toHaveBeenCalledWith(testJoints, testValues)
253
+ expect(mockCallback).toHaveBeenCalledTimes(1)
254
+ })
255
+
256
+ it("should test race condition prevention logic", () => {
257
+ // Test the initialization state logic that prevents race conditions
258
+ let isInitialized = false
259
+ const joints: Object3D[] = []
260
+
261
+ // Simulate setGroupRef logic
262
+ function simulateSetGroupRef(group: any) {
263
+ if (!group) {
264
+ isInitialized = false
265
+ return
266
+ }
267
+
268
+ // Simulate collectJoints
269
+ const collectedJoints = group ? [new Object3D(), new Object3D()] : []
270
+
271
+ // Only mark as initialized if we have valid joint objects
272
+ if (collectedJoints.length > 0 && collectedJoints.every((obj) => obj != null)) {
273
+ isInitialized = true
274
+ } else {
275
+ isInitialized = false
276
+ }
277
+ }
278
+
279
+ // Test null group
280
+ simulateSetGroupRef(null)
281
+ expect(isInitialized).toBe(false)
282
+
283
+ // Test valid group
284
+ simulateSetGroupRef({ type: "Group" })
285
+ expect(isInitialized).toBe(true)
286
+ })
287
+ })
@@ -0,0 +1,109 @@
1
+ import '@testing-library/jest-dom'
2
+ import { beforeAll, vi } from 'vitest'
3
+
4
+ // Mock ResizeObserver
5
+ global.ResizeObserver = vi.fn().mockImplementation(() => ({
6
+ observe: vi.fn(),
7
+ unobserve: vi.fn(),
8
+ disconnect: vi.fn(),
9
+ }))
10
+
11
+ // Mock IntersectionObserver
12
+ global.IntersectionObserver = vi.fn().mockImplementation(() => ({
13
+ observe: vi.fn(),
14
+ unobserve: vi.fn(),
15
+ disconnect: vi.fn(),
16
+ }))
17
+
18
+ // Mock matchMedia
19
+ Object.defineProperty(window, 'matchMedia', {
20
+ writable: true,
21
+ value: vi.fn().mockImplementation(query => ({
22
+ matches: false,
23
+ media: query,
24
+ onchange: null,
25
+ addListener: vi.fn(),
26
+ removeListener: vi.fn(),
27
+ addEventListener: vi.fn(),
28
+ removeEventListener: vi.fn(),
29
+ dispatchEvent: vi.fn(),
30
+ })),
31
+ })
32
+
33
+ // Mock WebGL context for Three.js
34
+ const mockWebGLContext = {
35
+ getExtension: vi.fn(),
36
+ getParameter: vi.fn(),
37
+ createShader: vi.fn(),
38
+ shaderSource: vi.fn(),
39
+ compileShader: vi.fn(),
40
+ createProgram: vi.fn(),
41
+ attachShader: vi.fn(),
42
+ linkProgram: vi.fn(),
43
+ useProgram: vi.fn(),
44
+ createBuffer: vi.fn(),
45
+ bindBuffer: vi.fn(),
46
+ bufferData: vi.fn(),
47
+ createTexture: vi.fn(),
48
+ bindTexture: vi.fn(),
49
+ texImage2D: vi.fn(),
50
+ texParameteri: vi.fn(),
51
+ generateMipmap: vi.fn(),
52
+ viewport: vi.fn(),
53
+ clearColor: vi.fn(),
54
+ clear: vi.fn(),
55
+ drawArrays: vi.fn(),
56
+ drawElements: vi.fn(),
57
+ enableVertexAttribArray: vi.fn(),
58
+ vertexAttribPointer: vi.fn(),
59
+ uniform1f: vi.fn(),
60
+ uniform2f: vi.fn(),
61
+ uniform3f: vi.fn(),
62
+ uniform4f: vi.fn(),
63
+ uniformMatrix4fv: vi.fn(),
64
+ getUniformLocation: vi.fn(),
65
+ getAttribLocation: vi.fn(),
66
+ enable: vi.fn(),
67
+ disable: vi.fn(),
68
+ depthFunc: vi.fn(),
69
+ blendFunc: vi.fn(),
70
+ cullFace: vi.fn(),
71
+ frontFace: vi.fn(),
72
+ VERTEX_SHADER: 35633,
73
+ FRAGMENT_SHADER: 35632,
74
+ ARRAY_BUFFER: 34962,
75
+ ELEMENT_ARRAY_BUFFER: 34963,
76
+ STATIC_DRAW: 35044,
77
+ DYNAMIC_DRAW: 35048,
78
+ TEXTURE_2D: 3553,
79
+ RGBA: 6408,
80
+ UNSIGNED_BYTE: 5121,
81
+ TEXTURE_MAG_FILTER: 10240,
82
+ TEXTURE_MIN_FILTER: 10241,
83
+ LINEAR: 9729,
84
+ COLOR_BUFFER_BIT: 16384,
85
+ DEPTH_BUFFER_BIT: 256,
86
+ DEPTH_TEST: 2929,
87
+ BLEND: 3042,
88
+ SRC_ALPHA: 770,
89
+ ONE_MINUS_SRC_ALPHA: 771,
90
+ CULL_FACE: 2884,
91
+ BACK: 1029,
92
+ CCW: 2305,
93
+ FLOAT: 5126,
94
+ FALSE: 0,
95
+ TRUE: 1,
96
+ }
97
+
98
+ HTMLCanvasElement.prototype.getContext = vi.fn().mockImplementation((contextType) => {
99
+ if (contextType === 'webgl' || contextType === 'experimental-webgl') {
100
+ return mockWebGLContext
101
+ }
102
+ return null
103
+ })
104
+
105
+ beforeAll(() => {
106
+ // Suppress console errors in tests
107
+ vi.spyOn(console, 'error').mockImplementation(() => {})
108
+ vi.spyOn(console, 'warn').mockImplementation(() => {})
109
+ })