Skip to content

Commit

Permalink
test: add test for .findByProps() with manually managed components
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctypeRosenthal committed Jan 8, 2022
1 parent ea31a72 commit c3bb106
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions packages/test-renderer/src/__tests__/RTTR.methods.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { BoxHelper, Object3D } from 'three'

jest.mock('scheduler', () => require('scheduler/unstable_mock'))

import * as React from 'react'

import ReactThreeTestRenderer from '../index'
import { useThree } from '@react-three/fiber/src'
import { useRef } from 'react'

describe('ReactThreeTestRenderer instance methods', () => {
const ExampleComponent = () => {
Expand All @@ -20,6 +24,28 @@ describe('ReactThreeTestRenderer instance methods', () => {
)
}

const MANUALLY_MOUNTED_COMPONENT_NAME = 'not-mounted-by-r3f'

const WithManuallyManagedComponent = () => {
const { scene } = useThree((three) => three)
const ref = useRef<Object3D>()

React.useEffect(() => {
if (!ref.current) return
const manuallyManagedComponent = new BoxHelper(ref.current)
manuallyManagedComponent.name = MANUALLY_MOUNTED_COMPONENT_NAME
manuallyManagedComponent.visible = false
scene.add(manuallyManagedComponent)
}, [])

return (
<mesh name="mesh_01" ref={ref}>
<boxBufferGeometry args={[2, 2]} />
<meshStandardMaterial color={0x0000ff} />
</mesh>
)
}

it('should pass the parent', async () => {
const { scene } = await ReactThreeTestRenderer.create(<ExampleComponent />)

Expand Down Expand Up @@ -98,4 +124,14 @@ describe('ReactThreeTestRenderer instance methods', () => {

expect(() => scene.findByProps({ color: 0x0000ff })).toThrow()
})

it('should also find three components which are not mounted via r3f and not throw', async () => {
const { scene } = await ReactThreeTestRenderer.create(<WithManuallyManagedComponent />)
expect(() => scene.findByProps({ name: MANUALLY_MOUNTED_COMPONENT_NAME })).not.toThrow()
expect(() => scene.findByProps({ name: MANUALLY_MOUNTED_COMPONENT_NAME })).not.toThrow(
"Cannot read properties of undefined (reading 'memoizedProps')",
)
const instance = scene.findByProps({ name: MANUALLY_MOUNTED_COMPONENT_NAME }).instance
expect(instance).toBeDefined()
})
})

0 comments on commit c3bb106

Please sign in to comment.