diff --git a/test/unit/APIConsumer_unit_test.js b/test/unit/APIConsumer_unit_test.js index 057ca907e..97f2b963d 100755 --- a/test/unit/APIConsumer_unit_test.js +++ b/test/unit/APIConsumer_unit_test.js @@ -4,12 +4,13 @@ const chai = require('chai') const { expect } = require('chai') const BN = require('bn.js') const { getChainId } = require('hardhat') +const {numToBytes32} = require("@chainlink/test-helpers/dist/src/helpers"); chai.use(require('chai-bn')(BN)) skip.if(!developmentChains.includes(network.name)). describe('APIConsumer Unit Tests', async function () { - let apiConsumer, linkToken + let apiConsumer, linkToken, mockOracle beforeEach(async () => { const chainId = await getChainId() @@ -27,6 +28,13 @@ skip.if(!developmentChains.includes(network.name)). if (await autoFundCheck(apiConsumer.address, networkName, linkTokenAddress, additionalMessage)) { await hre.run("fund-link", { contract: apiConsumer.address, linkaddress: linkTokenAddress }) } + + const MockOracle = await deployments.get("MockOracle"); + mockOracle = await ethers.getContractAt("MockOracle", MockOracle.address); + }) + + afterEach(async () => { + mockOracle.removeAllListeners() }) it('Should successfully make an API request', async () => { @@ -37,4 +45,19 @@ skip.if(!developmentChains.includes(network.name)). console.log("requestId: ", requestId) expect(requestId).to.not.be.null }) + + it("Should successfully make an API request and get a result", (done) => { + mockOracle.once("OracleRequest", + async (_specId, _sender, requestId, _payment, _cbAddress, _callbackFuncId, expiration, _dataVersion, _data) => { + console.log("OracleRequest:", requestId, _data); + // Mock the fulfillment of the request + const callbackValue = 1 + await mockOracle.fulfillOracleRequest(requestId, numToBytes32(callbackValue)); + // Now check the result + const volume = await apiConsumer.volume() + expect(volume).to.equal(ethers.BigNumber.from(callbackValue)) + done(); + }); + apiConsumer.requestVolumeData(); + }); })