Testing Clarity Contracts
1
Test donate with a message
it("accepts STX donation with a message", () => {
initCampaign(100000);
const message = "Great project! Happy to support.";
const response = simnet.callPublicFn(
"fundraising",
"donate-stx",
[Cl.uint(5000), Cl.some(Cl.stringUtf8(message))],
donor1,
);
expect(response.result).toBeOk(Cl.bool(true));
// verify donation was recorded
const getDonationResponse = simnet.callReadOnlyFn(
"fundraising",
"get-stx-donation",
[Cl.principal(donor1)],
donor1,
);
expect(getDonationResponse.result).toBeOk(Cl.uint(5000));
// verify message was recorded
const getMessageResponse = simnet.callReadOnlyFn(
"fundraising",
"get-donation-message",
[Cl.principal(donor1)],
donor1,
);
expect(getMessageResponse.result).toBeOk(Cl.some(Cl.stringUtf8(message)));
});2
Update message on subsequent donations
it("allows updating message with subsequent donation", () => {
initCampaign(100000);
// first donation with message
simnet.callPublicFn(
"fundraising",
"donate-stx",
[Cl.uint(5000), Cl.some(Cl.stringUtf8("First message"))],
donor1,
);
// second donation with new message
simnet.callPublicFn(
"fundraising",
"donate-stx",
[Cl.uint(3000), Cl.some(Cl.stringUtf8("Updated message"))],
donor1,
);
// verify total donation amount
const getDonationResponse = simnet.callReadOnlyFn(
"fundraising",
"get-stx-donation",
[Cl.principal(donor1)],
donor1,
);
expect(getDonationResponse.result).toBeOk(Cl.uint(8000));
// verify message was updated
const getMessageResponse = simnet.callReadOnlyFn(
"fundraising",
"get-donation-message",
[Cl.principal(donor1)],
donor1,
);
expect(getMessageResponse.result).toBeOk(
Cl.some(Cl.stringUtf8("Updated message")),
);
});3
Test that it returns none with no message
it("returns none for donor with no message", () => {
initCampaign(100000);
// donate without message
simnet.callPublicFn(
"fundraising",
"donate-stx",
[Cl.uint(5000), Cl.none()],
donor1,
);
const getMessageResponse = simnet.callReadOnlyFn(
"fundraising",
"get-donation-message",
[Cl.principal(donor1)],
donor1,
);
expect(getMessageResponse.result).toBeOk(Cl.none());
});4
Last updated
Was this helpful?