Skip to content

Commit 1b68849

Browse files
refactored unit tests, moved mocks to common mocks folder
1 parent 0d41849 commit 1b68849

6 files changed

Lines changed: 535 additions & 519 deletions

File tree

packages/dev/babylonjs/lib/api/__mocks__/babylonjs.mock.ts

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,3 +525,159 @@ export function createBabylonJSMock() {
525525
}
526526
};
527527
}
528+
529+
// Scene helper specific mock classes
530+
export class MockEngine {
531+
_canvas: HTMLCanvasElement;
532+
_renderLoop: (() => void) | null = null;
533+
534+
constructor(canvas: HTMLCanvasElement, _antialias?: boolean, _options?: object) {
535+
this._canvas = canvas;
536+
}
537+
538+
setHardwareScalingLevel(_level: number) { /* mock */ }
539+
resize() { /* mock */ }
540+
541+
runRenderLoop(callback: () => void) {
542+
this._renderLoop = callback;
543+
}
544+
545+
stopRenderLoop() {
546+
this._renderLoop = null;
547+
}
548+
549+
dispose() { /* mock */ }
550+
}
551+
552+
export class MockBabylonScene {
553+
_meshes: MockMesh[] = [];
554+
metadata: { shadowGenerators: MockShadowGenerator[] } = { shadowGenerators: [] };
555+
clearColor: MockColor4 | null = null;
556+
activeCamera: MockArcRotateCamera | null = null;
557+
558+
dispose() { /* mock */ }
559+
render() { /* mock */ }
560+
}
561+
562+
export class MockHemisphericLight {
563+
name: string;
564+
direction: MockVector3;
565+
diffuse: MockColor3;
566+
groundColor: MockColor3;
567+
intensity = 1;
568+
569+
constructor(name: string, direction: MockVector3, _scene: MockBabylonScene) {
570+
this.name = name;
571+
this.direction = direction;
572+
this.diffuse = new MockColor3(1, 1, 1);
573+
this.groundColor = new MockColor3(0.5, 0.5, 0.5);
574+
}
575+
576+
dispose() { /* mock */ }
577+
}
578+
579+
export class MockDirectionalLight {
580+
name: string;
581+
direction: MockVector3;
582+
position: MockVector3;
583+
diffuse: MockColor3;
584+
intensity = 1;
585+
586+
constructor(name: string, direction: MockVector3, _scene: MockBabylonScene) {
587+
this.name = name;
588+
this.direction = direction;
589+
this.position = new MockVector3();
590+
this.diffuse = new MockColor3(1, 1, 1);
591+
}
592+
593+
dispose() { /* mock */ }
594+
}
595+
596+
export class MockShadowGenerator {
597+
useBlurExponentialShadowMap = false;
598+
blurKernel = 0;
599+
darkness = 0;
600+
601+
constructor(_mapSize: number, _light: MockDirectionalLight) { /* mock */ }
602+
}
603+
604+
export class MockArcRotateCamera {
605+
name: string;
606+
alpha: number;
607+
beta: number;
608+
radius: number;
609+
target: MockVector3;
610+
angularSensibilityX = 1000;
611+
angularSensibilityY = 1000;
612+
lowerRadiusLimit: number | null = null;
613+
upperRadiusLimit: number | null = null;
614+
panningSensibility = 1000;
615+
wheelPrecision = 3;
616+
maxZ = 10000;
617+
minZ = 0.1;
618+
lowerBetaLimit: number | null = null;
619+
upperBetaLimit: number | null = null;
620+
lowerAlphaLimit: number | null = null;
621+
upperAlphaLimit: number | null = null;
622+
623+
constructor(
624+
name: string,
625+
alpha: number,
626+
beta: number,
627+
radius: number,
628+
target: MockVector3,
629+
scene: MockBabylonScene
630+
) {
631+
this.name = name;
632+
this.alpha = alpha;
633+
this.beta = beta;
634+
this.radius = radius;
635+
this.target = target;
636+
scene.activeCamera = this;
637+
}
638+
639+
attachControl(_canvas: HTMLCanvasElement, _noPreventDefault?: boolean) { /* mock */ }
640+
dispose() { /* mock */ }
641+
}
642+
643+
export const MockTools = {
644+
ToRadians: (degrees: number) => degrees * (Math.PI / 180)
645+
};
646+
647+
// Type definitions for test assertions
648+
export interface MockMeshType {
649+
name: string;
650+
position: MockVector3;
651+
receiveShadows: boolean;
652+
material: object | null;
653+
_groundWidth?: number;
654+
_groundHeight?: number;
655+
}
656+
657+
/**
658+
* Create scene helper mock for jest.mock("@babylonjs/core")
659+
*/
660+
export function createSceneHelperMock() {
661+
return {
662+
Engine: MockEngine,
663+
Scene: MockBabylonScene,
664+
Vector3: MockVector3,
665+
Color3: MockColor3,
666+
Color4: MockColor4,
667+
HemisphericLight: MockHemisphericLight,
668+
DirectionalLight: MockDirectionalLight,
669+
ShadowGenerator: MockShadowGenerator,
670+
ArcRotateCamera: MockArcRotateCamera,
671+
MeshBuilder: {
672+
CreateGround: (name: string, options: { width: number; height: number }, _scene: MockBabylonScene) => {
673+
const mesh = new MockMesh(name, null) as unknown as MockMeshType;
674+
mesh._groundWidth = options.width;
675+
mesh._groundHeight = options.height;
676+
mesh.receiveShadows = false;
677+
return mesh;
678+
}
679+
},
680+
StandardMaterial: MockStandardMaterial,
681+
Tools: MockTools,
682+
};
683+
}

packages/dev/babylonjs/lib/api/bitbybit/babylon/scene-helper.test.ts

Lines changed: 4 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -1,188 +1,15 @@
11
/**
22
* @jest-environment jsdom
33
*/
4-
/* eslint-disable @typescript-eslint/no-unused-vars */
5-
/* eslint-disable @typescript-eslint/no-empty-function */
64
import { initBabylonJS } from "./scene-helper";
75
import { BabylonJSScene } from "../../inputs/babylon-scene-helper-inputs";
86
import { BabylonCamera } from "../../inputs/babylon-camera-inputs";
7+
import { MockMeshType } from "../../__mocks__/babylonjs.mock";
98

10-
// Type definitions for mock objects
11-
interface MockVector3Type {
12-
x: number;
13-
y: number;
14-
z: number;
15-
set(x: number, y: number, z: number): void;
16-
}
17-
18-
interface MockColor3Type {
19-
r: number;
20-
g: number;
21-
b: number;
22-
}
23-
24-
interface MockColor4Type {
25-
r: number;
26-
g: number;
27-
b: number;
28-
a: number;
29-
}
30-
31-
interface MockMeshType {
32-
name: string;
33-
position: MockVector3Type;
34-
receiveShadows: boolean;
35-
material: object | null;
36-
_groundWidth?: number;
37-
_groundHeight?: number;
38-
}
39-
40-
// Mock BabylonJS core module
9+
// Mock BabylonJS core module using centralized mocks
4110
jest.mock("@babylonjs/core", () => {
42-
const actualMock = jest.requireActual("../../__mocks__/babylonjs.mock");
43-
44-
class MockEngine {
45-
_canvas: HTMLCanvasElement;
46-
_renderLoop: (() => void) | null = null;
47-
48-
constructor(canvas: HTMLCanvasElement, _antialias?: boolean, _options?: object) {
49-
this._canvas = canvas;
50-
}
51-
52-
setHardwareScalingLevel(_level: number) { /* mock */ }
53-
54-
resize() { /* mock */ }
55-
56-
runRenderLoop(callback: () => void) {
57-
this._renderLoop = callback;
58-
}
59-
60-
stopRenderLoop() {
61-
this._renderLoop = null;
62-
}
63-
64-
dispose() { /* mock */ }
65-
}
66-
67-
class MockBabylonScene {
68-
_meshes: MockMeshType[] = [];
69-
metadata: { shadowGenerators: MockShadowGenerator[] } = { shadowGenerators: [] };
70-
clearColor: MockColor4Type | null = null;
71-
activeCamera: MockArcRotateCamera | null = null;
72-
73-
dispose() { /* mock */ }
74-
render() { /* mock */ }
75-
}
76-
77-
class MockHemisphericLight {
78-
name: string;
79-
direction: MockVector3Type;
80-
diffuse: MockColor3Type;
81-
groundColor: MockColor3Type;
82-
intensity = 1;
83-
84-
constructor(name: string, direction: MockVector3Type, _scene: MockBabylonScene) {
85-
this.name = name;
86-
this.direction = direction;
87-
this.diffuse = new actualMock.MockColor3(1, 1, 1);
88-
this.groundColor = new actualMock.MockColor3(0.5, 0.5, 0.5);
89-
}
90-
91-
dispose() { /* mock */ }
92-
}
93-
94-
class MockDirectionalLight {
95-
name: string;
96-
direction: MockVector3Type;
97-
position: MockVector3Type;
98-
diffuse: MockColor3Type;
99-
intensity = 1;
100-
101-
constructor(name: string, direction: MockVector3Type, _scene: MockBabylonScene) {
102-
this.name = name;
103-
this.direction = direction;
104-
this.position = new actualMock.MockVector3();
105-
this.diffuse = new actualMock.MockColor3(1, 1, 1);
106-
}
107-
108-
dispose() { /* mock */ }
109-
}
110-
111-
class MockShadowGenerator {
112-
useBlurExponentialShadowMap = false;
113-
blurKernel = 0;
114-
darkness = 0;
115-
116-
constructor(_mapSize: number, _light: MockDirectionalLight) { /* mock */ }
117-
}
118-
119-
class MockArcRotateCamera {
120-
name: string;
121-
alpha: number;
122-
beta: number;
123-
radius: number;
124-
target: MockVector3Type;
125-
angularSensibilityX = 1000;
126-
angularSensibilityY = 1000;
127-
lowerRadiusLimit: number | null = null;
128-
upperRadiusLimit: number | null = null;
129-
panningSensibility = 1000;
130-
wheelPrecision = 3;
131-
maxZ = 10000;
132-
minZ = 0.1;
133-
lowerBetaLimit: number | null = null;
134-
upperBetaLimit: number | null = null;
135-
lowerAlphaLimit: number | null = null;
136-
upperAlphaLimit: number | null = null;
137-
138-
constructor(
139-
name: string,
140-
alpha: number,
141-
beta: number,
142-
radius: number,
143-
target: MockVector3Type,
144-
scene: MockBabylonScene
145-
) {
146-
this.name = name;
147-
this.alpha = alpha;
148-
this.beta = beta;
149-
this.radius = radius;
150-
this.target = target;
151-
scene.activeCamera = this;
152-
}
153-
154-
attachControl(_canvas: HTMLCanvasElement, _noPreventDefault?: boolean) { /* mock */ }
155-
dispose() { /* mock */ }
156-
}
157-
158-
const MockMeshBuilder = {
159-
CreateGround: (name: string, options: { width: number; height: number }, _scene: MockBabylonScene) => {
160-
const mesh = new actualMock.MockMesh(name, null) as MockMeshType;
161-
mesh._groundWidth = options.width;
162-
mesh._groundHeight = options.height;
163-
mesh.receiveShadows = false;
164-
return mesh;
165-
}
166-
};
167-
168-
const MockTools = {
169-
ToRadians: (degrees: number) => degrees * (Math.PI / 180)
170-
};
171-
172-
return {
173-
Engine: MockEngine,
174-
Scene: MockBabylonScene,
175-
Vector3: actualMock.MockVector3,
176-
Color3: actualMock.MockColor3,
177-
Color4: actualMock.MockColor4,
178-
HemisphericLight: MockHemisphericLight,
179-
DirectionalLight: MockDirectionalLight,
180-
ShadowGenerator: MockShadowGenerator,
181-
ArcRotateCamera: MockArcRotateCamera,
182-
MeshBuilder: MockMeshBuilder,
183-
StandardMaterial: actualMock.MockStandardMaterial,
184-
Tools: MockTools,
185-
};
11+
const { createSceneHelperMock } = jest.requireActual("../../__mocks__/babylonjs.mock");
12+
return createSceneHelperMock();
18613
});
18714

18815
describe("initBabylonJS unit tests", () => {

0 commit comments

Comments
 (0)