-
Notifications
You must be signed in to change notification settings - Fork 23
/
01.basic-transactions.ava.ts
62 lines (54 loc) · 2.16 KB
/
01.basic-transactions.ava.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* This test demonstrates basic behavior of near-workspaces, making simple
* function calls and view calls to the contract from
* https://github.com/near-examples/rust-status-message
*
* Note that the same tests will be run on both a local sandbox environment and
* on testnet by using the `test:sandbox` and `test:testnet` scripts in
* package.json.
*/
import {Worker, NEAR, NearAccount} from 'near-workspaces';
import anyTest, {TestFn} from 'ava';
const test = anyTest as TestFn<{
worker: Worker;
accounts: Record<string, NearAccount>;
}>;
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;
const contract = await root.devDeploy(
'__tests__/build/debug/status_message.wasm',
{initialBalance: NEAR.parse('3 N').toJSON()},
);
const ali = await root.createSubAccount('ali', {initialBalance: NEAR.parse('3 N').toJSON()});
// Save state for test runs, it is unique for each test
t.context.worker = worker;
t.context.accounts = {root, contract, ali};
});
test.afterEach.always(async t => {
// Stop Sandbox server
await t.context.worker.tearDown().catch(error => {
console.log('Failed to tear down the worker:', error);
});
});
test('Root gets null status', async t => {
const {root, contract} = t.context.accounts;
const result: null = await contract.view('get_status', {account_id: root.accountId});
t.is(result, null);
});
test('Ali sets then gets status', async t => {
const {ali, contract} = t.context.accounts;
await ali.call(contract, 'set_status', {message: 'hello'});
const result: string = await contract.view('get_status', {account_id: ali});
t.is(result, 'hello');
});
test('Root and Ali have different statuses', async t => {
const {root, contract, ali} = t.context.accounts;
await root.call(contract, 'set_status', {message: 'world'});
const rootStatus: string = await contract.view('get_status', {account_id: root});
t.is(rootStatus, 'world');
const aliStatus: null = await contract.view('get_status', {account_id: ali});
t.is(aliStatus, null);
});