counter_anchor.ts 1.6 KB

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