Best JavaScript code snippet using root
Artifact.test.js
Source:Artifact.test.js
...15 super();16 this.doStart = jest.fn().mockImplementation(() => super.doStart());17 this.doStop = jest.fn().mockImplementation(() => super.doStop());18 this.doSave = jest.fn().mockImplementation(() => super.doSave());19 this.doDiscard = jest.fn().mockImplementation(() => super.doDiscard());20 }21 }22 beforeEach(() => {23 artifact = new ArifactExtensionTest();24 });25 it('should have a name', () => {26 expect(artifact.name).toBe(ArifactExtensionTest.name);27 });28 it('should pass a regular save flow', async () => {29 expect(artifact.doStart).not.toHaveBeenCalled();30 await artifact.start();31 expect(artifact.doStart).toHaveBeenCalled();32 expect(artifact.doStop).not.toHaveBeenCalled();33 await artifact.stop();34 expect(artifact.doStop).toHaveBeenCalled();35 expect(artifact.doSave).not.toHaveBeenCalled();36 await artifact.save('path/to/artifact');37 expect(artifact.doSave).toHaveBeenCalledWith('path/to/artifact');38 });39 it('should pass a regular discard flow', async () => {40 expect(artifact.doStart).not.toHaveBeenCalled();41 await artifact.start();42 expect(artifact.doStart).toHaveBeenCalled();43 expect(artifact.doStop).not.toHaveBeenCalled();44 await artifact.stop();45 expect(artifact.doStop).toHaveBeenCalled();46 expect(artifact.doDiscard).not.toHaveBeenCalled();47 await artifact.discard();48 expect(artifact.doDiscard).toHaveBeenCalled();49 });50 it('should pass a fast synchronous workflow', async () => {51 artifact.start();52 artifact.stop();53 artifact.save('/path/to/artifact');54 await artifact.save();55 expect(artifact.doStart).toHaveBeenCalledTimes(1);56 expect(artifact.doStop).toHaveBeenCalledTimes(1);57 expect(artifact.doSave).toHaveBeenCalledTimes(1);58 expect(artifact.doSave).toHaveBeenCalledWith('/path/to/artifact');59 });60 describe('.start()', () => {61 describe('if no other methods have been called', () => {62 it('should call protected .doStart() method and pass args to it', async () => {63 await artifact.start(1, 2, 3);64 expect(artifact.doStart).toHaveBeenCalledWith(1, 2, 3);65 });66 it('should reject if the protected .doStart() rejects', async () => {67 const err = new Error();68 artifact.doStart.mockReturnValue(Promise.reject(err));69 await expect(artifact.start()).rejects.toThrow(err);70 });71 });72 describe('if .start() has been called before', () => {73 beforeEach(async () => artifact.start());74 it('should call .stop() and .start() again', async () => {75 expect(artifact.doStop).not.toHaveBeenCalled();76 await artifact.start(1, 2, 3, 4);77 expect(artifact.doStop).toHaveBeenCalled();78 expect(artifact.doStart).toHaveBeenCalledTimes(2);79 });80 });81 describe('if .save() has been called before', () => {82 beforeEach(async () => {83 await artifact.start();84 await artifact.save('artifactPath');85 });86 it('should wait till .save() ends and .start() again', async () => {87 await artifact.start(1, 2, 3, 4);88 expect(artifact.doStart).toHaveBeenCalledTimes(2);89 // TODO: assert the correct execution order90 });91 });92 describe('if .save() has been rejected before', () => {93 let err;94 beforeEach(async () => {95 artifact.doSave.mockReturnValue(Promise.reject(err = new Error()));96 await artifact.start();97 await artifact.save('artifactPath').catch(_.noop);98 });99 it('should reject as well', async () => {100 await expect(artifact.start()).rejects.toThrow(err);101 });102 });103 describe('if .discard() has been called before', () => {104 beforeEach(async () => {105 await artifact.start();106 await artifact.discard();107 });108 it('should wait till .discard() ends and .start() again', async () => {109 await artifact.start(1, 2, 3, 4);110 expect(artifact.doStart).toHaveBeenCalledTimes(2);111 // TODO: assert the correct execution order112 });113 });114 describe('if .discard() has been rejected before', () => {115 let err;116 beforeEach(async () => {117 artifact.doDiscard.mockReturnValue(Promise.reject(err = new Error()));118 await artifact.start();119 await artifact.discard().catch(_.noop);120 });121 it('should reject as well', async () => {122 await expect(artifact.start()).rejects.toThrow(err);123 });124 });125 });126 describe('.stop()', () => {127 describe('if .start() has never been called', () => {128 it('should resolve as an empty stub', async () => {129 await artifact.stop();130 });131 it('should not call protected .doStop()', async () => {132 await artifact.stop();133 expect(artifact.doStop).not.toHaveBeenCalled();134 });135 it('should not call protected .doStart()', async () => {136 await artifact.stop();137 expect(artifact.doStart).not.toHaveBeenCalled();138 });139 });140 describe('if .start() has been resolved', () => {141 beforeEach(async () => artifact.start());142 it('should call protected .doStop()', async () => {143 await artifact.stop(9, 1, 1);144 expect(artifact.doStop).toHaveBeenCalledWith(9, 1, 1);145 });146 it('should keep returning the same promise on consequent calls', async () => {147 expect(artifact.stop()).toBe(artifact.stop());148 await artifact.stop();149 expect(artifact.doStop).toHaveBeenCalledTimes(1);150 });151 it('should reject if .doStop() rejects', async () => {152 const err = new Error();153 artifact.doStop.mockReturnValue(Promise.reject(err));154 await expect(artifact.stop()).rejects.toThrow(err);155 });156 });157 describe('if .start() has been rejected', () => {158 let error;159 beforeEach(async () => {160 error = new Error();161 artifact.doStart.mockReturnValue(Promise.reject(error));162 await artifact.start().catch(_.noop);163 });164 it('should reject the same error too', async () => {165 await expect(artifact.stop()).rejects.toThrow(error);166 });167 it('should not call protected .doStop()', async () => {168 await artifact.stop().catch(_.noop);169 expect(artifact.doStop).not.toHaveBeenCalled();170 });171 });172 });173 describe('.discard()', () => {174 describe('if .start() has never been called', () => {175 it('should not throw an error', async () => {176 await artifact.discard();177 });178 it('should not call protected .doStart()', async () => {179 await artifact.discard();180 expect(artifact.doStart).not.toHaveBeenCalled();181 });182 it('should not call protected .doStop()', async () => {183 await artifact.discard();184 expect(artifact.doStop).not.toHaveBeenCalled();185 });186 it('should not call protected .doDiscard()', async () => {187 await artifact.discard();188 expect(artifact.doDiscard).not.toHaveBeenCalled();189 });190 it('should resolve .stop() with the same stub promise', async () => {191 expect(artifact.discard()).toBe(artifact.stop());192 });193 });194 describe('if .start() has been resolved', () => {195 beforeEach(async () => artifact.start());196 it('should call protected .doStop()', async () => {197 await artifact.discard();198 expect(artifact.doStop).toHaveBeenCalled();199 });200 it('should call protected .doDiscard()', async () => {201 await artifact.discard();202 expect(artifact.doDiscard).toHaveBeenCalled();203 });204 it('should keep returning the same promise on consequent calls', async () => {205 expect(artifact.discard()).toBe(artifact.discard());206 await artifact.discard();207 expect(artifact.doStop).toHaveBeenCalledTimes(1);208 expect(artifact.doDiscard).toHaveBeenCalledTimes(1);209 });210 });211 describe('if .start() has been rejected', () => {212 let error;213 beforeEach(async () => {214 error = new Error();215 artifact.doStart.mockReturnValue(Promise.reject(error));216 await artifact.start().catch(_.noop);217 });218 it('should return the .start() error', async () => {219 await artifact.discard().catch(_.noop);220 await expect(artifact.discard()).rejects.toThrow(error);221 });222 it('should not call protected .doStop()', async () => {223 await artifact.discard().catch(_.noop);224 expect(artifact.doStop).not.toHaveBeenCalled();225 });226 it('should not call protected .doDiscard()', async () => {227 await artifact.discard().catch(_.noop);228 expect(artifact.doDiscard).not.toHaveBeenCalled();229 });230 });231 describe('if .stop() has been rejected', () => {232 let error;233 beforeEach(async () => {234 error = new Error();235 artifact.doStop.mockReturnValue(Promise.reject(error));236 await artifact.start();237 await artifact.stop().catch(_.noop);238 });239 it('should return the .stop() error', async () => {240 await artifact.discard().catch(_.noop);241 await expect(artifact.discard()).rejects.toThrow(error);242 });243 it('should not call protected .doDiscard()', async () => {244 await artifact.discard().catch(_.noop);245 expect(artifact.doDiscard).not.toHaveBeenCalled();246 });247 });248 describe('if .save() has been called', () => {249 beforeEach(async () => {250 await artifact.start();251 await artifact.stop();252 await artifact.save('artifactPath');253 });254 it('should not call protected .doDiscard()', async () => {255 await artifact.discard();256 expect(artifact.doDiscard).not.toHaveBeenCalled();257 });258 it('should return .save() promise', () => {259 expect(artifact.discard()).toBe(artifact.save());260 });261 });262 });263 describe('.save(artifactPath)', () => {264 describe('if .start() has never been called', () => {265 beforeEach(async () => artifact.save('artifactPath'));266 it('should not call protected .doStart()', async () => {267 expect(artifact.doStart).not.toHaveBeenCalled();268 });269 it('should not call protected .doStop()', async () => {270 expect(artifact.doStop).not.toHaveBeenCalled();271 });272 it('should not call protected .doDiscard()', async () => {273 expect(artifact.doDiscard).not.toHaveBeenCalled();274 });275 it('should return the same promise on next calls', async () => {276 expect(artifact.save('artifactPath')).toBe(artifact.stop());277 });278 it('should resolve .stop() with the same stub promise', async () => {279 expect(artifact.save()).toBe(artifact.stop());280 });281 });282 describe('if .discard() has been called before', () => {283 beforeEach(async () => artifact.discard());284 it('should return discard promise instead', async () => {285 expect(artifact.save('artifactPath')).toBe(artifact.discard());286 });287 it('should not call protected .doSave(artifactPath)', async () => {288 await artifact.save('artifactPath');289 expect(artifact.doSave).not.toHaveBeenCalled();290 });291 });292 describe('if .start() has been called', () => {293 beforeEach(async () => artifact.start());294 it('should call protected .doSave(artifactPath)', async () => {295 await artifact.save('artifactPath');296 expect(artifact.doSave).toHaveBeenCalledWith('artifactPath');297 });298 it('should call protected .doStop()', async () => {299 await artifact.save('artifactPath');300 expect(artifact.doStop).toHaveBeenCalled();301 });302 });303 describe('if .start() has been rejected', () => {304 let error;305 beforeEach(async () => {306 error = new Error();307 artifact.doStart.mockReturnValue(Promise.reject(error));308 await artifact.start().catch(_.noop);309 });310 it('should return the same error', async () => {311 await expect(artifact.save('artifactPath')).rejects.toThrow(error);312 });313 it('should not call protected .doStop()', async () => {314 await artifact.save('artifactPath').catch(_.noop);315 expect(artifact.doStop).not.toHaveBeenCalled();316 });317 it('should not call protected .doSave()', async () => {318 await artifact.save('artifactPath').catch(_.noop);319 expect(artifact.doSave).not.toHaveBeenCalled();320 });321 });322 describe('if .stop() has been rejected', () => {323 let error;324 beforeEach(async () => {325 error = new Error();326 artifact.doStop.mockReturnValue(Promise.reject(error));327 await artifact.start();328 await artifact.stop().catch(_.noop);329 });330 it('should return the same error', async () => {331 await expect(artifact.save('artifactPath')).rejects.toThrow(error);332 });333 it('should not call protected .doStop()', async () => {334 artifact.doStop.mockClear();335 await artifact.save('artifactPath').catch(_.noop);336 expect(artifact.doStop).not.toHaveBeenCalled();337 });338 it('should not call protected .doSave()', async () => {339 await artifact.save('artifactPath').catch(_.noop);340 expect(artifact.doSave).not.toHaveBeenCalled();341 });342 });343 });344 });345 describe('methods as constructor arg', () => {346 let artifact;347 it('should replace protected .name with arg.name', () => {348 artifact = new Artifact({ name: 'SomeName' });349 expect(artifact.name).toBe('SomeName');350 });351 it('should replace protected .doStart() with arg.start()', async () => {352 const start = jest.fn();353 artifact = new Artifact({ start });354 await artifact.start(1, 2, 3);355 expect(start).toHaveBeenCalledWith(1, 2, 3);356 });357 it('should replace protected .doStop() with arg.stop()', async () => {358 const stop = jest.fn();359 artifact = new Artifact({ stop });360 await artifact.start();361 await artifact.stop(3, 4, 5);362 expect(stop).toHaveBeenCalledWith(3, 4, 5);363 });364 it('should replace protected .doSave() with arg.save()', async () => {365 const save = jest.fn();366 artifact = new Artifact({ save });367 await artifact.start();368 await artifact.save('path', 100);369 expect(save).toHaveBeenCalledWith('path', 100);370 });371 it('should replace protected .doDiscard() with arg.discard()', async () => {372 const discard = jest.fn();373 artifact = new Artifact({ discard });374 await artifact.start();375 await artifact.discard(200);376 expect(discard).toHaveBeenCalledWith(200);377 });378 });379 describe('static helper methods', () => {380 describe('.moveTemporaryFile', () => {381 let source = '', destination = '';382 beforeEach(() => {383 source = tempfile('.tmp');384 });385 afterEach(async () => {...
CardMJMediator.ts
Source:CardMJMediator.ts
...47 break;48 case UIEventConsts.REMOVE_READYHAND:49 break;50 case UIEventConsts.USER_DODISCARD:51 // this._cardVc.doDiscard(uniLib.UserInfo.uid,ddzGame.GameData.getInstance().waitCard);52 break;53 case UIEventConsts.USER_DODISCARD_FAIL:54 // this._cardVc.removeOutPutCard(0,evt.data);55 break;56 }57 if(obj){58 this.sendNotification(PokerFourFacadeConst.SEND_DATA,obj,DataRequestCommand.GAME_DATA);59 uniLib.NetMgr.setMsgTimeout(8,"OutCardMahjongCmd_C");60 } 61 }62 public listNotificationInterests(): Array<any> {63 return [ 64 PokerFourFacadeConst.RELOGIN, 65 PokerFourFacadeConst.GAME_START, 66 PokerFourFacadeConst.SEND_CARDS,67 PokerFourFacadeConst.RESET_TABLE,68 PokerFourFacadeConst.GM_CHANGE_CARD,69 PokerFourFacadeConst.DISCARD_NOTICE,70 PokerFourFacadeConst.OUT_CARD_REFRESH,71 PokerFourFacadeConst.USER_ENTER_ROOM,72 PokerFourFacadeConst.GAME_SET_LANDOWNER,73 PokerFourFacadeConst.NOTIFY_COMMON_CHAT,74 PokerFourFacadeConst.NOTICE_CARD_ENABLE,75 PokerFourFacadeConst.GAME_NOTIFY_OPERATE,76 PokerFourFacadeConst.SELF_HANDCARD_CHANGE,77 PokerFourFacadeConst.NOTIFY_HOST,78 PokerFourFacadeConst.RESULT_NOTICE,79 PokerFourFacadeConst.MINGPAI_GUANGBO,80 ];81 }82 public handleNotification(notification: puremvc.INotification): void {83 switch(notification.getName()) {84 case PokerFourFacadeConst.RESET_TABLE:85 this._cardVc.resetTable();86 break;87 case PokerFourFacadeConst.GAME_START:88 this._cardVc.startGame();89 if (PKGame.RoomInfo.getInstance().isMingPlay){90 this._cardVc.resetMingPaiNode();91 }92 break;93 case PokerFourFacadeConst.GAME_SET_LANDOWNER:94 //åºçå å
¥æç95 let land:Cmd.SetLandownerPokerCmd_Brd = notification.getBody();96 if(land.landownerId == uniLib.UserInfo.uid){97 this._cardVc.insertDipai();98 }99 //å°ä¸»æå
¥åºçè°ç¨æ¥å£100 this._cardVc.insertDiCards(land.landownerCardSet,land.landownerId);101 break;102 case PokerFourFacadeConst.USER_ENTER_ROOM:103 this._cardVc.creatUserCardSide();104 if(PKGame.RoomInfo.getInstance().isHostMode == 1){105 this._cardVc.setHostMask(1);106 }107 break;108 case PokerFourFacadeConst.SELF_HANDCARD_CHANGE:109 let thisId: number = notification.getBody();110 if(!thisId)111 this._cardVc.refreshHandCards();112 else113 this._cardVc.draw(thisId);114 break;115 case PokerFourFacadeConst.SEND_CARDS:116 this._cardVc.setUserCards();117 this._cardVc.startGame();118 break;119 case PokerFourFacadeConst.RELOGIN: 120 this._cardVc.creatUserCardSide();121 this._cardVc.setRelogindata(notification.getBody());122 if(RoomInfo.getInstance().isHostMode == 1){123 this._cardVc.setHostMask(1);124 }125 var Carddata:any = notification.getBody();126 if (Carddata["othersShowCard"].length && PKGame.RoomInfo.getInstance().isMingPlay) {127 this._cardVc.resetMingPaiNode();128 for (var i:number = 0; i < Carddata["othersShowCard"].length; i++) {129 var showData:Cmd.UserCardObj = Carddata["othersShowCard"][i];130 var arr:any = showData["handCardSet"];131 if (arr && arr.length) {132 arr.sort((a,b)=>{133 var num1 = GameUtil.cardValue(a);134 var num2 = GameUtil.cardValue(b);135 if (num1 > num2) {136 return -1;137 }else if (num1 == num2) {138 return 0;139 }else {140 return 1;141 }142 })143 }144 var seat:number = PKGame.RoomInfo.getInstance().getSeatNoByUserId(showData["uid"]);145 this._cardVc.setMingPaiNode(seat,arr,showData["uid"]);146 }147 }148 //éè¿æææ°åºçç149 if (Carddata.newOutCard && !GameUtil.isObjectOfNull(Carddata.newOutCard.outCardSet)) {150 var seatId:number = PKGame.RoomInfo.getInstance().getSeatNoByUserId(Carddata.newOutCard.uid);151 if (seatId == 2) {152 seatId = 3;153 }154 PKGame.CardInfo.getInstance().outCardSeatId = seatId;155 this._cardVc.doDiscard(Carddata.newOutCard);156 }157 break;158 case PokerFourFacadeConst.RESULT_NOTICE:159 this._cardVc.doWin(notification.getBody());160 this._cardVc.hideCardListenIcon();161 this._cardVc.setEnable(false);162 this._cardVc.clearCards();163 this._cardVc.resetMingPaiNode();164 break;165 case PokerFourFacadeConst.OUT_CARD_REFRESH:166 this._cardVc.refreshHandCards(1,notification.getBody());167 break;168 case PokerFourFacadeConst.GM_CHANGE_CARD:169 this._cardVc.refreshHandCards();170 break;171 case PokerFourFacadeConst.NOTICE_CARD_ENABLE:172 this._cardVc.setEnable(true);173 break;174 case PokerFourFacadeConst.DISCARD_NOTICE:175 var data = PKGame.RoomInfo.getInstance().OutCard;176 if (data.outCardSet && data.outCardSet.length > 0) {177 var seatId:number = PKGame.RoomInfo.getInstance().getSeatNoByUserId(data.uid);178 if (seatId == 2) {179 seatId = 3;180 }181 PKGame.CardInfo.getInstance().outCardSeatId = seatId;182 this._cardVc.clearMingPaiCards(data.outCardSet,data.uid);183 }184 this._cardVc.clearDiscard();185 this._cardVc.doDiscard(data);186 187 break;188 case PokerFourFacadeConst.GAME_NOTIFY_OPERATE:189 // this._cardVc.clearDiscard();190 break;191 case PokerFourFacadeConst.MINGPAI_GUANGBO:192 var mingPaiData:Cmd.ShowCardPokerCmd_Brd = notification.getBody();193 if (mingPaiData.handCard.uid != PKGame.MyUserInfo.getInstance().userId && PKGame.RoomInfo.getInstance().isMingPlay) {194 var seat:number = PKGame.RoomInfo.getInstance().getSeatNoByUserId(mingPaiData.handCard["uid"]);195 196 this._cardVc.setMingPaiNode(seat,mingPaiData.handCard["handCardSet"],mingPaiData.handCard["uid"]);197 }198 break;199 }...
Using AI Code Generation
1var rootNode = app.project.rootItem.children[0];2rootNode.doDiscard();3var rootNode = app.project.rootItem.children[0];4rootNode.doDiscard();5var rootNode = app.project.rootItem.children[0];6rootNode.doDiscard();7var rootNode = app.project.rootItem.children[0];8rootNode.doDiscard();9var rootNode = app.project.rootItem.children[0];10rootNode.doDiscard();11var rootNode = app.project.rootItem.children[0];12rootNode.doDiscard();13var rootNode = app.project.rootItem.children[0];14rootNode.doDiscard();15var rootNode = app.project.rootItem.children[0];16rootNode.doDiscard();17var rootNode = app.project.rootItem.children[0];18rootNode.doDiscard();19var rootNode = app.project.rootItem.children[0];20rootNode.doDiscard();21var rootNode = app.project.rootItem.children[0];22rootNode.doDiscard();23var rootNode = app.project.rootItem.children[0];24rootNode.doDiscard();25var rootNode = app.project.rootItem.children[0];26rootNode.doDiscard();27var rootNode = app.project.rootItem.children[0];28rootNode.doDiscard();
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!!