Best JavaScript code snippet using pact-foundation-pact
Trades.test.ts
Source:Trades.test.ts
1import { renderHook } from '@testing-library/react-hooks'2import { mainnetTokens } from 'config/constants/tokens'3import { createReduxWrapper } from 'testUtils'4import { Pair, TokenAmount, CurrencyAmount, Trade } from '@pancakeswap/sdk'5import * as UsePairs from './usePairs'6import * as Trades from './Trades'7const { PairState } = UsePairs8describe('Trade', () => {9 describe('#useAllCommonPairs', () => {10 const mockUsePairs = jest.spyOn(UsePairs, 'usePairs')11 it('should filter only exist Pair', () => {12 mockUsePairs.mockReturnValue([13 [14 PairState.EXISTS,15 new Pair(new TokenAmount(mainnetTokens.wbnb, '1'), new TokenAmount(mainnetTokens.cake, '1')),16 ],17 [18 PairState.INVALID,19 new Pair(new TokenAmount(mainnetTokens.busd, '1'), new TokenAmount(mainnetTokens.cake, '1')),20 ],21 [22 PairState.LOADING,23 new Pair(new TokenAmount(mainnetTokens.busd, '1'), new TokenAmount(mainnetTokens.wbnb, '1')),24 ],25 [PairState.EXISTS, null],26 ])27 const { result } = renderHook(() => {28 const pairs = Trades.useAllCommonPairs(mainnetTokens.wbnb, mainnetTokens.cake)29 return {30 pairs,31 }32 })33 expect(result.current.pairs).toStrictEqual([34 new Pair(new TokenAmount(mainnetTokens.wbnb, '1'), new TokenAmount(mainnetTokens.cake, '1')),35 ])36 })37 it('should filter out duplicated Pair', () => {38 mockUsePairs.mockReturnValue([39 [40 PairState.EXISTS,41 new Pair(new TokenAmount(mainnetTokens.wbnb, '1'), new TokenAmount(mainnetTokens.cake, '1')),42 ],43 [44 PairState.EXISTS,45 new Pair(new TokenAmount(mainnetTokens.wbnb, '1'), new TokenAmount(mainnetTokens.cake, '1')),46 ],47 [48 PairState.EXISTS,49 new Pair(new TokenAmount(mainnetTokens.cake, '1'), new TokenAmount(mainnetTokens.wbnb, '1')),50 ],51 [PairState.EXISTS, null],52 ])53 const { result } = renderHook(() => {54 const pairs = Trades.useAllCommonPairs(mainnetTokens.wbnb, mainnetTokens.cake)55 return {56 pairs,57 }58 })59 expect(result.current.pairs).toStrictEqual([60 new Pair(new TokenAmount(mainnetTokens.wbnb, '1'), new TokenAmount(mainnetTokens.cake, '1')),61 ])62 })63 it('should get all pair combinations wbnb, cake', () => {64 mockUsePairs.mockClear()65 renderHook(() => {66 Trades.useAllCommonPairs(mainnetTokens.wbnb, mainnetTokens.cake)67 })68 expect(mockUsePairs).toMatchSnapshot()69 })70 it('should get all pair combinations, wbnb, wbnb', () => {71 mockUsePairs.mockClear()72 renderHook(() => {73 Trades.useAllCommonPairs(mainnetTokens.wbnb, mainnetTokens.wbnb)74 })75 expect(mockUsePairs).toMatchSnapshot()76 })77 })78 describe('#useTradeExactIn/Out', () => {79 const mockUseAllCommonPairs = jest.spyOn(Trades, 'useAllCommonPairs')80 const mockTradeExactIn = jest.spyOn(Trade, 'bestTradeExactIn')81 const mockTradeExactOut = jest.spyOn(Trade, 'bestTradeExactOut')82 it('should call with maxHops 1 with singleHopOnly', () => {83 const allowPairs = [new Pair(new TokenAmount(mainnetTokens.wbnb, '1'), new TokenAmount(mainnetTokens.cake, '1'))]84 const argA = CurrencyAmount.ether('1000000')85 const argB = mainnetTokens.cake86 renderHook(87 () => {88 mockUseAllCommonPairs.mockReturnValue(allowPairs)89 Trades.useTradeExactIn(argA, argB)90 },91 {92 wrapper: createReduxWrapper({ user: { userSingleHopOnly: true } }),93 },94 )95 expect(mockTradeExactIn).toBeCalledWith(allowPairs, argA, argB, { maxHops: 1, maxNumResults: 1 })96 mockTradeExactIn.mockClear()97 renderHook(98 () => {99 mockUseAllCommonPairs.mockReturnValue(allowPairs)100 Trades.useTradeExactOut(argB, argA)101 },102 {103 wrapper: createReduxWrapper({ user: { userSingleHopOnly: true } }),104 },105 )106 expect(mockTradeExactOut).toBeCalledWith(allowPairs, argB, argA, { maxHops: 1, maxNumResults: 1 })107 mockTradeExactOut.mockClear()108 })109 it('should call with 3 times without singleHopOnly', () => {110 const allowPairs = [new Pair(new TokenAmount(mainnetTokens.wbnb, '1'), new TokenAmount(mainnetTokens.cake, '1'))]111 const argA = CurrencyAmount.ether('1000000')112 const argB = mainnetTokens.cake113 renderHook(114 () => {115 mockUseAllCommonPairs.mockReturnValue(allowPairs)116 Trades.useTradeExactIn(argA, argB)117 },118 {119 wrapper: createReduxWrapper({ user: { userSingleHopOnly: false } }),120 },121 )122 renderHook(123 () => {124 mockUseAllCommonPairs.mockReturnValue(allowPairs)125 Trades.useTradeExactOut(argB, argA)126 },127 {128 wrapper: createReduxWrapper({ user: { userSingleHopOnly: false } }),129 },130 )131 expect(mockTradeExactOut).toBeCalledTimes(3)132 mockTradeExactOut.mockClear()133 })134 })...
Pairs.js
Source:Pairs.js
1/**2* The `Matter.Pairs` module contains methods for creating and manipulating collision pair sets.3*4* @class Pairs5*/6var Pairs = {};7module.exports = Pairs;8var Pair = require('./Pair');9var Common = require('../core/Common');10(function() {11 12 Pairs._pairMaxIdleLife = 1000;13 /**14 * Creates a new pairs structure.15 * @method create16 * @param {object} options17 * @return {pairs} A new pairs structure18 */19 Pairs.create = function(options) {20 return Common.extend({ 21 table: {},22 list: [],23 collisionStart: [],24 collisionActive: [],25 collisionEnd: []26 }, options);27 };28 /**29 * Updates pairs given a list of collisions.30 * @method update31 * @param {object} pairs32 * @param {collision[]} collisions33 * @param {number} timestamp34 */35 Pairs.update = function(pairs, collisions, timestamp) {36 var pairsList = pairs.list,37 pairsTable = pairs.table,38 collisionStart = pairs.collisionStart,39 collisionEnd = pairs.collisionEnd,40 collisionActive = pairs.collisionActive,41 collision,42 pairId,43 pair,44 i;45 // clear collision state arrays, but maintain old reference46 collisionStart.length = 0;47 collisionEnd.length = 0;48 collisionActive.length = 0;49 for (i = 0; i < pairsList.length; i++) {50 pairsList[i].confirmedActive = false;51 }52 for (i = 0; i < collisions.length; i++) {53 collision = collisions[i];54 if (collision.collided) {55 pairId = Pair.id(collision.bodyA, collision.bodyB);56 pair = pairsTable[pairId];57 58 if (pair) {59 // pair already exists (but may or may not be active)60 if (pair.isActive) {61 // pair exists and is active62 collisionActive.push(pair);63 } else {64 // pair exists but was inactive, so a collision has just started again65 collisionStart.push(pair);66 }67 // update the pair68 Pair.update(pair, collision, timestamp);69 pair.confirmedActive = true;70 } else {71 // pair did not exist, create a new pair72 pair = Pair.create(collision, timestamp);73 pairsTable[pairId] = pair;74 // push the new pair75 collisionStart.push(pair);76 pairsList.push(pair);77 }78 }79 }80 // deactivate previously active pairs that are now inactive81 for (i = 0; i < pairsList.length; i++) {82 pair = pairsList[i];83 if (pair.isActive && !pair.confirmedActive) {84 Pair.setActive(pair, false, timestamp);85 collisionEnd.push(pair);86 }87 }88 };89 90 /**91 * Finds and removes pairs that have been inactive for a set amount of time.92 * @method removeOld93 * @param {object} pairs94 * @param {number} timestamp95 */96 Pairs.removeOld = function(pairs, timestamp) {97 var pairsList = pairs.list,98 pairsTable = pairs.table,99 indexesToRemove = [],100 pair,101 collision,102 pairIndex,103 i;104 for (i = 0; i < pairsList.length; i++) {105 pair = pairsList[i];106 collision = pair.collision;107 108 // never remove sleeping pairs109 if (collision.bodyA.isSleeping || collision.bodyB.isSleeping) {110 pair.timeUpdated = timestamp;111 continue;112 }113 // if pair is inactive for too long, mark it to be removed114 if (timestamp - pair.timeUpdated > Pairs._pairMaxIdleLife) {115 indexesToRemove.push(i);116 }117 }118 // remove marked pairs119 for (i = 0; i < indexesToRemove.length; i++) {120 pairIndex = indexesToRemove[i] - i;121 pair = pairsList[pairIndex];122 delete pairsTable[pair.id];123 pairsList.splice(pairIndex, 1);124 }125 };126 /**127 * Clears the given pairs structure.128 * @method clear129 * @param {pairs} pairs130 * @return {pairs} pairs131 */132 Pairs.clear = function(pairs) {133 pairs.table = {};134 pairs.list.length = 0;135 pairs.collisionStart.length = 0;136 pairs.collisionActive.length = 0;137 pairs.collisionEnd.length = 0;138 return pairs;139 };...
Using AI Code Generation
1const { Matchers } = require('@pact-foundation/pact');2const { somethingLike } = Matchers;3const pact = require('@pact-foundation/pact').Pact;4const { like, term, eachLike, eachKeyLike } = require('@pact-foundation/pact').Matchers;5const { Pact } = require('@pact-foundation/pact');6const { somethingLike } = require('@pact-foundation/pact').Matchers;7const { eachLike, like, term, eachKeyLike } = require('@pact-foundation/pact').Matchers;8const { Pact } = require('@pact-foundation/pact-node');9const { somethingLike } = require('@pact-foundation/pact-node').Matchers;10const { eachLike, like, term, eachKeyLike } = require('@pact-foundation/pact-node').Matchers;11const { Matchers } = require('@pact-foundation/pact-node');12const { somethingLike } = Matchers;13const { eachLike, like, term, eachKeyLike } = Matchers;14const { Matchers } = require('@pact-foundation/pact');15const { somethingLike } = Matchers;16const pact = require('@pact-foundation/pact').Pact;17const { like, term, eachLike, eachKeyLike } = require('@pact-foundation/pact').Matchers;18const { Pact } = require('@pact-foundation/pact-node');19const { somethingLike } = require('@pact-foundation/pact-node').Matchers;20const { eachLike, like, term, eachKeyLike } = require('@pact-foundation/pact-node').Matchers;21const { Matchers } = require('@pact-foundation/pact-node');22const { somethingLike } = Matchers;23const { eachLike, like, term, eachKeyLike } = Matchers;24const { Pact } = require('@pact-foundation/pact-node');25const { somethingLike } = require('@pact-foundation/pact-node').Matchers;26const { eachLike, like, term, eachKeyLike } = require('@pact-foundation/pact-node').Matchers;27const { Matchers } = require
Using AI Code Generation
1const { Matchers } = require("@pact-foundation/pact");2const { somethingLike } = Matchers;3const { like, term } = require("@pact-foundation/pact/dsl/matchers");4const { Pact } = require("@pact-foundation/pact");5const { fetch } = require("cross-fetch");6const { assert } = require("chai");7describe("Pact", () => {8 const provider = new Pact({9 });10 before(() => provider.setup());11 after(() => provider.finalize());12 afterEach(() => provider.verify());13 describe("when a request to get a user is made", () => {14 before(() => {15 const interaction = {16 withRequest: {17 headers: {18 },19 },20 willRespondWith: {21 headers: {22 "Content-Type": "application/json;charset=utf-8",23 },24 body: {25 id: like(1),26 name: term({27 }),28 age: somethingLike(30),29 },30 },31 };32 return provider.addInteraction(interaction);33 });34 it("returns the user details", () => {35 headers: {36 },37 }).then((res) => {38 assert.equal(res.status, 200);39 return res.json().then((body) => {40 assert.equal(body.id, 1);41 assert.equal(body.name, "Bob
Using AI Code Generation
1var pact = require('pact-foundation/pact-node');2var opts = {3};4 .pact(opts)5 .then(function (url) {6 console.log('Pact Mock Service running on', url);7 });8 .verifyPacts(opts)9 .then(function (output) {10 console.log('Pact Verification Complete!')11 console.log(output)12 })13 .catch(function (e) {14 console.log('Pact Verification Failed: ', e)15 });16 .removeInteractions(opts)17 .then(function () {18 console.log('Interactions removed')19 });
Using AI Code Generation
1const Pact = require('pact-foundation/pact');2const { eachLike, like, term } = require('@pact-foundation/pact/dsl/matchers');3const { somethingLike } = require('@pact-foundation/pact/dsl/matchers');4const { Matchers } = require('@pact-foundation/pact');5const { somethingLike } = require('@pact-foundation/pact/dsl/matchers');6const { Pact, Matchers } = require('@pact-foundation/pact');7const { somethingLike } = require('@pact-foundation/pact/dsl/matchers');8describe('Pact with NodeJS', () => {9 const EXPECTED_BODY = {10 id: somethingLike(1),11 name: somethingLike('John'),12 surname: somethingLike('Smith'),13 email: somethingLike('
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!!