Skip to content

Commit 9943e86

Browse files
committed
add bankrun test
1 parent 3697146 commit 9943e86

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import * as anchor from "@coral-xyz/anchor";
2+
import { Keypair, PublicKey } from "@solana/web3.js";
3+
import { BankrunProvider } from "anchor-bankrun";
4+
import { assert } from "chai";
5+
import { startAnchor } from "solana-bankrun";
6+
import type { CounterProgramPoseidon } from "../target/types/counter_program_poseidon";
7+
8+
const IDL = require("../target/idl/counter_program.json");
9+
const PROGRAM_ID = new PublicKey(IDL.address);
10+
11+
describe("counter_program", async () => {
12+
// Configure the client to use the anchor-bankrun
13+
const context = await startAnchor(
14+
"",
15+
[{ name: "counter_program", programId: PROGRAM_ID }],
16+
[]
17+
);
18+
19+
const provider = new BankrunProvider(context);
20+
21+
const payer = provider.wallet as anchor.Wallet;
22+
23+
const program = new anchor.Program<CounterProgramPoseidon>(IDL, provider);
24+
25+
// Generate a new keypair for the counter account
26+
const counterKeypair = new Keypair();
27+
28+
it("Initialize Counter", async () => {
29+
await program.methods
30+
.initializeCounter()
31+
.accounts({
32+
counter: counterKeypair.publicKey,
33+
payer: payer.publicKey,
34+
systemProgram: anchor.web3.SystemProgram.programId,
35+
})
36+
.rpc();
37+
38+
const currentCount = await program.account.counterState.fetch(
39+
counterKeypair.publicKey
40+
);
41+
42+
assert(
43+
currentCount.count.toNumber() === 0,
44+
"Expected initialized count to be 0"
45+
);
46+
});
47+
48+
it("Increment Counter", async () => {
49+
await program.methods
50+
.incrementCounter()
51+
.accounts({ counter: counterKeypair.publicKey })
52+
.rpc();
53+
54+
const currentCount = await program.account.counterState.fetch(
55+
counterKeypair.publicKey
56+
);
57+
58+
assert(currentCount.count.toNumber() === 1, "Expected count to be 1");
59+
});
60+
61+
it("Decrement Counter", async () => {
62+
await program.methods
63+
.decrementCounter()
64+
.accounts({ counter: counterKeypair.publicKey })
65+
.rpc();
66+
67+
const currentCount = await program.account.counterState.fetch(
68+
counterKeypair.publicKey
69+
);
70+
71+
assert(
72+
currentCount.count.toNumber() === 0,
73+
"Expected count to be 0 after decrement"
74+
);
75+
});
76+
77+
78+
});

0 commit comments

Comments
 (0)