Skip to content

Commit

Permalink
docs: docs for extending the test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbate committed Jul 12, 2024
1 parent 050836c commit 1296cbe
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe(__filename, () => {
});

it('should successfully use "get" to read from the blockchain', async () => {
const { waitForResult } = await counterContract.functions.increment_count(1).call();
const { waitForResult } = await counterContract.functions.increment_counter(1).call();
await waitForResult();

const { id: contractId, interface: abi } = counterContract;
Expand All @@ -37,7 +37,7 @@ describe(__filename, () => {
// #region interacting-with-contracts-2
const contract = new Contract(contractId, abi, provider);

const { value } = await contract.functions.increment_count(1).dryRun();
const { value } = await contract.functions.increment_counter(1).dryRun();
// #endregion interacting-with-contracts-2
expect(value.toNumber()).toBeGreaterThanOrEqual(1);
});
Expand All @@ -50,7 +50,7 @@ describe(__filename, () => {
// #region interacting-with-contracts-3
const contract = new Contract(contractId, abi, fundedWallet);

const { value } = await contract.functions.increment_count(10).simulate();
const { value } = await contract.functions.increment_counter(10).simulate();
// #endregion interacting-with-contracts-3
expect(value.toNumber()).toBeGreaterThanOrEqual(10);
});
Expand All @@ -59,7 +59,9 @@ describe(__filename, () => {
const contract = counterContract;

// #region interacting-with-contracts-4
const { transactionId, waitForResult } = await contract.functions.increment_count(10).call();
const { transactionId, waitForResult } = await contract.functions
.increment_counter(10)
.call();

const { value } = await waitForResult();
// #endregion interacting-with-contracts-4
Expand All @@ -72,7 +74,7 @@ describe(__filename, () => {
const contract = counterContract;

// #region interacting-with-contracts-5
const { waitForResult } = await contract.functions.increment_count(10).call();
const { waitForResult } = await contract.functions.increment_counter(10).call();
const { value } = await waitForResult();
// #endregion interacting-with-contracts-5

Expand Down
6 changes: 3 additions & 3 deletions apps/docs-snippets/src/guide/contracts/multicalls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ describe(__filename, () => {
const { waitForResult } = await counterContract
.multiCall([
counterContract.functions.get_count(),
counterContract.functions.increment_count(2),
counterContract.functions.increment_count(4),
counterContract.functions.increment_counter(2),
counterContract.functions.increment_counter(4),
])
.call();

Expand All @@ -83,7 +83,7 @@ describe(__filename, () => {
const chain = echoContract.multiCall([
echoContract.functions.echo_u8(17),
counterContract.functions.get_count(),
counterContract.functions.increment_count(5),
counterContract.functions.increment_counter(5),
]);

const { waitForResult } = await chain.call();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('Custom Transactions from Contract Calls', () => {
// Connect to the contract
const contractInstance = new Contract(contract.id, abi, senderWallet);
// Create an invocation scope for the contract function you'd like to call in the transaction
const scope = contractInstance.functions.increment_count(amountToRecipient).addTransfer({
const scope = contractInstance.functions.increment_counter(amountToRecipient).addTransfer({
amount: amountToRecipient,
destination: receiverWallet.address,
assetId: baseAssetId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { launchTestNode } from 'fuels/test-utils';
import { describe, test, expect } from 'vitest';

import { CounterAbi__factory } from '../../../test/typegen';
import bytecode from '../../../test/typegen/contracts/CounterAbi.hex';

describe('Counter Contract', () => {
// #region decrement-counter
// #context import { CounterAbi__factory } from './sway-programs-api';
// #context import bytecode from './sway-programs-api/contracts/CounterAbi.hex';

test('calls the decrement_counter function', async () => {
// First, we'll launch a test node, passing the contract factory and bytecode. This will deploy the contract
// to our test node so we can test against it.
using launched = await launchTestNode({
// The test node will be killed automatically once the `launched` variable goes out of scope,
// because we are instantiating it with the `using` keyword.
contractsConfigs: [
{
deployer: CounterAbi__factory,
bytecode,
},
],
});

// We can now destructure the contract from the launched object.
const {
contracts: [contract],
} = launched;

// Lets setup some values to use in the test.
const initialCount = 0;
const incrementedValue = 5;
const decrementedValue = 2;

// We can now call the contract functions and test the results. Lets assert the initial value of the counter.
const { waitForResult: initWaitForResult } = await contract.functions.get_count().call();
const { value: initValue } = await initWaitForResult();
expect(initValue.toNumber()).toBe(initialCount);

// Next we'll increment the counter, so that we can decrement it.
const { waitForResult: incWaitForResult } = await contract.functions
.increment_counterer(5)
.call();
const { value: incValue } = await incWaitForResult();
expect(incValue.toNumber()).toBe(incrementedValue);

// Next, we'll decrement the counter by 3 and assert the new value.
const { waitForResult: decWaitForResult } = await contract.functions
.decrement_counter(3)
.call();
const { value: decValue } = await decWaitForResult();
expect(decValue.toNumber()).toBe(decrementedValue);

// Finally, we'll test the get count function again to ensure parity.
const { waitForResult: finalWaitForResult } = await contract.functions.get_count().call();
const { value: finalValue } = await finalWaitForResult();
expect(finalValue.toNumber()).toBe(decrementedValue);
});
// #endregion decrement-counter
});
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe(__filename, () => {
it('executes contract call with txParams', async () => {
// #region transaction-parameters-8
const { waitForResult } = await contract.functions
.increment_count(15)
.increment_counter(15)
.txParams({
variableOutputs: 1,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('Transaction Response', () => {
it('gets transaction response from contract call', async () => {
// #region transaction-response-1
// Call a contract function
const call = await contract.functions.increment_count(15).call();
const call = await contract.functions.increment_counter(15).call();

// Wait for the result
const { transactionResponse } = await call.waitForResult();
Expand Down
14 changes: 12 additions & 2 deletions apps/docs-snippets/test/fixtures/forc-projects/counter/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ abi Counter {
fn get_count() -> u64;

#[storage(write, read)]
fn increment_count(amount: u64) -> u64;
fn increment_counter(amount: u64) -> u64;

#[storage(write, read)]
fn decrement_counter(amount: u64) -> u64;
}

storage {
Expand All @@ -19,9 +22,16 @@ impl Counter for Contract {
}

#[storage(write, read)]
fn increment_count(amount: u64) -> u64 {
fn increment_counter(amount: u64) -> u64 {
let current = storage.counter.read();
storage.counter.write(current + amount);
storage.counter.read()
}

#[storage(write, read)]
fn decrement_counter(amount: u64) -> u64 {
let current = storage.counter.read();
storage.counter.write(current - amount);
storage.counter.read()
}
}
12 changes: 10 additions & 2 deletions apps/docs/src/guide/creating-a-fuel-dapp/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,22 @@ Second, we will add a new button to the UI that will call the `onDecrementPresse
</Button>
```

Congratulations! That's all. You should now be able to see the counter dApp running at `http://localhost:3000` with our newly added decrement functionality.
Congratulations! You should now be able to see the counter dApp running at `http://localhost:3000` with our newly added decrement functionality.

You can find the complete source code of the dApp we built [here](https://github.com/FuelLabs/fuels-ts/tree/master/apps/create-fuels-counter-guide).

![End result of this guide](../../public/creating-a-fuel-dapp-create-fuels-end-result.png)

Whenever you want to add a new feature to your dApp and quickly prototype things, you can follow the same steps we followed in this guide.

### 3. Extending the Test Suite (Optional)

Testing the integration with your smart contract isn't essential, but it's good practice to ensure that your application is working as expected. It also gives you the ability to test your application in a controlled environment against a local node.

We've provided some examples for each program type in the `./test` directory of your project. But let's also add a test for our new `decrement_counter` function in the `./test/contract.test.ts` file:

<<< @/../../docs-snippets/src/guide/create-fuels/decrement_counter.test.ts#decrement-counter{ts:line-numbers}

## Next Steps

- Now that you have a basic counter dApp running and have the `npm create fuels` workflow powering you, you can start building more complex dApps using the Fuel Stack. A good place to start for ideas and reference code is the [Sway Applications Repo](https://github.com/FuelLabs/sway-applications).
Expand All @@ -199,7 +207,7 @@ Whenever you want to add a new feature to your dApp and quickly prototype things

- If you want to deploy your dApp to the testnet, check out our [Deploying a dApp to Testnet](./deploying-a-dapp-to-testnet.md) guide.

- If you want to further validate the functionality of your dApp and program types, check out the `test` directory in your `create fuels` project. Couple this with our [testing guide](https://docs.fuel.network/docs/fuels-ts/testing/) to get a better understanding of how to test your dApp. This would be a good opportunity to add a test for your newly implemented `decrement_counter` function.
- If you want to further validate the functionality of your dApp and program types, check out the `test` directory in your `create fuels` project. Couple this with our [testing guide](https://docs.fuel.network/docs/fuels-ts/testing/) to get a better understanding of how to test your dApp.

- If you have any questions or need help, feel free to reach out to us on the [Official Fuel Forum](https://forum.fuel.network/).

Expand Down

0 comments on commit 1296cbe

Please sign in to comment.