anchor/examples/tutorial/basic-2/tests/basic-2.js

49 lines
1.3 KiB
JavaScript
Raw Normal View History

const assert = require("assert");
const anchor = require("@project-serum/anchor");
const { SystemProgram } = anchor.web3;
describe("basic-2", () => {
const provider = anchor.AnchorProvider.local();
2021-03-01 01:25:59 -08:00
// Configure the client to use the local cluster.
anchor.setProvider(provider);
2021-03-01 01:25:59 -08:00
// Counter for the tests.
const counter = anchor.web3.Keypair.generate();
2021-03-01 01:25:59 -08:00
// Program for the tests.
const program = anchor.workspace.Basic2;
2021-03-01 01:25:59 -08:00
it("Creates a counter", async () => {
await program.rpc.create(provider.wallet.publicKey, {
accounts: {
2021-03-01 01:25:59 -08:00
counter: counter.publicKey,
user: provider.wallet.publicKey,
systemProgram: SystemProgram.programId,
},
signers: [counter],
});
2021-03-01 01:25:59 -08:00
let counterAccount = await program.account.counter.fetch(counter.publicKey);
2021-03-01 01:25:59 -08:00
assert.ok(counterAccount.authority.equals(provider.wallet.publicKey));
assert.ok(counterAccount.count.toNumber() === 0);
});
2021-03-01 01:25:59 -08:00
it("Updates a counter", async () => {
await program.rpc.increment({
accounts: {
2021-03-01 01:25:59 -08:00
counter: counter.publicKey,
authority: provider.wallet.publicKey,
},
});
2021-03-01 01:25:59 -08:00
const counterAccount = await program.account.counter.fetch(
counter.publicKey
);
2021-03-01 01:25:59 -08:00
assert.ok(counterAccount.authority.equals(provider.wallet.publicKey));
assert.ok(counterAccount.count.toNumber() == 1);
});
});