counter_anchor.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import * as anchor from '@coral-xyz/anchor';
  2. import type { Program } from '@coral-xyz/anchor';
  3. import { Keypair } from '@solana/web3.js';
  4. import { assert } from 'chai';
  5. import type { 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(counterKeypair.publicKey);
  24. assert(currentCount.count.toNumber() === 0, 'Expected initialized count to be 0');
  25. });
  26. it('Increment Counter', async () => {
  27. await program.methods.increment().accounts({ counter: counterKeypair.publicKey }).rpc();
  28. const currentCount = await program.account.counter.fetch(counterKeypair.publicKey);
  29. assert(currentCount.count.toNumber() === 1, 'Expected count to be 1');
  30. });
  31. it('Increment Counter Again', async () => {
  32. await program.methods.increment().accounts({ counter: counterKeypair.publicKey }).rpc();
  33. const currentCount = await program.account.counter.fetch(counterKeypair.publicKey);
  34. assert(currentCount.count.toNumber() === 2, 'Expected count to be 2');
  35. });
  36. });