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

Include js tests for MonsterMaker contract #5

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

flow.json
cadence/lib/js/test/node_modules
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: test
test:
$(MAKE) test -C cadence/lib/js/test

.PHONY: ci
ci:
$(MAKE) ci -C cadence/lib/js/test
6 changes: 6 additions & 0 deletions cadence/lib/js/test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.PHONY: test
test:
npm test

.PHONY: ci
ci: test
96 changes: 96 additions & 0 deletions cadence/lib/js/test/NO_template_test.test copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import path from "path";
import {
emulator,
init,
getAccountAddress,
deployContractByName,
sendTransaction,
shallPass,
shallRevert,
executeScript,
mintFlow
} from "@onflow/flow-js-testing";
import fs from "fs";


// Auxiliary function for deploying the cadence contracts
async function deployContract(param) {
const [result, error] = await deployContractByName(param);
if (error != null) {
console.log(`Error in deployment - ${error}`);
emulator.stop();
process.exit(1);
}
}

const script_file_name = fs.readFileSync(path.resolve(__dirname, "script/path/filename.cdc"), {encoding:'utf8', flag:'r'});


describe("Test suite name", ()=>{

// define some variables for any account you need
let serviceAccount;
let gameAccount;
let parentAccount;

// Before each test...
beforeEach(async () => {
// We do some scaffolding...

// Getting the base path of the project
const basePath = path.resolve(__dirname, "./../../../");
// You can specify different port to parallelize execution of describe blocks
const port = 8080;
// Setting logging flag to true will pipe emulator output to console
const logging = false;

await init(basePath);
await emulator.start({ logging });

// ...then we deploy the ft and example token contracts using the getAccountAddress function
// from the flow-js-testing library...

// Create a service account and deploy contracts to it
serviceAccount = await getAccountAddress("ServiceAccount")
//if you were to use more accounts get their addresses here

//no need to over fund the account except if you are gonna deploy a lot of contracts to it
await mintFlow(serviceAccount, 10000000.0)
await deployContract({ to: serviceAccount, name: "utility/FungibleToken"});



});

// After each test we stop the emulator, so it could be restarted
afterEach(async () => {
return emulator.stop();
});

// First test checks if service account can create a orphan child account
test("First test", async () => {
// First step: create a child creator
const tx_result = await shallPass(
sendTransaction({
name: "transaction/path/from/base/path/previously/defined/without dot cdc",
args: [],
signers: []
})
);

const scriptResult = await executeScript({
code: get_child_address_from_creator,
args: [gameAccount, pubKey]
});

//Getting a tx result into a constant is only needed if you are gonna check something (with scripts usually that's the only reason to call them)

expect(tx_result).not.toBe(null)

expect(scriptResult).toEqual(3)

//this functions belong to jest library

});

});
12 changes: 12 additions & 0 deletions cadence/lib/js/test/babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
]
}
6 changes: 6 additions & 0 deletions cadence/lib/js/test/jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"testEnvironment": "node",
"verbose": true,
"coveragePathIgnorePatterns": ["/node_modules/"],
"testTimeout": 100000
}
25 changes: 25 additions & 0 deletions cadence/lib/js/test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"files": [
"../../../contracts/*",
"../../../transactions/*",
"../../../scripts/*"
],
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.18.0",
"@babel/preset-env": "^7.18.0",
"babel-jest": "^28.1.0",
"@onflow/flow-js-testing": "0.3.0-alpha.13",
"jest": "^28.1.0",
"jest-environment-node": "^28.1.0"
}
}
93 changes: 93 additions & 0 deletions cadence/lib/js/test/template_test.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import path from "path";
import {
emulator,
init,
getAccountAddress,
deployContractByName,
sendTransaction,
shallPass,
shallRevert,
executeScript,
mintFlow
} from "@onflow/flow-js-testing";
import fs from "fs";

import {
mintMonster
} from ".lib/js/test/templates/transaction_templates"

// Auxiliary function for deploying the cadence contracts
async function deployContract(param) {
const [result, error] = await deployContractByName(param);
if (error != null) {
console.log(`Error in deployment - ${error}`);
emulator.stop();
process.exit(1);
}
}

const script_file_name = fs.readFileSync(path.resolve(__dirname, "script/path/filename.cdc"), {encoding:'utf8', flag:'r'});


describe("Test suite name", ()=>{

// define some variables for any account you need
let serviceAccount;
let gameAccount;
let parentAccount;

// Before each test...
beforeEach(async () => {
// We do some scaffolding...

// Getting the base path of the project
const basePath = path.resolve(__dirname, "./../../../");
// You can specify different port to parallelize execution of describe blocks
const port = 8080;
// Setting logging flag to true will pipe emulator output to console
const logging = false;

await init(basePath);
await emulator.start({ logging });

// ...then we deploy the ft and example token contracts using the getAccountAddress function
// from the flow-js-testing library...

// Create a service account and deploy contracts to it
serviceAccount = await getAccountAddress("ServiceAccount")
//if you were to use more accounts get their addresses here

//no need to over fund the account except if you are gonna deploy a lot of contracts to it
await mintFlow(serviceAccount, 10000000.0)
await deployContract({ to: serviceAccount, name: "utility/FungibleToken"});



});

// After each test we stop the emulator, so it could be restarted
afterEach(async () => {
return emulator.stop();
});

// First test checks if service account can create a orphan child account
test("First test", async () => {
// First step: create a child creator
const tx_result = await mintMonster()

const scriptResult = await executeScript({
code: get_child_address_from_creator,
args: [gameAccount, pubKey]
});

//Getting a tx result into a constant is only needed if you are gonna check something (with scripts usually that's the only reason to call them)

expect(tx_result).not.toBe(null)

expect(scriptResult).toEqual(3)

//this functions belong to jest library

});

});
29 changes: 29 additions & 0 deletions cadence/lib/js/test/templates/assertion_templates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect } from "@jest/globals";
import { executeScript } from "@onflow/flow-js-testing";
import { getCollectionIDs } from "./script_templates";

// Asserts whether length of account's collection matches
// the expected collection length
export async function assertCollectionLength(account, expectedCollectionLength) {
const [collectionIDs, e] = await executeScript(
"game_piece_nft/get_collection_ids",
[account]
);
expect(e).toBeNull();
expect(collectionIDs.length).toBe(expectedCollectionLength);
};

// Asserts that total supply of ExampleNFT matches passed expected total supply
export async function assertTotalSupply(expectedTotalSupply) {
const [actualTotalSupply, e] = await executeScript(
"get_total_supply"
);
expect(e).toBeNull();
expect(actualTotalSupply).toBe(expectedTotalSupply.toString());
};

// Asserts whether the NFT corresponding to the id is in address's collection
export async function assertNFTInCollection(address, id) {
const ids = await getCollectionIDs(address);
expect(ids.includes(id.toString())).toBe(true);
};
13 changes: 13 additions & 0 deletions cadence/lib/js/test/templates/script_templates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect } from "@jest/globals";
import { executeScript } from "@onflow/flow-js-testing";

// Executes get_collection_ids script with passed params,
// returning array of NFT IDs contained in the address's collection
export async function getCollectionIDs(address) {
const [result, err] = await executeScript(
"game_piece_nft/get_collection_ids",
[address]
);
expect(err).toBeNull();
return result;
};
13 changes: 13 additions & 0 deletions cadence/lib/js/test/templates/transaction_templates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {
sendTransaction,
shallPass,
shallRevert
} from "@onflow/flow-js-testing";


export async function mintMonster(/*accounts and tx arguments used should go here*/) {
const [txn, e] = await shallPass(
sendTransaction("mint_monster", [signer], [])
);
};

1 change: 1 addition & 0 deletions cadence/transactions/mint_monster.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ transaction(
MonsterMaker.totalSupply == self.mintingIDBefore + 1: "The total supply should have been increased by 1"
}
}