NgtTestBed
NgtTestBed
is a utility from angular-three/testing
that abstracts TestBed
and provides a set of utilities to help you write unit tests for scene graphs built with Angular Three.
create()
NgtTestBed
exposes a single static method create()
that accepts a Component
class for the scene graph you want to test and returns an object with the following properties:
const ngtTestBed = NgtTestBed.create(SceneGraph);ngtTestBed.fixture; // ComponentFixture<NgtTestCanvas>ngtTestBed.store; // SignalState<NgtState>ngtTestBed.scene; // root THREE.ScenengtTestBed.sceneInstanceNode; // root Scene instance statengtTestBed.canvas; // the mocked HTMLCanvasElementngtTestBed.destroy; // method to destroy the fixturengtTestBed.fireEvent; // method to fire events on an element in the scene graphngtTestBed.advance; // method to advance the animation loop manually per framengtTestBed.toGraph; // method to convert the scene graph to a simple object
mockCanvasOptions
You can customize the mocked HTMLCanvasElement
with mockCanvasOptions
. It accepts width
and height
as well as a beforeReturn
callback which allows you to return a mocked HTMLCanvasElement
before the TestBed
is created.
NgtTestBed.create(SceneGraph, { mockCanvasOptions: { width: 1280, height: 720 }});
canvasConfiguration
You can customize the ngt-canvas
via canvasConfiguration
. It accepts everything that ngt-canvas
accepts.
NgtTestBed.create(SceneGraph, { canvasConfiguration: { camera: { position: [0, 0, 5] }, shadows: true, },});
Example Scenario
For the example scenario, scene
, fireEvent
, and advance
will allow you to test the SceneGraph
component.
scene
allows you to assert the state of the scene graphfireEvent
allows you to fire events on the cubeadvance
allows you to advance the animation loop manually per frame
import { NgtTestBed } from 'angular-three/testing';
describe('SceneGraph', () => { it('should render', async () => { const { scene, fireEvent, advance } = NgtTestBed.create(SceneGraph); });});
With scene
, you can assert the state of the scene graph and you can do so however you want to. To keep things simple, you can try asserting that the root THREE.Scene
has a child which is a THREE.Mesh
import { is } from 'angular-three';import { NgtTestBed } from 'angular-three/testing';
describe('SceneGraph', () => { it('should render', async () => { const { scene, fireEvent, advance } = NgtTestBed.create(SceneGraph);
expect(scene.children.length).toEqual(1); const mesh = scene.children[0]; expect(is.three<THREE.Mesh>(mesh, 'isMesh')).toEqual(true); });});