Skip to content

Commit aa1e7ae

Browse files
committed
feat(router): add router step tests
1 parent 56e7943 commit aa1e7ae

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

packages/builder/src/steps/router.test.ts

+52-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import action from './router';
2-
import { fixtureContractData, fixtureCtx, fixtureRuntime } from '../../test/fixtures';
2+
import { fixtureContractData, fixtureCtx, fixtureRuntime, fixtureSigner, mockDeployTransaction } from '../../test/fixtures';
33

44
describe('steps/router.ts', () => {
55
describe('configInject()', () => {
@@ -47,4 +47,55 @@ describe('steps/router.ts', () => {
4747
expect(result.contractAddresses.GreeterTwo).toStrictEqual(contracts.GreeterTwo.address);
4848
});
4949
});
50+
51+
describe('exec()', () => {
52+
it('throws an error on missing contract file', async () => {
53+
const contracts = { Greeter: fixtureContractData('Greeter') };
54+
const step = {
55+
name: 'router-test',
56+
version: '0.0.0',
57+
currentLabel: 'router.Router',
58+
};
59+
60+
const runtime = fixtureRuntime();
61+
const ctx = fixtureCtx({}); // generate a ctx without the contracts
62+
const config = { contracts: Object.keys(contracts) };
63+
64+
await expect(action.exec(runtime, ctx, config, step)).rejects.toThrow('contract not found: Greeter');
65+
});
66+
67+
it('generates and deploys Router with a single contract', async () => {
68+
const signer = fixtureSigner();
69+
const contracts = { Greeter: fixtureContractData('Greeter') };
70+
71+
const step = {
72+
name: 'router-test',
73+
version: '0.0.0',
74+
currentLabel: 'router.Router',
75+
};
76+
77+
const runtime = fixtureRuntime();
78+
const ctx = fixtureCtx({ contracts });
79+
const config = { from: await signer.getAddress(), contracts: Object.keys(contracts) };
80+
81+
jest.spyOn(runtime, 'getSigner').mockResolvedValue(signer);
82+
83+
const { tx, rx } = await mockDeployTransaction(signer);
84+
const res = await action.exec(runtime, ctx, config, step);
85+
86+
expect(signer.sendTransaction).toHaveBeenCalledTimes(1);
87+
expect(tx.wait).toHaveBeenCalledTimes(1);
88+
89+
expect(res.contracts).toMatchObject({
90+
Router: {
91+
address: rx.contractAddress,
92+
abi: contracts.Greeter.abi,
93+
deployedOn: step.currentLabel,
94+
deployTxnHash: tx.hash,
95+
contractName: 'Router',
96+
sourceName: 'Router.sol',
97+
},
98+
});
99+
});
100+
});
50101
});

packages/builder/test/fixtures.ts

+23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'node:fs';
22
import _ from 'lodash';
33
import { ethers } from 'ethers';
44
import { JsonFragment } from '@ethersproject/abi';
5+
import { TransactionResponse } from '@ethersproject/abstract-provider';
56
import { ChainBuilderContextWithHelpers, ChainBuilderRuntimeInfo } from '../src/types';
67
import { ChainBuilderRuntime } from '../src/runtime';
78
import { CannonWrapperGenericProvider, InMemoryRegistry } from '../src';
@@ -70,3 +71,25 @@ export const fixtureRuntime = (
7071
loaders?: ConstructorParameters<typeof ChainBuilderRuntime>[2],
7172
defaultLoaderScheme?: ConstructorParameters<typeof ChainBuilderRuntime>[3]
7273
) => new ChainBuilderRuntime(info, registry, loaders, defaultLoaderScheme);
74+
75+
export const fixtureSigner = () => ethers.Wallet.createRandom();
76+
77+
export async function mockDeployTransaction(signer: ethers.Signer) {
78+
const hash = fixtureTxHash();
79+
80+
const rx = {
81+
contractAddress: fixtureAddress(),
82+
transactionHash: hash,
83+
logs: [],
84+
};
85+
86+
const tx = {
87+
hash,
88+
from: await signer.getAddress(),
89+
wait: jest.fn().mockResolvedValue(rx),
90+
};
91+
92+
jest.spyOn(signer, 'sendTransaction').mockResolvedValue(tx as unknown as TransactionResponse);
93+
94+
return { tx, rx };
95+
}

0 commit comments

Comments
 (0)