Skip to content

Commit

Permalink
tests with jest (#3)
Browse files Browse the repository at this point in the history
* updated: tests config

* fixed: typo

* added: invalid address test, readme, version bump
  • Loading branch information
Salmandabbakuti authored Nov 23, 2023
1 parent 417abb9 commit e6c00f5
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 50 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Superfluid Web3 Plugin

[![npm version](https://img.shields.io/badge/npm-0.2.2-brightgreen)](https://www.npmjs.com/package/web3-plugin-superfluid)
[![npm version](https://img.shields.io/badge/npm-0.2.3-brightgreen)](https://www.npmjs.com/package/web3-plugin-superfluid)

The Superfluid Web3.js Plugin extends the capabilities of the Web3.js library to interact seamlessly with the [Superfluid Protocol](https://superfluid.finance). This plugin provides convenient methods for interacting with the Superfluid protocol contracts.

Expand Down Expand Up @@ -222,6 +222,11 @@ npm publish

## Change Log

#### 0.2.3

- Using jest for tests instead of mocha
- Added tests for invalid address to cfav1Forwarder, cfav1, idav1, host

#### 0.2.1

- Updated README.md examples and docs
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "web3-plugin-superfluid",
"version": "0.2.2",
"version": "0.2.3",
"description": "Superfluid Web3.js Plugin",
"author": "Salman Dabbakuti",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/superfluid-plugin.d.ts",
"scripts": {
"test": "mocha --r ts-node/register 'test/**/*.ts'",
"test": "jest --config=./test/jest.config.js",
"build": "tsc"
},
"contributors": [
Expand All @@ -23,11 +23,11 @@
"web3": ">= 4.0.3"
},
"devDependencies": {
"@types/chai": "^4.3.11",
"@types/mocha": "^10.0.6",
"chai": "^4.3.10",
"mocha": "^10.2.0",
"ts-node": "^10.9.1",
"@types/jest": "^29.5.10",
"@types/node": "^20.9.4",
"jest": "^29.7.0",
"jest-extended": "^4.0.2",
"ts-jest": "^29.1.1",
"typescript": "^5.3.2",
"web3": "^4.2.2"
},
Expand Down
8 changes: 4 additions & 4 deletions src/superfluid-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class SuperfluidPlugin extends Web3PluginBase {
*/
public cfav1Forwarder(address: string): CFAV1Forwarder {
if (!validator.isAddress(address))
throw new Error("Superfluid Plugn: Invalid CFA Forwarder Address");
throw new Error("Superfluid Plugin: Invalid CFA Forwarder Address");
const cfav1ForwarderContract = new Contract(cfav1ForwarderAbi, address);
cfav1ForwarderContract.link(this);
return cfav1ForwarderContract;
Expand All @@ -46,7 +46,7 @@ export class SuperfluidPlugin extends Web3PluginBase {
*/
public cfav1(address: string): CFAV1 {
if (!validator.isAddress(address))
throw new Error("Superfluid Plugn: Invalid CFA Address");
throw new Error("Superfluid Plugin: Invalid CFA Address");
const cfav1Contract = new Contract(cfav1Abi, address);
cfav1Contract.link(this);
return cfav1Contract;
Expand All @@ -66,7 +66,7 @@ export class SuperfluidPlugin extends Web3PluginBase {
*/
public idav1(address: string): IDAV1 {
if (!validator.isAddress(address))
throw new Error("Superfluid Plugn: Invalid IDA Address");
throw new Error("Superfluid Plugin: Invalid IDA Address");
const idav1Contract = new Contract(idav1Abi, address);
idav1Contract.link(this);
return idav1Contract;
Expand All @@ -86,7 +86,7 @@ export class SuperfluidPlugin extends Web3PluginBase {
*/
public host(address: string): Host {
if (!validator.isAddress(address))
throw new Error("Superfluid Plugn: Invalid Host Address");
throw new Error("Superfluid Plugin: Invalid Host Address");
const hostContract = new Contract(hostAbi, address);
hostContract.link(this);
return hostContract;
Expand Down
5 changes: 5 additions & 0 deletions test/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
};
94 changes: 56 additions & 38 deletions test/superfluid-plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Web3, Web3Eth, Web3Context } from "web3";
import { expect } from "chai";
import { SuperfluidPlugin, CFAV1Forwarder, CFAV1, IDAV1, Host } from "../src";

// Test data: Mumbai testnet
Expand All @@ -17,37 +16,58 @@ describe("SuperfluidPlugin Tests", () => {
it("should register Superfluid plugin to Web3", () => {
const web3 = new Web3("http://127.0.0.1:8545");
web3.registerPlugin(new SuperfluidPlugin());
// expect to have superfluid in web3
expect(web3.superfluid).to.be.instanceOf(SuperfluidPlugin);
expect(web3.superfluid.pluginNamespace).to.be.equal("superfluid");
expect(web3.superfluid.cfav1Forwarder).to.be.a("function");
expect(web3.superfluid.cfav1).to.be.a("function");
expect(web3.superfluid.idav1).to.be.a("function");
expect(web3.superfluid.host).to.be.a("function");

expect(web3.superfluid).toBeInstanceOf(SuperfluidPlugin);
expect(web3.superfluid.pluginNamespace).toBe("superfluid");
expect(web3.superfluid.cfav1Forwarder).toBeInstanceOf(Function);
expect(web3.superfluid.cfav1).toBeInstanceOf(Function);
expect(web3.superfluid.idav1).toBeInstanceOf(Function);
expect(web3.superfluid.host).toBeInstanceOf(Function);
});

it("should register Superfluid plugin to Web3Context", () => {
const web3Context = new Web3Context("http://127.0.0.1:8545");
web3Context.registerPlugin(new SuperfluidPlugin());
// expect to have superfluid in Web3Context
expect(web3Context.superfluid).to.be.instanceOf(SuperfluidPlugin);
expect(web3Context.superfluid.pluginNamespace).to.be.equal("superfluid");
expect(web3Context.superfluid.cfav1Forwarder).to.be.a("function");
expect(web3Context.superfluid.cfav1).to.be.a("function");
expect(web3Context.superfluid.idav1).to.be.a("function");
expect(web3Context.superfluid.host).to.be.a("function");

expect(web3Context.superfluid).toBeInstanceOf(SuperfluidPlugin);
expect(web3Context.superfluid.pluginNamespace).toBe("superfluid");
expect(web3Context.superfluid.cfav1Forwarder).toBeInstanceOf(Function);
expect(web3Context.superfluid.cfav1).toBeInstanceOf(Function);
expect(web3Context.superfluid.idav1).toBeInstanceOf(Function);
expect(web3Context.superfluid.host).toBeInstanceOf(Function);
});

it("should register Superfluid plugin to Web3Eth", () => {
const web3EthContext = new Web3Eth("http://127.0.0.1:8545");
web3EthContext.registerPlugin(new SuperfluidPlugin());
// expect to have superfluid in Web3Context
expect(web3EthContext.superfluid).to.be.instanceOf(SuperfluidPlugin);
expect(web3EthContext.superfluid.pluginNamespace).to.be.equal("superfluid");
expect(web3EthContext.superfluid.cfav1Forwarder).to.be.a("function");
expect(web3EthContext.superfluid.cfav1).to.be.a("function");
expect(web3EthContext.superfluid.idav1).to.be.a("function");
expect(web3EthContext.superfluid.host).to.be.a("function");

expect(web3EthContext.superfluid).toBeInstanceOf(SuperfluidPlugin);
expect(web3EthContext.superfluid.pluginNamespace).toBe("superfluid");
expect(web3EthContext.superfluid.cfav1Forwarder).toBeInstanceOf(Function);
expect(web3EthContext.superfluid.cfav1).toBeInstanceOf(Function);
expect(web3EthContext.superfluid.idav1).toBeInstanceOf(Function);
expect(web3EthContext.superfluid.host).toBeInstanceOf(Function);
});

it("should throw error if address passed to plugin functions is not valid", () => {
const web3 = new Web3("http://127.0.0.1:8545");
web3.registerPlugin(new SuperfluidPlugin());

expect(() => {
web3.superfluid.cfav1Forwarder("0x123");
}).toThrow("Superfluid Plugin: Invalid CFA Forwarder Address");

expect(() => {
web3.superfluid.cfav1("0x123");
}).toThrow("Superfluid Plugin: Invalid CFA Address");

expect(() => {
web3.superfluid.idav1("0x123");
}).toThrow("Superfluid Plugin: Invalid IDA Address");

expect(() => {
web3.superfluid.host("0x123");
}).toThrow("Superfluid Plugin: Invalid Host Address");
});
});

Expand All @@ -58,7 +78,7 @@ describe("SuperfluidPlugin Method Tests", () => {
let idav1: IDAV1;
let host: Host;

before(() => {
beforeAll(() => {
web3 = new Web3(rpcUrl);
web3.registerPlugin(new SuperfluidPlugin());
cfav1Forwarder = web3.superfluid.cfav1Forwarder(cfav1ForwarderAddress);
Expand All @@ -71,23 +91,23 @@ describe("SuperfluidPlugin Method Tests", () => {
const { lastUpdated, flowrate } = await cfav1Forwarder.methods
.getFlowInfo(token, sender, receiver)
.call();
expect(lastUpdated).to.be.a("bigint");
expect(flowrate).to.be.a("bigint");
expect(typeof lastUpdated).toBe("bigint");
expect(typeof flowrate).toBe("bigint");
});

it("should get flowrate with forwarder", async () => {
const flowrate = await cfav1Forwarder.methods
.getFlowrate(token, sender, receiver)
.call();
expect(flowrate).to.be.a("bigint");
expect(typeof flowrate).toBe("bigint");
});

it("should get flow info with cfav1", async () => {
const { timestamp, flowRate } = await cfav1.methods
.getFlow(token, sender, receiver)
.call();
expect(timestamp).to.be.a("bigint");
expect(flowRate).to.be.a("bigint");
expect(typeof timestamp).toBe("bigint");
expect(typeof flowRate).toBe("bigint");
});

it("should get token IDA subscription details with idav1", async () => {
Expand All @@ -100,10 +120,9 @@ describe("SuperfluidPlugin Method Tests", () => {
const { units, exist, approved } = await idav1.methods
.getSubscription(token, publisher, indexId, subscriber)
.call();
// expect to have units, exist, approved
expect(units).to.be.a("bigint");
expect(exist).to.be.a("boolean");
expect(approved).to.be.a("boolean");
expect(typeof units).toBe("bigint");
expect(typeof exist).toBe("boolean");
expect(typeof approved).toBe("boolean");
});

it("should get token IDA index details with idav1", async () => {
Expand All @@ -114,17 +133,16 @@ describe("SuperfluidPlugin Method Tests", () => {

const { exist, indexValue, totalUnitsApproved, totalUnitsPending } =
await idav1.methods.getIndex(token, publisher, indexId).call();
// expect to have indexValue, exist, totalUnitsApproved, totalUnitsPending
expect(indexValue).to.be.a("bigint");
expect(exist).to.be.a("boolean");
expect(totalUnitsApproved).to.be.a("bigint");
expect(totalUnitsPending).to.be.a("bigint");
expect(typeof indexValue).toBe("bigint");
expect(typeof exist).toBe("boolean");
expect(typeof totalUnitsApproved).toBe("bigint");
expect(typeof totalUnitsPending).toBe("bigint");
});

it("should check if address is trusted forwarder using host", async () => {
const result = await host.methods
.isTrustedForwarder(cfav1ForwarderAddress)
.call();
expect(result).to.be.true;
expect(result).toBe(true);
});
});

0 comments on commit e6c00f5

Please sign in to comment.