Warning, /firebird/firebird-ng/src/app/services/three.service.spec.ts is written in an unsupported language. File is not indexed.
0001 import { TestBed } from '@angular/core/testing';
0002 import { NgZone } from '@angular/core';
0003 import { ThreeService } from './three.service';
0004 import * as THREE from 'three';
0005
0006 describe('ThreeService', () => {
0007 let service: ThreeService;
0008 let ngZone: NgZone;
0009 let testContainer: HTMLElement;
0010
0011 beforeEach(() => {
0012 TestBed.configureTestingModule({
0013 providers: [ThreeService],
0014 });
0015 service = TestBed.inject(ThreeService);
0016 ngZone = TestBed.inject(NgZone);
0017
0018 // Create a dummy container element for testing
0019 testContainer = document.createElement('div');
0020 testContainer.style.width = '800px';
0021 testContainer.style.height = '600px';
0022 document.body.appendChild(testContainer);
0023 });
0024
0025 afterEach(() => {
0026 // Cleanup: remove the test container and stop rendering
0027 if (testContainer.parentNode) {
0028 testContainer.parentNode.removeChild(testContainer);
0029 }
0030 service.stopRendering();
0031 });
0032
0033 it('should throw error if container not found when using string id', () => {
0034 expect(() => service.init('nonexistent-id')).toThrowError(/Container element #nonexistent-id not found/);
0035 });
0036
0037 it('should initialize scene, camera, renderer, and controls when provided an HTMLElement', () => {
0038 service.init(testContainer);
0039 expect(service.scene).toBeDefined();
0040 expect(service.renderer).toBeDefined();
0041 expect(service.controls).toBeDefined();
0042 // Check that the renderer’s DOM element was appended to the container
0043 expect(testContainer.contains(service.renderer.domElement)).toBeTrue();
0044 });
0045
0046 it('should update camera aspect when setSize is called', () => {
0047 service.init(testContainer);
0048 const initialAspect = service['perspectiveCamera'].aspect;
0049 service.setSize(1000, 1000);
0050 expect(service['perspectiveCamera'].aspect).toBeCloseTo(1, 0.1);
0051 });
0052
0053 it('should start and then stop the rendering loop', () => {
0054 service.init(testContainer);
0055 spyOn(window, 'requestAnimationFrame').and.callFake((cb: FrameRequestCallback) => {
0056 return 123; // dummy id
0057 });
0058 service.startRendering();
0059 expect((service as any).shouldRender).toBeTrue();
0060 service.stopRendering();
0061 expect((service as any).shouldRender).toBeFalse();
0062 });
0063
0064 it('should add and remove frame callbacks correctly', () => {
0065 service.init(testContainer);
0066 const callback = jasmine.createSpy('callback');
0067 service.addFrameCallback(callback);
0068 expect((service as any).frameCallbacks).toContain(callback);
0069 service.removeFrameCallback(callback);
0070 expect((service as any).frameCallbacks).not.toContain(callback);
0071 });
0072
0073 it('toggleOrthographicView should switch between cameras', () => {
0074 service.init(testContainer);
0075 const initialCamera = service.camera;
0076 service.toggleOrthographicView(true);
0077 expect(service.camera).not.toBe(initialCamera);
0078 service.toggleOrthographicView(false);
0079 expect(service.camera).toBe(service['perspectiveCamera']);
0080 });
0081 });