Best JavaScript code snippet using playwright-internal
texas-holdem-spec.js
Source:texas-holdem-spec.js
...41 game.start(playerDms, 0);42 43 // Sad Patrik.44 players[3].chips = 2;45 scheduler.advanceBy(5000);46 messages.onNext({user: 4, text: "Fold"});47 scheduler.advanceBy(5000);48 messages.onNext({user: 5, text: "Fold"});49 scheduler.advanceBy(5000);50 messages.onNext({user: 1, text: "Fold"});51 scheduler.advanceBy(5000);52 messages.onNext({user: 2, text: "Fold"});53 scheduler.advanceBy(10000);54 55 messages.onNext({user: 5, text: "Fold"});56 scheduler.advanceBy(5000);57 messages.onNext({user: 1, text: "Fold"});58 scheduler.advanceBy(5000);59 messages.onNext({user: 2, text: "Fold"});60 scheduler.advanceBy(5000);61 messages.onNext({user: 3, text: "Call"});62 scheduler.advanceBy(5000);63 64 // Patrik either doubled up (2 * 2 = 4, minus the SB = 3), or lost it all.65 assert(game.potManager.outcomes.length === 2);66 assert(players[3].chips === 3 || players[3].chips === 0);67 });68 69 it('should award the pot to an all-in player if everyone else folds', function() {70 game.start(playerDms, 0);71 scheduler.advanceBy(5000);72 messages.onNext({user: 4, text: "Fold"});73 scheduler.advanceBy(5000);74 messages.onNext({user: 5, text: "Raise 200"});75 scheduler.advanceBy(5000);76 messages.onNext({user: 1, text: "Fold"});77 scheduler.advanceBy(5000);78 messages.onNext({user: 2, text: "Fold"});79 scheduler.advanceBy(5000);80 messages.onNext({user: 3, text: "Fold"});81 scheduler.advanceBy(5000);82 83 assert(players[4].chips === 203);84 });85 86 it('should handle multiple rounds with all-ins', function() {87 game.start(playerDms, 0);88 89 players[0].chips = 200;90 players[1].chips = 149;91 players[2].chips = 98;92 players[3].chips = 75;93 players[4].chips = 50;94 scheduler.advanceBy(5000);95 96 messages.onNext({user: 4, text: "Fold"});97 scheduler.advanceBy(5000);98 messages.onNext({user: 5, text: "Call"});99 scheduler.advanceBy(5000);100 messages.onNext({user: 1, text: "Call"});101 scheduler.advanceBy(5000);102 messages.onNext({user: 2, text: "Call"});103 scheduler.advanceBy(5000);104 messages.onNext({user: 3, text: "Raise 8"});105 scheduler.advanceBy(5000);106 messages.onNext({user: 5, text: "Raise 50"});107 scheduler.advanceBy(5000);108 assert(game.potManager.getTotalChips() === 62);109 110 messages.onNext({user: 1, text: "Call"});111 scheduler.advanceBy(5000);112 messages.onNext({user: 2, text: "Call"});113 scheduler.advanceBy(5000);114 messages.onNext({user: 3, text: "Call"});115 scheduler.advanceBy(5000);116 assert(game.potManager.getTotalChips() === 200);117 assert(game.potManager.pots.length === 2);118 119 messages.onNext({user: 2, text: "Bet 60"});120 scheduler.advanceBy(5000);121 assert(game.potManager.pots[0].amount === 200);122 assert(game.potManager.pots[1].amount === 60);123 124 // Stu only has 50 chips left, so this is an all-in.125 messages.onNext({user: 3, text: "Call"});126 scheduler.advanceBy(5000);127 assert(game.potManager.pots[0].amount === 200);128 assert(game.potManager.pots[1].amount === 110);129 // 60 - 50 = 10 * 2 callers = 20 chips on the side.130 messages.onNext({user: 1, text: "Call"});131 scheduler.advanceBy(5000);132 assert(game.potManager.pots.length === 3);133 assert(game.potManager.pots[0].amount === 200);134 assert(game.potManager.pots[1].amount === 150);135 assert(game.potManager.pots[2].amount === 20);136 137 messages.onNext({user: 2, text: "Check"});138 scheduler.advanceBy(5000);139 messages.onNext({user: 1, text: "Bet 10"});140 scheduler.advanceBy(5000);141 messages.onNext({user: 2, text: "Call"});142 scheduler.advanceBy(5000);143 assert(game.potManager.pots.length === 3);144 assert(game.potManager.pots[0].amount === 200);145 assert(game.potManager.pots[1].amount === 150);146 assert(game.potManager.pots[2].amount === 40);147 148 messages.onNext({user: 2, text: "Bet 30"});149 scheduler.advanceBy(5000);150 assert(game.potManager.pots.length === 3);151 assert(game.potManager.pots[0].amount === 200);152 assert(game.potManager.pots[1].amount === 150);153 assert(game.potManager.pots[2].amount === 70);154 155 assert(players[0].chips === 80);156 assert(players[1].chips === 0);157 assert(players[2].chips === 0);158 assert(players[3].chips === 75);159 assert(players[4].chips === 0);160 161 messages.onNext({user: 1, text: "Call"});162 scheduler.advanceBy(5000);163 164 var chipTotalAfter = _.reduce(players, function(total, player) { 165 return total + player.chips; 166 }, 0);167 168 assert(game.isRunning);169 assert(game.potManager.pots.length === 1);170 assert(game.potManager.pots[0].amount === 3);171 assert(chipTotalAfter === 572);172 });173 174 it('should handle multiple side pots and all-ins over the top (scenario 1)', function() {175 game.start(playerDms, 0);176 177 // Lots of short stacks this time around.178 players[0].chips = 200;179 players[1].chips = 149;180 players[2].chips = 98;181 players[3].chips = 75;182 players[4].chips = 50;183 scheduler.advanceBy(5000);184 185 var chipTotalBefore = _.reduce(players, function(total, player) { 186 return total + player.chips; 187 }, 0);188 messages.onNext({user: 4, text: "Call"});189 scheduler.advanceBy(5000);190 messages.onNext({user: 5, text: "Raise 50"});191 scheduler.advanceBy(5000);192 assert(players[4].chips === 0);193 assert(game.potManager.pots[0].amount === 55);194 195 messages.onNext({user: 1, text: "Call"});196 scheduler.advanceBy(5000);197 messages.onNext({user: 2, text: "Call"});198 scheduler.advanceBy(5000);199 messages.onNext({user: 3, text: "Call"});200 scheduler.advanceBy(5000);201 assert(game.potManager.pots[0].amount === 202);202 203 // Over the top all-in.204 messages.onNext({user: 4, text: "Raise 75"});205 scheduler.advanceBy(5000);206 assert(players[3].chips === 0);207 assert(game.potManager.pots[0].amount === 275);208 209 messages.onNext({user: 1, text: "Call"});210 scheduler.advanceBy(5000);211 messages.onNext({user: 2, text: "Call"});212 scheduler.advanceBy(5000);213 messages.onNext({user: 3, text: "Raise 100"});214 scheduler.advanceBy(5000);215 assert(players[2].chips === 0);216 assert(game.potManager.pots[0].amount === 375);217 218 messages.onNext({user: 1, text: "Call"});219 scheduler.advanceBy(5000);220 messages.onNext({user: 2, text: "Call"});221 scheduler.advanceBy(5000);222 223 assert(game.potManager.pots.length === 4);224 assert(game.potManager.pots[0].amount === 250);225 assert(game.potManager.pots[1].amount === 100);226 assert(game.potManager.pots[2].amount === 75);227 assert(game.potManager.pots[3].amount === 0);228 229 messages.onNext({user: 2, text: "Bet 50"});230 scheduler.advanceBy(5000);231 messages.onNext({user: 1, text: "Call"});232 scheduler.advanceBy(5000);233 var chipTotalAfter = _.reduce(players, function(total, player) {234 return total + player.chips;235 }, 0);236 237 // If the game has ended, blinds won't be posted, causing the chip total to238 // differ slightly.239 assert(!game.isRunning || chipTotalBefore === chipTotalAfter);240 });241 242 it('should handle multiple side pots and all-ins over the top (scenario 2)', function() {243 game.start(playerDms, 0);244 245 players[1].chips = 149;246 players[2].chips = 73;247 players[3].chips = 75;248 players[4].chips = 50;249 scheduler.advanceBy(5000);250 messages.onNext({user: 4, text: "Call"});251 scheduler.advanceBy(5000);252 messages.onNext({user: 5, text: "Raise 50"});253 scheduler.advanceBy(5000);254 assert(players[4].chips === 0);255 assert(game.potManager.pots[0].amount === 55);256 257 messages.onNext({user: 1, text: "Call"});258 scheduler.advanceBy(5000);259 messages.onNext({user: 2, text: "Call"});260 scheduler.advanceBy(5000);261 messages.onNext({user: 3, text: "Call"});262 scheduler.advanceBy(5000);263 assert(game.potManager.pots[0].amount === 202);264 messages.onNext({user: 4, text: "Raise 75"});265 scheduler.advanceBy(5000);266 assert(players[3].chips === 0);267 assert(game.potManager.pots[0].amount === 275);268 269 messages.onNext({user: 1, text: "Call"});270 scheduler.advanceBy(5000);271 messages.onNext({user: 2, text: "Call"});272 scheduler.advanceBy(5000);273 messages.onNext({user: 3, text: "Call"});274 scheduler.advanceBy(5000);275 276 assert(players[2].chips === 0);277 assert(game.potManager.pots.length === 3);278 assert(game.potManager.pots[0].amount === 250);279 assert(game.potManager.pots[1].amount === 100);280 assert(game.potManager.pots[2].amount === 0);281 });282 283 it("should divide pots based on a player's stake", function() {284 game.start(playerDms, 0);285 // Give Chip a small stack for this test.286 players[4].chips = 50;287 scheduler.advanceBy(5000);288 messages.onNext({user: 4, text: "Fold"});289 scheduler.advanceBy(5000);290 messages.onNext({user: 5, text: "Raise 50"});291 scheduler.advanceBy(5000);292 assert(players[4].isAllIn);293 messages.onNext({user: 1, text: "Fold"});294 scheduler.advanceBy(5000);295 messages.onNext({user: 2, text: "Call"});296 scheduler.advanceBy(5000);297 assert(players[1].chips === 150);298 assert(game.potManager.pots[0].amount === 102);299 messages.onNext({user: 3, text: "Call"});300 scheduler.advanceBy(5000);301 assert(game.potManager.pots[0].amount === 150);302 // Get a side pot going.303 assert(game.actingPlayer.name === 'Doyle Brunson');304 messages.onNext({user: 2, text: "Bet 10"});305 scheduler.advanceBy(5000);306 messages.onNext({user: 3, text: "Call"});307 scheduler.advanceBy(5000);308 assert(game.potManager.pots[0].amount === 150);309 assert(game.potManager.pots[1].amount === 20);310 messages.onNext({user: 2, text: "Bet 20"});311 scheduler.advanceBy(5000);312 messages.onNext({user: 3, text: "Call"});313 scheduler.advanceBy(5000);314 assert(game.potManager.pots[1].amount === 60);315 // Override the game board and player hands to guarantee Chip wins.316 game.board = [317 new Card('A', 'Hearts'),318 new Card('K', 'Hearts'),319 new Card('Q', 'Hearts'),320 new Card('J', 'Hearts'),321 new Card('2', 'Hearts'),322 ];323 game.playerHands[5] = [324 new Card('T', 'Hearts'),325 new Card('9', 'Hearts')326 ];327 game.playerHands[2] = [328 new Card('2', 'Clubs'),329 new Card('3', 'Clubs')330 ];331 game.playerHands[3] = [332 new Card('4', 'Clubs'),333 new Card('5', 'Clubs')334 ];335 messages.onNext({user: 2, text: "Check"});336 scheduler.advanceBy(5000);337 messages.onNext({user: 3, text: "Bet 20"});338 scheduler.advanceBy(5000);339 messages.onNext({user: 2, text: "Raise 80"});340 scheduler.advanceBy(5000);341 messages.onNext({user: 3, text: "Call"});342 scheduler.advanceBy(5000);343 // Chip triples up his initial stack of 50.344 var lastResult = game.potManager.outcomes.pop();345 assert(lastResult.length === 2);346 assert(!lastResult[0].isSplitPot);347 assert(lastResult[0].winners[0].name === 'Chip Reese');348 assert(lastResult[0].winners[0].chips === 150);349 350 // Doyle and Stu split the remainder (Stu would be 150, but posted SB).351 assert(lastResult[1].isSplitPot);352 assert(lastResult[1].winners.length === 2);353 assert(lastResult[1].winners[0].name === 'Doyle Brunson');354 assert(lastResult[1].winners[1].name === 'Stu Ungar');355 assert(players[1].chips === 150);356 assert(players[2].chips === 149);357 358 game.quit();359 });360 it('should end the game when all players have been eliminated', function() {361 game.start(playerDms, 0);362 scheduler.advanceBy(5000);363 messages.onNext({user: 4, text: "Raise 200"});364 scheduler.advanceBy(5000);365 messages.onNext({user: 5, text: "Call"});366 scheduler.advanceBy(5000);367 messages.onNext({user: 1, text: "Call"});368 scheduler.advanceBy(5000);369 messages.onNext({user: 2, text: "Call"});370 scheduler.advanceBy(5000);371 messages.onNext({user: 3, text: "Call"});372 scheduler.advanceBy(5000);373 // If the game is still running, the last hand was a tie.374 var lastResult = game.potManager.outcomes.pop();375 assert(!game.isRunning || (lastResult && lastResult.isSplitPot));376 });377 it('should handle default bets and raises', function() {378 game.start(playerDms, 0);379 scheduler.advanceBy(5000);380 messages.onNext({user: 4, text: "raise"});381 scheduler.advanceBy(5000);382 assert(game.potManager.currentBet === 4);383 assert(game.potManager.getTotalChips() === 7);384 messages.onNext({user: 5, text: "raise"});385 scheduler.advanceBy(5000);386 assert(game.potManager.currentBet === 8);387 assert(game.potManager.getTotalChips() === 15);388 messages.onNext({user: 1, text: "raise"});389 scheduler.advanceBy(5000);390 assert(game.potManager.currentBet === 16);391 assert(game.potManager.getTotalChips() === 31);392 messages.onNext({user: 2, text: "fold"});393 scheduler.advanceBy(5000);394 messages.onNext({user: 3, text: "fold"});395 scheduler.advanceBy(5000);396 messages.onNext({user: 4, text: "call"});397 scheduler.advanceBy(5000);398 assert(game.potManager.currentBet === 16);399 assert(game.potManager.getTotalChips() === 43);400 messages.onNext({user: 5, text: "call"});401 scheduler.advanceBy(5000);402 assert(game.potManager.currentBet === 0);403 assert(game.potManager.getTotalChips() === 51);404 messages.onNext({user: 4, text: "bet"});405 scheduler.advanceBy(5000);406 assert(game.potManager.currentBet === 1);407 assert(game.potManager.getTotalChips() === 52);408 game.quit();409 });410 it('should handle all-ins correctly', function() {411 game.start(playerDms, 0);412 scheduler.advanceBy(5000);413 messages.onNext({user: 4, text: "call"});414 scheduler.advanceBy(5000);415 messages.onNext({user: 5, text: "fold"});416 scheduler.advanceBy(5000);417 messages.onNext({user: 1, text: "raise 20"});418 scheduler.advanceBy(5000);419 messages.onNext({user: 2, text: "Fold"});420 scheduler.advanceBy(5000);421 messages.onNext({user: 3, text: "Fold"});422 scheduler.advanceBy(5000);423 messages.onNext({user: 4, text: "Raise 200"});424 scheduler.advanceBy(5000);425 assert(game.potManager.currentBet === 200);426 assert(game.potManager.getTotalChips() === 223);427 assert(players[3].chips === 0);428 assert(players[3].isAllIn);429 messages.onNext({user: 1, text: "Call"});430 scheduler.advanceBy(5000);431 var lastResult = game.potManager.outcomes.pop();432 var winner = lastResult.winners[0];433 assert(winner.id === 1 || winner.id === 4);434 // Check that the losing player was eliminated, or that the pot was split.435 assert(game.board.length === 0);436 assert(game.getPlayersInHand().length === 4 || lastResult.isSplitPot);437 game.quit();438 });439 it('should handle split pots correctly', function() {440 game.start(playerDms, 0);441 scheduler.advanceBy(5000);442 messages.onNext({user: 4, text: "Call"});443 scheduler.advanceBy(5000);444 messages.onNext({user: 5, text: "Fold"});445 scheduler.advanceBy(5000);446 messages.onNext({user: 1, text: "Fold"});447 scheduler.advanceBy(5000);448 messages.onNext({user: 2, text: "Fold"});449 scheduler.advanceBy(5000);450 messages.onNext({user: 3, text: "Check"});451 scheduler.advanceBy(5000);452 messages.onNext({user: 3, text: "Check"});453 scheduler.advanceBy(5000);454 messages.onNext({user: 4, text: "Check"});455 scheduler.advanceBy(5000);456 messages.onNext({user: 3, text: "Check"});457 scheduler.advanceBy(5000);458 messages.onNext({user: 4, text: "Check"});459 scheduler.advanceBy(5000);460 // Override the game board and player hands to guarantee a split pot.461 game.board = [462 new Card('A', 'Hearts'),463 new Card('8', 'Spades'),464 new Card('8', 'Diamonds'),465 new Card('8', 'Clubs'),466 new Card('8', 'Hearts'),467 ];468 game.playerHands[3] = [469 new Card('2', 'Clubs'),470 new Card('3', 'Hearts')471 ];472 game.playerHands[4] = [473 new Card('2', 'Diamonds'),474 new Card('3', 'Spades')475 ];476 messages.onNext({user: 3, text: "Check"});477 scheduler.advanceBy(5000);478 messages.onNext({user: 4, text: "Check"});479 scheduler.advanceBy(5000);480 var lastResult = game.potManager.outcomes.pop();481 assert(lastResult.isSplitPot);482 assert(lastResult.winners.length === 2);483 assert(lastResult.handName === 'four of a kind');484 game.quit();485 });486 it('should assign a winner if everyone folds', function() {487 game.start(playerDms, 0);488 scheduler.advanceBy(5000);489 messages.onNext({user: 4, text: "Fold"});490 scheduler.advanceBy(5000);491 messages.onNext({user: 5, text: "Fold"});492 scheduler.advanceBy(5000);493 messages.onNext({user: 1, text: "Fold"});494 scheduler.advanceBy(5000);495 messages.onNext({user: 2, text: "Fold"});496 scheduler.advanceBy(5000);497 var lastResult = game.potManager.outcomes.pop();498 assert(lastResult.winners[0].name === 'Stu Ungar');499 assert(!lastResult.isSplitPot);500 game.quit();501 });502 it('should handle consecutive raises correctly', function() {503 game.start(playerDms, 0);504 scheduler.advanceBy(5000);505 // A flurry of raises, starting with Patrik.506 messages.onNext({user: 4, text: "Raise"});507 scheduler.advanceBy(5000);508 messages.onNext({user: 5, text: "raise"});509 scheduler.advanceBy(5000);510 messages.onNext({user: 1, text: "Raise"});511 scheduler.advanceBy(5000);512 messages.onNext({user: 2, text: "Fold"});513 scheduler.advanceBy(5000);514 messages.onNext({user: 3, text: "fold"});515 scheduler.advanceBy(5000);516 var playersInHand = game.getPlayersInHand();517 assert(playersInHand.length === 3);518 assert(game.actingPlayer.name === 'Patrik Antonius');519 messages.onNext({user: 4, text: "Call"});520 scheduler.advanceBy(5000);521 messages.onNext({user: 5, text: "Fold"});522 scheduler.advanceBy(5000);523 // Patrik and Phil are still left in the hand.524 playersInHand = game.getPlayersInHand();525 assert(playersInHand.length === 2);526 assert(game.actingPlayer.name === 'Patrik Antonius');527 game.quit();528 });529 it('should handle player timeout by folding, or if possible, checking', function() {530 game.start(playerDms, 0);531 scheduler.advanceBy(5000);532 // Patrik is UTG and is going to timeout.533 assert(game.actingPlayer.name === 'Patrik Antonius');534 scheduler.advanceBy(30000);535 // Bye bye Patrik.536 var playersInHand = game.getPlayersInHand();537 assert(playersInHand.length === 4);538 assert(game.actingPlayer.name === 'Chip Reese');539 // Everyone else calls.540 messages.onNext({user: 5, text: "Call"});541 scheduler.advanceBy(5000);542 messages.onNext({user: 1, text: "call"});543 scheduler.advanceBy(5000);544 messages.onNext({user: 2, text: "call"});545 scheduler.advanceBy(5000);546 // Option to Stu, who also times out.547 assert(game.actingPlayer.name === 'Stu Ungar');548 assert(game.board.length === 0);549 scheduler.advanceBy(30000);550 // But we kindly checked for him since he's in the BB.551 playersInHand = game.getPlayersInHand();552 assert(playersInHand.length === 4);553 assert(game.actingPlayer.name === 'Doyle Brunson');554 game.quit();555 });556 it('should handle a complex hand correctly', function() {557 // Start with Phil Ivey (index 0) as dealer.558 game.start(playerDms, 0);559 scheduler.advanceBy(5000);560 // Doyle is SB, Stu is BB, Patrik is UTG.561 assert(game.actingPlayer.name === 'Patrik Antonius');562 // Call all the way down to Stu.563 messages.onNext({user: 4, text: "Call"});564 scheduler.advanceBy(5000);565 messages.onNext({user: 5, text: "Call"});566 scheduler.advanceBy(5000);567 messages.onNext({user: 1, text: "call"});568 scheduler.advanceBy(5000);569 messages.onNext({user: 2, text: "Call"});570 scheduler.advanceBy(5000);571 // Stu has the option, and raises.572 assert(game.potManager.getTotalChips() === 10);573 assert(game.actingPlayer.name === 'Stu Ungar');574 messages.onNext({user: 3, text: "Raise"});575 scheduler.advanceBy(5000);576 // Everyone folds except Doyle.577 messages.onNext({user: 4, text: "fold"});578 scheduler.advanceBy(5000);579 messages.onNext({user: 5, text: "Fold"});580 scheduler.advanceBy(5000);581 messages.onNext({user: 1, text: "fold"});582 scheduler.advanceBy(5000);583 messages.onNext({user: 2, text: "Call"});584 scheduler.advanceBy(5000);585 var playersInHand = game.getPlayersInHand();586 assert(playersInHand.length === 2);587 assert(playersInHand[0].name === 'Doyle Brunson');588 assert(playersInHand[1].name === 'Stu Ungar');589 assert(game.board.length === 3);590 assert(game.actingPlayer.name === 'Doyle Brunson');591 // Stu tries a continuation bet, which Doyle calls.592 messages.onNext({user: 2, text: "check"});593 scheduler.advanceBy(5000);594 messages.onNext({user: 3, text: "Bet"});595 scheduler.advanceBy(5000);596 messages.onNext({user: 2, text: "call"});597 scheduler.advanceBy(5000);598 assert(game.actingPlayer.name === 'Doyle Brunson');599 assert(game.board.length === 4);600 // Stu fires another round, but Doyle check-raises him.601 messages.onNext({user: 2, text: "Check"});602 scheduler.advanceBy(5000);603 messages.onNext({user: 3, text: "Bet"});604 scheduler.advanceBy(5000);605 messages.onNext({user: 2, text: "Raise"});606 scheduler.advanceBy(5000);607 assert(game.actingPlayer.name === 'Stu Ungar');608 assert(game.board.length === 4);609 // Stu reluctantly calls.610 messages.onNext({user: 3, text: "Call"});611 scheduler.advanceBy(5000);612 assert(game.actingPlayer.name === 'Doyle Brunson');613 assert(game.board.length === 5);614 // Now Doyle leads on the river and Stu calls him down.615 messages.onNext({user: 2, text: "Bet"});616 scheduler.advanceBy(5000);617 messages.onNext({user: 3, text: "Call"});618 scheduler.advanceBy(5000);619 // Check that one of the last two players won, although the result is620 // random. Also assert that the hand was ended and the dealer button moved.621 var lastResult = game.potManager.outcomes.pop();622 var winner = lastResult.winners[0];623 assert(winner.id === 2 || winner.id === 3);624 assert(game.board.length === 0);625 assert(game.dealerButton === 1);626 game.quit();627 });...
avalon-spec.js
Source:avalon-spec.js
...60 messages.onNext({user: userId, channel: playerDms[userId].id, text: text});61 }62 function untilLastQuest() {63 game.start(playerDms, 0);64 scheduler.advanceBy(5000);65 assert(lastMessage.match(/MERLIN/));66 assert(lastMessage.match(/PERCIVAL/));67 assert(lastMessage.match(/MORGANA/));68 messages.onNext({user: game.players[0].id, text: 'send player_1, player_2, player_3'});69 scheduler.advanceBy(5000);70 assert(lastMessage.match(/to the first quest/));71 assert(lastMessage.match(/player_1/));72 assert(lastMessage.match(/player_2/));73 assert(lastMessage.match(/player_3/));74 dm(1, 'approve');75 dm(2, 'approve');76 dm(3, 'approve');77 dm(4, 'reject');78 dm(5, 'reject');79 dm(6, 'reject');80 dm(7, 'reject');81 dm(8, 'approve');82 assert(lastMessage.match(/first quest/));83 assert(lastMessage.match(/going was rejected/));84 scheduler.advanceBy(5000);85 messages.onNext({user: game.players[1].id, text: 'send player_1, player_2, player_3'});86 scheduler.advanceBy(5000);87 assert(lastMessage.match(/to the first quest/));88 assert(lastMessage.match(/player_1/));89 assert(lastMessage.match(/player_2/));90 assert(lastMessage.match(/player_3/));91 dm(1, 'approve');92 dm(2, 'approve');93 dm(3, 'approve');94 dm(4, 'reject');95 dm(5, 'approve');96 dm(6, 'reject');97 dm(7, 'reject');98 dm(8, 'approve');99 assert(lastMessage.match(/first quest/));100 assert(lastMessage.match(/going was approved/));101 scheduler.advanceBy(5000);102 dm(1, 'succeed');103 dm(2, 'succeed');104 dm(3, 'succeed');105 assert(lastMessage.match(/succeeded the first quest/));106 scheduler.advanceBy(5000);107 messages.onNext({user: game.players[2].id, text: 'send player_1, player_2, player_3, player_4'});108 scheduler.advanceBy(5000);109 assert(lastMessage.match(/to the second quest/));110 assert(lastMessage.match(/player_1/));111 assert(lastMessage.match(/player_2/));112 assert(lastMessage.match(/player_3/));113 assert(lastMessage.match(/player_4/));114 dm(1, 'approve');115 dm(2, 'approve');116 dm(3, 'approve');117 dm(4, 'approve');118 dm(5, 'reject');119 dm(6, 'reject');120 dm(7, 'reject');121 dm(8, 'approve');122 assert(lastMessage.match(/second quest/));123 assert(lastMessage.match(/going was approved/));124 scheduler.advanceBy(5000);125 dm(1, 'succeed');126 dm(2, 'succeed');127 dm(3, 'succeed');128 dm(4, 'fail');129 assert(lastMessage.match(/failed the second quest/));130 scheduler.advanceBy(5000);131 messages.onNext({user: game.players[3].id, text: 'send player_1, player_2, player_3, player_4'});132 scheduler.advanceBy(5000);133 assert(lastMessage.match(/to the third quest/));134 assert(lastMessage.match(/player_1/));135 assert(lastMessage.match(/player_2/));136 assert(lastMessage.match(/player_3/));137 assert(lastMessage.match(/player_4/));138 dm(1, 'approve');139 dm(2, 'approve');140 dm(3, 'approve');141 dm(4, 'approve');142 dm(5, 'reject');143 dm(6, 'reject');144 dm(7, 'reject');145 dm(8, 'approve');146 assert(lastMessage.match(/third quest/));147 assert(lastMessage.match(/going was approved/));148 scheduler.advanceBy(5000);149 dm(1, 'succeed');150 dm(2, 'succeed');151 dm(3, 'succeed');152 dm(4, 'fail');153 assert(lastMessage.match(/failed the third quest/));154 scheduler.advanceBy(5000);155 messages.onNext({user: game.players[4].id, text: 'send player_1, player_2, player_3, player_4, player_5'});156 scheduler.advanceBy(5000);157 assert(lastMessage.match(/to the fourth quest/));158 assert(lastMessage.match(/player_1/));159 assert(lastMessage.match(/player_2/));160 assert(lastMessage.match(/player_3/));161 assert(lastMessage.match(/player_4/));162 assert(lastMessage.match(/player_5/));163 dm(1, 'approve');164 dm(2, 'approve');165 dm(3, 'approve');166 dm(4, 'approve');167 dm(5, 'reject');168 dm(6, 'reject');169 dm(7, 'reject');170 dm(8, 'approve');171 assert(lastMessage.match(/fourth quest/));172 assert(lastMessage.match(/going was approved/));173 scheduler.advanceBy(5000);174 dm(1, 'succeed');175 dm(2, 'succeed');176 dm(3, 'succeed');177 dm(4, 'fail');178 dm(5, 'succeed');179 assert(lastMessage.match(/succeeded the fourth quest with 1 fail/));180 scheduler.advanceBy(5000);181 messages.onNext({user: game.players[5].id, text: 'send player_1, player_2, player_3, player_4, player_5'});182 scheduler.advanceBy(5000);183 assert(lastMessage.match(/to the last quest/));184 assert(lastMessage.match(/player_1/));185 assert(lastMessage.match(/player_2/));186 assert(lastMessage.match(/player_3/));187 assert(lastMessage.match(/player_4/));188 assert(lastMessage.match(/player_5/));189 dm(1, 'approve');190 dm(2, 'approve');191 dm(3, 'approve');192 dm(4, 'approve');193 dm(5, 'reject');194 dm(6, 'reject');195 dm(7, 'reject');196 dm(8, 'approve');197 assert(lastMessage.match(/last quest/));198 assert(lastMessage.match(/going was approved/));199 scheduler.advanceBy(5000);200 }201 it('good win', function() {202 untilLastQuest();203 dm(1, 'succeed');204 dm(2, 'succeed');205 dm(3, 'succeed');206 dm(4, 'succeed');207 dm(5, 'succeed');208 assert(lastMessage.match(/Victory is near/));209 scheduler.advanceBy(5000);210 var assassin = game.players.filter(player => player.role == 'assassin');211 assert(assassin.length > 0);212 assassin = assassin[0];213 assert(lastMessage.match(assassin.name));214 var merlin = game.players.filter(player => player.role == 'merlin');215 assert(merlin.length > 0);216 messages.onNext({user: assassin.id, text: 'kill '+assassin.name });217 assert(lastMessage.match(/You cannot kill yourself/));218 var goodPerson = game.players.filter(player => player.role == 'good')[0];219 messages.onNext({user: assassin.id, text: 'kill '+goodPerson.name });220 assert(lastMessage.match(/Loyal Servants of Arthur win!/));221 });222 it('assassin win', function() {223 untilLastQuest();224 dm(1, 'succeed');225 dm(2, 'succeed');226 dm(3, 'succeed');227 dm(4, 'succeed');228 dm(5, 'succeed');229 assert(lastMessage.match(/Victory is near/));230 scheduler.advanceBy(5000);231 var assassin = game.players.filter(player => player.role == 'assassin');232 assert(assassin.length > 0);233 assassin = assassin[0];234 assert(lastMessage.match(assassin.name));235 var merlin = game.players.filter(player => player.role == 'merlin');236 assert(merlin.length > 0);237 merlin = merlin[0];238 messages.onNext({user: assassin.id, text: 'kill '+merlin.name });239 assert(lastMessage.match(/Minions of Mordred win!/));240 });241 it('evil win', function() {242 untilLastQuest();243 dm(1, 'succeed');244 dm(2, 'succeed');245 dm(3, 'fail');246 dm(4, 'succeed');247 dm(5, 'fail');248 assert(lastMessage.match(/Minions of Mordred win by failing 3 quests!/));249 });250 it('rejection win', function() {251 game.start(playerDms, 0);252 scheduler.advanceBy(5000);253 assert(lastMessage.match(/MERLIN/));254 assert(lastMessage.match(/PERCIVAL/));255 assert(lastMessage.match(/MORGANA/));256 messages.onNext({user: game.players[0].id, text: 'send player_1, player_2, player_3'});257 scheduler.advanceBy(5000);258 assert(lastMessage.match(/to the first quest/));259 assert(lastMessage.match(/player_1/));260 assert(lastMessage.match(/player_2/));261 assert(lastMessage.match(/player_3/));262 dm(1, 'approve');263 dm(2, 'approve');264 dm(3, 'approve');265 dm(4, 'reject');266 dm(5, 'reject');267 dm(6, 'reject');268 dm(7, 'reject');269 dm(8, 'approve');270 assert(lastMessage.match(/first quest/));271 assert(lastMessage.match(/going was rejected/));272 scheduler.advanceBy(5000);273 messages.onNext({user: game.players[1].id, text: 'send player_4, player_5, player_6'});274 scheduler.advanceBy(5000);275 assert(lastMessage.match(/to the first quest/));276 assert(lastMessage.match(/player_4/));277 assert(lastMessage.match(/player_5/));278 assert(lastMessage.match(/player_6/));279 dm(1, 'reject');280 dm(2, 'approve');281 dm(3, 'approve');282 dm(4, 'reject');283 dm(5, 'approve');284 dm(6, 'reject');285 dm(7, 'reject');286 dm(8, 'approve');287 assert(lastMessage.match(/first quest/));288 assert(lastMessage.match(/going was rejected/));289 scheduler.advanceBy(5000);290 messages.onNext({user: game.players[2].id, text: 'send player_7, player_8, player_1'});291 scheduler.advanceBy(5000);292 assert(lastMessage.match(/to the first quest/));293 assert(lastMessage.match(/player_7/));294 assert(lastMessage.match(/player_8/));295 assert(lastMessage.match(/player_1/));296 dm(1, 'reject');297 dm(2, 'approve');298 dm(3, 'approve');299 dm(4, 'reject');300 dm(5, 'approve');301 dm(6, 'reject');302 dm(7, 'reject');303 dm(8, 'approve');304 assert(lastMessage.match(/first quest/));305 assert(lastMessage.match(/going was rejected/));306 scheduler.advanceBy(5000);307 messages.onNext({user: game.players[3].id, text: 'send player_1, player_5, player_7'});308 scheduler.advanceBy(5000);309 assert(lastMessage.match(/to the first quest/));310 assert(lastMessage.match(/player_1/));311 assert(lastMessage.match(/player_5/));312 assert(lastMessage.match(/player_7/));313 dm(1, 'reject');314 dm(2, 'approve');315 dm(3, 'approve');316 dm(4, 'reject');317 dm(5, 'approve');318 dm(6, 'reject');319 dm(7, 'reject');320 dm(8, 'approve');321 assert(lastMessage.match(/first quest/));322 assert(lastMessage.match(/going was rejected/));323 scheduler.advanceBy(5000);324 messages.onNext({user: game.players[4].id, text: 'send player_1, player_2, player_7'});325 scheduler.advanceBy(5000);326 assert(lastMessage.match(/to the first quest/));327 assert(lastMessage.match(/player_1/));328 assert(lastMessage.match(/player_2/));329 assert(lastMessage.match(/player_7/));330 dm(1, 'reject');331 dm(2, 'reject');332 dm(3, 'reject');333 dm(4, 'reject');334 dm(5, 'reject');335 dm(6, 'reject');336 dm(7, 'reject');337 dm(8, 'reject');338 assert(lastMessage.match(/Minions of Mordred win due to the first quest rejected 5 times/));339 });...
parse.js
Source:parse.js
...100function parseTag(context) {101 const tagNameRE = /^<\/?([a-z][^\t\r\n\f />]*)/i;102 const match = tagNameRE.exec(context.source);103 const tag = match[1];104 advanceBy(context, match[0].length);105 advanceSpaces(context);106 let props = parseAttributes(context);107 let isSelfClosing = startsWith(context.source, '/>');108 advanceBy(context, isSelfClosing ? 2 : 1);109 const tagType = isComponent(tag, context)110 ? ElementTypes.COMPONENT111 : ElementTypes.ELEMENT;112 return {113 type: NodeTypes.ELEMENT,114 tag,115 tagType,116 props,117 isSelfClosing,118 children: [],119 codegenNode: undefined120 };121}122// è¿æ¥æ¶é¿è¿æ ·123// class="a" v-bind:b="c">parse {{ element }}</div>124function parseAttributes(context) {125 const props = [];126 const attributeNames = new Set();127 // éå° ">" æè
"/>" æè
context.source 为空å符串äºæåæ¢è§£æ128 while (129 context.source.length > 0 &&130 !startsWith(context.source, '>') &&131 !startsWith(context.source, '/>')132 ) {133 const attr = parseAttribute(context, attributeNames);134 props.push(attr);135 advanceSpaces(context);136 }137 return props;138}139// è¿æ¥æ¶é¿è¿æ ·140// class="a" v-bind:b="c">parse {{ element }}</div>141function parseAttribute(context, nameSet) {142 const attrNameRE = /^[^\t\r\n\f />][^\t\r\n\f />=]*/;143 const match = attrNameRE.exec(context.source);144 const name = match[0];145 nameSet.add(name);146 advanceBy(context, name.length);147 let value;148 if (/^[\t\r\n\f ]*=/.test(context.source)) {149 advanceSpaces(context);150 advanceBy(context, 1);151 advanceSpaces(context);152 value = parseAttributeValue(context);153 }154 if (/^(v-[A-Za-z0-9-]|:|\.|@|#)/.test(name)) {155 const match =156 /(?:^v-([a-z0-9-]+))?(?:(?::|^\.|^@|^#)(\[[^\]]+\]|[^\.]+))?(.+)?$/i.exec(157 name158 );159 let isPropShorthand = startsWith(name, '.');160 let dirName =161 match[1] ||162 (isPropShorthand || startsWith(name, ':')163 ? 'bind'164 : startsWith(name, '@')165 ? 'on'166 : '');167 let arg;168 if (match[2]) {169 let content = match[2];170 let isStatic = true;171 if (content.startsWith('[')) {172 isStatic = false;173 if (content.endsWith(']')) {174 content = content.slice(1, content.length - 1);175 }176 }177 arg = {178 type: NodeTypes.SIMPLE_EXPRESSION,179 content,180 isStatic181 };182 }183 const modifiers = match[3] ? match[3].slice(1).split('.') : [];184 if (isPropShorthand) modifiers.push('prop');185 return {186 type: NodeTypes.DIRECTIVE,187 name: dirName,188 exp: value && {189 type: NodeTypes.SIMPLE_EXPRESSION,190 content: value,191 isStatic: false192 },193 arg,194 modifiers195 };196 }197 return {198 type: NodeTypes.ATTRIBUTE,199 name,200 value: value && {201 type: NodeTypes.TEXT,202 content: value203 }204 };205}206// è¿æ¥æ¶æ¯è¿æ ·ç207// "a" v-bind:b="c">parse {{ element }}</div>208function parseAttributeValue(context) {209 const quote = context.source[0];210 advanceBy(context, 1);211 const endIndex = context.source.indexOf(quote);212 const content = parseTextData(context, endIndex);213 advanceBy(context, 1);214 return content;215}216// è¿æ¥æ¶æ¯è¿æ ·ç217// {{ element }}</div>218function parseInterpolation(context) {219 const [open, close] = context.options.delimiters;220 advanceBy(context, open.length);221 const closeIndex = context.source.indexOf(close);222 const content = parseTextData(context, closeIndex).trim();223 advanceBy(context, close.length);224 return {225 type: NodeTypes.INTERPOLATION,226 content: {227 type: NodeTypes.SIMPLE_EXPRESSION,228 content229 }230 };231}232// è¿æ¥æ¶æ¯è¿æ ·ç233// parse {{ element }}</div>234function parseText(context) {235 const endTokens = ['<', context.options.delimiters[0]];236 let endIndex = context.source.length;237 for (let i = 0; i < endTokens.length; i++) {238 const index = context.source.indexOf(endTokens[i], 1);239 // æ¾æé åçä¸ä¸ªç»ææ è¯240 if (index !== -1 && endIndex > index) {241 endIndex = index;242 }243 }244 const content = parseTextData(context, endIndex);245 return {246 type: NodeTypes.TEXT,247 content248 };249}250// utils251function startsWith(source, searchString) {252 return source.startsWith(searchString);253}254function advanceBy(context, numberOfCharacters) {255 const { source } = context;256 context.source = source.slice(numberOfCharacters);257}258function advanceSpaces(context) {259 const spaceRE = /^[\t\r\n\f ]+/;260 const match = spaceRE.exec(context.source);261 if (match) {262 advanceBy(context, match[0].length);263 }264}265function startsWithEndTagOpen(source, tag) {266 return (267 startsWith(source, '</') &&268 source.slice(2, 2 + tag.length).toLowerCase() === tag.toLowerCase() &&269 /[\t\r\n\f />]/.test(source[2 + tag.length] || '>')270 );271}272function isEnd(context) {273 const s = context.source;274 return !s || startsWith(s, '</');275}276function isComponent(tag, context) {277 const { isHTMLTag } = context.options;278 return !isHTMLTag(tag);279}280function parseTextData(context, length) {281 const rawText = context.source.slice(0, length);282 advanceBy(context, length);283 return rawText;...
logger.test.js
Source:logger.test.js
...85});86describe("when addEvent is called", () => {87 test("it should add event in events array", () => {88 expect(req.logger.log.events).toHaveLength(0);89 advanceBy(100);90 req.logger.addEvent("MOCK EVENT 1");91 advanceBy(200);92 req.logger.addEvent("MOCK EVENT 2");93 req.logger.addEvent(null);94 expect(req.logger.log.events).toHaveLength(2);95 expect(req.logger.log.events[0]).toBe("MOCK EVENT 1 : 100ms");96 expect(req.logger.log.events[1]).toBe("MOCK EVENT 2 : 200ms");97 });98});99describe("when addElement is called", () => {100 test("it should add element in log object", () => {101 req.logger.addElement("MOCK_KEY", "MOCK_VALUE");102 expect(req.logger.log).toEqual({103 ...initialLogObj,104 MOCK_KEY: "MOCK_VALUE"105 });106 });107 describe("when key is null", () => {108 test("it should not add element in log object", () => {109 req.logger.addElement(null, "MOCK_VALUE");110 expect(req.logger.log).toEqual(initialLogObj);111 });112 });113 describe("when value is null", () => {114 test("it should not add element in log object", () => {115 req.logger.addElement("MOCK_KEY", null);116 expect(req.logger.log).toEqual(initialLogObj);117 });118 });119});120describe("when addSubEvent is called", () => {121 test("it should add subEvent as first array element", () => {122 advanceBy(200);123 req.logger.addSubEvent("MOCK_SUB_EVENT_1");124 advanceBy(100);125 req.logger.addSubEvent("MOCK_SUB_EVENT_2");126 advanceBy(100);127 req.logger.addEvent("MOCK_EVENT_2");128 expect(req.logger.log).toEqual({129 ...initialLogObj,130 events: [131 ["MOCK_SUB_EVENT_1 : 200ms", "MOCK_SUB_EVENT_2 : 100ms"],132 "MOCK_EVENT_2 : 400ms"133 ]134 });135 });136 test("it should add subEvent in log object", () => {137 advanceBy(100);138 req.logger.addEvent("MOCK_EVENT_1");139 advanceBy(200);140 req.logger.addSubEvent("MOCK_SUB_EVENT_1");141 advanceBy(100);142 req.logger.addSubEvent("MOCK_SUB_EVENT_2");143 advanceBy(100);144 req.logger.addEvent("MOCK_EVENT_2");145 expect(req.logger.log).toEqual({146 ...initialLogObj,147 events: [148 "MOCK_EVENT_1 : 100ms",149 ["MOCK_SUB_EVENT_1 : 200ms", "MOCK_SUB_EVENT_2 : 100ms"],150 "MOCK_EVENT_2 : 400ms"151 ]152 });153 });154 describe("when subEvent is null", () => {155 test("it should not add subEvent in log object", () => {156 req.logger.addSubEvent(null);157 expect(req.logger.log).toEqual(initialLogObj);158 });159 });160});161describe("when done is called", () => {162 test("it should log to console", () => {163 global.console.log = jest.fn();164 advanceBy(100);165 req.logger.addEvent("MOCK_EVENT_1");166 req.logger.done();167 expect(global.console.log).toHaveBeenCalledWith({168 events: ["MOCK_EVENT_1 : 100ms"],169 request: {170 clientIP: "MOCK_IP",171 method: "MOCK_METHOD",172 path: "MOCK_PATH",173 query: "MOCK_QUERY"174 },175 started: "2/1/2021, 12:00:00 AM",176 time: "100ms"177 });178 });...
index.js
Source:index.js
...35 advanceTo(1000);36 expect(Date.now()).toBe(1000);37 advanceTo();38 expect(Date.now()).toBe(0);39 advanceBy(520);40 expect(Date.now()).toBe(520);41 });42 test('performance.now', () => {43 const performance = global.window ?44 global.window.performance : require('perf_hooks').performance;45 advanceTo(1000);46 expect(performance.now()).toBe(1000);47 advanceTo();48 expect(performance.now()).toBe(0);49 advanceBy(520);50 expect(performance.now()).toBe(520);51 });52 test('Date.current', () => {53 advanceTo();54 expect(Date.now()).toBe(0);55 expect(Date.current() - new Date('2018-05-20') > 0).toBe(true);56 });57 test('advanceTo', () => {58 advanceTo(1000);59 expect(+new Date()).toBe(1000);60 advanceTo();61 expect(+new Date()).toBe(0);62 });63 test('advanceBy', () => {64 advanceBy(3000); // advanceBy time 3 seconds65 const now = new Date();66 advanceBy(4000);67 expect(+new Date() - now).toBe(4000);68 advanceBy(-4000); // advanceBy time -4 seconds69 expect(+new Date() - now).toBe(0);70 advanceBy();71 expect(+new Date() - now).toBe(0);72 });73 test('clear', () => {74 clear();75 expect(Date.now() - new Date('2018-05-20') > 0).toBe(true);76 advanceTo();77 expect(+new Date()).toBe(0);78 });79 test('usage', () => {80 advanceTo(new Date(2018, 5, 27, 0, 0, 0)); // advanceTo to date time.81 const now = Date.now();82 // 0 timezone + timezone offset83 expect(now).toBe(1530057600000 + new Date().getTimezoneOffset() * 60000);84 advanceBy(3000); // advanceBy time 3 seconds85 expect(+new Date() - now).toBe(3000);86 advanceBy(-1000); // advanceBy time -1 second87 expect(+new Date() - now).toBe(2000);88 });89 test('version', () => {90 expect(version).toBe(pkg.version);91 });...
CardTimerEvent.test.js
Source:CardTimerEvent.test.js
...22 advanceTo(new Date(1530075600000)); // 2018, 5, 27, 0, 0, 023 onFocusAccordion(0);24 expect(result).toEqual(expectation[0]);25 // 3000ms spent on First Accordion and focused to Second26 advanceBy(3000);27 onFocusAccordion(1);28 expect(result).toEqual(expectation[1]);29 // 3000ms spent on Second Accordion30 advanceBy(3000);31 onFocusAccordion(1);32 expect(result).toEqual(expectation[1]);33 // 6000ms spent on Second Accordion and focused to Third34 advanceBy(3000);35 onFocusAccordion(2);36 expect(result).toEqual(expectation[2]);37 // 3000ms spent on Third Accordion and focused to First & 3000ms spent on First38 advanceBy(3000);39 onFocusAccordion(0);40 expect(result).toEqual(expectation[3]);41 init();42 clear();43 });44 test('getResult method should get the final result so far ', () => {45 advanceTo(new Date(1530075600000)); // 2018, 5, 27, 0, 0, 046 onFocusAccordion(0);47 expect(result).toEqual(expectation[0]);48 // 3000ms spent on First Accordion and focused to Second49 advanceBy(3000);50 onFocusAccordion(1);51 expect(result).toEqual(expectation[1]);52 // 3000ms spent on Second Accordion53 advanceBy(3000);54 onFocusAccordion(1);55 expect(result).toEqual(expectation[1]);56 // 6000ms spent on Second Accordion and focused to Third57 advanceBy(3000);58 onFocusAccordion(2);59 expect(result).toEqual(expectation[2]);60 // 3000ms spent on Third Accordion and focused to First & 3000ms spent on First61 advanceBy(3000);62 onFocusAccordion(0);63 expect(result).toEqual(expectation[3]);64 // another 3000ms spent on First Accordion but didn't focus to other accordion65 advanceBy(3000);66 getResult();67 expect(result).toEqual(expectation[4]);68 init();69 expect(result).toEqual(expectation[5]);70 advanceBy(3000);71 onFocusAccordion(3);72 expect(result).toEqual(expectation[6]);73 clear();74 });...
input.js
Source:input.js
...30const keyboard = {31 ...eventable,32 events: { ...pressable('keydown', 'keyup', 'code') },33 actions: {34 KeyW: () => advanceBy([0,0,-30]),35 KeyS: () => advanceBy([0,0,30]),36 KeyA: () => advanceBy([30,0,0]),37 KeyD: () => advanceBy([-30,0,0]),38 ShiftLeft: () => advanceBy([0,-30,0]),39 ShiftRight: () => advanceBy([0,-30,0]),40 Space: () => advanceBy([0,30,0]),41 },42 run() {43 for (let pressed in this.events._pressed) {44 this.events._pressed[pressed] && this.actions[pressed] && this.actions[pressed]();45 }46 window.requestAnimationFrame(() => this.run());47 },48};...
TimeSpentEvent.test.js
Source:TimeSpentEvent.test.js
...10 test('onFocusSection & onBlurSection method should store time spent on each section', () => {11 const expectation = { '0': '0.20', '1': '0.20', '2': '0.30' };12 advanceTo(0);13 onFocusSection(0, Date.now());14 advanceBy(100);15 onBlurSection(0, Date.now());16 onFocusSection(1, Date.now());17 advanceBy(200);18 onBlurSection(1, Date.now());19 onFocusSection(0, Date.now());20 advanceBy(100);21 onBlurSection(0, Date.now());22 onFocusSection(2, Date.now());23 advanceBy(300);24 getResult();25 expect(result).toEqual(expectation);26 });...
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 await page.click('text=Docs');7 await page.waitForLoadState();8 await page.click('text=Core concepts');9 await page.waitForLoadState();10 await page.click('text=Locator Strategies');11 await page.waitForLoadState();12 await page.click('text=Selector Engine');13 await page.waitForLoadState();14 await page.click('text=Selectors');15 await page.waitForLoadState();16 await page.click('text=Text');17 await page.waitForLoadState();18 await page.click('text=Attributes');19 await page.waitForLoadState();20 await page.click('text=Classes');21 await page.waitForLoadState();22 await page.click('text=Data Attributes');23 await page.waitForLoadState();24 await page.click('text=HTML Attributes');25 await page.waitForLoadState();26 await page.click('text=
Using AI Code Generation
1const { test, expect } = require('@playwright/test');2test('test', async ({ page }) => {3 await page.click('text="Get Started"');4 await page.waitForLoadState('networkidle');5 await page.waitForSelector('text="API"');6 await page.click('text="API"');7 await page.waitForLoadState('networkidle');8 await page.waitForSelector('text="Page"');9 await page.click('text="Page"');10 await page.waitForLoadState('networkidle');11 await page.waitForSelector('text="Page.goto"');12 await page.click('text="Page.goto"');13 await page.waitForLoadState('networkidle');14 await page.waitForSelector('text="Page.goto"');15 await page.click('text="Page.goto"');16 await page.waitForLoadState('networkidle');17 await page.waitForSelector('text="Page.goto"');18 await page.click('text="Page.goto"');19 await page.waitForLoadState('networkidle');20 await page.waitForSelector('text="Page.goto"');21 await page.click('text="Page.goto"');22 await page.waitForLoadState('networkidle');23 await page.waitForSelector('text="Page.goto"');24 await page.click('text="Page.goto"');25 await page.waitForLoadState('networkidle');26 await page.waitForSelector('text="Page.goto"');27 await page.click('text="Page.goto"');28 await page.waitForLoadState('networkidle');29 await page.waitForSelector('text="Page.goto"');30 await page.click('text="Page.goto"');
Using AI Code Generation
1const { test, expect } = require('@playwright/test');2const { advanceBy } = require('@playwright/test/lib/server/progress');3test('example', async ({ page }) => {4 advanceBy(10);5 expect(await page.screenshot()).toMatchSnapshot('example.png');6});7const { test, expect } = require('@playwright/test');8const { advanceBy } = require('@playwright/test/lib/server/progress');9test('example', async ({ page }) => {10 advanceBy(10);11 expect(await page.screenshot()).toMatchSnapshot('example.png');12});
Using AI Code Generation
1const { test, expect } = require('@playwright/test');2test('test', async ({ page }) => {3 await page.waitForSelector('text=Learn');4 await page.click('text=Learn');5 await page.waitForSelector('text=Get started');6 await page.click('text=Get started');
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext({5 recordVideo: { dir: 'videos/' }6 });7 const page = await context.newPage();8 await page.screenshot({ path: `example.png` });9 await browser.close();10})();11const { chromium } = require('playwright');12(async () => {13 const browser = await chromium.launch();14 const context = await browser.newContext({15 recordVideo: { dir: 'videos/' }16 });17 const page = await context.newPage();18 await page.screenshot({ path: `example.png` });19 await browser.close();20})();21const { chromium } = require('playwright');22(async () => {23 const browser = await chromium.launch();24 const context = await browser.newContext({25 recordVideo: { dir: 'videos/' }26 });27 const page = await context.newPage();28 await page.screenshot({ path: `example.png` });29 await browser.close();30})();31const { chromium } = require('playwright');32(async () => {33 const browser = await chromium.launch();34 const context = await browser.newContext({35 recordVideo: { dir: 'videos/' }36 });37 const page = await context.newPage();38 await page.screenshot({ path: `example.png` });39 await browser.close();40})();41const { chromium } = require('playwright');42(async () => {43 const browser = await chromium.launch();44 const context = await browser.newContext({
Using AI Code Generation
1await page.evaluate(() => {2 const now = Date.now();3 jest.advanceTimersByTime(60 * 1000);4 expect(Date.now()).toBe(now + 60 * 1000);5});6await page.evaluate(() => {7 const now = Date.now();8 jest.advanceTimersByTime(60 * 1000);9 expect(Date.now()).toBe(now + 60 * 1000);10});11await page.evaluate(() => {12 const now = Date.now();13 jest.advanceTimersByTime(60 * 1000);14 expect(Date.now()).toBe(now + 60 * 1000);15});16await page.evaluate(() => {17 const now = Date.now();18 jest.advanceTimersByTime(60 * 1000);19 expect(Date.now()).toBe(now + 60 * 1000);20});21await page.evaluate(() => {22 const now = Date.now();23 jest.advanceTimersByTime(60 * 1000);24 expect(Date.now()).toBe(now + 60 * 1000);25});26await page.evaluate(() => {27 const now = Date.now();28 jest.advanceTimersByTime(60 * 1000);29 expect(Date.now()).toBe(now + 60 * 1000);30});31await page.evaluate(() => {32 const now = Date.now();33 jest.advanceTimersByTime(60 * 1000);34 expect(Date.now()).toBe(now + 60 * 1000);35});36await page.evaluate(() => {37 const now = Date.now();38 jest.advanceTimersByTime(60 * 100
Using AI Code Generation
1await page.advanceTime(3600000);2await page.advanceTime(new Date(2021, 8, 27, 1, 0, 0));3await page.clearCookies();4await page.clearPermissions();5await page.close();6await page.emulate({7 viewport: {8 },9});10await page.emulateMedia({11});12await page.exposeBinding('myBinding', (source, ...args) => {13 console.log('myBinding called');14});15await page.exposeFunction('myFunction', (source, ...args) => {16 console.log('myFunction called');17});18await page.exposeFunction('myFunction', (source, ...args) => {19 console.log('myFunction called');20});21await page.exposeFunction('myFunction', (source, ...args) => {22 console.log('myFunction called');23});24await page.exposeFunction('myFunction', (source, ...args) => {25 console.log('myFunction called');26});
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!