bankrun.test.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { describe, it } from 'node:test';
  2. import * as anchor from '@coral-xyz/anchor';
  3. import { PublicKey, Transaction } from '@solana/web3.js';
  4. import { BankrunProvider } from 'anchor-bankrun';
  5. import { startAnchor } from 'solana-bankrun';
  6. import type { AnchorProgramExample } from '../target/types/anchor_program_example';
  7. const IDL = require('../target/idl/anchor_program_example.json');
  8. const PROGRAM_ID = new PublicKey(IDL.address);
  9. describe('PDAs', async () => {
  10. const context = await startAnchor('', [{ name: 'anchor_program_example', programId: PROGRAM_ID }], []);
  11. const provider = new BankrunProvider(context);
  12. const client = context.banksClient;
  13. const payer = provider.wallet as anchor.Wallet;
  14. const program = new anchor.Program<AnchorProgramExample>(IDL, provider);
  15. // PDA for the page visits account
  16. const [pageVisitPDA] = PublicKey.findProgramAddressSync([Buffer.from('page_visits'), payer.publicKey.toBuffer()], program.programId);
  17. it('Create the page visits tracking PDA', async () => {
  18. await program.methods
  19. .createPageVisits()
  20. .accounts({
  21. payer: payer.publicKey,
  22. })
  23. .rpc();
  24. });
  25. it('Visit the page!', async () => {
  26. await program.methods
  27. .incrementPageVisits()
  28. .accounts({
  29. user: payer.publicKey,
  30. })
  31. .rpc();
  32. });
  33. it('Visit the page!', async () => {
  34. await program.methods
  35. .incrementPageVisits()
  36. .accounts({
  37. user: payer.publicKey,
  38. })
  39. .rpc();
  40. });
  41. it('View page visits', async () => {
  42. const pageVisits = await program.account.pageVisits.fetch(pageVisitPDA);
  43. console.log(`Number of page visits: ${pageVisits.pageVisits}`);
  44. });
  45. });