Skip to content

Commit 172257c

Browse files
committed
test: Added mock for XRController
had to make private XRController fields public because of weird Typescript behaviour: implementing a class requires to implement private fields, but now adequate way to do so
1 parent 1e1a403 commit 172257c

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/XRController.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ export class XRController extends THREE.Group {
3030
this.controller.addEventListener('disconnected', this._onDisconnected)
3131
}
3232

33-
private _onConnected(event: XRControllerEvent) {
33+
_onConnected(event: XRControllerEvent) {
3434
if (event.fake) return
3535

3636
this.visible = true
3737
this.inputSource = event.data!
3838
this.dispatchEvent(event)
3939
}
4040

41-
private _onDisconnected(event: XRControllerEvent) {
41+
_onDisconnected(event: XRControllerEvent) {
4242
if (event.fake) return
4343

4444
this.visible = false

src/mocks/XRControllerMock.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { XRController, XRControllerEvent } from '@react-three/xr'
2+
import { Group, XRTargetRaySpace, XRGripSpace, XRHandSpace, Vector3, XRHandInputState, XRHandJoints } from 'three'
3+
4+
export class XRTargetRaySpaceMock extends Group implements XRTargetRaySpace {
5+
readonly angularVelocity: Vector3 = new Vector3()
6+
hasAngularVelocity = false
7+
hasLinearVelocity = false
8+
readonly linearVelocity: Vector3 = new Vector3()
9+
}
10+
11+
export class XRGripSpaceMock extends Group implements XRGripSpace {
12+
readonly angularVelocity: Vector3 = new Vector3()
13+
hasAngularVelocity = false
14+
hasLinearVelocity = false
15+
readonly linearVelocity: Vector3 = new Vector3()
16+
}
17+
18+
export class XRHandSpaceMock extends Group implements XRHandSpace {
19+
readonly inputState: XRHandInputState = { pinching: false }
20+
readonly joints: Partial<XRHandJoints> = {}
21+
}
22+
23+
export class XRControllerMock extends Group implements XRController {
24+
readonly controller: XRTargetRaySpace
25+
readonly grip: XRGripSpace
26+
readonly hand: XRHandSpace
27+
readonly index: number
28+
29+
// TODO Implement mocks for inputSource
30+
// @ts-ignore
31+
inputSource: XRInputSource
32+
33+
constructor(index: number) {
34+
super()
35+
this.index = index
36+
this.controller = new XRTargetRaySpaceMock()
37+
this.grip = new XRGripSpaceMock()
38+
this.hand = new XRHandSpaceMock()
39+
}
40+
41+
_onConnected(_event: XRControllerEvent): void {
42+
throw new Error('Method not implemented.')
43+
}
44+
45+
_onDisconnected(_event: XRControllerEvent): void {
46+
throw new Error('Method not implemented.')
47+
}
48+
49+
dispose(): void {}
50+
}

0 commit comments

Comments
 (0)