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

Unit tests setup added (workspaces approach) #62

Merged
merged 1 commit into from
May 27, 2022
Merged
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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
],
"license": "(MIT AND Apache-2.0)",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 0"
"test": "cd tests && yarn && yarn build && yarn test && cd .."
},
"bin": {
"near-sdk": "cli/cli.js"
Expand Down
2 changes: 2 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
build
58 changes: 58 additions & 0 deletions tests/__tests__/main.ava.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Worker } from 'near-workspaces';
import { readFile } from 'fs/promises'
import test from 'ava';

function encodeCall(contract, method, args) {
return Buffer.concat([Buffer.from(contract), Buffer.from([0]), Buffer.from(method), Buffer.from([0]), Buffer.from(JSON.stringify(args))])
}

test.beforeEach(async t => {
// Init the worker and start a Sandbox server
const worker = await Worker.init();

// Prepare sandbox for tests, create accounts, deploy contracts, etx.
const root = worker.rootAccount;

// Deploy the jsvm contract.
const jsvm = await root.createAndDeploy(
root.getSubAccount('jsvm').accountId,
'../res/jsvm.wasm',
);

// Deploy test JS contract
const testContract = await root.createSubAccount('test-contract');
let contract_base64 = (await readFile('build/contract.base64')).toString();
await testContract.call(jsvm, 'deploy_js_contract', Buffer.from(contract_base64, 'base64'), { attachedDeposit: '400000000000000000000000' });
await testContract.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'init', []), { attachedDeposit: '400000000000000000000000' });

// Test users
const ali = await root.createSubAccount('ali');
const bob = await root.createSubAccount('bob');
const carl = await root.createSubAccount('carl');

// Save state for test runs
t.context.worker = worker;
t.context.accounts = { root, jsvm, testContract, ali, bob, carl };
});

test.afterEach(async t => {
await t.context.worker.tearDown().catch(error => {
console.log('Failed to tear down the worker:', error);
});
});

test('UnorderedMap is empty by default', async t => {
const { root, jsvm, testContract } = t.context.accounts;
const result = await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'len', [root.accountId]));
t.is(result, 0);
});

test('UnorderedMap set() get()', async t => {
const { ali, jsvm, testContract } = t.context.accounts;
await ali.call(jsvm, 'call_js_contract', encodeCall(testContract.accountId, 'set', ['hello', 'world']), { attachedDeposit: '100000000000000000000000' });

t.is(
await jsvm.view('view_js_contract', encodeCall(testContract.accountId, 'get', ['hello'])),
'world'
);
});
8 changes: 8 additions & 0 deletions tests/ava.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require('util').inspect.defaultOptions.depth = 5; // Increase AVA's printing depth

module.exports = {
timeout: '300000',
files: ['**/*.ava.js'],
failWithoutAssertions: false,
extensions: ['js'],
};
6 changes: 6 additions & 0 deletions tests/babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": [
"near-sdk-js/src/build-tools/near-bindgen-exporter",
["@babel/plugin-proposal-decorators", {"version": "legacy"}]
]
}
20 changes: 20 additions & 0 deletions tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "tests",
"version": "1.0.0",
"description": "near-sdk-js tests",
"main": "index.js",
"type": "module",
"scripts": {
"build": "near-sdk build",
"test": "ava"
},
"author": "Near Inc <hello@nearprotocol.com>",
"license": "Apache-2.0",
"dependencies": {
"near-sdk-js": "file:../"
},
"devDependencies": {
"ava": "^4.2.0",
"near-workspaces": "^2.0.0"
}
}
79 changes: 79 additions & 0 deletions tests/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
NearContract,
NearBindgen,
call,
view,
UnorderedMap,
Vector
} from 'near-sdk-js'

@NearBindgen
class UnorderedMapTestContract extends NearContract {
constructor() {
super()
this.unorderedMap = new UnorderedMap('a');
}

deserialize() {
super.deserialize()
this.unorderedMap.keys = Object.assign(new Vector, this.unorderedMap.keys)
this.unorderedMap.values = Object.assign(new Vector, this.unorderedMap.values)
this.unorderedMap = Object.assign(new UnorderedMap, this.unorderedMap)
}

@view
len() {
return this.unorderedMap.len();
}

@view
isEmpty() {
return this.unorderedMap.isEmpty();
}

@view
serializeIndex(index) {
return this.unorderedMap.serializeIndex(index);
}

@view
deserializeIndex(rawIndex) {
return this.unorderedMap.deserializeIndex(rawIndex);
}

@view
getIndexRaw(key) {
return this.unorderedMap.getIndexRaw(key);
}

@view
get(key) {
return this.unorderedMap.get(key);
}

@call
set(key, value) {
this.unorderedMap.set(key, value);
}

@call
remove(key) {
this.unorderedMap.remove(key);
}

@call
clear() {
this.unorderedMap.clear();
}

@view
toArray() {
this.unorderedMap.toArray();
}

@view
extend(kvs) {
this.unorderedMap.extend(kvs);
}
}

Loading