Best JavaScript code snippet using wpt
game.spec.js
Source: game.spec.js
1import { expect } from 'chai';2import { createStore, applyMiddleware } from 'redux';3import combineReducer from '../../client/redux';4import thunkMiddleware from 'redux-thunk';5describe('Game Reducer', () => {6 let testStore;7 beforeEach('Create testing store', () => {8 testStore = createStore(combineReducer, applyMiddleware(thunkMiddleware));9 });10 const testLocalPlayer = {11 number: 2,12 damage: 110,13 xCoord: 110,14 yCoord: 110,15 lives: 3,16 animation: 'swing',17 isHit: false,18 flyRigh: false,19 characterGraphic: 'newSpritePath',20 weaponGraphic: 'newSpritePath'21 };22 const testRemotePlayers = {23 1: {24 number: 1,25 damage: 50,26 xCoord: 10,27 yCoord: 10,28 lives: 2,29 animation: 'stop',30 isHit: true,31 flyRigh: true,32 characterGraphic: 'newSpritePath',33 weaponGraphic: 'newSpritePath'34 },35 3: {36 number: 3,37 damage: 20,38 xCoord: 2,39 yCoord: 2,40 lives: 1,41 animation: 'left',42 isHit: true,43 flyRigh: false,44 characterGraphic: 'newSpritePath',45 weaponGraphic: 'newSpritePath'46 }47 }48 it('has expected initial state', () => {49 expect(testStore.getState().game).to.be.deep.equal({50 activePlayers: 4,51 isGamePlaying: false,52 playerNumber: 0,53 localPlayer: {},54 remotePlayers: {},55 playerStateChanges: {},56 winner: "",57 });58 });59 describe('START_GAME', () => {60 it('changes the isGamePlaying boolean to true', () => {61 testStore.dispatch({ type: 'START_GAME' });62 expect(testStore.getState().game.isGamePlaying).to.be.true;63 });64 }); // end describe('START_GAME')65 describe('END_GAME', () => {66 it('changes the isGamePlaying boolean to false', () => {67 testStore.dispatch({ type: 'END_GAME' });68 expect(testStore.getState().game.isGamePlaying).to.be.false;69 });70 }); // end describe('END_GAME')71 describe('UPDATE_PLAYERS_STATE', () => {72 beforeEach(() => testStore.dispatch({73 type: 'UPDATE_PLAYERS_STATE',74 localPlayer: testLocalPlayer,75 remotePlayers: testRemotePlayers76 }));77 it('changes all remote players states to the new states', () => {78 expect(testStore.getState().game.remotePlayers).to.deep.equal(testRemotePlayers);79 });80 it('only updates attack info for the local players state', () => {81 const storePlayer = testStore.getState().game.localPlayer;82 expect(storePlayer.damage).to.equal(testLocalPlayer.damage);83 expect(storePlayer.isHit).to.equal(testLocalPlayer.isHit);84 expect(storePlayer.flyRight).to.equal(testLocalPlayer.flyRight);85 const equalKeys = ['damage', 'isHit', 'flyRight'];86 Object.keys(storePlayer).forEach(key => {87 if(!equalKeys.includes(key))88 expect(storePlayer[key]).to.not.equal(testLocalPlayer[key]);89 });90 });91 }); // end describe('UPDATE_PLAYERS_STATE')92 describe('SET_PLAYER', () => {93 it('changes the playerNumber state to the given player number', () => {94 testStore.dispatch({ type: 'SET_PLAYER', playerNumber: testLocalPlayer.number });95 expect(testStore.getState().game.playerNumber).to.equal(testLocalPlayer.number);96 });97 }); // end describe('SET_PLAYER')98 describe('INIT_PLAYERS', () => {99 it('changes the localPlayer and remotePlayers states to the given state', () => {100 testStore.dispatch({101 type: 'INIT_PLAYERS',102 localPlayer: testLocalPlayer,103 remotePlayers: testRemotePlayers104 });105 expect(testStore.getState().game.localPlayer).to.deep.equal(testLocalPlayer);106 expect(testStore.getState().game.remotePlayers).to.deep.equal(testRemotePlayers);107 });108 }); // end describe('INIT_PLAYERS')109 describe('UPDATE_LOCAL_STATE', () => {110 beforeEach(() => testStore.dispatch({111 type: 'UPDATE_LOCAL_STATE',112 localPlayer: testLocalPlayer,113 remotePlayers: testRemotePlayers114 }));115 it('adds only lives, animation, x and y coordinates for localPlayer to the playerStateChanges object', () => {116 const storePlayer = testStore.getState().game.playerStateChanges[testLocalPlayer.number];117 expect(storePlayer.xCoord).to.equal(testLocalPlayer.xCoord);118 expect(storePlayer.yCoord).to.equal(testLocalPlayer.yCoord);119 expect(storePlayer.animation).to.equal(testLocalPlayer.animation);120 expect(storePlayer.lives).to.equal(testLocalPlayer.lives);121 const equalKeys = ['xCoord', 'yCoord', 'animation', 'lives'];122 Object.keys(storePlayer).forEach(key => {123 if(!equalKeys.includes(key))124 expect(storePlayer[key]).to.not.equal(testLocalPlayer[key]);125 });126 });127 it('adds only damage for remotePlayers to the playerStateChanges object', () => {128 const storePlayer = testStore.getState().game.playerStateChanges;129 Object.keys(testRemotePlayers).forEach(playerNum => {130 expect(storePlayer[playerNum].damage).to.equal(testRemotePlayers[playerNum].damage);131 });132 Object.keys(storePlayer[testLocalPlayer.number]).forEach(key => {133 if(key !== 'damage')134 expect(storePlayer[key]).to.not.equal(testLocalPlayer[key]);135 });136 });137 }); // end describe('UPDATE_LOCAL_STATE')...
hash-map.ts
Source: hash-map.ts
...19 const bucket = this.map.get(this.hashKey(key));20 if (!bucket) { return false; }21 if (Array.isArray(bucket)) {22 for (const item of bucket) {23 if (this.equalKeys(item.k, key)) { return true; }24 }25 } else {26 return this.equalKeys(bucket.k, key);27 }28 return false;29 }30 get(key: K): V | undefined {31 const bucket = this.map.get(this.hashKey(key));32 if (!bucket) { return undefined; }33 if (Array.isArray(bucket)) {34 for (const item of bucket) {35 if (this.equalKeys(item.k, key)) { return item.v; }36 }37 } else if (this.equalKeys(bucket.k, key)) {38 return bucket.v;39 }40 return undefined;41 }42 set(key: K, value: V): this {43 const hash = this.hashKey(key);44 let bucket = this.map.get(hash);45 if (!bucket) {46 bucket = {k: key, v: value};47 this.map.set(hash, bucket);48 this._size++;49 } else if (Array.isArray(bucket)) {50 let index = -1;51 for (let i = 0; i < bucket.length; i++) {52 if (this.equalKeys(bucket[i].k, key)) {53 index = i;54 break;55 }56 }57 if (index >= 0) {58 bucket.splice(index, 1);59 } else {60 this._size++;61 }62 bucket.push({k: key, v: value});63 } else if (this.equalKeys(bucket.k, key)) {64 this.map.set(hash, {k: key, v: value});65 } else {66 const single = bucket;67 bucket = [single, {k: key, v: value}];68 this.map.set(hash, bucket);69 this._size++;70 }71 return this;72 }73 delete(key: K): boolean {74 const hash = this.hashKey(key);75 const bucket = this.map.get(hash);76 if (!bucket) { return false; }77 if (Array.isArray(bucket)) {78 for (let i = 0; i < bucket.length; i++) {79 if (this.equalKeys(bucket[i].k, key)) {80 bucket.splice(i, 1);81 this._size--;82 return true;83 }84 }85 } else if (this.equalKeys(bucket.k, key)) {86 this.map.delete(hash);87 this._size--;88 return true;89 }90 return false;91 }92 clear(): void {93 this.map.clear();94 this._size = 0;95 }96 clone(): HashMap<K, V> {97 const clone = new HashMap<K, V>(this.hashKey, this.equalKeys);98 clone._size = this.size;99 for (const [hash, bucket] of this.map) {...
EnterFrameComp.js
Source: EnterFrameComp.js
1import React from 'react';2import { shallowEqual } from "../utils/MyUtil";3class EnterFrameComp extends React.Component {4 5 static defaultProps = {6 frameRate: 10,7 equalKeys: []8 };9 10 constructor(props)11 {12 super(props);13 14 this._timeOutId = -1;15 this.delayRun = this.delayRun.bind(this);16 }17 18 get frameTime()19 {20 let rate = this.props.frameRate || 10; //1000/1021 22 return parseInt(1000 / rate);23 }24 25 shouldComponentUpdate(nextProps, nextState)26 {27 if(this._timeOutId > -1)28 return false;29 30 let bChange = this.compare(nextProps, nextState, this.props, this.state);31 32 if(bChange)33 {34 this._timeOutId = setTimeout(this.delayRun, this.frameTime);35 }36 37 return false;38 }39 40 componentWillUnmount()41 {42 clearTimeout(this._timeOutId);43 this._timeOutId = -1;44 }45 46 setState(value, callback)47 {48 if(this._timeOutId > -1)49 {50 if(this.state)51 {52 Object.assign(this.state, value);53 }54 return false;55 }56 57 super.setState(value, callback);58 }59 60 compare(nextProps, nextState, thisProps, thisState)61 {62 let equalKeys = thisProps.equalKeys && thisProps.equalKeys.length > 0 ? thisProps.equalKeys : Object.keys(nextProps),63 deep = nextProps.deep || 3,64 index = equalKeys.findIndex(key => !shallowEqual(nextProps[key], thisProps[key], true, deep));65 66 return index > -1 || !shallowEqual(nextState, thisState, true, 2);67 }68 69 delayRun()70 {71 clearTimeout(this._timeOutId);72 this._timeOutId = -1;73 74 this.forceUpdate();75 }76}...
Using AI Code Generation
1var wptools = require('wptools');2var page = wptools.page('Narendra Modi');3page.get(function(err, resp) {4 if (err) {5 console.log(err);6 } else {7 console.log(resp.equalKeys());8 }9});10MIT © [Suyash Soni](
Using AI Code Generation
1var wptools = require('wptools');2var wp = wptools.page('Barack Obama');3wp.get(function(err, data) {4 if (err) {5 console.log(err);6 }7 else {8 console.log(data);9 }10});11 at errnoException (child_process.js:988:11)12 at Process.ChildProcess._handle.onexit (child_process.js:779:34)
Check out the latest blogs from LambdaTest on this topic:
People love to watch, read and interact with quality content — especially video content. Whether it is sports, news, TV shows, or videos captured on smartphones, people crave digital content. The emergence of OTT platforms has already shaped the way people consume content. Viewers can now enjoy their favorite shows whenever they want rather than at pre-set times. Thus, the OTT platform’s concept of viewing anything, anytime, anywhere has hit the right chord.
Software testing is fueling the IT sector forward by scaling up the test process and continuous product delivery. Currently, this profession is in huge demand, as it needs certified testers with expertise in automation testing. When it comes to outsourcing software testing jobs, whether it’s an IT company or an individual customer, they all look for accredited professionals. That’s why having an software testing certification has become the need of the hour for the folks interested in the test automation field. A well-known certificate issued by an authorized institute kind vouches that the certificate holder is skilled in a specific technology.
This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Mobile App Testing Tutorial.
Before we discuss the Joomla testing, let us understand the fundamentals of Joomla and how this content management system allows you to create and maintain web-based applications or websites without having to write and implement complex coding requirements.
Unit and functional testing are the prime ways of verifying the JavaScript code quality. However, a host of tools are available that can also check code before or during its execution in order to test its quality and adherence to coding standards. With each tool having its unique features and advantages contributing to its testing capabilities, you can use the tool that best suits your need for performing JavaScript testing.
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!!