Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodus committed Sep 19, 2023
1 parent 89f125d commit 5f77574
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 3 deletions.
24 changes: 24 additions & 0 deletions contracts/build/Registry.abi
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,30 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_id",
"type": "uint256"
}
],
"name": "getEntry",
"outputs": [
{
"internalType": "address[]",
"name": "",
"type": "address[]"
},
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand Down
2 changes: 1 addition & 1 deletion contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"license": "MIT",
"scripts": {
"build": "npx hardhat compile && bash extract-abis.bash",
"test": "scripts/test test/contract.test.ts",
"test": "scripts/test test/*.test.ts",
"deploy-local": "npx hardhat run scripts/deploy.ts --network localhost | tee contract-deployment.json"
},
"dependencies": {},
Expand Down
95 changes: 95 additions & 0 deletions contracts/test/registry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import '@nomicfoundation/hardhat-chai-matchers';

import {expect} from 'chai';
import * as deployment from '../utils/deploy';
import {getAccounts, Account} from '../utils/helpers';

import {Registry} from '../types/contracts/Registry';
import {BigNumber} from 'ethers';
import {setAutoMine} from './helpers';

describe('Registry contract', () => {
// Accounts
let deployer: Account;
let other: Account;

// Contracts
let registry: Registry;

before(async function () {
[deployer, other] = await getAccounts();

setAutoMine(true);
});

beforeEach(async function () {
registry = await deployment.deployRegistry(deployer.signer, false);
});

describe('constructor', function () {
it('should set the owner to the contract deployer address', async function () {
expect(await registry.owner()).to.eq(deployer.address);
});
});

describe('transferOwnership', function () {
it('should set the owner to the new owner', async function () {
await registry.transferOwnership(other.address);
expect(await registry.owner()).to.eq(other.address);
});
});

describe('insertEntry', function () {
it('should set entry', async function () {
const entryID = BigNumber.from(1);
const entry = {
subscriptions: [
'0x0000000000000000000000000000000000000001',
'0x0000000000000000000000000000000000000002',
],
metadataHash: BigNumber.from(
'0x0000000000000000000000000000000000000000000000000000000000000001'
),
};
await registry.insertEntry(entryID, entry);
const result = await registry.getEntry(entryID);
expect(result[0]).to.eql(entry.subscriptions);
expect(result[1]).to.eql(entry.metadataHash);
});

it('should overwrite entry', async function () {
const entryID = BigNumber.from(1);
const entry1 = {
subscriptions: ['0x0000000000000000000000000000000000000001'],
metadataHash: BigNumber.from(
'0x0000000000000000000000000000000000000000000000000000000000000001'
),
};
const entry2 = {
subscriptions: ['0x0000000000000000000000000000000000000002'],
metadataHash: BigNumber.from(
'0x0000000000000000000000000000000000000000000000000000000000000002'
),
};
await registry.insertEntry(entryID, entry1);
await registry.insertEntry(entryID, entry2);
const result = await registry.getEntry(entryID);
expect(result[0]).to.eql(entry2.subscriptions);
expect(result[1]).to.eql(entry2.metadataHash);
});
});

describe('removeEntry', function () {
it('should remove entry', async function () {
const entryID = BigNumber.from(1);
const subscriptions = ['0x0000000000000000000000000000000000000001'];
const metadataHash =
'0x0000000000000000000000000000000000000000000000000000000000000001';
await registry.insertEntry(entryID, {subscriptions, metadataHash});
await registry.removeEntry(entryID);
const result = await registry.getEntry(entryID);
expect(result[0]).to.eql([]);
expect(result[1]).to.eql(BigNumber.from(0));
});
});
});
11 changes: 9 additions & 2 deletions contracts/utils/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Artifacts } from 'hardhat/internal/artifacts'
import { LinkReferences } from 'hardhat/types'

import { Subscriptions } from '../types/contracts/Subscriptions'
import { StableToken } from '../types'
import { Registry, StableToken } from '../types'

type Abi = Array<string | utils.FunctionFragment | utils.EventFragment | utils.ParamType>

Expand Down Expand Up @@ -71,4 +71,11 @@ export async function deployStableToken(
logging = true
): Promise<StableToken> {
return deployContract(args, sender, 'StableToken', logging) as unknown as Promise<StableToken>
}
}

export async function deployRegistry(
sender: Signer,
logging = true
): Promise<Registry> {
return deployContract([], sender, 'Registry', logging) as unknown as Promise<Registry>
}

0 comments on commit 5f77574

Please sign in to comment.