Best JavaScript code snippet using ng-mocks
main.js
Source:main.js
1//import * as THREE from "/js/three.module.js"2import { LineBasicMaterial } from "./js/three.module.js";3import { Mesh } from "./js/three.module.js";4import * as THREE from "./js/three.module.js"5import {Handle} from "./handle.js";6// ------------ Setting up camera and scene ------------ //7THREE.WebGLRenderer.alpha = true;8const scene = new THREE.Scene();9const camera = new THREE.PerspectiveCamera( 75, 500 / 400, 0.1, 1000 );10var canvRef = document.getElementById("my_canvas")11const renderer = new THREE.WebGLRenderer({canvas: canvRef});12renderer.setSize( 500, 400);13var container = document.getElementsByClassName("content");14//container[0].appendChild( renderer.domElement );15container[0].insertBefore(renderer.domElement, document.getElementsByClassName("content").item(0).lastElementChild);16var mouse = new THREE.Vector3();17document.addEventListener('mousemove', onDocumentMouseMove, false);18document.addEventListener("mousedown", onMouseDown, false);19camera.position.z = 20;20var testPos = new THREE.Vector3(0, 0, 0);21// ----------------------------------------------------- //22var centreMat = new THREE.LineBasicMaterial({color: 0xFF0000});23var nodeMat = new THREE.LineBasicMaterial({color: 0xFFFFFF});24var clickedMat = new LineBasicMaterial({color: 0x00FF00});25// ---- Actual circum circle stuff ---- //26var radius = 5;27var segments = 50;28var circleMat = new THREE.LineBasicMaterial({color: 0x0000ff});29var circleGeometry = new THREE.CircleGeometry(radius, segments);30//circleGeometry.vertices.shift();31let pts = new THREE.Path().absarc(0, 0, 2, 0, Math.PI * 2).getPoints(64);32const testGeo = new THREE.BufferGeometry().setFromPoints(pts);33var circleLine = new THREE.Line(testGeo, circleMat)34scene.add(circleLine);35// ------------------------------------ //36var heldObj;37const handleArray = [new Handle(0.5, 0, 0, scene, nodeMat, clickedMat), new Handle(0.5, 5, 0, scene, nodeMat, clickedMat), new Handle(0.5, -1, -3, scene, nodeMat, clickedMat)];38var testHandle1 = new Handle(0.1, 0, 0, scene, nodeMat, clickedMat);39var testHandle2 = new Handle(0.1, 0, 0, scene, nodeMat, clickedMat);40var normal1Handle = new Handle(0.1, 0, 0, scene, nodeMat, clickedMat);41var normal2Handle = new Handle(0.1, 0, 0, scene, nodeMat, clickedMat);42var centreHandle = new Handle(0.1, 0, 0, scene, centreMat, clickedMat);43// ------------- Debug Line Stuff ------------- //44const line1Points = [];45line1Points.push(new THREE.Vector3(handleArray[0].mesh.position.x, handleArray[0].mesh.position.y, 0));46line1Points.push(new THREE.Vector3(handleArray[1].mesh.position.x, handleArray[1].mesh.position.y, 0));47const line1Geo = new THREE.BufferGeometry().setFromPoints(line1Points);48const line1 = new THREE.Line(line1Geo, clickedMat);49const line2Points = [];50line2Points.push(new THREE.Vector3(handleArray[1].mesh.position.x, handleArray[1].mesh.position.y, 0));51line2Points.push(new THREE.Vector3(handleArray[2].mesh.position.x, handleArray[2].mesh.position.y, 0));52const line2Geo = new THREE.BufferGeometry().setFromPoints(line2Points);53const line2 = new THREE.Line(line2Geo, clickedMat);54scene.add(line1);55scene.add(line2);56// -------------------------------------------- //57function update(event)58{ 59 requestAnimationFrame( update );60 for(var i = 0; i < handleArray.length; i++)61 {62 handleArray[i].Update(event, mouse, camera, scene);63 }64 // handle1.Update(event, mouse, camera, scene);65 // handle2.Update(event, mouse, camera, scene);66 // handle3.Update(event, mouse, camera, scene);67 var centrePoint1X = handleArray[0].mesh.position.x + handleArray[1].mesh.position.x;68 var centrePoint1Y = handleArray[0].mesh.position.y + handleArray[1].mesh.position.y; 69 var testVec = new THREE.Vector3(centrePoint1X, centrePoint1Y, 0);70 var centrePoint2X = handleArray[1].mesh.position.x + handleArray[2].mesh.position.x;71 var centrePoint2Y = handleArray[1].mesh.position.y + handleArray[2].mesh.position.y; 72 var testVec2 = new THREE.Vector3(centrePoint2X, centrePoint2Y, 0);73 testHandle1.mesh.position.x = testVec.x * 0.5;74 testHandle1.mesh.position.y = testVec.y * 0.5;75 testHandle2.mesh.position.x = testVec2.x * 0.5;76 testHandle2.mesh.position.y = testVec2.y * 0.5;77 78 var normal1X = handleArray[1].mesh.position.x - handleArray[0].mesh.position.x;79 var normal1Y = handleArray[1].mesh.position.y - handleArray[0].mesh.position.y;80 var normal1Mag = Math.sqrt(normal1X * normal1X + normal1Y * normal1Y);81 normal1X /= normal1Mag;82 normal1Y /= normal1Mag;83 normal1Handle.mesh.position.x = testHandle1.mesh.position.x + (normal1Y);84 normal1Handle.mesh.position.y = testHandle1.mesh.position.y + -(normal1X);85 var normal2X = handleArray[2].mesh.position.x - handleArray[1].mesh.position.x;86 var normal2Y = handleArray[2].mesh.position.y - handleArray[1].mesh.position.y;87 var normal2Mag = Math.sqrt(normal2X * normal2X + normal2Y * normal2Y);88 normal2X /= normal2Mag;89 normal2Y /= normal2Mag;90 normal2Handle.mesh.position.x = testHandle2.mesh.position.x + (normal2Y);91 normal2Handle.mesh.position.y = testHandle2.mesh.position.y + -(normal2X);92 var t2 = normal1Y * (testHandle2.mesh.position.y - testHandle1.mesh.position.y) - -normal1X * (testHandle2.mesh.position.x - testHandle1.mesh.position.x);93 t2 /= -normal1X * normal2Y - normal1Y * -normal2X;94 centreHandle.mesh.position.x = testHandle2.mesh.position.x + (normal2Y * t2);95 centreHandle.mesh.position.y = testHandle2.mesh.position.y + (-normal2X * t2);96 var handle0ToCentreX = centreHandle.mesh.position.x - handleArray[0].mesh.position.x;97 var handle0ToCentreY = centreHandle.mesh.position.y - handleArray[0].mesh.position.y;98 var handle0ToCentreMag = Math.sqrt(handle0ToCentreX * handle0ToCentreX + handle0ToCentreY * handle0ToCentreY);99 var radius = handle0ToCentreMag;100 // updating circle radius:101 const newPts = new THREE.Path().absarc(0, 0, radius, 0, Math.PI * 2).getPoints(64);102 let newTestGeo = new THREE.BufferGeometry().setFromPoints(newPts);103 circleLine.geometry = newTestGeo;104 circleLine.position.x = centreHandle.mesh.position.x;105 circleLine.position.y = centreHandle.mesh.position.y;106}107update();108function animate() {109 requestAnimationFrame( animate );110 renderer.render( scene, camera );111 112}113animate();114function onDocumentMouseMove(event) {115 event.preventDefault();116 // Getting screen space mouse coordinates in device coordinates.117 mouse.x = (event.offsetX / 500) * 2 - 1;118 mouse.y = -(event.offsetY / 400) * 2 + 1;119 120 var vec = new THREE.Vector3(); // create once and reuse121 var pos = new THREE.Vector3(); // create once and reuse122 // Getting the mouse location in input device coordinates.123 vec.set(124 ( event.offsetX / 500 ) * 2 - 1,125 -( event.offsetY / 400 ) * 2 + 1,126 0);127 // After making them normalised device coordniates, we get them back to world space coordinates on z-level 0.128 vec.unproject( camera );129 // Moving the vector to the camera's position.130 vec.sub( camera.position ).normalize();131 132 var distance = 0;133 if(heldObj)134 distance = (heldObj.mesh.position.z - camera.position.z) / vec.z;135 136 pos.copy( camera.position ).add( vec.multiplyScalar( distance ) );137 //testPos = pos;138 139 if(heldObj)140 { 141 heldObj.mesh.translateX(pos.x - heldObj.mesh.getWorldPosition(heldObj.mesh.position).x);142 heldObj.mesh.translateY(pos.y - heldObj.mesh.getWorldPosition(heldObj.mesh.position).y); 143 }144 // Old debugging line.145 //var temp = new THREE.Vector3(mouse.x, mouse.y, mouse.z);146 //var circleToMouse = temp.sub(nodeMesh.position);147 //var mag = circleToMouse.x * circleToMouse.x + circleToMouse.y * circleToMouse.y;148 //mag = Math.sqrt(mag);149 //points[1].x = testPos.x;150 //points[1].y = testPos.y;151 //const lineGeo = new THREE.BufferGeometry().setFromPoints(points);152 //line.geometry = new THREE.BufferGeometry().setFromPoints(points);153 154 // ----------------- Updating debug lines if player moves the handles ----------------- //155 line1Points[0].x = handleArray[0].mesh.position.x;156 line1Points[0].y = handleArray[0].mesh.position.y;157 line1Points[1].x = handleArray[1].mesh.position.x;158 line1Points[1].y = handleArray[1].mesh.position.y;159 line2Points[0].x = handleArray[1].mesh.position.x;160 line2Points[0].y = handleArray[1].mesh.position.y;161 line2Points[1].x = handleArray[2].mesh.position.x;162 line2Points[1].y = handleArray[2].mesh.position.y;163 const line1Geo = new THREE.BufferGeometry().setFromPoints(line1Points);164 line1.geometry = line1Geo;165 166 const line2Geo = new THREE.BufferGeometry().setFromPoints(line2Points);167 line2.geometry = line2Geo;168}169function CheckOverlap(hit){170 var ray = new THREE.Raycaster();171 ray.setFromCamera(mouse, camera);172 173 const hits = ray.intersectObjects(scene.children);174 if(hits.length > 0)175 {176 // We check if the passed in object was one of the hits.177 for(var i = 0; i < hits.length; i++)178 {179 if(hit.id == hits[i].object.id)180 {181 return true;182 }183 }184 return false;185 }186 else187 {188 return false;189 }190}191function onMouseDown(event)192{193 if(heldObj) // If we are already holding a handle, drop it if we click again.194 {195 heldObj = null;196 }197 else198 {199 for(var i = 0; i < handleArray.length; i++)200 {201 if(handleArray[i].m_IsHovering)202 {203 console.log("You clicked on a handle!");204 heldObj = handleArray[i];205 206 }207 208 }209 }...
linecollide.ts
Source:linecollide.ts
1import { Vector } from './vector';2import { DisplayString, Input, renderLine, renderLoop, VectorEngine } from '../../engine/index'3export { VectorEngineExports } from '../../engine/index';4VectorEngine.init();5const handleArray: StaticArray<Vector> = [6 new Vector(-0.5, -0.5),7 new Vector(0.5, 0.5),8 new Vector(0.5, -0.5),9 new Vector(-0.5, 0.5),10];11const red: u32 = 0xff_00_00_ff;12const green: u32 = 0x00_ff_00_ff;13const yellow: u32 = 0xff_ff_00_ff;14const white: u32 = 0xff_ff_ff_ff;15const blue: u32 = 0x99_99_ff_ff;16const purple: u32 = 0xff_00_ff_ff;17const handleLoop: StaticArray<f32> = [18 -0.02, -0.02,19 0.02, -0.02,20 0.02, 0.02,21 -0.02, 0.02,22]23const line1: StaticArray<f32> = [24 -1, -1,25 1, 1,26];27const line2: StaticArray<f32> = [28 -1, 1,29 1, -1,30]31var dragIndex: i32 = -1;32function mouseDownCheck(): i32 {33 for (let i: i32 = 0; i < handleArray.length; i++) {34 if (Mathf.abs(handleArray[i].x - Input.MouseX) < 0.05 &&35 Mathf.abs(handleArray[i].y - Input.MouseY) < 0.05) {36 return i;37 }38 }39 return -1;40}41var directionA: Vector = new Vector();42var directionB: Vector = new Vector();43var distancePoint1: Vector = new Vector();44var distancePoint2: Vector = new Vector();45var rotatedDirection: Vector = new Vector();46function segmentTest(SegmentBegin: Vector, SegmentEnd: Vector,47 LineBegin: Vector, LineEnd: Vector48): bool {49 directionA.copy(SegmentEnd);50 directionA.subtract(SegmentBegin);51 if (directionA.x == 0 && directionA.y == 0) {52 return false;53 }54 directionB.copy(SegmentEnd);55 distancePoint1.copy(SegmentBegin)56 distancePoint2.copy(SegmentBegin)57 distancePoint1.subtract(LineBegin)58 distancePoint2.subtract(LineEnd)59 rotatedDirection.copy(directionA);60 rotatedDirection.rotate90();61 if (rotatedDirection.dot(distancePoint1) * rotatedDirection.dot(distancePoint2) > 0) {62 return false;63 }64 directionB.subtract(LineBegin);65 if (directionA.x == 0 && directionA.y == 0) {66 return false;67 }68 distancePoint1.copy(LineBegin);69 distancePoint1.subtract(SegmentBegin);70 distancePoint2.copy(LineBegin);71 distancePoint2.subtract(SegmentEnd);72 rotatedDirection.copy(directionB);73 rotatedDirection.rotate90();74 if (rotatedDirection.dot(distancePoint1) * rotatedDirection.dot(distancePoint2) > 0) {75 return false;76 }77 return true;78}79function lineCollision(): bool {80 return segmentTest(handleArray[0], handleArray[1],81 handleArray[2], handleArray[3]) &&82 segmentTest(handleArray[2], handleArray[3],83 handleArray[0], handleArray[1]);84}85export function gameLogic(delta: i32): void {86 let mouse_down: bool = Input.MouseLeftButton;87 if (dragIndex == -1 && mouse_down) {88 dragIndex = mouseDownCheck();89 }90 if (mouse_down == false) {91 dragIndex = -1;92 }93 if (dragIndex != -1 && mouse_down) {94 handleArray[dragIndex].x = Input.MouseX;95 handleArray[dragIndex].y = Input.MouseY;96 }97 line1[0] = handleArray[0].x;98 line1[1] = handleArray[0].y;99 line1[2] = handleArray[1].x;100 line1[3] = handleArray[1].y;101 line2[0] = handleArray[2].x;102 line2[1] = handleArray[2].y;103 line2[2] = handleArray[3].x;104 line2[3] = handleArray[3].y;105 let collision: bool = lineCollision();106 renderLine(line1, 0.0, 0.0, collision ? red : green, 0.0, 1.0);107 renderLine(line2, 0.0, 0.0, collision ? red : green, 0.0, 1.0);108 renderLoop(handleLoop, handleArray[0].x, handleArray[0].y, yellow, 0.0, 1.0);109 renderLoop(handleLoop, handleArray[1].x, handleArray[1].y, yellow, 0.0, 1.0);110 renderLoop(handleLoop, handleArray[2].x, handleArray[2].y, blue, 0.0, 1.0);111 renderLoop(handleLoop, handleArray[3].x, handleArray[3].y, blue, 0.0, 1.0);...
WeatherPage.js
Source:WeatherPage.js
...12 const modifiedgivenData = givenData.slice(day.length, givenData.length);13 return [day, modifiedgivenData];14 };15 // #########################16 const [firstDay, firstModData] = handleArray(data);17 const [secondDay, secondModData] = handleArray(firstModData);18 const [thirdDay, thirdModData] = handleArray(secondModData);19 const [fourthdDay, fourthModData] = handleArray(thirdModData);20 const [fifthdDay, fifthModData] = handleArray(fourthModData);21 const [sixthdDay, sixthModData] = handleArray(fifthModData);22 const [seventhdDay, seventhModData] = handleArray(sixthModData);23 const [eigthDay, eigthModData] = handleArray(seventhModData);24 const [ninethdDay, ninethModData] = handleArray(eigthModData);25 const [tenthdDay, tenthModData] = handleArray(ninethModData);26 return (27 <div className="weather_page">28 <WeatherRow dayData={firstDay} />29 <WeatherRow dayData={secondDay} />30 <WeatherRow dayData={thirdDay} />31 <WeatherRow dayData={fourthdDay} />32 <WeatherRow dayData={fifthdDay} />33 <WeatherRow dayData={sixthdDay} />34 <WeatherRow dayData={seventhdDay} />35 <WeatherRow dayData={eigthDay} />36 <WeatherRow dayData={ninethdDay} />37 <WeatherRow dayData={tenthdDay} />38 </div>39 );...
Using AI Code Generation
1import { handleArray } from 'ng-mocks-universal';2describe('handleArray', () => {3 it('should return an array', () => {4 const result = handleArray([1, 2, 3]);5 expect(result).toEqual([1, 2, 3]);6 });7});8import { mockProvider } from 'ng-mocks-universal';9describe('mockProvider', () => {10 it('should return a mock class', () => {11 const result = mockProvider(FooService);12 expect(result).toBeDefined();13 });14});15import { mockMethod } from 'ng-mocks-universal';16describe('mockMethod', () => {17 it('should return a mock method', () => {18 const result = mockMethod(FooService, 'foo');19 expect(result).toBeDefined();20 });21});22import { mockProperty } from 'ng-mocks
Using AI Code Generation
1let result = ngMocks.handleArray(array);2let result = ngMocks.handleObject(object);3let result = ngMocks.handleString(string);4let result = ngMocks.handleValue(value);5let result = ngMocks.hasClass(element, className);6let result = ngMocks.hasDirective(element, directive);7let result = ngMocks.hasInput(element, input);8let result = ngMocks.hasInput(element, input, value);9let result = ngMocks.hasInput(element, input, value, strict);10let result = ngMocks.hasOutput(element, output);11let result = ngMocks.hasOutput(element, output, value);12let result = ngMocks.hasOutput(element, output, value, strict);13let result = ngMocks.hasSelector(element, selector);14let result = ngMocks.hasSelector(element, selector, count);15let result = ngMocks.hasSelector(element, selector, count, strict);16let result = ngMocks.hasStyle(element, style);17let result = ngMocks.hasStyle(element, style, value);
Using AI Code Generation
1import { handleArray } from 'ng-mocks';2const result = handleArray([1, 2, 3]);3import { handleFunction } from 'ng-mocks';4const result = handleFunction(() => 'test');5import { handleObject } from 'ng-mocks';6const result = handleObject({ name: 'test' });7import { handleString } from 'ng-mocks';8const result = handleString('test');9import { handleUndefined } from 'ng-mocks';10const result = handleUndefined(undefined);11import { instance } from 'ng-mocks';12const result = instance(mockObject);13import { mock } from 'ng-mocks';14const result = mock(OriginalClass);15import { mockAll } from 'ng-mocks';16const result = mockAll(OriginalClass);17import { mockProvider } from 'ng-mocks';
Using AI Code Generation
1import { handleArray } from 'ng-mocks';2import { MockService } from './mock-service';3describe('MockService', () => {4 let service: MockService;5 beforeEach(() => {6 service = handleArray(MockService);7 });8 it('should return an array of strings', () => {9 expect(service.getValues()).toEqual(['value1', 'value2']);10 });11});12import { Injectable } from '@angular/core';13@Injectable()14export class MockService {15 getValues(): string[] {16 return ['value1', 'value2'];17 }18}19What is the difference between ng-mocks and jasmine.createSpyObj()?20To mock a service in Angular 5, we can use the ng-mocks library. This library provides the handleArray() method that can be used to
Using AI Code Generation
1var newArray = ngMocks.handleArray(originalArray);2var newObject = ngMocks.handleObject(originalObject);3var newFunction = ngMocks.handleFunction(originalFunction);4var newPromise = ngMocks.handlePromise(originalPromise);5var newObservable = ngMocks.handleObservable(originalObservable);6var newService = ngMocks.handleService(originalService);7var newRoute = ngMocks.handleRoute(originalRoute);8var newDirective = ngMocks.handleDirective(originalDirective);9var newComponent = ngMocks.handleComponent(originalComponent);10var newPipe = ngMocks.handlePipe(originalPipe);11var newModule = ngMocks.handleModule(originalModule);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!