Skip to content

Commit 93d0e4c

Browse files
committed
test: Wrote basic tests for XREvents module
1 parent bef0c64 commit 93d0e4c

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/XREvents.test.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import * as React from 'react'
2+
import { describe, expect, it, vi } from 'vitest'
3+
import { XRContext, XRState } from './context'
4+
import { renderHook } from './testUtils'
5+
import { useXREvent } from './XREvents'
6+
import { createStoreMock } from './mocks/storeMock'
7+
import { XRControllerMock } from './mocks/XRControllerMock'
8+
import { PropsWithChildren } from 'react'
9+
import { StoreApi, UseBoundStore } from 'zustand'
10+
11+
const createStoreProvider =
12+
(store: UseBoundStore<XRState, StoreApi<XRState>>) =>
13+
({ children }: PropsWithChildren) =>
14+
<XRContext.Provider value={store} children={children} />
15+
16+
describe('XREvents', () => {
17+
it('should not call callback if no events happened', async () => {
18+
const selectSpy = vi.fn()
19+
const store = createStoreMock()
20+
21+
await renderHook(() => useXREvent('select', selectSpy), {
22+
wrapper: createStoreProvider(store)
23+
})
24+
25+
expect(selectSpy).not.toBeCalled()
26+
})
27+
28+
it('should call callback with custom data including native event and target when xr event is dispatched', async () => {
29+
const selectSpy = vi.fn()
30+
const store = createStoreMock()
31+
const xrControllerMock = new XRControllerMock(0)
32+
store.setState({
33+
controllers: [xrControllerMock]
34+
})
35+
36+
await renderHook(() => useXREvent('select', selectSpy), {
37+
wrapper: createStoreProvider(store)
38+
})
39+
xrControllerMock.controller.dispatchEvent({ type: 'select' })
40+
41+
expect(selectSpy).toBeCalledTimes(1)
42+
expect(selectSpy).toBeCalledWith(
43+
expect.objectContaining({
44+
nativeEvent: expect.objectContaining({ type: 'select' }),
45+
target: xrControllerMock
46+
})
47+
)
48+
})
49+
})

0 commit comments

Comments
 (0)