From 987f1b08cbffd94a317b2473a2b368e8b376fcd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Lillo=20Igualada?= Date: Fri, 27 Jan 2023 20:43:41 +0100 Subject: [PATCH] Create test template --- .gitignore | 2 +- Makefile | 7 ++ cadence/lib/js/test/Makefile | 6 ++ .../lib/js/test/NO_template_test.test copy.js | 96 +++++++++++++++++++ cadence/lib/js/test/babel.config.json | 12 +++ cadence/lib/js/test/jest.config.json | 6 ++ cadence/lib/js/test/package.json | 25 +++++ cadence/lib/js/test/template_test.test.js | 93 ++++++++++++++++++ .../js/test/templates/assertion_templates.js | 29 ++++++ .../lib/js/test/templates/script_templates.js | 13 +++ .../test/templates/transaction_templates.js | 13 +++ cadence/transactions/mint_monster.cdc | 1 + 12 files changed, 302 insertions(+), 1 deletion(-) create mode 100644 Makefile create mode 100644 cadence/lib/js/test/Makefile create mode 100644 cadence/lib/js/test/NO_template_test.test copy.js create mode 100644 cadence/lib/js/test/babel.config.json create mode 100644 cadence/lib/js/test/jest.config.json create mode 100644 cadence/lib/js/test/package.json create mode 100644 cadence/lib/js/test/template_test.test.js create mode 100644 cadence/lib/js/test/templates/assertion_templates.js create mode 100644 cadence/lib/js/test/templates/script_templates.js create mode 100644 cadence/lib/js/test/templates/transaction_templates.js diff --git a/.gitignore b/.gitignore index ee72142..5641a45 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ - flow.json +cadence/lib/js/test/node_modules \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ecefd08 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +.PHONY: test +test: + $(MAKE) test -C cadence/lib/js/test + +.PHONY: ci +ci: + $(MAKE) ci -C cadence/lib/js/test diff --git a/cadence/lib/js/test/Makefile b/cadence/lib/js/test/Makefile new file mode 100644 index 0000000..e263380 --- /dev/null +++ b/cadence/lib/js/test/Makefile @@ -0,0 +1,6 @@ +.PHONY: test +test: + npm test + +.PHONY: ci +ci: test \ No newline at end of file diff --git a/cadence/lib/js/test/NO_template_test.test copy.js b/cadence/lib/js/test/NO_template_test.test copy.js new file mode 100644 index 0000000..3eaa7dd --- /dev/null +++ b/cadence/lib/js/test/NO_template_test.test copy.js @@ -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 + + }); + +}); \ No newline at end of file diff --git a/cadence/lib/js/test/babel.config.json b/cadence/lib/js/test/babel.config.json new file mode 100644 index 0000000..394c543 --- /dev/null +++ b/cadence/lib/js/test/babel.config.json @@ -0,0 +1,12 @@ +{ + "presets": [ + [ + "@babel/preset-env", + { + "targets": { + "node": "current" + } + } + ] + ] +} diff --git a/cadence/lib/js/test/jest.config.json b/cadence/lib/js/test/jest.config.json new file mode 100644 index 0000000..bbbc3ff --- /dev/null +++ b/cadence/lib/js/test/jest.config.json @@ -0,0 +1,6 @@ +{ + "testEnvironment": "node", + "verbose": true, + "coveragePathIgnorePatterns": ["/node_modules/"], + "testTimeout": 100000 +} \ No newline at end of file diff --git a/cadence/lib/js/test/package.json b/cadence/lib/js/test/package.json new file mode 100644 index 0000000..52cf1b7 --- /dev/null +++ b/cadence/lib/js/test/package.json @@ -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" + } +} diff --git a/cadence/lib/js/test/template_test.test.js b/cadence/lib/js/test/template_test.test.js new file mode 100644 index 0000000..1dcb018 --- /dev/null +++ b/cadence/lib/js/test/template_test.test.js @@ -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 + + }); + +}); \ No newline at end of file diff --git a/cadence/lib/js/test/templates/assertion_templates.js b/cadence/lib/js/test/templates/assertion_templates.js new file mode 100644 index 0000000..ca96acc --- /dev/null +++ b/cadence/lib/js/test/templates/assertion_templates.js @@ -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); +}; diff --git a/cadence/lib/js/test/templates/script_templates.js b/cadence/lib/js/test/templates/script_templates.js new file mode 100644 index 0000000..664dfe0 --- /dev/null +++ b/cadence/lib/js/test/templates/script_templates.js @@ -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; +}; \ No newline at end of file diff --git a/cadence/lib/js/test/templates/transaction_templates.js b/cadence/lib/js/test/templates/transaction_templates.js new file mode 100644 index 0000000..1f0bbb4 --- /dev/null +++ b/cadence/lib/js/test/templates/transaction_templates.js @@ -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], []) + ); +}; + diff --git a/cadence/transactions/mint_monster.cdc b/cadence/transactions/mint_monster.cdc index 29d6775..960881e 100644 --- a/cadence/transactions/mint_monster.cdc +++ b/cadence/transactions/mint_monster.cdc @@ -58,3 +58,4 @@ transaction( MonsterMaker.totalSupply == self.mintingIDBefore + 1: "The total supply should have been increased by 1" } } + \ No newline at end of file