Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(account): Add TestCoin helper class for testing purposes #3647

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions packages/account/src/test-utils/test-coin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { describe, expect, test } from 'vitest';
import { Coin } from '@fuel-ts/account';
import { bn } from '@fuel-ts/math';
import { getRandomB256 } from '@fuel-ts/crypto';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { getRandomB256 } from '@fuel-ts/crypto';
import { getRandomB256 } from '@fuel-ts/address';


import { TestCoin } from './TestCoin';

/**
* @group node
*/
describe('TestCoin', () => {
test('create() returns coin with default values', () => {
const coin = TestCoin.create();

expect(coin.id).toBeDefined();
expect(coin.id).toMatch(/^0x[a-f0-9]{64}$/);
expect(coin.owner).toBeDefined();
expect(coin.owner).toMatch(/^0x[a-f0-9]{64}$/);
expect(coin.amount).toBeDefined();
expect(coin.amount.toString()).toBe('1000000');
expect(coin.type).toBe(0);
});

test('create() accepts custom values', () => {
const customCoin: Partial<Coin> = {
id: getRandomB256(),
owner: getRandomB256(),
amount: bn(500),
type: 1,
};

const coin = TestCoin.create(customCoin);

expect(coin.id).toBe(customCoin.id);
expect(coin.owner).toBe(customCoin.owner);
expect(coin.amount).toBe(customCoin.amount);
expect(coin.type).toBe(customCoin.type);
});

test('many() creates specified number of coins', () => {
const count = 3;
const coins = TestCoin.many({}, count);

expect(coins).toHaveLength(count);
expect(coins[0].id).not.toBe(coins[1].id);
expect(coins[1].id).not.toBe(coins[2].id);
});

test('many() applies same base parameters to all coins', () => {
const baseParams: Partial<Coin> = {
owner: getRandomB256(),
amount: bn(1000),
type: 2,
};

const coins = TestCoin.many(baseParams, 2);

expect(coins[0].owner).toBe(baseParams.owner);
expect(coins[0].amount).toBe(baseParams.amount);
expect(coins[0].type).toBe(baseParams.type);
expect(coins[1].owner).toBe(baseParams.owner);
expect(coins[1].amount).toBe(baseParams.amount);
expect(coins[1].type).toBe(baseParams.type);
});
});
25 changes: 25 additions & 0 deletions packages/account/src/test-utils/test-coin.ts
danielbate marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Coin } from '@fuel-ts/account';
import { bn } from '@fuel-ts/math';
import { getRandomB256 } from '@fuel-ts/crypto';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { getRandomB256 } from '@fuel-ts/crypto';
import { getRandomB256 } from '@fuel-ts/address';


export class TestCoin {
/**
* Creates a single test coin with given parameters
*/
static create(params: Partial<Coin> = {}): Coin {
return {
id: params.id || getRandomB256(),
owner: params.owner || getRandomB256(),
amount: params.amount || bn(1000000),
type: params.type || 0,
...params
};
}

/**
* Generates an array of test coins with the same base parameters
*/
static many(params: Partial<Coin> = {}, count = 1): Coin[] {
return Array.from({ length: count }, () => TestCoin.create(params));
}
}