Skip to content

Commit

Permalink
Add tests for CrowdfundingFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
TobaSenju committed Oct 13, 2023
1 parent 71fb165 commit 7d55492
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
2 changes: 1 addition & 1 deletion contracts/crowdfunding_factory.tact
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "./crowdfunding.tact";

contract CrowdfundingFactory with Deployable {
const MIN_VALUE_TO_START: Int = ton("1.0");
const MAX_DEADLINE: Int = 31536000; // 1 year
const MAX_DEADLINE: Int = 365 * 24 * 60 * 60; // 1 year

seqno: Int as uint256 = 0;

Expand Down
81 changes: 76 additions & 5 deletions tests/CrowdfundingFactory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { Blockchain, SandboxContract } from '@ton-community/sandbox';
import { Blockchain, SandboxContract, TreasuryContract } from '@ton-community/sandbox';
import { toNano } from 'ton-core';
import { CrowdfundingFactory } from '../wrappers/CrowdfundingFactory';
import { getUnixTimestampNow } from './utils';
import '@ton-community/test-utils';

const MIN_VALUE_TO_START = toNano('1');
const MAX_DEADLINE = 365 * 24 * 60 * 60;
const ONE_DAY: number = 1 * 24 * 60 * 60;

describe('CrowdfundingFactory', () => {
let blockchain: Blockchain;
let crowdfundingFactory: SandboxContract<CrowdfundingFactory>;
let deployer: SandboxContract<TreasuryContract>;

beforeEach(async () => {
blockchain = await Blockchain.create();

crowdfundingFactory = blockchain.openContract(await CrowdfundingFactory.fromInit());

const deployer = await blockchain.treasury('deployer');
deployer = await blockchain.treasury('deployer');

const deployResult = await crowdfundingFactory.send(
deployer.getSender(),
Expand All @@ -33,8 +39,73 @@ describe('CrowdfundingFactory', () => {
});
});

it('should deploy', async () => {
// the check is done inside beforeEach
// blockchain and crowdfundingFactory are ready to use
it('increase seqno after creating the crowdfunding', async () => {
let seqno: bigint = await crowdfundingFactory.getGetLastSeqno();
expect(seqno).toEqual(0n);

await startCrowdfunding();

seqno = await crowdfundingFactory.getGetLastSeqno();
expect(seqno).toEqual(1n);
});

it("don't start crowdfunding if value is not enough", async () => {
await crowdfundingFactory.send(
deployer.getSender(),
{
value: MIN_VALUE_TO_START - toNano('0.01'),
},
{
$$type: 'CrowdfundingParams',
title: 'Test Title',
description: 'Test Description',
minContribution: toNano('0.01'),
targetContribution: toNano('5'),
deadline: BigInt(getUnixTimestampNow()),
beneficiary: deployer.getSender().address,
},
);

const seqno: bigint = await crowdfundingFactory.getGetLastSeqno();
expect(seqno).toEqual(0n);
});

it("don't start crowdfunding if deadline is too big", async () => {
await crowdfundingFactory.send(
deployer.getSender(),
{
value: MIN_VALUE_TO_START,
},
{
$$type: 'CrowdfundingParams',
title: 'Test Title',
description: 'Test Description',
minContribution: toNano('0.01'),
targetContribution: toNano('5'),
deadline: BigInt(getUnixTimestampNow() + MAX_DEADLINE + ONE_DAY),
beneficiary: deployer.getSender().address,
},
);

const seqno: bigint = await crowdfundingFactory.getGetLastSeqno();
expect(seqno).toEqual(0n);
});

const startCrowdfunding = async () => {
await crowdfundingFactory.send(
deployer.getSender(),
{
value: MIN_VALUE_TO_START,
},
{
$$type: 'CrowdfundingParams',
title: 'Test Title',
description: 'Test Description',
minContribution: toNano('0.01'),
targetContribution: toNano('5'),
deadline: BigInt(getUnixTimestampNow() + ONE_DAY),
beneficiary: deployer.getSender().address,
},
);
};
});

0 comments on commit 7d55492

Please sign in to comment.