Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full APIConsumer unit test mocking MockOracle request fulfillment #70

Merged
merged 4 commits into from
Jan 22, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions test/unit/APIConsumer_unit_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ 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 () => {
before(async () => {
rogargon marked this conversation as resolved.
Show resolved Hide resolved
const chainId = await getChainId()
await deployments.fixture(['mocks', 'api'])
const LinkToken = await deployments.get('LinkToken')
Expand All @@ -27,6 +28,14 @@ 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()
apiConsumer.removeAllListeners()
})
rogargon marked this conversation as resolved.
Show resolved Hide resolved

it('Should successfully make an API request', async () => {
Expand All @@ -37,4 +46,21 @@ 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",
(_specId, _sender, requestId, _payment, _cbAddress, _callbackFuncId, expiration, _dataVersion, _data) => {
console.log("OracleRequest:", requestId, _data);
// Mock the fulfillment of the request
mockOracle.fulfillOracleRequest(requestId, numToBytes32(1));
// Now check the result
apiConsumer.volume().then((result) => {
console.log("API Consumer Volume: ", new ethers.BigNumber.from(result._hex).toString());
expect(new ethers.BigNumber.from(result._hex).toString())
.to.be.a.bignumber.that.is.greaterThan(new ethers.BigNumber.from(0).toString());
rogargon marked this conversation as resolved.
Show resolved Hide resolved
done();
});
});
apiConsumer.requestVolumeData();
});
})