Best JavaScript code snippet using ng-mocks
02_BftToken.test.js
Source:02_BftToken.test.js
1require('babel-register');2require('babel-polyfill');3import EVMRevert from "../zeppelin/test/helpers/EVMRevert";4import {accounts} from './common/common';5import ether from '../zeppelin/test/helpers/ether';6import {increaseTimeTo} from "../zeppelin/test/helpers/increaseTime";7const BigNumber = web3.BigNumber;8let chai = require('chai');9let assert = chai.assert;10const should = require('chai')11 .use(require('chai-as-promised'))12 .use(require('chai-bignumber')(BigNumber))13 .should();14const BftToken = artifacts.require("../contracts/BftToken.sol");15const MintableToken = artifacts.require("../zeppelin/contracts/token/MintableToken.sol");16const Crowdsale = artifacts.require("../zeppelin/contracts/crowdsale/Crowdsale.sol");17contract('02_BftToken.sol', function(rpc_accounts) {18 let ac = accounts(rpc_accounts);19 console.log(JSON.stringify(ac, null, 2));20 const NULL_ADDRESS = '0x0000000000000000000000000000000000000000';21 const ETH_PRICE = 752.0;22 const TOKEN_PRICE = 0.1;23 const MINT_RATE = ETH_PRICE / TOKEN_PRICE;24 const TOKEN_CAP = ether(1000000000);25 const TOKEN_DEC = 18;26 let startTime = 1518818400; //Friday, 16th February 2018, 10:00pm UTC27 let endTime = 1520373600; //Tuesday, 6th of March 2018, 10:00pm UTC28 let crowdsale = null;29 let myToken = null;30 let newToken = null;31 it('should be able to deploy the BftCrowdsale contract so we can call it from the token sc', async () => {32 crowdsale = await Crowdsale.new(33 startTime,34 endTime,35 MINT_RATE,36 ac.crowdsaleWallet,37 {from: ac.admin, gas: 6000000, gasPrice: 5 * Math.pow(10, 9)}38 ).should.be.fulfilled;39 console.log("crowdsale.address= " +crowdsale.address);40 })41 it('should be able to deploy the BftToken contract and set initial state', async () => {42 myToken = await BftToken.new(43 TOKEN_CAP,44 TOKEN_DEC,45 crowdsale.address,46 {from: ac.admin, gas: 6000000}47 );48 console.log("myToken.address= " +myToken.address);49 newToken = await MintableToken.new({from: ac.admin, gas: 7000000});50 console.log("newToken.address= " +newToken.address);51 let canStartTransfers = await myToken.hasCrowdsaleFinished();52 assert.isFalse(canStartTransfers, "should not be able to start transfers now");53 })54 it('should be able mint some tokens to my buyers', async () => {55 await myToken.mint(ac.buyer1, ether(1), {from: ac.admin}).should.be.fulfilled;56 let b1 = await myToken.balanceOf(ac.buyer1);57 b1.should.be.bignumber.equal(ether(1));58 await myToken.mint(ac.buyer2, ether(2), {from: ac.admin}).should.be.fulfilled;59 let b2 = await myToken.balanceOf(ac.buyer2);60 b2.should.be.bignumber.equal(ether(2));61 await myToken.mint(ac.buyer3, ether(3), {from: ac.admin}).should.be.fulfilled;62 let b3 = await myToken.balanceOf(ac.buyer3);63 b3.should.be.bignumber.equal(ether(3));64 await myToken.mint(ac.buyer4, ether(4), {from: ac.admin}).should.be.fulfilled;65 let b4 = await myToken.balanceOf(ac.buyer4);66 b4.should.be.bignumber.equal(ether(4));67 await myToken.mint(ac.buyer5, ether(5), {from: ac.admin}).should.be.fulfilled;68 let b5 = await myToken.balanceOf(ac.buyer5);69 b5.should.be.bignumber.equal(ether(5));70 })71 it('should not allow ERC20 interface use before end of crowdsale', async() => {72 await myToken.transfer(ac.buyer5, ether(0.1),{from: ac.buyer4}).should.be.rejectedWith(EVMRevert);73 await myToken.approve(ac.intruder2, ether(0.1),{from: ac.buyer4}).should.be.rejectedWith(EVMRevert);74 await myToken.increaseApproval(ac.intruder2, ether(0.1),{from: ac.buyer4}).should.be.rejectedWith(EVMRevert);75 await myToken.decreaseApproval(ac.intruder2, ether(0.1),{from: ac.buyer4}).should.be.rejectedWith(EVMRevert);76 await myToken.transferFrom(ac.buyer4, ac.buyer5, ether(0.1),{from: ac.buyer4}).should.be.rejectedWith(EVMRevert);77 })78 it('should allow ERC20 interface use after end of crowdsale', async() => {79 await increaseTimeTo(endTime+1);80 assert.isTrue(await crowdsale.hasEnded());81 await myToken.transfer(ac.buyer1, ether(0.1),{from: ac.buyer4}).should.be.fulfilled;82 await myToken.approve(ac.buyer5, ether(0.1),{from: ac.buyer4}).should.be.fulfilled;83 await myToken.increaseApproval(ac.buyer5, ether(0.1),{from: ac.buyer4}).should.be.fulfilled;84 await myToken.decreaseApproval(ac.buyer5, ether(0.1),{from: ac.buyer4}).should.be.fulfilled;85 await myToken.transferFrom(ac.buyer4, ac.buyer1, ether(0.1),{from: ac.buyer5}).should.be.fulfilled;86 })87 it('should not allow public access to the burn function', async() => {88 await myToken.burn(ether(0.5), {from: ac.buyer1}).should.be.rejectedWith(EVMRevert);89 })90 it('should not allow a token holder to redeem before token is paused', async() => {91 await myToken.redeem({from: ac.buyer1}).should.be.rejectedWith(EVMRevert);92 })93 it('should not allow pausing from a non-owner', async() => {94 await myToken.pause({from: ac.intruder1}).should.be.rejectedWith(EVMRevert);95 })96 it('should not allow a token holder to redeem before having an upgrade token', async() => {97 await myToken.redeem({from: ac.buyer1}).should.be.rejectedWith(EVMRevert);98 })99 it('should not allow ERC20 interface use when paused', async() => {100 await myToken.pause({from: ac.admin}).should.be.fulfilled;101 assert.isTrue(await myToken.paused());102 await myToken.transfer(ac.buyer5, ether(0.1),{from: ac.buyer4}).should.be.rejectedWith(EVMRevert);103 await myToken.approve(ac.intruder2, ether(0.1),{from: ac.buyer4}).should.be.rejectedWith(EVMRevert);104 await myToken.increaseApproval(ac.intruder2, ether(0.1),{from: ac.buyer4}).should.be.rejectedWith(EVMRevert);105 await myToken.decreaseApproval(ac.intruder2, ether(0.1),{from: ac.buyer4}).should.be.rejectedWith(EVMRevert);106 await myToken.transferFrom(ac.buyer4, ac.buyer5, ether(0.1),{from: ac.buyer4}).should.be.rejectedWith(EVMRevert);107 await myToken.unpause({from: ac.admin}).should.be.fulfilled;108 assert.isFalse(await myToken.paused());109 })110 it('should allow upgrading even if not paused', async() => {111 assert.equal(await myToken.newToken(), NULL_ADDRESS);112 await myToken.upgrade(newToken.address, {from: ac.admin}).should.be.fulfilled;113 assert.equal(await myToken.newToken(), newToken.address);114 await newToken.transferOwnership(myToken.address, {from: ac.admin});115 })116 it('should allow a token holder to redeem after we have an upgrade token', async() => {117 let b0 = await myToken.balanceOf(ac.buyer1);118 await myToken.redeem({from: ac.buyer1}).should.be.fulfilled;119 let b1 = await myToken.balanceOf(ac.buyer1);120 b1.should.be.bignumber.equal(ether(0));121 let b2 = await newToken.balanceOf(ac.buyer1);122 b2.should.be.bignumber.equal(b0);123 })124 it('should allow a non-token holder to redeem ', async() => {125 await myToken.redeem({from: ac.intruder1}).should.be.fulfilled;126 })127 it('should allow a token holders to redeem twice - allow redeem with 0 balance', async() => {128 await myToken.redeem({from: ac.buyer1}).should.be.fulfilled;129 })130 it('should allow all token holders to redeem ', async() => {131 let totalSupply = await myToken.totalSupply();132 totalSupply.should.be.bignumber.equal(ether(15-1.2)); // 1.2 redeemed before by buyer1133 await myToken.redeem({from: ac.buyer2}).should.be.fulfilled;134 await myToken.redeem({from: ac.buyer3}).should.be.fulfilled;135 await myToken.redeem({from: ac.buyer4}).should.be.fulfilled;136 await myToken.redeem({from: ac.buyer5}).should.be.fulfilled;137 totalSupply = await myToken.totalSupply();138 totalSupply.should.be.bignumber.equal(ether(0));139 let newTotalSupply = await newToken.totalSupply();140 newTotalSupply.should.be.bignumber.equal(ether(15));141 })142 it('should allow owner to change symbol and name of token contract', async() => {143 await myToken.changeSymbol("TTT", {from: ac.admin});144 await myToken.changeName("TTT Token", {from: ac.admin});145 let symbol = await myToken.symbol();146 let name = await myToken.name();147 assert.equal(symbol, "TTT");148 assert.equal(name, "TTT Token");149 })...
API.js
Source:API.js
1const axios = require('axios');2const URL_PREFIX = 'http://localhost:3002';3// const URL_PREFIX = 'https://api-kinetikapp.herokuapp.com';4const API = {5 login: function (userData) {6 return axios.post(`${URL_PREFIX}/login`, userData)7 },8 signup: async function (userData) {9 const newUser = await axios.post(`${URL_PREFIX}/signup`, userData);10 // console.log(newUser);11 window.location.href = '/dashboard';12 return newUser;13 },14 getDashboard: function (token) {15 // console.log(token);16 return axios.get(`${URL_PREFIX}/dashboard`, {17 headers: {18 authorization: `Bearer ${token}`19 }20 })21 },22 getIncompleteGoals: function (token) {23 return axios.get(`${URL_PREFIX}/incomplete-goals`, {24 headers: {25 authorization: `Bearer ${token}`26 }27 })28 },29 getCompleteGoals: function (token) {30 return axios.get(`${URL_PREFIX}/complete-goals`, {31 headers: {32 authorization: `Bearer ${token}`33 }34 })35 },36 // working on this37 getUser: function (id) {38 return axios.get(`${URL_PREFIX}/api/users/${id}`)39 },40 getAllGoals: function () {41 return axios.get(`${URL_PREFIX}/api/goals`);42 },43 getAllGroups: function () {44 return axios.get(`${URL_PREFIX}/api/groups`)45 },46 getOneGoal: function (id) {47 return axios.get(`${URL_PREFIX}/api/goals/${id}`);48 },49 getOneGroup: function (id) {50 return axios.get(`${URL_PREFIX}/api/groups/${id}`);51 },52 deleteGoal: function (id, token) {53 return axios.delete(`${URL_PREFIX}/api/goals/${id}`, {54 headers: {55 authorization: `Bearer ${token}`56 }57 });58 },59 createGroup: function (newGroupData, token) {60 return axios.post(`${URL_PREFIX}/api/groups`, newGroupData, {61 headers: {62 authorization: `Bearer ${token}`63 }64 })65 },66 createGoal: function (newGoalData, mytoken) {67 return axios.post(`${URL_PREFIX}/api/goals/`, newGoalData, {68 headers: {69 authorization: `Bearer ${mytoken}`70 }71 });72 },73 editGoal: function (id, editGoalData, mytoken) {74 return axios.put(`${URL_PREFIX}/api/goals/${id}`, editGoalData, {75 headers: {76 authorization: `Bearer ${mytoken}`77 }78 })79 },80 editCheer: function (id, editGoalData, mytoken) {81 return axios.put(`${URL_PREFIX}/api/goals/${id}/cheer`, editGoalData, {82 headers: {83 authorization: `Bearer ${mytoken}`84 }85 })86 },87 uploadProfilePic: function (profilePicData, mytoken) {88 return axios.post(`${URL_PREFIX}/api/profile-pics/`, profilePicData, {89 headers: {90 authorization: `Bearer ${mytoken}`91 }92 }, {93 onUploadProgress: progressEvent => console.log('Upload Progress: ' + Math.round((progressEvent.loaded / progressEvent.total) * 100))94 });95 },96 deleteProfilePic: function (id, token) {97 return axios.delete(`${URL_PREFIX}/api/profile-pics/${id}`, {98 headers: {99 authorization: `Bearer ${token}`100 }101 });102 },103 completeGoal: async function (completedDate) {104 const goalComp = await axios.post(`${URL_PREFIX}/api/completed`, completedDate)105 console.log("goalComp", goalComp)106 },107 getAllDates: function () {108 axios.get(`${URL_PREFIX}/api/completed`)109 },110 inviteUser: function (inviteObj, mytoken) {111 return axios.post(`${URL_PREFIX}/invite`, inviteObj, {112 headers: {113 authorization: `Bearer ${mytoken}`114 }115 })116 },117 addUserToGroup: function (id, emptyObj, mytoken) {118 return axios.put(`${URL_PREFIX}/api/groups/${id}`, emptyObj, {119 headers: {120 authorization: `Bearer ${mytoken}`121 }122 })123 },124 leaveGroup: function (goal_id, emptyObj, mytoken) {125 return axios.put(`${URL_PREFIX}/api/groups/removeuser/${goal_id}`, emptyObj, {126 headers: {127 authorization: `Bearer ${mytoken}`128 }129 })130 },131 deleteGroup: function (goal_id, mytoken) {132 return axios.delete(`${URL_PREFIX}/api/groups/${goal_id}`, {133 headers: {134 authorization: `Bearer ${mytoken}`135 }136 })137 },138 addGoalToGroup: function (id, newGoalObj, mytoken) {139 return axios.put(`${URL_PREFIX}/api/groups/addgoal/${id}`, newGoalObj, {140 headers: {141 authorization: `Bearer ${mytoken}`142 }143 })144 },145 146 removeGoalFromGroup: function (id, newGoalObj, mytoken) {147 return axios.put(`${URL_PREFIX}/api/groups/removegoal/${id}`, newGoalObj, {148 headers: {149 authorization: `Bearer ${mytoken}`150 }151 })152 },153 createComment: function (newCommentData, token) {154 return axios.post(`${URL_PREFIX}/api/comments`, newCommentData, {155 headers: {156 authorization: `Bearer ${token}`157 }158 })159 },160 161 deleteComment: function (id, token) {162 return axios.delete(`${URL_PREFIX}/api/comments/${id}`, {163 headers: {164 authorization: `Bearer ${token}`165 }166 })167 },168}...
test.js
Source:test.js
1const {expect } = require('chai');2const{ ethers } = require('hardhat');3describe('MyToken', function(){4 let mytoken, owner, bob, jane, sara;5 const ZeroAddress = '0x0000000000000000000000000000000000000000';6 7 const id1 = 123;8 const id2 = 124;9 const uri1 = 'http://nibbstack.com/1';10 const uri2 = 'http://nibbstack.com/2';11 const uri3 = 'http://nibbstack.com/3';12 beforeEach(async () => {13 const contract = await ethers.getContractFactory('MyToken');14 mytoken = await contract.deploy();15 [owner, bob, jane, sara] = await ethers.getSigners();16 await mytoken.deployed();17 });18 it('returns the correct contract name', async function(){19 expect(await mytoken.name()).to.equal('MyToken');20 });21 it('returns the correct contract symbol', async function(){22 expect(await mytoken.symbol()).to.equal('MTK');23 });24 it('correctly mints a NFT', async function(){25 expect(await mytoken.connect(owner).safeMint(bob.address)).emit(mytoken, 'Transfer');26 expect(await mytoken.balanceOf(bob.address)).to.equal(1);27 });28 it('returns correct balanceOf', async function(){29 await mytoken.connect(owner).safeMint(bob.address);30 expect(await mytoken.balanceOf(bob.address)).to.equal(1);31 await mytoken.connect(owner).safeMint(bob.address);32 expect(await mytoken.balanceOf(bob.address)).to.equal(2);33 });34 it('finds the correct owner of NFTid',async function(){35 await mytoken.connect(owner).safeMint(bob.address);36 expect(await mytoken.ownerOf(1)).to.equal(bob.address);37 });38 it ('correctly approves account',async function()39 {40 await mytoken.connect(owner).safeMint(bob.address);41 expect(await mytoken.connect(bob).approve(sara.address,1)).to.emit(mytoken, 'Approval');42 expect(await mytoken.getApproved(1)).to.equal(sara.address);43 });44 it ('correctly cancels Approval', async function(){45 await mytoken.connect(owner).safeMint(bob.address);46 await mytoken.connect(bob).approve(sara.address,1);47 await mytoken.connect(bob).approve(ZeroAddress, 1);48 expect(await mytoken.getApproved(1)).to.equal(ZeroAddress);49 });50it ('correclty sets an operator', async function(){51 await mytoken.connect(owner).safeMint(bob.address);52 expect(await mytoken.connect(bob).setApprovalForAll(sara.address,true)).to.emit(mytoken,'ApproveForAll');53 expect(await mytoken.isApprovedForAll(bob.address,sara.address)).to.equal(true);54});55it('sets and cancels an opertor', async function(){56 await mytoken.connect(owner).safeMint(bob.address);57 await mytoken.connect(bob).setApprovalForAll(sara.address,true);58 await mytoken.connect(bob).setApprovalForAll(sara.address, false);59 expect(await mytoken.isApprovedForAll(bob.address, sara.address)).to.equal(false);60});61it('correctly transfer nft from owner', async function(){62 await mytoken.connect(owner).safeMint(bob.address);63 await mytoken.connect(bob).approve(sara.address, 1);64 await mytoken.connect(sara).transferFrom(bob.address,jane.address,1);65 expect(await mytoken.balanceOf(bob.address)).to.equal(0);66 expect (await mytoken.balanceOf(jane.address)).to.equal(1);67 expect(await mytoken.ownerOf(1)).to.equal(jane.address);68});69it ('correctly transfer NFT as operator', async function(){70 71 await mytoken.connect(owner).safeMint(bob.address);72 await mytoken.connect(bob).setApprovalForAll(sara.address, true);73 await mytoken.connect(sara).transferFrom(bob.address,jane.address, 1);74 expect(await mytoken.balanceOf(bob.address)).to.equal(0);75 expect(await mytoken.balanceOf(jane.address)).to.equal(1);76 expect(await mytoken.ownerOf(1)).to.equal(jane.address);77});78it('correctly burns a NFT', async function(){79 await mytoken.connect(owner).safeMint(bob.address);80 expect(await mytoken.connect(owner).burn(1)).to.emit(mytoken, 'Transfer');81 expect(await mytoken.balanceOf(bob.address)).to.equal(0);82 await expect(mytoken.ownerOf(1)).to.be.revertedWith('003002');83});...
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!!