Skip to content

Commit

Permalink
near-bingen tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
volovyks committed Sep 1, 2022
1 parent b449518 commit a6e4341
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 3 deletions.
59 changes: 59 additions & 0 deletions tests/__tests__/near_bindgen.ava.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Worker } from 'near-workspaces';
import test from 'ava';

test.beforeEach(async t => {
const worker = await Worker.init();
const root = worker.rootAccount;

const reqireInitFalse = await root.devDeploy('build/require_init_false.wasm');
const reqireInitTrue = await root.devDeploy('build/require_init_true.wasm');

const ali = await root.createSubAccount('ali');

t.context.worker = worker;
t.context.accounts = {
root,
reqireInitFalse,
reqireInitTrue,
ali,
};
});

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

test('Uninitialized contract throw error if requireInit = true', async t => {
const { ali, reqireInitTrue } = t.context.accounts;

const callResult = await ali.callRaw(reqireInitTrue, 'setStatus', { status: 'hello' });
t.assert(callResult.result.status.Failure.ActionError.kind.FunctionCallError.ExecutionError.includes('Contract must be initialized'));

const err = await t.throwsAsync(() => reqireInitTrue.view('getStatus', {}));
t.assert(err.message.includes('Contract must be initialized'));
})

test('Uninitialized contract does not throw error if requireInit = false', async t => {
const { ali, reqireInitFalse } = t.context.accounts;

await ali.callRaw(reqireInitFalse, 'setStatus', { status: 'hello' });

t.is(await reqireInitFalse.view('getStatus', {}), 'hello')

})

test('Init function panics if called more then once', async t => {
const { ali, reqireInitTrue, reqireInitFalse } = t.context.accounts;

await ali.call(reqireInitTrue, 'init', { status: 'hello' });
const res1 = await ali.callRaw(reqireInitTrue, 'init', { status: 'hello' });
t.assert(res1.result.status.Failure.ActionError.kind.FunctionCallError.ExecutionError.includes('Contract already initialized'));


await ali.call(reqireInitFalse, 'init', { status: 'hello' });
const res2 = await ali.callRaw(reqireInitFalse, 'init', { status: 'hello' });
t.assert(res2.result.status.Failure.ActionError.kind.FunctionCallError.ExecutionError.includes('Contract already initialized'));
})

3 changes: 2 additions & 1 deletion tests/babel.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"plugins": [
"near-sdk-js/lib/build-tools/near-bindgen-exporter",
["@babel/plugin-proposal-decorators", {"version": "legacy"}]
]
],
"presets": ["@babel/preset-typescript"]
}
6 changes: 4 additions & 2 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"scripts": {
"postinstall": "cd .. && yarn link && cd tests && yarn link near-sdk-js",
"build": "yarn build:context-api && yarn build:math-api && yarn build:storage-api && yarn build:log-panic-api && yarn build:promise-api && yarn build:promise-batch-api && yarn build:function-params && yarn build:lookup-map && yarn build:lookup-set && yarn build:unordered-map && yarn build:unordered-set && yarn build:vector && yarn build:bytes && yarn build:typescript && yarn build:public-key",
"build": "yarn build:context-api && yarn build:math-api && yarn build:storage-api && yarn build:log-panic-api && yarn build:promise-api && yarn build:promise-batch-api && yarn build:function-params && yarn build:lookup-map && yarn build:lookup-set && yarn build:unordered-map && yarn build:unordered-set && yarn build:vector && yarn build:bytes && yarn build:typescript && yarn build:public-key && yarn build:near-bindgen",
"build:context-api": "near-sdk-js build src/context_api.js build/context_api.wasm",
"build:math-api": "near-sdk-js build src/math_api.js build/math_api.wasm",
"build:storage-api": "near-sdk-js build src/storage_api.js build/storage_api.wasm",
Expand All @@ -22,6 +22,7 @@
"build:bytes": "near-sdk-js build src/bytes.js build/bytes.wasm",
"build:typescript": "near-sdk-js build src/typescript.ts build/typescript.wasm",
"build:public-key": "near-sdk-js build src/public-key.js build/public-key.wasm",
"build:near-bindgen": "near-sdk-js build src/near_bindgen/require_init_true.ts build/require_init_true.wasm && near-sdk-js build src/near_bindgen/require_init_false.ts build/require_init_false.wasm",
"test": "ava",
"test:context-api": "ava __tests__/test_context_api.ava.js",
"test:math-api": "ava __tests__/test_math_api.ava.js",
Expand All @@ -36,7 +37,8 @@
"test:vector": "ava __tests__/vector.ava.js",
"test:bytes": "ava __tests__/bytes.ava.js",
"test:typescript": "ava __tests__/typescript.ava.js",
"test:public-key": "ava __tests__/test-public-key.ava.js"
"test:public-key": "ava __tests__/test-public-key.ava.js",
"test:near-bindgen": "ava __tests__/near_bindgen.ava.js"
},
"author": "Near Inc <[email protected]>",
"license": "Apache-2.0",
Expand Down
34 changes: 34 additions & 0 deletions tests/src/near_bindgen/require_init_false.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
near,
NearBindgen,
call,
view,
initialize,
} from 'near-sdk-js'

@NearBindgen({ requireInit: false })
class NBTest {
status: string;

constructor() {
this.status = '';
}

@initialize
init({ status }: { status: string }): void {
near.log(`init: ${status}`)
this.status = status;
}

@view
getStatus(): string {
near.log(`getStatus: ${this.status}`)
return this.status;
}

@call
setStatus({ status }: { status: string }): void {
near.log(`setStatus: ${status}`)
this.status = status;
}
}
36 changes: 36 additions & 0 deletions tests/src/near_bindgen/require_init_true.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
near,
NearBindgen,
call,
view,
initialize,
} from 'near-sdk-js'

@NearBindgen({ requireInit: true })
class NBTest {
status: string;

constructor() {
this.status = '';
}

@initialize
init({ status }: { status: string }): void {
near.log(`init: ${status}`)
this.status = status;
}

@view
getStatus(): string {
near.log(`getStatus: ${this.status}`)
return this.status;
}

@call
setStatus({ status }: { status: string }): void {
near.log(`setStatus: ${status}`)
this.status = status;
}
}


0 comments on commit a6e4341

Please sign in to comment.