Skip to content

Commit 473cf4f

Browse files
committed
Step 7: writing the tests
1 parent 4c4ca5b commit 473cf4f

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

tests/solana-hello-world.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,62 @@ describe("solana-hello-world", () => {
99

1010
const program = anchor.workspace
1111
.SolanaHelloWorld as Program<SolanaHelloWorld>;
12+
13+
it("Can create a message", async () => {
14+
const message = anchor.web3.Keypair.generate();
15+
const messageContent = "Hello World!";
16+
await program.rpc.createMessage(messageContent, {
17+
accounts: {
18+
message: message.publicKey,
19+
author: provider.wallet.publicKey,
20+
systemProgram: anchor.web3.SystemProgram.programId,
21+
},
22+
signers: [message],
23+
});
24+
25+
const messageAccount = await program.account.message.fetch(
26+
message.publicKey
27+
);
28+
29+
assert.equal(
30+
messageAccount.author.toBase58(),
31+
provider.wallet.publicKey.toBase58()
32+
);
33+
assert.equal(messageAccount.content, messageContent);
34+
assert.ok(messageAccount.timestamp);
35+
});
36+
37+
it("Can create and then update a message", async () => {
38+
const message = anchor.web3.Keypair.generate();
39+
const messageContent = "Hello World!";
40+
await program.rpc.createMessage(messageContent, {
41+
accounts: {
42+
message: message.publicKey,
43+
author: provider.wallet.publicKey,
44+
systemProgram: anchor.web3.SystemProgram.programId,
45+
},
46+
signers: [message],
47+
});
48+
49+
const updatedMessageContent = "Solana is cool!";
50+
await program.rpc.updateMessage(updatedMessageContent, {
51+
accounts: {
52+
message: message.publicKey,
53+
author: provider.wallet.publicKey,
54+
systemProgram: anchor.web3.SystemProgram.programId,
55+
},
56+
});
57+
58+
const messageAccount = await program.account.message.fetch(
59+
message.publicKey
60+
);
61+
62+
assert.equal(
63+
messageAccount.author.toBase58(),
64+
provider.wallet.publicKey.toBase58()
65+
);
66+
assert.notEqual(messageAccount.content, messageContent);
67+
assert.equal(messageAccount.content, updatedMessageContent);
68+
assert.ok(messageAccount.timestamp);
69+
});
1270
});

0 commit comments

Comments
 (0)