Best JavaScript code snippet using wpt
ERC20Wrapper.test.js
Source:ERC20Wrapper.test.js
1const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');2const { expect } = require('chai');3const { ZERO_ADDRESS, MAX_UINT256 } = constants;4const { shouldBehaveLikeERC20 } = require('../ERC20.behavior');5const NotAnERC20 = artifacts.require('CallReceiverMock');6const ERC20Mock = artifacts.require('ERC20DecimalsMock');7const ERC20WrapperMock = artifacts.require('ERC20WrapperMock');8contract('ERC20', function (accounts) {9 const [ initialHolder, recipient, anotherAccount ] = accounts;10 const name = 'My Token';11 const symbol = 'MTKN';12 const initialSupply = new BN(100);13 beforeEach(async function () {14 this.underlying = await ERC20Mock.new(name, symbol, 9);15 this.token = await ERC20WrapperMock.new(this.underlying.address, `Wrapped ${name}`, `W${symbol}`);16 await this.underlying.mint(initialHolder, initialSupply);17 });18 afterEach(async function () {19 expect(await this.underlying.balanceOf(this.token.address)).to.be.bignumber.equal(await this.token.totalSupply());20 });21 it('has a name', async function () {22 expect(await this.token.name()).to.equal(`Wrapped ${name}`);23 });24 it('has a symbol', async function () {25 expect(await this.token.symbol()).to.equal(`W${symbol}`);26 });27 it('has the same decimals as the underlying token', async function () {28 expect(await this.token.decimals()).to.be.bignumber.equal('9');29 });30 it('decimals default back to 18 if token has no metadata', async function () {31 const noDecimals = await NotAnERC20.new();32 const otherToken = await ERC20WrapperMock.new(noDecimals.address, `Wrapped ${name}`, `W${symbol}`);33 expect(await otherToken.decimals()).to.be.bignumber.equal('18');34 });35 it('has underlying', async function () {36 expect(await this.token.underlying()).to.be.bignumber.equal(this.underlying.address);37 });38 describe('deposit', function () {39 it('valid', async function () {40 await this.underlying.approve(this.token.address, initialSupply, { from: initialHolder });41 const { tx } = await this.token.depositFor(initialHolder, initialSupply, { from: initialHolder });42 await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {43 from: initialHolder,44 to: this.token.address,45 value: initialSupply,46 });47 await expectEvent.inTransaction(tx, this.token, 'Transfer', {48 from: ZERO_ADDRESS,49 to: initialHolder,50 value: initialSupply,51 });52 });53 it('missing approval', async function () {54 await expectRevert(55 this.token.depositFor(initialHolder, initialSupply, { from: initialHolder }),56 'ERC20: insufficient allowance',57 );58 });59 it('missing balance', async function () {60 await this.underlying.approve(this.token.address, MAX_UINT256, { from: initialHolder });61 await expectRevert(62 this.token.depositFor(initialHolder, MAX_UINT256, { from: initialHolder }),63 'ERC20: transfer amount exceeds balance',64 );65 });66 it('to other account', async function () {67 await this.underlying.approve(this.token.address, initialSupply, { from: initialHolder });68 const { tx } = await this.token.depositFor(anotherAccount, initialSupply, { from: initialHolder });69 await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {70 from: initialHolder,71 to: this.token.address,72 value: initialSupply,73 });74 await expectEvent.inTransaction(tx, this.token, 'Transfer', {75 from: ZERO_ADDRESS,76 to: anotherAccount,77 value: initialSupply,78 });79 });80 });81 describe('withdraw', function () {82 beforeEach(async function () {83 await this.underlying.approve(this.token.address, initialSupply, { from: initialHolder });84 await this.token.depositFor(initialHolder, initialSupply, { from: initialHolder });85 });86 it('missing balance', async function () {87 await expectRevert(88 this.token.withdrawTo(initialHolder, MAX_UINT256, { from: initialHolder }),89 'ERC20: burn amount exceeds balance',90 );91 });92 it('valid', async function () {93 const value = new BN(42);94 const { tx } = await this.token.withdrawTo(initialHolder, value, { from: initialHolder });95 await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {96 from: this.token.address,97 to: initialHolder,98 value: value,99 });100 await expectEvent.inTransaction(tx, this.token, 'Transfer', {101 from: initialHolder,102 to: ZERO_ADDRESS,103 value: value,104 });105 });106 it('entire balance', async function () {107 const { tx } = await this.token.withdrawTo(initialHolder, initialSupply, { from: initialHolder });108 await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {109 from: this.token.address,110 to: initialHolder,111 value: initialSupply,112 });113 await expectEvent.inTransaction(tx, this.token, 'Transfer', {114 from: initialHolder,115 to: ZERO_ADDRESS,116 value: initialSupply,117 });118 });119 it('to other account', async function () {120 const { tx } = await this.token.withdrawTo(anotherAccount, initialSupply, { from: initialHolder });121 await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {122 from: this.token.address,123 to: anotherAccount,124 value: initialSupply,125 });126 await expectEvent.inTransaction(tx, this.token, 'Transfer', {127 from: initialHolder,128 to: ZERO_ADDRESS,129 value: initialSupply,130 });131 });132 });133 describe('recover', function () {134 it('nothing to recover', async function () {135 await this.underlying.approve(this.token.address, initialSupply, { from: initialHolder });136 await this.token.depositFor(initialHolder, initialSupply, { from: initialHolder });137 const { tx } = await this.token.recover(anotherAccount);138 await expectEvent.inTransaction(tx, this.token, 'Transfer', {139 from: ZERO_ADDRESS,140 to: anotherAccount,141 value: '0',142 });143 });144 it('something to recover', async function () {145 await this.underlying.transfer(this.token.address, initialSupply, { from: initialHolder });146 const { tx } = await this.token.recover(anotherAccount);147 await expectEvent.inTransaction(tx, this.token, 'Transfer', {148 from: ZERO_ADDRESS,149 to: anotherAccount,150 value: initialSupply,151 });152 });153 });154 describe('erc20 behaviour', function () {155 beforeEach(async function () {156 await this.underlying.approve(this.token.address, initialSupply, { from: initialHolder });157 await this.token.depositFor(initialHolder, initialSupply, { from: initialHolder });158 });159 shouldBehaveLikeERC20('ERC20', initialSupply, initialHolder, recipient, anotherAccount);160 });...
gui.js
Source:gui.js
1import {applyMiddleware, compose, combineReducers} from 'redux';2import alertsReducer, {alertsInitialState} from './alerts';3import assetDragReducer, {assetDragInitialState} from './asset-drag';4import cardsReducer, {cardsInitialState} from './cards';5import colorPickerReducer, {colorPickerInitialState} from './color-picker';6import connectionModalReducer, {connectionModalInitialState} from './connection-modal';7import customProceduresReducer, {customProceduresInitialState} from './custom-procedures';8import blockDragReducer, {blockDragInitialState} from './block-drag';9import editorTabReducer, {editorTabInitialState} from './editor-tab';10import hoveredTargetReducer, {hoveredTargetInitialState} from './hovered-target';11import menuReducer, {menuInitialState} from './menus';12import micIndicatorReducer, {micIndicatorInitialState} from './mic-indicator';13import modalReducer, {modalsInitialState} from './modals';14import modeReducer, {modeInitialState} from './mode';15import monitorReducer, {monitorsInitialState} from './monitors';16import monitorLayoutReducer, {monitorLayoutInitialState} from './monitor-layout';17import projectChangedReducer, {projectChangedInitialState} from './project-changed';18import projectStateReducer, {projectStateInitialState} from './project-state';19import projectTitleReducer, {projectTitleInitialState} from './project-title';20import fontsLoadedReducer, {fontsLoadedInitialState} from './fonts-loaded';21import restoreDeletionReducer, {restoreDeletionInitialState} from './restore-deletion';22import stageSizeReducer, {stageSizeInitialState} from './stage-size';23import targetReducer, {targetsInitialState} from './targets';24import timeoutReducer, {timeoutInitialState} from './timeout';25import toolboxReducer, {toolboxInitialState} from './toolbox';26import vmReducer, {vmInitialState} from './vm';27import vmStatusReducer, {vmStatusInitialState} from './vm-status';28import throttle from 'redux-throttle';29import decks from '../lib/libraries/decks/index.jsx';30const guiMiddleware = compose(applyMiddleware(throttle(300, {leading: true, trailing: true})));31const guiInitialState = {32 alerts: alertsInitialState,33 assetDrag: assetDragInitialState,34 blockDrag: blockDragInitialState,35 cards: cardsInitialState,36 colorPicker: colorPickerInitialState,37 connectionModal: connectionModalInitialState,38 customProcedures: customProceduresInitialState,39 editorTab: editorTabInitialState,40 mode: modeInitialState,41 hoveredTarget: hoveredTargetInitialState,42 stageSize: stageSizeInitialState,43 menus: menuInitialState,44 micIndicator: micIndicatorInitialState,45 modals: modalsInitialState,46 monitors: monitorsInitialState,47 monitorLayout: monitorLayoutInitialState,48 projectChanged: projectChangedInitialState,49 projectState: projectStateInitialState,50 projectTitle: projectTitleInitialState,51 fontsLoaded: fontsLoadedInitialState,52 restoreDeletion: restoreDeletionInitialState,53 targets: targetsInitialState,54 timeout: timeoutInitialState,55 toolbox: toolboxInitialState,56 vm: vmInitialState,57 vmStatus: vmStatusInitialState58};59const initPlayer = function (currentState) {60 return Object.assign(61 {},62 currentState,63 {mode: {64 isFullScreen: currentState.mode.isFullScreen,65 isPlayerOnly: true,66 // When initializing in player mode, make sure to reset67 // hasEverEnteredEditorMode68 hasEverEnteredEditor: false69 }}70 );71};72const initFullScreen = function (currentState) {73 return Object.assign(74 {},75 currentState,76 {mode: {77 isFullScreen: true,78 isPlayerOnly: currentState.mode.isPlayerOnly,79 hasEverEnteredEditor: currentState.mode.hasEverEnteredEditor80 }}81 );82};83const initEmbedded = function (currentState) {84 return Object.assign(85 {},86 currentState,87 {mode: {88 showBranding: true,89 isFullScreen: true,90 isPlayerOnly: true,91 hasEverEnteredEditor: false92 }}93 );94};95const initTutorialCard = function (currentState, deckId) {96 return Object.assign(97 {},98 currentState,99 {100 cards: {101 visible: true,102 content: decks,103 activeDeckId: deckId,104 expanded: true,105 step: 0,106 x: 0,107 y: 0,108 dragging: false109 }110 }111 );112};113const initTelemetryModal = function (currentState) {114 return Object.assign(115 {},116 currentState,117 {118 modals: {119 telemetryModal: true // this key must match `MODAL_TELEMETRY` in modals.js120 }121 }122 );123};124const guiReducer = combineReducers({125 alerts: alertsReducer,126 assetDrag: assetDragReducer,127 blockDrag: blockDragReducer,128 cards: cardsReducer,129 colorPicker: colorPickerReducer,130 connectionModal: connectionModalReducer,131 customProcedures: customProceduresReducer,132 editorTab: editorTabReducer,133 mode: modeReducer,134 hoveredTarget: hoveredTargetReducer,135 stageSize: stageSizeReducer,136 menus: menuReducer,137 micIndicator: micIndicatorReducer,138 modals: modalReducer,139 monitors: monitorReducer,140 monitorLayout: monitorLayoutReducer,141 projectChanged: projectChangedReducer,142 projectState: projectStateReducer,143 projectTitle: projectTitleReducer,144 fontsLoaded: fontsLoadedReducer,145 restoreDeletion: restoreDeletionReducer,146 targets: targetReducer,147 timeout: timeoutReducer,148 toolbox: toolboxReducer,149 vm: vmReducer,150 vmStatus: vmStatusReducer151});152export {153 guiReducer as default,154 guiInitialState,155 guiMiddleware,156 initEmbedded,157 initFullScreen,158 initPlayer,159 initTelemetryModal,160 initTutorialCard...
P10.js
Source:P10.js
1function ticTac(playerMoves) {2 let messages = [];3 var initialBoard = [[false, false, false],4 [false, false, false],5 [false, false, false]]6 let counter = 0;7 let xWins = false;8 let oWins = false;9 for (const move of playerMoves) {10 if (!isWon(initialBoard)) {11 if (counter % 2 == 0) {12 if (!placeOnBoard(initialBoard, 'X', move)) {13 counter--;14 }15 if (isWon(initialBoard)) { xWins = true; break; }16 } else {17 if (!placeOnBoard(initialBoard, 'O', move)) {18 counter--;19 }20 if (isWon(initialBoard)) { oWins = true; break; }21 }22 counter++;23 }24 else { break; }25 }26 function placeOnBoard(initialBoard, player, move) {27 let x = Number(move[0]);28 let y = Number(move[2]);29 if (isSpaceTaken(initialBoard, x, y)) {30 messages.push('This place is already taken. Please choose another!');31 return false;32 }33 initialBoard[x][y] = player;34 if (isWon(initialBoard)) {35 return true;36 }37 return true;38 }39 function isSpaceTaken(initialBoard, x, y) {40 return initialBoard[x][y] == false ? false : true;41 }42 function isWon(initialBoard) {43 let isGameWon = false;44 let leftDiag = 0;45 let rightDiag = 0;46 for (let i = 0; i < initialBoard.length; i++) {47 if (initialBoard[i][i] == 'X') {48 leftDiag++;49 }50 else if(initialBoard[i][i] == 'O'){51 rightDiag++;52 }53 }54 if (leftDiag == 3) {55 isGameWon = true;56 }57 if (rightDiag == 3) {58 isGameWon = true;59 }60 if (initialBoard.every((el, i, arr) => { return el == arr[0] })) { isGameWon = true; }61 if (initialBoard.every((el, i, arr) => { return el == arr[1] })) { isGameWon = true; }62 if (initialBoard.every((el, i, arr) => { return el == arr[2] })) { isGameWon = true; }63 if (initialBoard[0][0] == 'X' && initialBoard[0][1] == 'X' && initialBoard[0][2] == 'X') { isGameWon = true }64 if (initialBoard[1][0] == 'X' && initialBoard[1][1] == 'X' && initialBoard[1][2] == 'X') { isGameWon = true }65 if (initialBoard[2][0] == 'X' && initialBoard[2][1] == 'X' && initialBoard[2][2] == 'X') { isGameWon = true }66 if (initialBoard[0][0] == 'O' && initialBoard[0][1] == 'O' && initialBoard[0][2] == 'O') { isGameWon = true }67 if (initialBoard[1][0] == 'O' && initialBoard[1][1] == 'O' && initialBoard[1][2] == 'O') { isGameWon = true }68 if (initialBoard[2][0] == 'O' && initialBoard[2][1] == 'O' && initialBoard[2][2] == 'O') { isGameWon = true }69 return isGameWon;70 }71 let result = [[], [], []]72 for (let i = 0; i < initialBoard.length; i++) {73 for (let y = 0; y < initialBoard.length; y++) {74 result[i].push(initialBoard[i][y])75 }76 }77 let tempRes = [[result[0].join('\t')],[result[1].join('\t')],[result[2].join('\t')]]78 if (isWon(initialBoard)) {79 80 return messages.join('\n') + '\n' + `Player ${xWins == true ? 'X' : 'O'} wins!` + '\n' + tempRes.join('\n');81 }82 return "The game ended! Nobody wins :(" + '\n' + tempRes.join('\n')83}84console.log(ticTac(["0 1", 85 "0 0",86 "0 2",87 "2 0",88 "1 0",89 "1 1",90 "1 2",91 "2 2",92 "2 1",93 "0 0"]));94console.log('\n');95console.log('New Game '.repeat(5))96console.log('\n');97console.log(ticTac(["0 0",98"0 0",99"1 1",100"0 1",101"1 2",102"0 2",103"2 2",104"1 2",105"2 2",106"2 1"]107));108console.log('\n');109console.log('New Game '.repeat(5))110console.log('\n');111console.log(ticTac(["0 1",112"0 0",113"0 2",114"2 0",115"1 0",116"1 2",117"1 1",118"2 1",119"2 2",120"0 0"]...
Using AI Code Generation
1var wpt = require('webpagetest');2var test = new wpt('www.webpagetest.org');3 if (err) {4 console.log(err);5 } else {6 console.log(data);7 }8});9var wpt = require('webpagetest');10var test = new wpt('www.webpagetest.org');11}, function(err, data) {12 if (err) {13 console.log(err);14 } else {15 console.log(data);16 }17});18var wpt = require('webpagetest');19var test = new wpt('www.webpagetest.org');20test.getLocations(function(err, data) {21 if (err) {22 console.log(err);23 } else {24 console.log(data);25 }26});27var wpt = require('webpagetest');28var test = new wpt('www.webpagetest.org');29test.getTesters(function(err, data) {30 if (err) {31 console.log(err);32 } else {33 console.log(data);34 }35});36var wpt = require('webpagetest');37var test = new wpt('www.webpagetest.org');38test.getTestStatus('141220_0K_1a8b2c2b6e3c6a2a6c8d3a3f6a3b6e1', function(err, data) {39 if (err) {40 console.log(err);41 } else {42 console.log(data);43 }44});45var wpt = require('webpagetest');46var test = new wpt('www.webpagetest.org');47test.getTestResults('141220_0K_1a8b2c2b6e3c6a2a6c8d3a3f6a3b6e1', function(err, data) {48 if (err) {49 console.log(err);50 } else {51 console.log(data);52 }53});
Using AI Code Generation
1var wpt = require("webpagetest");2var test = new wpt("www.webpagetest.org");3 if (err) {4 console.log(err);5 } else {6 console.log(data);7 }8});9var WebPageTest = require('webpagetest');10var wpt = new WebPageTest('www.webpagetest.org');11 if (err) {12 console.log(err);13 } else {14 console.log(data);15 }16});17var WebPageTest = require('webpagetest');18var wpt = new WebPageTest('www.webpagetest.org', 'A.12345678901234567890123456789012');19 if (err) {20 console.log(err);21 } else {22 console.log(data);23 }24});25var WebPageTest = require('webpagetest');26var wpt = new WebPageTest('www.webpagetest.org', 'A.12345678901234567890123456789012');27}, function(err, data) {28 if (err) {29 console.log(err);30 } else {31 console.log(data);32 }33});34var WebPageTest = require('webpagetest');35var wpt = new WebPageTest('www.webpagetest.org', 'A.12345678901234567890123456789012');36}, function(err, data) {37 if (err) {38 console.log(err);39 } else {40 console.log(data);41 }42});
Using AI Code Generation
1module.exports = function (grunt) {2 grunt.registerMultiTask('wpt', 'Run WebPageTest.org from grunt.', function () {3 var done = this.async();4 var wpt = require('webpagetest');5 var options = this.options({6 });7 if (!options.key || !options.url) {8 grunt.log.error('You must specify a key and a url');9 return false;10 }11 var wpt = new WebPageTest('www.webpagetest.org', options.key);12 wpt.runTest(options.url, options, function (err, data) {13 if (err) {14 grunt.log.error(err);15 return false;16 }17 grunt.log.ok('Test submitted. Polling results...');18 wpt.getTestResults(data.data.testId, function (err, data) {19 if (err) {20 grunt.log.error(err);21 return false;22 }23 grunt.log.ok('Got results.');24 grunt.log.ok(JSON.stringify(data));25 done();26 });27 });28 });29};30module.exports = function (grunt) {31 grunt.initConfig({32 pkg: grunt.file.readJSON('package.json'),33 wpt: {34 test: {35 options: {36 }37 }38 }39 });40 grunt.loadNpmTasks('grunt-wpt');41 grunt.registerTask('default', ['wpt']);42};43Running "wpt:test" (wpt) task44{"statusCode":200,"statusText":"Ok","data":{"testId":"150107_6S_5","ownerKey":"A.2f6c9f6d8d1c19c6b7f6c9f6d
Using AI Code Generation
1var wpt = require('webpagetest');2var options = {3};4var webPageTest = new wpt('www.webpagetest.org', options.key);5 if (err) return console.error(err);6 console.log('Test status:', data.statusText);7 console.log('Test ID:', data.data.testId);8 console.log('Test URL:', data.data.summary);9 console.log('View results:', data.data.userUrl);10});11var wpt = require('webpagetest');12var options = {13};14var webPageTest = new wpt(options);15 if (err) return console.error(err);16 console.log('Test status:', data.statusText);17 console.log('Test ID:', data.data.testId);18 console.log('Test URL:', data.data.summary);19 console.log('View results:', data.data.userUrl);20});21var wpt = require('webpagetest');22var options = {23};24var webPageTest = new wpt(options);25 if (err) return console.error(err);26 console.log('Test status:', data.statusText);27 console.log('Test ID:', data.data.testId);28 console.log('Test URL:', data
Using AI Code Generation
1var wpt = require('webpagetest');2var test = new wpt('A.1f7b3b6d1c7b8e6b2c7f0e6d9d7f8b6e');3if (err) return console.error(err);4console.log('Test status:', data.statusCode);5console.log('Test ID:', data.data.testId);6console.log('Test URL:', data.data.summary);7});8var wpt = require('webpagetest');9var test = new wpt('A.1f7b3b6d1c7b8e6b2c7f0e6d9d7f8b6e');10var options = {11};12if (err) return console.error(err);13console.log('Test status:', data.statusCode);14console.log('Test ID:', data.data.testId);15console.log('Test URL:', data.data.summary);16});17var wpt = require('webpagetest');18var test = new wpt('A.1f7b3b6d1c7b8e6b2c7f0e6d9d7f8b6e');19var options = {20};21if (err) return console.error(err);22console.log('Test status:', data.statusCode);23console.log('Test ID:', data.data.testId);24console.log('Test URL:', data.data.summary);25});26var wpt = require('webpagetest');27var test = new wpt('A.1f7b3b6d1c7b8e6b2c7f0e6d9d7f8b6e');28var options = {
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!!