Skip to content

Commit

Permalink
feat(test): better example test and contract
Browse files Browse the repository at this point in the history
  • Loading branch information
thepiwo committed Feb 7, 2022
1 parent 0c6d010 commit f98e1df
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src/init/artifacts/contracts/ExampleContract.aes
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
@compiler >= 6

main contract Example =
entrypoint example(x : int) = x
type state = option(int)

datatype event = SetXEvent(address, int)
entrypoint init() : state = None

stateful entrypoint set(x : int) : unit =
Chain.event(SetXEvent(Call.caller, x))
put(Some(x))

entrypoint get() : option(int) = state
20 changes: 15 additions & 5 deletions src/init/artifacts/test/exampleTests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { assert } = require('chai');
const { utils } = require('@aeternity/aeproject');
const { utils, wallets } = require('@aeternity/aeproject');

const EXAMPLE_CONTRACT_SOURCE = './contracts/ExampleContract.aes';

Expand All @@ -14,10 +14,10 @@ describe('ExampleContract', () => {
const filesystem = utils.getFilesystem(EXAMPLE_CONTRACT_SOURCE);

// get content of contract
const contract_content = utils.getContractContent(EXAMPLE_CONTRACT_SOURCE);
const source = utils.getContractContent(EXAMPLE_CONTRACT_SOURCE);

// initialize the contract instance
contract = await client.getContractInstance(contract_content, { filesystem });
contract = await client.getContractInstance(source, { filesystem });
await contract.deploy();

// create a snapshot of the blockchain state
Expand All @@ -29,8 +29,18 @@ describe('ExampleContract', () => {
await utils.rollbackSnapshot(client);
});

it('call ExampleContract', async () => {
const { decodedResult } = await contract.methods.example(42);
it('ExampleContract: set and get', async () => {
const set = await contract.methods.set(42);
assert.equal(set.decodedEvents[0].name, 'SetXEvent')
assert.equal(set.decodedEvents[0].decoded[0], wallets[0].publicKey);
assert.equal(set.decodedEvents[0].decoded[1], 42);

const { decodedResult } = await contract.methods.get();
assert.equal(decodedResult, 42);
});

it('ExampleContract: get undefined when not set before', async () => {
const { decodedResult } = await contract.methods.get();
assert.equal(decodedResult, undefined);
});
});

0 comments on commit f98e1df

Please sign in to comment.