1
- import { CUSTOM_ELEMENTS_SCHEMA , provideEnvironmentInitializer , Type } from '@angular/core' ;
1
+ import {
2
+ ComponentRef ,
3
+ CUSTOM_ELEMENTS_SCHEMA ,
4
+ provideEnvironmentInitializer ,
5
+ RendererFactory2 ,
6
+ Type ,
7
+ } from '@angular/core' ;
2
8
import { ComponentFixture , TestBed } from '@angular/core/testing' ;
9
+ import { ɵDomRendererFactory2 as DomRendererFactory2 } from '@angular/platform-browser' ;
3
10
import {
4
- getLocalState ,
11
+ getInstanceState ,
5
12
injectCanvasRootInitializer ,
6
13
NGT_STORE ,
7
- NgtAnyRecord ,
8
- NgtCanvasOptions ,
9
- NgtEventHandlers ,
10
- NgtInstanceNode ,
11
- NgtSignalStore ,
12
- NgtState ,
13
- provideStore ,
14
+ type NgtAnyRecord ,
15
+ type NgtCanvasOptions ,
16
+ type NgtEventHandlers ,
17
+ type NgtInstanceNode ,
18
+ NgtRendererFactory2 ,
19
+ type NgtState ,
20
+ type SignalState ,
21
+ storeFactory ,
14
22
} from 'angular-three' ;
15
- import { Object3D } from 'three' ;
23
+ import type * as THREE from 'three' ;
16
24
import { NgtTestCanvas } from './test-canvas' ;
17
25
import { createMockCanvas } from './utils/mock-canvas' ;
18
26
@@ -26,6 +34,7 @@ export class NgtTestBed {
26
34
static create < T extends Type < any > > (
27
35
sceneGraph : T ,
28
36
{
37
+ sceneGraphInputs = { } ,
29
38
mockCanvasOptions = { } ,
30
39
canvasConfiguration = { } ,
31
40
errorOnUnknownElements,
@@ -35,6 +44,7 @@ export class NgtTestBed {
35
44
teardown,
36
45
deferBlockBehavior,
37
46
} : {
47
+ sceneGraphInputs ?: NgtAnyRecord ;
38
48
mockCanvasOptions ?: { width ?: number ; height ?: number ; beforeReturn ?: ( canvas : HTMLCanvasElement ) => void } ;
39
49
canvasConfiguration ?: Partial < Omit < NgtCanvasOptions , 'frameloop' | 'size' | 'events' > > ;
40
50
} & Omit < Parameters < TestBed [ 'configureTestingModule' ] > [ 0 ] , 'schemas' > = { } ,
@@ -43,23 +53,28 @@ export class NgtTestBed {
43
53
44
54
const fixture = TestBed . configureTestingModule ( {
45
55
providers : [
46
- provideStore ( ) ,
56
+ {
57
+ provide : RendererFactory2 ,
58
+ useFactory : ( delegate : RendererFactory2 ) => new NgtRendererFactory2 ( delegate ) ,
59
+ deps : [ DomRendererFactory2 ] ,
60
+ } ,
61
+ { provide : NGT_STORE , useFactory : storeFactory } ,
47
62
provideEnvironmentInitializer ( ( ) => {
48
63
const initializerFn = ( ( ) => {
49
64
const initRoot = injectCanvasRootInitializer ( ) ;
50
65
51
66
return ( ) => {
52
67
const configurator = initRoot ( mockedCanvas ) ;
53
68
configurator . configure ( {
69
+ ...canvasConfiguration ,
70
+ events : undefined ,
54
71
frameloop : 'never' ,
55
72
size : {
56
73
width : mockCanvasOptions . width ?? mockedCanvas . width ?? 1280 ,
57
74
height : mockCanvasOptions . height ?? mockedCanvas . height ?? 800 ,
58
75
top : 0 ,
59
76
left : 0 ,
60
77
} ,
61
- ...canvasConfiguration ,
62
- events : undefined ,
63
78
} ) ;
64
79
} ;
65
80
} ) ( ) ;
@@ -76,6 +91,7 @@ export class NgtTestBed {
76
91
} ) . createComponent ( NgtTestCanvas ) ;
77
92
78
93
fixture . componentRef . setInput ( 'sceneGraph' , sceneGraph ) ;
94
+ fixture . componentRef . setInput ( 'sceneGraphInputs' , sceneGraphInputs ) ;
79
95
fixture . detectChanges ( ) ;
80
96
81
97
const store = TestBed . inject ( NGT_STORE ) ;
@@ -85,8 +101,9 @@ export class NgtTestBed {
85
101
return {
86
102
store,
87
103
fixture,
104
+ sceneGraphComponentRef : fixture . componentInstance . sceneRef as ComponentRef < T > ,
88
105
scene : store . snapshot . scene ,
89
- sceneInstanceNode : getLocalState ( store . snapshot . scene ) ! ,
106
+ sceneInstanceNode : getInstanceState ( store . snapshot . scene ) ! ,
90
107
canvas : mockedCanvas ,
91
108
destroy : fixture . componentInstance . destroy . bind ( fixture . componentInstance ) ,
92
109
fireEvent : this . createEventFirer ( store , fixture ) ,
@@ -95,12 +112,12 @@ export class NgtTestBed {
95
112
} ;
96
113
}
97
114
98
- static createToGraph ( store : NgtSignalStore < NgtState > ) {
115
+ static createToGraph ( store : SignalState < NgtState > ) {
99
116
function graphify ( type : string , name : string , children : NgtTestGraphedObject [ ] ) : NgtTestGraphedObject {
100
117
return { type, name, children } ;
101
118
}
102
119
103
- function toGraph ( node : Object3D ) : NgtTestGraphedObject [ ] {
120
+ function toGraph ( node : THREE . Object3D ) : NgtTestGraphedObject [ ] {
104
121
return node . children . map ( ( child ) => graphify ( child . type , child . name || '' , toGraph ( child ) ) ) ;
105
122
}
106
123
@@ -110,7 +127,7 @@ export class NgtTestBed {
110
127
} ;
111
128
}
112
129
113
- static createAdvance ( store : NgtSignalStore < NgtState > ) {
130
+ static createAdvance ( store : SignalState < NgtState > ) {
114
131
return async ( frames : number , delta : number | number [ ] = 1 ) => {
115
132
const state = store . snapshot ;
116
133
const subscribers = state . internal . subscribers ;
@@ -141,17 +158,17 @@ export class NgtTestBed {
141
158
} ;
142
159
}
143
160
144
- static createEventFirer ( store : NgtSignalStore < NgtState > , fixture : ComponentFixture < NgtTestCanvas > ) {
161
+ static createEventFirer ( store : SignalState < NgtState > , fixture : ComponentFixture < NgtTestCanvas > ) {
145
162
let autoDetectChanges = true ;
146
163
147
164
async function fireEvent ( el : NgtInstanceNode , eventName : keyof NgtEventHandlers , eventData : NgtAnyRecord = { } ) {
148
- const localState = getLocalState ( el ) ;
149
- if ( ! localState ) {
165
+ const instanceState = getInstanceState ( el ) ;
166
+ if ( ! instanceState ) {
150
167
console . warn ( `[NGT Test] ${ el } has no local state` ) ;
151
168
return ;
152
169
}
153
170
154
- const handler = localState . handlers [ eventName ] ;
171
+ const handler = instanceState . handlers [ eventName ] ;
155
172
if ( ! handler ) {
156
173
console . warn ( `[NGT Test] ${ el } has no ${ eventName } handler` ) ;
157
174
return ;
0 commit comments