counter_anchor.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. const provider = anchor.AnchorProvider.env()
  9. anchor.setProvider(provider)
  10. const payer = provider.wallet as anchor.Wallet
  11. const program = anchor.workspace.CounterAnchor as Program<CounterAnchor>
  12. // Generate a new keypair for the counter account
  13. const counterKeypair = new Keypair()
  14. it("Initialize Counter", async () => {
  15. await program.methods
  16. .initializeCounter()
  17. .accounts({
  18. counter: counterKeypair.publicKey,
  19. payer: payer.publicKey,
  20. })
  21. .signers([counterKeypair])
  22. .rpc()
  23. const currentCount = await program.account.counter.fetch(
  24. counterKeypair.publicKey
  25. )
  26. assert(
  27. currentCount.count.toNumber() === 0,
  28. "Expected initialized count to be 0"
  29. )
  30. })
  31. it("Increment Counter", async () => {
  32. await program.methods
  33. .increment()
  34. .accounts({ counter: counterKeypair.publicKey })
  35. .rpc()
  36. const currentCount = await program.account.counter.fetch(
  37. counterKeypair.publicKey
  38. )
  39. assert(currentCount.count.toNumber() === 1, "Expected count to be 1")
  40. })
  41. it("Increment Counter Again", async () => {
  42. await program.methods
  43. .increment()
  44. .accounts({ counter: counterKeypair.publicKey })
  45. .rpc()
  46. const currentCount = await program.account.counter.fetch(
  47. counterKeypair.publicKey
  48. )
  49. assert(currentCount.count.toNumber() === 2, "Expected count to be 2")
  50. })
  51. })