basic-4.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const assert = require("assert");
  2. const anchor = require("@coral-xyz/anchor");
  3. describe("basic-4", () => {
  4. const provider = anchor.AnchorProvider.local();
  5. // Configure the client to use the local cluster.
  6. anchor.setProvider(provider);
  7. const program = anchor.workspace.Basic4,
  8. counterSeed = anchor.utils.bytes.utf8.encode("counter");
  9. let counterPubkey;
  10. before(async () => {
  11. [counterPubkey] = await anchor.web3.PublicKey.findProgramAddress(
  12. [counterSeed],
  13. program.programId
  14. );
  15. });
  16. it("Is runs the constructor", async () => {
  17. // Initialize the program's state struct.
  18. await program.methods
  19. .initialize()
  20. .accounts({
  21. counter: counterPubkey,
  22. authority: provider.wallet.publicKey,
  23. systemProgram: anchor.web3.SystemProgram.programId,
  24. })
  25. .rpc();
  26. // Fetch the state struct from the network.
  27. const counterAccount = await program.account.counter.fetch(counterPubkey);
  28. assert.ok(counterAccount.count.eq(new anchor.BN(0)));
  29. });
  30. it("Executes a method on the program", async () => {
  31. await program.methods
  32. .increment()
  33. .accounts({
  34. counter: counterPubkey,
  35. authority: provider.wallet.publicKey,
  36. })
  37. .rpc();
  38. const counterAccount = await program.account.counter.fetch(counterPubkey);
  39. assert.ok(counterAccount.count.eq(new anchor.BN(1)));
  40. });
  41. });