counter_anchor.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { Keypair } from "@solana/web3.js";
  4. import { assert } from "chai";
  5. import { CounterAnchor } from "../target/types/counter_anchor";
  6. describe("counter_anchor", () => {
  7. // Configure the client to use the local cluster.
  8. anchor.setProvider(anchor.AnchorProvider.env());
  9. const program = anchor.workspace.CounterAnchor as Program<CounterAnchor>;
  10. it("Test increment", async () => {
  11. const counterKeypair = Keypair.generate();
  12. const counter = counterKeypair.publicKey;
  13. // Initialize counter
  14. await program.methods
  15. .initializeCounter()
  16. .accounts({ counter, payer: program.provider.publicKey })
  17. .signers([counterKeypair])
  18. .rpc({ skipPreflight: true, commitment: "confirmed" });
  19. let currentCount = (
  20. await program.account.counter.fetch(counter, "confirmed")
  21. ).count.toNumber();
  22. assert(currentCount === 0, "Expected initialized count to be 0");
  23. // Increment counter
  24. await program.methods
  25. .increment()
  26. .accounts({ counter })
  27. .rpc({ skipPreflight: true, commitment: "confirmed" });
  28. currentCount = (
  29. await program.account.counter.fetch(counter, "confirmed")
  30. ).count.toNumber();
  31. assert(currentCount === 1, "Expected count to be 1");
  32. // Increment counter
  33. await program.methods
  34. .increment()
  35. .accounts({ counter })
  36. .rpc({ skipPreflight: true, commitment: "confirmed" });
  37. currentCount = (
  38. await program.account.counter.fetch(counter, "confirmed")
  39. ).count.toNumber();
  40. assert(currentCount === 2, "Expected count to be 2");
  41. });
  42. });