forked from pmndrs/react-xr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controllers.test.tsx
213 lines (168 loc) · 8.89 KB
/
Controllers.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import * as React from 'react'
import { describe, it, expect, vi } from 'vitest'
import { createStoreMock, createStoreProvider } from './mocks/storeMock'
import { render } from './testUtils/testUtilsThree'
import { Controllers } from './Controllers'
import { XRControllerMock } from './mocks/XRControllerMock'
import { XRControllerModel } from './XRControllerModel'
import { XRControllerModelFactoryMock } from './mocks/XRControllerModelFactoryMock'
import { XRInputSourceMock } from './mocks/XRInputSourceMock'
import { act } from '@react-three/test-renderer'
import { Texture } from 'three'
vi.mock('./XRControllerModelFactory', async () => {
const { XRControllerModelFactoryMock } = await vi.importActual<typeof import('./mocks/XRControllerModelFactoryMock')>(
'./mocks/XRControllerModelFactoryMock'
)
return { XRControllerModelFactory: XRControllerModelFactoryMock }
})
describe('Controllers', () => {
it('should not render anything if controllers in state are empty', async () => {
const store = createStoreMock()
const xrControllerMock = new XRControllerMock(0)
store.setState({ controllers: [] })
const { renderer } = await render(<Controllers />, { wrapper: createStoreProvider(store) })
// We aren't rendering anything as a direct children, only in portals
const graph = renderer.toGraph()
expect(graph).toHaveLength(0)
// Checking portals
expect(xrControllerMock.grip.children).toHaveLength(0)
expect(xrControllerMock.controller.children).toHaveLength(0)
})
it('should render one xr controller model and one ray given one controller in state', async () => {
const store = createStoreMock()
const xrControllerMock = new XRControllerMock(0)
store.setState({ controllers: [xrControllerMock] })
await render(<Controllers />, { wrapper: createStoreProvider(store) })
// Checking portals
expect(xrControllerMock.grip.children).toHaveLength(1)
expect(xrControllerMock.grip.children[0]).toBeInstanceOf(XRControllerModel)
expect(xrControllerMock.controller.children).toHaveLength(1)
expect(xrControllerMock.controller.children[0].type).toBe('Line')
})
it('should render two xr controller models and two rays given one controller in state', async () => {
const store = createStoreMock()
const xrControllerMockLeft = new XRControllerMock(0)
const xrControllerMockRight = new XRControllerMock(1)
store.setState({ controllers: [xrControllerMockLeft, xrControllerMockRight] })
await render(<Controllers />, { wrapper: createStoreProvider(store) })
// Checking portals
// left
expect(xrControllerMockLeft.grip.children).toHaveLength(1)
expect(xrControllerMockLeft.grip.children[0]).toBeInstanceOf(XRControllerModel)
expect(xrControllerMockLeft.controller.children).toHaveLength(1)
expect(xrControllerMockLeft.controller.children[0].type).toBe('Line')
// right
expect(xrControllerMockRight.grip.children).toHaveLength(1)
expect(xrControllerMockRight.grip.children[0]).toBeInstanceOf(XRControllerModel)
expect(xrControllerMockRight.controller.children).toHaveLength(1)
expect(xrControllerMockRight.controller.children[0].type).toBe('Line')
})
it('should remove xr controller model when controller is removed from state', async () => {
const store = createStoreMock()
const xrControllerMock = new XRControllerMock(0)
xrControllerMock.inputSource = new XRInputSourceMock()
store.setState({ controllers: [xrControllerMock] })
const { renderer } = await render(<Controllers />, { wrapper: createStoreProvider(store) })
await act(async () => {
store.setState({ controllers: [] })
})
// We aren't rendering anything as a direct children, only in portals
const graph = renderer.toGraph()
expect(graph).toHaveLength(0)
// Checking portals
expect(xrControllerMock.grip.children).toHaveLength(0)
expect(xrControllerMock.controller.children).toHaveLength(0)
})
it('should handle xr controller model given one controller in state', async () => {
const store = createStoreMock()
const xrControllerMock = new XRControllerMock(0)
xrControllerMock.inputSource = new XRInputSourceMock()
store.setState({ controllers: [xrControllerMock] })
await render(<Controllers />, { wrapper: createStoreProvider(store) })
const xrControllerModelFactory = XRControllerModelFactoryMock.instance
expect(xrControllerModelFactory).toBeDefined()
expect(xrControllerMock.xrControllerModel).toBeInstanceOf(XRControllerModel)
expect(xrControllerModelFactory?.initializeControllerModel).toBeCalled()
})
it('should handle xr controller model when controller is removed from state', async () => {
const store = createStoreMock()
const xrControllerMock = new XRControllerMock(0)
xrControllerMock.inputSource = new XRInputSourceMock()
store.setState({ controllers: [xrControllerMock] })
await render(<Controllers />, { wrapper: createStoreProvider(store) })
const xrControllerModel = xrControllerMock.xrControllerModel
const disconnectSpy = vi.spyOn(xrControllerModel!, 'disconnect')
await act(async () => {
store.setState({ controllers: [] })
})
const xrControllerModelFactory = XRControllerModelFactoryMock.instance
expect(xrControllerModelFactory).toBeDefined()
expect(xrControllerMock.xrControllerModel).toBeNull()
expect(disconnectSpy).toBeCalled()
})
it('should not reconnect when component is rerendered', async () => {
const store = createStoreMock()
const xrControllerMock = new XRControllerMock(0)
xrControllerMock.inputSource = new XRInputSourceMock()
store.setState({ controllers: [xrControllerMock] })
const { rerender } = await render(<Controllers />, { wrapper: createStoreProvider(store) })
const xrControllerModel = xrControllerMock.xrControllerModel
const disconnectSpy = vi.spyOn(xrControllerModel!, 'disconnect')
await rerender(<Controllers />)
const xrControllerModelFactory = XRControllerModelFactoryMock.instance
expect(xrControllerModelFactory).toBeDefined()
expect(xrControllerMock.xrControllerModel).not.toBeNull()
expect(disconnectSpy).not.toBeCalled()
expect(xrControllerModelFactory?.initializeControllerModel).toBeCalledTimes(1)
})
describe('envMap', () => {
it("should not set env map if it's not provided in props", async () => {
const store = createStoreMock()
const xrControllerMock = new XRControllerMock(0)
store.setState({ controllers: [xrControllerMock] })
await render(<Controllers />, { wrapper: createStoreProvider(store) })
const xrControllerModel = xrControllerMock.xrControllerModel
expect(xrControllerModel!.envMap).toBeNull()
expect(xrControllerModel!.envMapIntensity).toBe(1)
})
it("should set env map if it's provided in props", async () => {
const store = createStoreMock()
const xrControllerMock = new XRControllerMock(0)
store.setState({ controllers: [xrControllerMock] })
const envMap = new Texture()
await render(<Controllers envMap={envMap} />, { wrapper: createStoreProvider(store) })
const xrControllerModel = xrControllerMock.xrControllerModel
expect(xrControllerModel!.envMap).toBe(envMap)
expect(xrControllerModel!.envMapIntensity).toBe(1)
})
it("should only set env map intensity if it's provided in props", async () => {
const store = createStoreMock()
const xrControllerMock = new XRControllerMock(0)
store.setState({ controllers: [xrControllerMock] })
await render(<Controllers envMapIntensity={0.5} />, { wrapper: createStoreProvider(store) })
const xrControllerModel = xrControllerMock.xrControllerModel
expect(xrControllerModel!.envMap).toBeNull()
expect(xrControllerModel!.envMapIntensity).toBe(0.5)
})
it("should change env map intensity if it's provided in props then updated to a different value", async () => {
const store = createStoreMock()
const xrControllerMock = new XRControllerMock(0)
store.setState({ controllers: [xrControllerMock] })
const { rerender } = await render(<Controllers envMapIntensity={0.5} />, { wrapper: createStoreProvider(store) })
const xrControllerModel = xrControllerMock.xrControllerModel
await rerender(<Controllers envMapIntensity={0.6} />)
expect(xrControllerModel!.envMap).toBeNull()
expect(xrControllerModel!.envMapIntensity).toBe(0.6)
})
it("should remove env map if it's provided in props first, and then removed", async () => {
const store = createStoreMock()
const xrControllerMock = new XRControllerMock(0)
store.setState({ controllers: [xrControllerMock] })
const envMap = new Texture()
const { rerender } = await render(<Controllers envMap={envMap} />, { wrapper: createStoreProvider(store) })
const xrControllerModel = xrControllerMock.xrControllerModel
await rerender(<Controllers />)
expect(xrControllerModel!.envMap).toBeNull()
})
})
})