Best JavaScript code snippet using sinon
basic_functionality.js
Source:basic_functionality.js
1const truffleAssert = require('truffle-assertions');2const { expect } = require('chai');3const timeMachine = require('ganache-time-traveler');4const bigNumber = require('bignumber.js');5const {6 constants,7 randomNumber,8 randomAddress,9 web310} = require('../../utils/test');11const YVault = artifacts.require('yVault');12const Controller = artifacts.require("Controller");13const StrategyStub = artifacts.require("YTestStrategy");14const ERC20Mock = artifacts.require('ERC20Mock');15const OneSplit = artifacts.require('OneSplitAudit');16describe('Basic test (controller)', () => {17 let erc20;18 let erc20test;19 let yVault;20 let yVault2;21 let controller;22 let strategyStub;23 let strategyStub2;24 const addr = [];25 let tokenName = "Token";26 let tokenSymbol = "TKN";27 let snapshotId;28 let newonesplit;29 let oneslpl;30 before(async() => {31 [32 addr.sender,33 addr.owner,34 addr.rewards,35 addr.governance,36 addr.arbitrary3,37 addr.otheruser,38 addr.newgovernance,39 addr.converter,40 ] = await web3.eth.getAccounts();41 controller = await Controller.new(addr.rewards, { from: addr.sender });42 erc20 = await ERC20Mock.new(tokenName, tokenSymbol, '1000000', { from: addr.sender });43 yVault = await YVault.new(erc20.address, controller.address,{ from: addr.sender });44 yVault2 = await YVault.new(erc20.address, controller.address,{ from: addr.sender });45 erc20test = await ERC20Mock.new("TestName", "TTN", '2000000', { from: addr.sender });46 strategyStub = await StrategyStub.new(erc20.address, controller.address, yVault.address, "name", { from: addr.sender });47 await controller.approveStrategy(yVault.address, strategyStub.address, { from: addr.sender });48 await controller.setStrategy(yVault.address, strategyStub.address, { from: addr.sender });49 await controller.setVault(erc20.address,yVault.address,{ from: addr.sender });50 await erc20test.transfer(strategyStub.address, '1000000',{ from: addr.sender });51 oneslpl = await OneSplit.new({ from: addr.sender });52 await oneslpl.setController(controller.address, { from: addr.sender});53 await controller.setOneSplit(oneslpl.address, { from: addr.sender});54 newonesplit = await OneSplit.new({ from: addr.sender });55 await newonesplit.setController(controller.address, { from: addr.sender});56 });57 beforeEach(async() => {58 // Create a snapshot59 const snapshot = await timeMachine.takeSnapshot();60 snapshotId = snapshot['result'];61 });62 afterEach(async() => await timeMachine.revertToSnapshot(snapshotId));63 describe('Constructor test', () => {64 it('Rewards work', async() => {65 expect((await controller.rewards()).toString()).to.equal(addr.rewards.toString());66 });67 it('Strategist work', async() => {68 expect((await controller.strategist()).toString()).to.equal(addr.sender.toString());69 });70 });71 describe('setOneSplit test', () => {72 it('setOneSplit work', async() => {73 await controller.setOneSplit(newonesplit.address, { from: addr.sender});74 expect((await controller.onesplit()).toString()).to.equal(newonesplit.address);75 });76 });77 describe('setSplit test', () => {78 it('setSplit work', async() => {79 await controller.setSplit("1000", { from: addr.sender});80 expect((await controller.split()).toString()).to.equal("1000");81 });82 });83 describe('setRewards test', () => {84 it('setRewards work', async() => {85 await controller.setRewards(addr.arbitrary3, { from: addr.sender});86 expect((await controller.rewards()).toString()).to.equal(addr.arbitrary3.toString());87 });88 });89 describe('setStrategist test', () => {90 it('setStrategist work', async() => {91 await controller.setStrategist(addr.arbitrary3, { from: addr.sender});92 expect((await controller.strategist({ from: addr.sender})).toString()).to.equal(addr.arbitrary3.toString());93 });94 it('setVault work after setStrategist', async() => {95 await controller.setStrategist(addr.arbitrary3, { from: addr.sender});96 expect((await controller.strategist()).toString()).to.equal(addr.arbitrary3.toString());97 await controller.setGovernance(addr.newgovernance, { from: addr.sender});98 expect((await controller.governance()).toString()).to.equal(addr.newgovernance.toString());99 await truffleAssert.reverts(100 controller.setVault(erc20test.address, yVault.address, { from: addr.sender}),101 'Not a governance or strategist'102 );103 await truffleAssert.reverts(104 controller.setVault(erc20test.address, yVault.address, { from: addr.governance}),105 'Not a governance or strategist'106 );107 });108 });109 describe('setGovernance test', () => {110 it('setGovernance work', async() => {111 await controller.setGovernance(addr.newgovernance, { from: addr.sender});112 expect((await controller.governance()).toString()).to.equal(addr.newgovernance.toString());113 });114 it('setRewards work', async() => {115 await controller.setGovernance(addr.newgovernance, { from: addr.sender});116 expect((await controller.governance()).toString()).to.equal(addr.newgovernance.toString());117 await truffleAssert.reverts(118 controller.setRewards(addr.arbitrary3, { from: addr.sender}),119 'Not the governance'120 );121 });122 it('setSplit work', async() => {123 await controller.setGovernance(addr.newgovernance, { from: addr.sender});124 expect((await controller.governance()).toString()).to.equal(addr.newgovernance.toString());125 await truffleAssert.reverts(126 controller.setSplit("1000", { from: addr.sender}),127 'Not the governance'128 );129 });130 it('setGovernance work', async() => {131 await controller.setGovernance(addr.newgovernance, { from: addr.sender});132 expect((await controller.governance()).toString()).to.equal(addr.newgovernance.toString());133 await truffleAssert.reverts(134 controller.setGovernance(addr.governance, { from: addr.sender}),135 'Not the governance'136 );137 });138 it('setVault work', async() => {139 await controller.setStrategist(addr.arbitrary3, { from: addr.sender});140 expect((await controller.strategist()).toString()).to.equal(addr.arbitrary3.toString());141 await controller.setGovernance(addr.newgovernance, { from: addr.sender});142 expect((await controller.governance()).toString()).to.equal(addr.newgovernance.toString());143 await truffleAssert.reverts(144 controller.setVault(erc20test.address, yVault.address, { from: addr.sender}),145 'Not a governance or strategist'146 );147 await truffleAssert.reverts(148 controller.setVault(erc20test.address, yVault.address, { from: addr.governance}),149 'Not a governance or strategist'150 );151 });152 it('approveStrategy work', async() => {153 await controller.setGovernance(addr.newgovernance, { from: addr.sender});154 expect((await controller.governance()).toString()).to.equal(addr.newgovernance.toString());155 await truffleAssert.reverts(156 controller.approveStrategy(erc20.address, strategyStub.address, { from: addr.sender }),157 'Not the governance'158 );159 });160 it('revokeStrategy work', async() => {161 await controller.setGovernance(addr.newgovernance, { from: addr.sender});162 expect((await controller.governance()).toString()).to.equal(addr.newgovernance.toString());163 await truffleAssert.reverts(164 controller.revokeStrategy(erc20.address, strategyStub.address, { from: addr.sender }),165 'Not the governance'166 );167 });168 //@vitalii under these conditions it is useless and never used - need to delete169 // it('setConverter work', async() => {170 // await controller.setStrategist(addr.arbitrary3, { from: addr.sender});171 // expect((await controller.strategist()).toString()).to.equal(addr.arbitrary3.toString());172 // await controller.setGovernance(addr.newgovernance, { from: addr.sender});173 // expect((await controller.governance()).toString()).to.equal(addr.newgovernance.toString());174 // await truffleAssert.reverts(175 // controller.setConverter(erc20.address, yVault.address, addr.arbitrary3, { from: addr.sender }),176 // '!strategist'177 // );178 // });179 it('setStrategy work', async() => {180 await controller.setStrategist(addr.arbitrary3, { from: addr.sender});181 expect((await controller.strategist()).toString()).to.equal(addr.arbitrary3.toString());182 await controller.setGovernance(addr.newgovernance, { from: addr.sender});183 expect((await controller.governance()).toString()).to.equal(addr.newgovernance.toString());184 await truffleAssert.reverts(185 controller.setStrategy(yVault.address, strategyStub.address, { from: addr.sender }),186 'Not a governance or strategist'187 );188 });189 it('withdrawAll work', async() => {190 await controller.setStrategist(addr.arbitrary3, { from: addr.sender});191 expect((await controller.strategist()).toString()).to.equal(addr.arbitrary3.toString());192 await controller.setGovernance(addr.newgovernance, { from: addr.sender});193 expect((await controller.governance()).toString()).to.equal(addr.newgovernance.toString());194 await truffleAssert.reverts(195 controller.withdrawAll(erc20.address, { from: addr.sender }),196 'Not a governance or strategist'197 );198 });199 it('inCaseTokensGetStuck work', async() => {200 await controller.setStrategist(addr.arbitrary3, { from: addr.sender});201 expect((await controller.strategist()).toString()).to.equal(addr.arbitrary3.toString());202 await controller.setGovernance(addr.newgovernance, { from: addr.sender});203 expect((await controller.governance()).toString()).to.equal(addr.newgovernance.toString());204 await truffleAssert.reverts(205 controller.inCaseTokensGetStuck(erc20.address, "1", { from: addr.sender }),206 'Not a governance or strategist'207 );208 });209 it('inCaseStrategyTokenGetStuck work', async() => {210 await controller.setStrategist(addr.arbitrary3, { from: addr.sender});211 expect((await controller.strategist()).toString()).to.equal(addr.arbitrary3.toString());212 await controller.setGovernance(addr.newgovernance, { from: addr.sender});213 expect((await controller.governance()).toString()).to.equal(addr.newgovernance.toString());214 await truffleAssert.reverts(215 controller.inCaseStrategyTokenGetStuck(strategyStub.address ,erc20.address, { from: addr.sender }),216 'Not a governance or strategist'217 );218 });219 it('yearn work', async() => {220 await controller.setStrategist(addr.arbitrary3, { from: addr.sender});221 expect((await controller.strategist()).toString()).to.equal(addr.arbitrary3.toString());222 await controller.setGovernance(addr.newgovernance, { from: addr.sender});223 expect((await controller.governance()).toString()).to.equal(addr.newgovernance.toString());224 await truffleAssert.reverts(225 controller.yearn(yVault.address, erc20test.address, 0,{ from: addr.sender}),226 'Not a governance or strategist'227 );228 });229 });230 describe('setVault test', () => {231 it('setVault work', async() => {232 await controller.setVault(erc20.address, yVault.address, { from: addr.sender})233 expect((await controller.tokens(yVault.address)).toString()).to.equal(erc20.address.toString());234 });235 it('withdraw work', async() => {236 await controller.setVault(erc20test.address, yVault.address, { from: addr.sender})237 expect((await controller.tokens(yVault.address)).toString()).to.equal(erc20test.address.toString());238 await truffleAssert.reverts(239 controller.withdraw(1, { from: addr.sender }),240 'Caller is not a vault'241 );242 });243 });244 describe('approveStrategy test', () => {245 it('approveStrategy and setStrategy work', async() => {246 strategyStub2 = await StrategyStub.new(erc20.address, controller.address, yVault.address, "name", { from: addr.sender });247 await truffleAssert.reverts(248 controller.setStrategy(yVault.address, strategyStub2.address, { from: addr.sender }),249 '!approved'250 );251 await controller.approveStrategy(yVault2.address, strategyStub2.address, { from: addr.sender });252 await controller.setStrategy(yVault2.address, strategyStub2.address, { from: addr.sender });253 expect((await controller.strategies(yVault2.address)).toString()).to.equal(strategyStub2.address.toString());254 });255 });256 describe('revokeStrategy test', () => {257 it('setStrategy, approveStrategy and revokeStrategy work', async() => {258 strategyStub2 = await StrategyStub.new(erc20.address, controller.address,yVault.address, "name", { from: addr.sender });259 await controller.approveStrategy(yVault2.address, strategyStub2.address, { from: addr.sender });260 await controller.setStrategy(yVault2.address, strategyStub2.address, { from: addr.sender });261 expect((await controller.strategies(yVault2.address)).toString()).to.equal(strategyStub2.address.toString());262 await controller.revokeStrategy(yVault2.address, strategyStub2.address, { from: addr.sender });263 await truffleAssert.reverts(264 controller.setStrategy(yVault2.address, strategyStub2.address, { from: addr.sender }),265 '!approved'266 );267 });268 });269 describe('setStrategy test', () => {270 it('setStrategy work', async() => {271 strategyStub2 = await StrategyStub.new(erc20.address, controller.address,yVault.address, "name", { from: addr.sender });272 await controller.approveStrategy(yVault2.address, strategyStub2.address, { from: addr.sender });273 await controller.setStrategy(yVault2.address, strategyStub2.address, { from: addr.sender });274 expect((await controller.strategies(yVault2.address)).toString()).to.equal(strategyStub2.address.toString());275 await controller.approveStrategy(yVault.address, strategyStub2.address, { from: addr.sender });276 await controller.setStrategy(yVault.address, strategyStub2.address, { from: addr.sender });277 });278 it('earn work', async() => {279 strategyStub2 = await StrategyStub.new(erc20.address, controller.address,yVault.address, "name", { from: addr.sender });280 await controller.approveStrategy(yVault2.address, strategyStub2.address, { from: addr.sender });281 await controller.setStrategy(yVault2.address, strategyStub2.address, { from: addr.sender });282 expect((await controller.strategies(yVault2.address)).toString()).to.equal(strategyStub2.address.toString());283 await controller.approveStrategy(yVault.address, strategyStub2.address, { from: addr.sender });284 await controller.setStrategy(yVault.address, strategyStub2.address, { from: addr.sender });285 await erc20.mint(controller.address, 1000);286 expect((await erc20.balanceOf(controller.address)).toString()).to.equal("1000");287 await controller.earn(yVault.address, erc20.address, 10,{ from: addr.sender});288 expect((await erc20.balanceOf(controller.address)).toString()).to.equal("990");289 expect((await erc20.balanceOf(strategyStub2.address)).toString()).to.equal("10");290 });291 it('balanceOf work', async() => {292 strategyStub2 = await StrategyStub.new(erc20.address, controller.address,yVault.address, "name", { from: addr.sender });293 await controller.approveStrategy(yVault2.address, strategyStub2.address, { from: addr.sender });294 await controller.setStrategy(yVault2.address, strategyStub2.address, { from: addr.sender });295 expect((await controller.strategies(yVault2.address)).toString()).to.equal(strategyStub2.address.toString());296 await controller.approveStrategy(yVault.address, strategyStub2.address, { from: addr.sender });297 await controller.setStrategy(yVault.address, strategyStub2.address, { from: addr.sender });298 await erc20.mint(controller.address, 1000);299 expect((await erc20.balanceOf(controller.address)).toString()).to.equal("1000");300 await controller.earn(yVault.address, erc20.address, 10,{ from: addr.sender});301 expect((await erc20.balanceOf(controller.address)).toString()).to.equal("990");302 expect((await erc20.balanceOf(strategyStub2.address)).toString()).to.equal("10");303 expect((await controller.balanceOf(yVault.address, { from: addr.sender })).toString()).to.equal("10");304 });305 it('withdrawAll work', async() => {306 strategyStub2 = await StrategyStub.new(erc20.address, controller.address,yVault.address, "name", { from: addr.sender });307 await controller.approveStrategy(yVault2.address, strategyStub2.address, { from: addr.sender });308 await controller.setStrategy(yVault2.address, strategyStub2.address, { from: addr.sender });309 expect((await controller.strategies(yVault2.address)).toString()).to.equal(strategyStub2.address.toString());310 await controller.approveStrategy(yVault.address, strategyStub2.address, { from: addr.sender });311 await controller.setStrategy(yVault.address, strategyStub2.address, { from: addr.sender });312 expect((await erc20.balanceOf(yVault.address)).toString()).to.equal("1000");//here 1000 because of withdraw with set strategy313 await controller.withdrawAll(yVault.address, { from: addr.sender});314 expect((await erc20.balanceOf(yVault.address)).toString()).to.equal("2000");315 });316 it('withdrawAll work whith bal != 0', async() => {317 strategyStub2 = await StrategyStub.new(erc20.address, controller.address,yVault.address, "name", { from: addr.sender });318 await controller.approveStrategy(yVault2.address, strategyStub2.address, { from: addr.sender });319 await controller.setStrategy(yVault2.address, strategyStub2.address, { from: addr.sender });320 expect((await controller.strategies(yVault2.address)).toString()).to.equal(strategyStub2.address.toString());321 await controller.approveStrategy(yVault.address, strategyStub2.address, { from: addr.sender });322 await controller.setStrategy(yVault.address, strategyStub2.address, { from: addr.sender });323 await erc20.mint(controller.address, 1000);324 expect((await erc20.balanceOf(controller.address)).toString()).to.equal("1000");325 await controller.earn(yVault.address, erc20.address, 10,{ from: addr.sender});326 expect((await erc20.balanceOf(controller.address)).toString()).to.equal("990");327 expect((await erc20.balanceOf(strategyStub2.address)).toString()).to.equal("10");328 expect((await controller.balanceOf(yVault.address, { from: addr.sender })).toString()).to.equal("10");329 await controller.withdrawAll(yVault.address, { from: addr.sender});330 expect((await erc20.balanceOf(yVault.address)).toString()).to.equal("2010");331 });332 });333 describe('earn test', () => {334 it('earn work', async() => {335 await erc20.mint(controller.address, 1000);336 expect((await erc20.balanceOf(controller.address)).toString()).to.equal("1000");337 await controller.earn(yVault.address, erc20.address, 10,{ from: addr.sender});338 expect((await erc20.balanceOf(controller.address)).toString()).to.equal("990");339 expect((await erc20.balanceOf(strategyStub.address)).toString()).to.equal("10");340 });341 });342 describe('balanceOf test', () => {343 it('balanceOf work', async() => {344 await erc20.mint(controller.address, 1000);345 expect((await erc20.balanceOf(controller.address)).toString()).to.equal("1000");346 await controller.earn(yVault.address, erc20.address, 10,{ from: addr.sender});347 expect((await erc20.balanceOf(controller.address)).toString()).to.equal("990");348 expect((await erc20.balanceOf(strategyStub.address)).toString()).to.equal("10");349 expect((await controller.balanceOf(yVault.address, { from: addr.sender })).toString()).to.equal("10");350 });351 });352 describe('withdrawAll test', () => {353 it('withdrawAll work', async() => {354 await erc20.mint(controller.address, 1000);355 expect((await erc20.balanceOf(controller.address)).toString()).to.equal("1000");356 await controller.earn(yVault.address, erc20.address, 10,{ from: addr.sender});357 expect((await erc20.balanceOf(controller.address)).toString()).to.equal("990");358 expect((await erc20.balanceOf(strategyStub.address)).toString()).to.equal("10");359 expect((await controller.balanceOf(yVault.address, { from: addr.sender })).toString()).to.equal("10");360 await controller.withdrawAll(yVault.address, { from: addr.sender});361 expect((await erc20.balanceOf(yVault.address)).toString()).to.equal("1010");362 });363 });364 describe('inCaseTokensGetStuck test', () => {365 it('inCaseTokensGetStuck work', async() => {366 await erc20.mint(controller.address, 1000);367 await controller.inCaseTokensGetStuck(erc20.address, 1, { from: addr.sender});368 expect((await erc20.balanceOf(addr.sender)).toString()).to.equal("1000001");369 });370 });371 describe('inCaseStrategyTokenGetStuck test', () => {372 it('inCaseStrategyTokenGetStuck work', async() => {373 await erc20.mint(controller.address, 1000);374 expect((await erc20.balanceOf(controller.address)).toString()).to.equal("1000");375 await controller.earn(yVault.address, erc20.address, 10,{ from: addr.sender});376 expect((await erc20.balanceOf(controller.address)).toString()).to.equal("990");377 expect((await erc20.balanceOf(strategyStub.address)).toString()).to.equal("10");378 expect((await controller.balanceOf(yVault.address, { from: addr.sender })).toString()).to.equal("10");379 await controller.inCaseStrategyTokenGetStuck(strategyStub.address, erc20.address, { from: addr.sender});380 expect((await erc20.balanceOf(yVault.address)).toString()).to.equal("1010");381 });382 });383 describe('withdraw test', () => {384 it('withdraw work', async() => {385 await truffleAssert.reverts(386 controller.withdraw( 0, { from: addr.sender }),387 'Caller is not a vault'388 );389 });390 });391 describe('yearn test', () => {392 it('yearn work', async() => {393 console.log((await erc20test.balanceOf(controller.address)).toString());394 const one = await OneSplit.new({ from: addr.sender });395 await erc20test.mint(strategyStub.address,1000,{ from: addr.sender });396 // await one.swap(erc20test.address, erc20.address, 10, 0, {}, 0);397 await controller.setOneSplit(one.address);398 await one.setController(controller.address);399 await controller.yearn(yVault.address, erc20test.address, 1, { from: addr.sender });400 console.log((await erc20test.balanceOf(controller.address)).toString());401 console.log((await erc20.balanceOf(controller.address)).toString());402 });403 });...
vote.spec.js
Source:vote.spec.js
1import tpInfo from "@/js/tp";2import voteInfo from "@/js/vote";3import multisigContractInstance from "@/js/contract";4import { MultisigContract } from "@/js/contract";5import sinon from "sinon";6import config from "@/test/unit/config";7const sandbox = sinon.createSandbox();8describe("test vote.ts", () => {9 describe("test destroy", () => {10 test("destroy should be a function", () => {11 expect(typeof voteInfo.destroy).toBe("function")12 })13 })14 describe("test getVotedCount", () => {15 afterEach(() => {16 sandbox.restore();17 voteInfo.destroy();18 });19 test("getVotedCount should be a function", () => {20 expect(typeof voteInfo.getVotedCount).toBe("function")21 })22 test("getVotedCount should be called and only be once if success when the account is voter", async () => {23 const stub1 = sandbox.stub(tpInfo, "getNode");24 stub1.resolves(config.testNode);25 const stub2 = sandbox.stub(MultisigContract.prototype, "getVotedCount");26 stub2.onCall(0).resolves(1);27 stub2.onCall(1).resolves(2);28 const spy = sandbox.spy(multisigContractInstance, "init");29 const spy1 = sandbox.spy(tpInfo, "getAddress");30 const spy2 = sandbox.spy(MultisigContract.prototype, "getMyVotedCount");31 let votedCount = await voteInfo.getVotedCount(true);32 expect(votedCount).toBe(1);33 expect(stub1.calledOnceWithExactly()).toBe(true);34 expect(stub2.calledOnceWithExactly()).toBe(true);35 expect(spy.calledOnceWithExactly(config.testNode)).toBe(true);36 expect(spy1.calledOnce).toBe(false);37 expect(spy2.calledOnce).toBe(false);38 votedCount = await voteInfo.getVotedCount(true);39 expect(votedCount).toBe(1);40 expect(stub1.calledOnce).toBe(true);41 expect(stub2.calledOnce).toBe(true);42 expect(spy.calledOnce).toBe(true);43 expect(spy1.called).toBe(false);44 expect(spy2.called).toBe(false);45 voteInfo.destroy();46 votedCount = await voteInfo.getVotedCount(true);47 expect(votedCount).toBe(2);48 expect(stub1.calledTwice).toBe(true);49 expect(stub2.calledTwice).toBe(true);50 expect(spy.calledTwice).toBe(true);51 expect(spy1.called).toBe(false);52 expect(spy2.called).toBe(false);53 })54 test("getMyVotedCount should be called and only be once if success when the account is not voter", async () => {55 const stub1 = sandbox.stub(tpInfo, "getNode");56 stub1.resolves(config.testNode);57 const stub2 = sandbox.stub(tpInfo, "getAddress");58 stub2.resolves(config.testAddress);59 const stub3 = sandbox.stub(MultisigContract.prototype, "getMyVotedCount");60 stub3.onCall(0).resolves(2);61 stub3.onCall(1).resolves(3);62 const spy = sandbox.spy(multisigContractInstance, "init");63 const spy1 = sandbox.spy(MultisigContract.prototype, "getVotedCount");64 let votedCount = await voteInfo.getVotedCount(false);65 expect(votedCount).toBe(2);66 expect(stub1.calledOnceWithExactly()).toBe(true);67 expect(stub2.calledOnceWithExactly()).toBe(true);68 expect(stub3.calledOnceWithExactly(config.testAddress)).toBe(true);69 expect(spy.calledOnceWithExactly(config.testNode)).toBe(true);70 expect(spy1.called).toBe(false);71 votedCount = await voteInfo.getVotedCount(false);72 expect(votedCount).toBe(2);73 expect(stub1.calledOnce).toBe(true);74 expect(stub2.calledOnce).toBe(true);75 expect(stub3.calledOnce).toBe(true);76 expect(spy.calledOnce).toBe(true);77 expect(spy1.called).toBe(false);78 voteInfo.destroy();79 votedCount = await voteInfo.getVotedCount(false);80 expect(votedCount).toBe(3);81 expect(stub1.calledTwice).toBe(true);82 expect(stub2.calledTwice).toBe(true);83 expect(stub3.calledTwice).toBe(true);84 expect(spy.calledTwice).toBe(true);85 expect(spy1.called).toBe(false);86 })87 test("return 0 if get fail", async () => {88 const stub1 = sandbox.stub(tpInfo, "getNode");89 stub1.resolves(config.testNode);90 const stub2 = sandbox.stub(MultisigContract.prototype, "getVotedCount");91 stub2.rejects();92 let votedCount = await voteInfo.getVotedCount(true);93 expect(votedCount).toBe(0);94 })95 })96 describe("test getPassPercent", () => {97 afterEach(() => {98 sandbox.restore();99 voteInfo.destroy();100 });101 test("getPassPercent should be a function", () => {102 expect(typeof voteInfo.getPassPercent).toBe("function")103 })104 test("getPercent should be called and only be once if success", async () => {105 const stub1 = sandbox.stub(tpInfo, "getNode");106 stub1.resolves(config.testNode);107 const stub2 = sandbox.stub(MultisigContract.prototype, "getPercent");108 stub2.onCall(0).resolves(1);109 stub2.onCall(1).resolves(2);110 const spy = sandbox.spy(multisigContractInstance, "init");111 let percent = await voteInfo.getPassPercent();112 expect(percent).toBe(1);113 expect(stub1.calledOnceWithExactly()).toBe(true);114 expect(stub2.calledOnceWithExactly()).toBe(true);115 expect(spy.calledOnceWithExactly(config.testNode)).toBe(true);116 percent = await voteInfo.getPassPercent();117 expect(percent).toBe(1);118 expect(stub1.calledOnce).toBe(true);119 expect(stub2.calledOnce).toBe(true);120 expect(spy.calledOnce).toBe(true);121 voteInfo.destroy();122 percent = await voteInfo.getPassPercent();123 expect(percent).toBe(2);124 expect(stub1.calledTwice).toBe(true);125 expect(stub2.calledTwice).toBe(true);126 expect(spy.calledTwice).toBe(true);127 })128 test("return 0 if get fail", async () => {129 const stub1 = sandbox.stub(tpInfo, "getNode");130 stub1.resolves(config.testNode);131 const stub2 = sandbox.stub(MultisigContract.prototype, "getPercent");132 stub2.rejects();133 let votedCount = await voteInfo.getPassPercent();134 expect(votedCount).toBe(0);135 })136 })...
suite.test.js
Source:suite.test.js
1"use strict";2const assert = require('chai').assert;3const sinon = require('sinon');4const Suite = require('../src/app/common/suite');5const Task = require('../src/app/common/task');6const TaskStatus = require('../src/app/common/task_status');7describe("Suite", () => {8 let taskStub;9 let testSuite;10 function createStub(taskStub2) {11 sinon.stub(taskStub2, "getData").returns("taskStub");12 sinon.stub(taskStub2, "run").callsFake(() => {13 taskStub2.status = TaskStatus.running;14 });15 sinon.stub(taskStub2, "stop").callsFake(() => {16 taskStub2.status = TaskStatus.idle;17 });18 }19 function restoreStub(taskStub2) {20 taskStub2.getData.restore();21 taskStub2.run.restore();22 taskStub2.stop.restore();23 }24 beforeEach(() => {25 taskStub = new Task({26 title: "test",27 commands: "command"});28 testSuite = new Suite("Test");29 createStub(taskStub);30 });31 afterEach(() => {32 restoreStub(taskStub);33 });34 it("Create New Suite", () => {35 const suite = new Suite("Suite Title");36 assert.strictEqual(suite.title, "Suite Title");37 assert.strictEqual(suite.length, 0);38 });39 it("Add and Remove Tasks", () => {40 assert.strictEqual(testSuite.length, 0);41 testSuite.addTask(taskStub);42 assert.strictEqual(testSuite.length, 1);43 testSuite.removeTask(0);44 assert.strictEqual(testSuite.length, 0);45 testSuite.removeTask(0);46 assert.strictEqual(testSuite.length, 0);47 });48 it("Stop All Tasks", () => {49 const taskStub2 = new Task({50 title: "test2",51 command: "command"52 });53 createStub(taskStub2);54 taskStub2.status = TaskStatus.running;55 testSuite.addTask(taskStub);56 testSuite.addTask(taskStub2);57 assert.isFalse(taskStub.isRunning());58 assert.isTrue(taskStub2.isRunning());59 testSuite.stopAll();60 assert.isFalse(taskStub.isRunning());61 assert.isFalse(taskStub2.isRunning());62 assert.isFalse(taskStub.stop.called);63 assert.isTrue(taskStub2.stop.called);64 restoreStub(taskStub2);65 });66 it("Replace Tasks", () => {67 const taskStub2 = new Task({68 title: "secondTask",69 command: "command"70 });71 createStub(taskStub2);72 testSuite.addTask(taskStub);73 testSuite.replaceTask(0, taskStub2);74 assert.strictEqual(testSuite.length, 1);75 assert.strictEqual(testSuite.tasks[0].title, "secondTask");76 });77 it("Get Data", () => {78 const suiteData = testSuite.getData();79 const expectedData = {80 title: "Test",81 tasks: []82 };83 assert.strictEqual(JSON.stringify(suiteData), JSON.stringify(expectedData));84 });85 it("Get Data With Tasks", () => {86 testSuite.addTask(taskStub);87 const suiteData = testSuite.getData();88 const expectedData = {89 title: "Test",90 tasks: ["taskStub"]91 };92 assert.strictEqual(JSON.stringify(suiteData), JSON.stringify(expectedData));93 });94 it("Add Task With Repeated Name", () => {95 const taskStub2 = new Task({96 title: "test",97 command: "command"});98 createStub(taskStub2);99 const taskStub3 = new Task({100 title: "test",101 command: "command"});102 createStub(taskStub3);103 testSuite.addTask(taskStub);104 testSuite.addTask(taskStub2);105 testSuite.addTask(taskStub3);106 assert.lengthOf(testSuite.tasks, 3);107 assert.strictEqual(testSuite.tasks[0].title, "test");108 assert.strictEqual(testSuite.tasks[1].title, "test (2)");109 assert.strictEqual(testSuite.tasks[2].title, "test (3)");110 restoreStub(taskStub2);111 restoreStub(taskStub3);112 });113 it("Replace Task With Repeated Name", () => {114 const taskStub2 = new Task({115 title: "different_name",116 command: "command"});117 createStub(taskStub2);118 const taskStub3 = new Task({119 title: "test",120 command: "command"});121 createStub(taskStub3);122 testSuite.addTask(taskStub);123 testSuite.addTask(taskStub2);124 testSuite.replaceTask(1, taskStub3);125 assert.lengthOf(testSuite.tasks, 2);126 assert.strictEqual(testSuite.tasks[0].title, "test");127 assert.strictEqual(testSuite.tasks[1].title, "test (2)");128 });129 it("Replace Task With Same Name", () => {130 const taskStub2 = new Task({131 title: "test",132 command: "command"});133 createStub(taskStub2);134 testSuite.addTask(taskStub);135 testSuite.replaceTask(0, taskStub2);136 assert.lengthOf(testSuite.tasks, 1);137 assert.strictEqual(testSuite.tasks[0].title, "test");138 });...
Using AI Code Generation
1var sinon = require('sinon');2var stub = sinon.stub();3stub.withArgs(42).returns(1);4stub.withArgs(43).returns(2);5var sinon = require('sinon');6var stub = sinon.stub();7stub.withArgs(42).returns(1);8stub.withArgs(43).returns(2);9var sinon = require('sinon');10var stub = sinon.stub();11stub.withArgs(42).returns(1);12stub.withArgs(43).returns(2);13var sinon = require('sinon');14var stub = sinon.stub();15stub.withArgs(42).returns(1);16stub.withArgs(43).returns(2);17var sinon = require('sinon');18var stub = sinon.stub();19stub.withArgs(42).returns(1);20stub.withArgs(43).returns(2);21var sinon = require('sinon');22var stub = sinon.stub();23stub.withArgs(42).returns(1);24stub.withArgs(43).returns(2);25var sinon = require('sinon');26var stub = sinon.stub();27stub.withArgs(42).returns(1);28stub.withArgs(43).returns(2);29var sinon = require('sinon');30var stub = sinon.stub();31stub.withArgs(42).returns(1);32stub.withArgs(43).returns(2);33var sinon = require('sinon');34var stub = sinon.stub();35stub.withArgs(42
Using AI Code Generation
1let sinon = require('sinon');2let stub2 = sinon.stub();3stub2.withArgs(1).returns('one');4stub2.withArgs(2).returns('two');5stub2.withArgs(3).returns('three');6console.log(stub2(1));7console.log(stub2(2));8console.log(stub2(3));9let sinon = require('sinon');10let stub2 = sinon.stub();11stub2.withArgs(1).returns('one');12stub2.withArgs(2).returns('two');13stub2.withArgs(3).returns('three');14stub2.withArgs(4).returns('four');15console.log(stub2(1));16console.log(stub2(2));17console.log(stub2(3));18console.log(stub2(4));19let sinon = require('sinon');20let stub2 = sinon.stub();21stub2.withArgs(1).returns('one');22stub2.withArgs(2).returns('two');23stub2.withArgs(3).returns('three');24stub2.withArgs(4).returns('four');25stub2.withArgs(5).returns('five');26console.log(stub2(1));27console.log(stub2(2));28console.log(stub2(3));29console.log(stub2(4));30console.log(stub2(5));31let sinon = require('sinon');32let stub2 = sinon.stub();33stub2.withArgs(1).returns('one');34stub2.withArgs(2).returns('two');35stub2.withArgs(3).returns('three');36stub2.withArgs(4).returns('four');37stub2.withArgs(5).returns('five');38stub2.withArgs(6).returns('six');39console.log(stub2(1));40console.log(stub2(2));41console.log(stub2(3));42console.log(stub2(4));43console.log(stub2(5));44console.log(stub2(6));
Using AI Code Generation
1var stub2 = sinon.stub2();2stub2.withArgs(1).returns(1);3stub2.withArgs(2).returns(2);4var stub2 = sinon.stub2();5stub2.withArgs(1).returns(1);6stub2.withArgs(2).returns(2);7var stub2 = sinon.stub2();8stub2.withArgs(1).returns(1);9stub2.withArgs(2).returns(2);10var stub2 = sinon.stub2();11stub2.withArgs(1).returns(1);12stub2.withArgs(2).returns(2);13var stub2 = sinon.stub2();14stub2.withArgs(1).returns(1);15stub2.withArgs(2).returns(2);16var stub2 = sinon.stub2();17stub2.withArgs(1).returns(1);18stub2.withArgs(2).returns(2);19var stub2 = sinon.stub2();20stub2.withArgs(1).returns(1);21stub2.withArgs(2).returns(2);22var stub2 = sinon.stub2();23stub2.withArgs(1).returns(1);24stub2.withArgs(2).returns(2);
Using AI Code Generation
1var stub2 = sinon.stub();2stub2.withArgs("foo").returns(42);3stub2.withArgs("bar").returns(43);4var stub2 = sinon.stub();5stub2.withArgs("foo").returns(42);6stub2.withArgs("bar").returns(43);7var stub2 = sinon.stub();8stub2.withArgs("foo").returns(42);9stub2.withArgs("bar").returns(43);10var stub2 = sinon.stub();11stub2.withArgs("foo").returns(42);12stub2.withArgs("bar").returns(43);13var stub2 = sinon.stub();14stub2.withArgs("foo").returns(42);15stub2.withArgs("bar").returns(43);16var stub2 = sinon.stub();17stub2.withArgs("foo").returns(42);18stub2.withArgs("bar").returns(43);19var stub2 = sinon.stub();20stub2.withArgs("foo").returns(42);21stub2.withArgs("bar").returns(43);22var stub2 = sinon.stub();23stub2.withArgs("foo").returns(42);24stub2.withArgs("
Using AI Code Generation
1var stub2 = sinon.stub();2stub2.withArgs(2).returns(4);3stub2.withArgs(3).returns(6);4console.log(stub2(2));5console.log(stub2(3));6var stub3 = sinon.stub();7stub3.withArgs(2).returns(4);8stub3.withArgs(3).returns(6);9console.log(stub3(2));10console.log(stub3(3));11var stub4 = sinon.stub();12stub4.withArgs(2).returns(4);13stub4.withArgs(3).returns(6);14console.log(stub4(2));15console.log(stub4(3));16var stub5 = sinon.stub();17stub5.withArgs(2).returns(4);18stub5.withArgs(3).returns(6);19console.log(stub5(2));20console.log(stub5(3));21var stub6 = sinon.stub();22stub6.withArgs(2).returns(4);23stub6.withArgs(3).returns(6);24console.log(stub6(2));25console.log(stub6(3));26var stub7 = sinon.stub();27stub7.withArgs(2).returns(4);28stub7.withArgs(3).returns(6);29console.log(stub7(2));30console.log(stub7(3));31var stub8 = sinon.stub();32stub8.withArgs(2).returns(4);33stub8.withArgs(3).returns(6);34console.log(stub8(2));35console.log(stub8(3));36var stub9 = sinon.stub();37stub9.withArgs(2).returns(4);38stub9.withArgs(3).returns(6);39console.log(stub9(2));40console.log(stub9(3));41var stub10 = sinon.stub();
Using AI Code Generation
1var stub = sinon.stub(obj, 'stub2').returns(20);2var stub = sinon.stub(obj, 'stub1').returns(10);3var stub = sinon.stub(obj, 'stub1').returns(10);4var stub = sinon.stub(obj, 'stub2').returns(20);5var stub = sinon.stub(obj, 'stub2').returns(20);6var stub = sinon.stub(obj, 'stub1').returns(10);7var stub = sinon.stub(obj, 'stub2').returns(20);8var stub = sinon.stub(obj, 'stub1').returns(10);9var stub = sinon.stub(obj, 'stub1').returns(10);10var stub = sinon.stub(obj, 'stub2').returns(20);11var stub = sinon.stub(obj, 'stub1').returns(10);12var stub = sinon.stub(obj, 'stub1').returns(10);13var stub = sinon.stub(obj, 'stub2').returns(20);14var stub = sinon.stub(obj, 'stub2').returns(20);15var stub = sinon.stub(obj, 'stub2').returns(20);16var stub = sinon.stub(obj, 'stub1').returns(10);17var stub = sinon.stub(obj, 'stub2').returns(20);18var stub = sinon.stub(obj, 'stub1').returns(10);19var stub = sinon.stub(obj, 'stub1').returns(10);20var stub = sinon.stub(obj, 'stub2').returns(20);
Using AI Code Generation
1var sinon = require('sinon');2var assert = require('assert');3var stub2 = sinon.stub().returns('foo');4assert.equal(stub2(), 'foo');5console.log(stub2());6var sinon = require('sinon');7var assert = require('assert');8var stub = sinon.stub().returns('foo');9assert.equal(stub(), 'foo');10console.log(stub());11Your name to display (optional):12Your name to display (optional):13var stub = sinon.stub().returns('foo');14assert.equal(stub(), 'foo');15console.log(stub());
Using AI Code Generation
1const sinon = require('sinon');2const assert = require('assert');3const myModule = require('./myModule');4const stub = sinon.stub(myModule, 'stub2').returns('testing stub2');5const stub3 = sinon.stub(myModule, 'stub3').returns('testing stub3');6describe('stubbing', () => {7 it('should stub stub2', () => {8 assert.equal(myModule.stub2(), 'testing stub2');9 });10 it('should stub stub3', () => {11 assert.equal(myModule.stub3(), 'testing stub3');12 });13});14module.exports = {15 stub2: () => {16 return 'stub2';17 },18 stub3: () => {19 return 'stub3';20 }21};
Using AI Code Generation
1var stub1 = sinon.stub(external, "stub2").returns("test");2var stub1 = sinon.stub(external, "stub2").returns("test");3function myFunction(a, b) {4 return a + b;5}6function myFunction(a, b) {7 return a + b;8}9function myFunction(a, b) {10 return a + b;11}12function myFunction(a, b) {13 return a + b;14}
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!!