Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

contract-tests: fix detectNetwork test and its implementation #4744

Merged
merged 2 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
48 changes: 20 additions & 28 deletions packages/contract-tests/test/contract/constructorMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,55 +32,51 @@ describe("TruffleContract.new()", () => {
});

describe("TruffleContract.at()", () => {
it("throws if passed an invalid address", () => {
it("throws if passed an invalid address", async () => {
const freshTruffleContract = TruffleContract();
assert.rejects(
await assert.rejects(
async () => {
await freshTruffleContract.at();
},
{
name: "Error",
message: /(Invalid address passed)/
},
"should have thrown!"
}
);

assert.rejects(
await assert.rejects(
async () => {
await freshTruffleContract.at(12345);
},
{
name: "Error",
message: /(Invalid address passed)/
},
"should have thrown!"
}
);

assert.rejects(
await assert.rejects(
async () => {
await freshTruffleContract.at("0x000323332");
},
{
name: "Error",
message: /(Invalid address passed)/
},
"should have thrown!"
}
);
});
});

describe("TruffleContract.deployed()", () => {
it("throws if called before setting a provider", () => {
it("throws if called before setting a provider", async () => {
const freshTruffleContract = TruffleContract();
assert.rejects(
await assert.rejects(
async () => {
await freshTruffleContract.deployed();
},
{
name: "Error",
message: /(Please call setProvider\(\) first)/
},
"should have thrown!"
}
);
});

Expand All @@ -95,15 +91,14 @@ describe("TruffleContract.deployed()", () => {
await freshTruffleContract.detectNetwork();
freshTruffleContract.networks[freshTruffleContract.network_id] =
"fakeNetworkRecord";
assert.rejects(
await assert.rejects(
async () => {
await freshTruffleContract.deployed();
},
{
name: "Error",
message: /(Contract).*(not).*(deployed to detected network)/
},
"should have thrown!"
}
);
});
});
Expand All @@ -124,29 +119,27 @@ describe("TruffleContract.isDeployed()", () => {
});

describe("TruffleContract.detectNetwork()", () => {
it("throws when provider not present", () => {
it("throws when provider not present", async () => {
const freshTruffleContract = TruffleContract();
assert.rejects(
await assert.rejects(
async () => await freshTruffleContract.detectNetwork(),
{
name: "Error",
message: /(Provider not set or invalid)/
},
"should have thrown!"
}
);
});
});

describe("TruffleContract.detectNetwork()", () => {
it("throws when network not set and provider not present", () => {
it("throws when network not set and provider not present", async () => {
const freshTruffleContract = TruffleContract();
assert.rejects(
await assert.rejects(
async () => await freshTruffleContract.detectNetwork(),
{
name: "Error",
message: /(Provider not set or invalid)/
},
"should have thrown!"
}
);
});

Expand All @@ -155,13 +148,12 @@ describe("TruffleContract.detectNetwork()", () => {
freshTruffleContract.network_id = 1234;
freshTruffleContract.networks[freshTruffleContract.network_id] =
"dummyNetwork";
assert.rejects(
await assert.rejects(
async () => await freshTruffleContract.detectNetwork(),
{
name: "Error",
message: /(Provider not set or invalid)/
},
"should have thrown!"
}
);
});
});
Expand Down
4 changes: 4 additions & 0 deletions packages/contract/lib/contract/constructorMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ module.exports = Contract => ({
},

async detectNetwork() {
// guard interfaceAdapter!
if (this.interfaceAdapter == null) {
throw new Error("Provider not set or invalid");
}
// if artifacts already have a network_id and network configuration synced,
// use that network and use latest block gasLimit
if (this.network_id && this.networks[this.network_id] != null) {
Expand Down