-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SceneViewer and throw sceneLoader error to ErrorBoundary (#38)
Co-authored-by: Xinyi Xu <[email protected]>
- Loading branch information
Showing
24 changed files
with
1,175 additions
and
1,266 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* eslint-disable */ | ||
const mockSceneComposerApi = { | ||
findSceneNodeRefBy: jest.fn(), | ||
setCameraTarget: jest.fn(), | ||
setSelectedSceneNodeRef: jest.fn(), | ||
}; | ||
|
||
jest.doMock('./components/SceneComposerInternal', () => { | ||
const original = jest.requireActual('./components/SceneComposerInternal'); | ||
return { | ||
...original, | ||
SceneComposerInternal: 'SceneComposerInternal', | ||
useSceneComposerApi: () => mockSceneComposerApi, | ||
} | ||
}); | ||
|
||
import * as React from 'react'; | ||
import renderer, { act } from 'react-test-renderer'; | ||
import { useStore } from './store'; | ||
import { SceneViewer } from './SceneViewer'; | ||
import { KnownComponentType } from './interfaces'; | ||
/* eslint-enable */ | ||
|
||
describe('SceneViewer', () => { | ||
const mockGetSceneObjectFunction = jest.fn(); | ||
const mockSceneLoader = { | ||
getSceneUri: () => Promise.resolve('https://test.url'), | ||
getSceneUrl: () => Promise.resolve('https://test.url'), | ||
getSceneObject: mockGetSceneObjectFunction, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should render correctly', async () => { | ||
let container; | ||
act(() => { | ||
container = renderer.create(<SceneViewer sceneComposerId={'123'} sceneLoader={mockSceneLoader} config={{}} />); | ||
}); | ||
|
||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it('should call sceneComposerApis when selectedDataBinding is available', async () => { | ||
const mockNodeRef = ['node-123']; | ||
mockSceneComposerApi.findSceneNodeRefBy.mockReturnValueOnce(mockNodeRef); | ||
const mockLabel = { label: 'label-1' }; | ||
|
||
let container; | ||
act(() => { | ||
container = renderer.create( | ||
<SceneViewer sceneLoader={mockSceneLoader} config={{}} selectedDataBinding={mockLabel} />, | ||
); | ||
}); | ||
|
||
expect(mockSceneComposerApi.findSceneNodeRefBy).toBeCalledTimes(1); | ||
expect(mockSceneComposerApi.findSceneNodeRefBy).toBeCalledWith(mockLabel, [KnownComponentType.Tag]); | ||
expect(mockSceneComposerApi.setCameraTarget).toBeCalledTimes(1); | ||
expect(mockSceneComposerApi.setCameraTarget).toBeCalledWith(mockNodeRef[0], 'transition'); | ||
expect(mockSceneComposerApi.setSelectedSceneNodeRef).toBeCalledTimes(1); | ||
expect(mockSceneComposerApi.setSelectedSceneNodeRef).toBeCalledWith(mockNodeRef[0]); | ||
|
||
// not re-setting camera with same data binding values | ||
container.update(<SceneViewer sceneLoader={mockSceneLoader} config={{}} selectedDataBinding={mockLabel} />); | ||
|
||
expect(mockSceneComposerApi.findSceneNodeRefBy).toBeCalledTimes(1); | ||
expect(mockSceneComposerApi.setCameraTarget).toBeCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React, { useEffect, useMemo, useRef } from 'react'; | ||
import { v4 as uuid } from 'uuid'; | ||
import { isEqual } from 'lodash'; | ||
import styled from 'styled-components'; | ||
|
||
import { IAnchorEvent, KnownComponentType, SceneViewerProps } from './interfaces'; | ||
import { SceneComposerInternal, useSceneComposerApi } from './components/SceneComposerInternal'; | ||
|
||
const SceneComposerContainer = styled.div` | ||
position: absolute; | ||
top: 0; | ||
right: 0; | ||
bottom: 0; | ||
left: 0; | ||
canvas { | ||
outline: none; | ||
-webkit-tap-highlight-color: rgba(255, 255, 255, 0); /* mobile webkit */ | ||
} | ||
`; | ||
|
||
export const SceneViewer: React.FC<SceneViewerProps> = ({ sceneComposerId, config, ...props }: SceneViewerProps) => { | ||
const composerId = useMemo(() => { | ||
return sceneComposerId || uuid(); | ||
}, [sceneComposerId]); | ||
const composerApis = useSceneComposerApi(composerId); | ||
const prevSelectedRef: any = useRef(); | ||
|
||
useEffect(() => { | ||
if (isEqual(prevSelectedRef.current, props.selectedDataBinding)) { | ||
return; | ||
} | ||
prevSelectedRef.current = props.selectedDataBinding; | ||
|
||
const nodeRefs = composerApis.findSceneNodeRefBy(props.selectedDataBinding || '', [KnownComponentType.Tag]); | ||
if (nodeRefs && nodeRefs.length > 0) { | ||
// TODO: auto select the first node for now, handle multiple nodes selection later. | ||
composerApis.setCameraTarget(nodeRefs[0], 'transition'); | ||
composerApis.setSelectedSceneNodeRef(nodeRefs[0]); | ||
} else { | ||
composerApis.setSelectedSceneNodeRef(undefined); | ||
} | ||
}, [props.selectedDataBinding]); | ||
|
||
const onAnchorClick = (data: IAnchorEvent) => { | ||
if (props.onTargetObjectChanged) { | ||
props.onTargetObjectChanged({ data: data }); | ||
} | ||
}; | ||
|
||
return ( | ||
<SceneComposerContainer data-testid={'webgl-root'}> | ||
<SceneComposerInternal | ||
sceneComposerId={composerId} | ||
onAnchorClick={onAnchorClick} | ||
config={{ | ||
...config, | ||
mode: 'Viewing', | ||
}} | ||
{...props} | ||
/> | ||
</SceneComposerContainer> | ||
); | ||
}; |
38 changes: 38 additions & 0 deletions
38
packages/scene-composer/src/__snapshots__/SceneViewer.spec.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`SceneViewer should render correctly 1`] = ` | ||
.c0 { | ||
position: absolute; | ||
top: 0; | ||
right: 0; | ||
bottom: 0; | ||
left: 0; | ||
} | ||
.c0 canvas { | ||
outline: none; | ||
-webkit-tap-highlight-color: rgba(255,255,255,0); | ||
} | ||
<div | ||
className="c0" | ||
data-testid="webgl-root" | ||
> | ||
<SceneComposerInternal | ||
config={ | ||
Object { | ||
"mode": "Viewing", | ||
} | ||
} | ||
onAnchorClick={[Function]} | ||
sceneComposerId="123" | ||
sceneLoader={ | ||
Object { | ||
"getSceneObject": [MockFunction], | ||
"getSceneUri": [Function], | ||
"getSceneUrl": [Function], | ||
} | ||
} | ||
/> | ||
</div> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.