Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
#122 updated comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ShravanSunder committed Jan 22, 2022
1 parent 5f399ad commit 7daeca5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/context/ethers/BlockNumberContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const reducer = (state: State = {}, payload: Payload): State => {
export const useBlockNumberContext = (): number => {
const blockNumber = useContext(BlockNumberReactContext);
// if (blockNumber == null) {
// console.log('blockNumber context is null');
// console.log('blockNumber context is null');
// }
// invariant(blockNumber != null, 'useBlockNumberContext needs to be used under BlockNumberContext');
return blockNumber ?? 0;
Expand Down
23 changes: 11 additions & 12 deletions test/useContractReader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ describe('useContractReader', function () {
expect(wrapper.result.current[0]).to.eql(finalPurpose);
});

it('When given options of block number interval to update; then the hook only updates once an interval over multiple intervals', async () => {
// Given
it('When given options of block number interval to update; then the hook only updates "once an interval" over multiple intervals', async () => {
// ::Given::
const blockIntervalToUpdate = 5;
const totalBlocksToTraverse = 21;

Expand All @@ -171,8 +171,9 @@ describe('useContractReader', function () {
useContractReader(yourContract, yourContract?.purpose, [], undefined, updateOptions)
);

// ::When:::
let periodStart = await wrapper.mockProvider.getBlockNumber();
// -- mine blocks up to block when update should occur
// mine blocks up to block when update should occur
await mineBlockUntil(wrapper.mockProvider, totalBlocksToTraverse, async (currentBlockNumber): Promise<void> => {
const intervalPurpose = `purpose ${currentBlockNumber}`;

Expand All @@ -186,21 +187,19 @@ describe('useContractReader', function () {
await yourContract?.setPurpose(intervalPurpose);
});

console.log(wrapper.result.all);
const uniqueHookResults = [
...new Set(wrapper.result.all.map((m) => (m as THookResult<string | undefined>)[0])),
];

console.log(uniqueHookResults);

// ::Then::
// 4 results (20/5) and 1 initial state
const expectedUpdatedCount = Math.floor(totalBlocksToTraverse / blockIntervalToUpdate) + 1;
expect(uniqueHookResults).to.have.lengthOf(expectedUpdatedCount);
});

it('When given option for refetchInterval; then ensures result is not returned before refetchInterval', async () => {
// Given
// -- turn off checkUpdateOptions to allow for lower refetchInterval time
// ::Given::
// turn off checkUpdateOptions to allow for lower refetchInterval time
sandbox = sinon.createSandbox();
sandbox.stub(hookHelpers, 'checkUpdateOptions').returns();
const purposeUpdate = 'higher purpose';
Expand All @@ -215,10 +214,10 @@ describe('useContractReader', function () {

await yourContract?.setPurpose(purposeUpdate);

// -- ensure mining block doesn't trigger update
// ensure mining block doesn't trigger update
await mineBlock(harness.mockProvider);

// -- ensure doesn't update before refetchInterval time
// ensure doesn't update before refetchInterval time
try {
await harness.waitForValueToChange(() => harness.result.current[0], {
timeout: updateOptions.refetchInterval - 100,
Expand All @@ -230,10 +229,10 @@ describe('useContractReader', function () {
expect(harness.result.current[0]).be.equal(initialPurpose);
}

// When
// ::When::
await harness.waitForValueToChange(() => harness.result.current[0], defaultBlockWaitOptions);

// Then
// ::Then::
expect(harness.result.current[0]).be.equal(purposeUpdate);
});
});
Expand Down
44 changes: 22 additions & 22 deletions test/useTokenBalance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ describe('useTokenBalance', function () {
});

it(`When the hook is called; then returns address's balance of given ERC20 token`, async () => {
// Given
// ::Given::
const harness = await hookTestWrapper((address: string) => useTokenBalance(basicERC20Contract, address));
const [wallet] = harness.mockProvider.getWallets();
expectValidWallets(wallet);

// When
// ::When::
harness.rerender(wallet.address);
await harness.waitForValueToChange(() => harness.result.current[0], defaultBlockWaitOptions);

// Then
// ::Then::
const [result] = harness.result.current;
expect(result).be.equal(0);
});

it('When wallet balances changes; then the hook returns the new balance', async () => {
// Given
// ::Given::
const amountOfTokensTransferringToWallet = 200;
const harness = await hookTestWrapper((address: string) => useTokenBalance(basicERC20Contract, address));
const [wallet, walletOfERC20TokenSigner] = harness.mockProvider.getWallets();
Expand All @@ -53,17 +53,17 @@ describe('useTokenBalance', function () {
});
const walletsBalanceAfterTransfer = await basicERC20Contract.balanceOf(wallet.address);

// When
// ::When::
harness.rerender(wallet.address);
await harness.waitForValueToChange(() => harness.result.current[0], defaultBlockWaitOptions);

// Then
// ::Then::
const [result] = harness.result.current;
expect(result).be.equal(walletsBalanceAfterTransfer).be.equal(amountOfTokensTransferringToWallet);
});

it('When given options of block number interval to update; then the hook does not update until that amount of blocks has passed', async () => {
// Given
// ::Given::
const initialAmountOfTokensInWallet = 0;
const amountOfTokensTransferringToWallet = 150;
const blockIntervalToUpdate = 7;
Expand All @@ -72,41 +72,41 @@ describe('useTokenBalance', function () {
);
const [wallet, walletOfERC20TokenSigner] = harness.mockProvider.getWallets();
expectValidWallets(wallet, walletOfERC20TokenSigner);
// -- start with 0 from useTokenBalance
// start with 0 from useTokenBalance
harness.rerender(wallet.address);
await harness.waitForValueToChange(() => harness.result.current[0], defaultBlockWaitOptions);
expect(harness.result.current[0]).be.equal(initialAmountOfTokensInWallet); // Just ensures test is set up correctly
await basicERC20Contract.transfer(wallet.address, amountOfTokensTransferringToWallet, {
from: walletOfERC20TokenSigner.address,
});
const walletsBalanceAfterTransfer = await basicERC20Contract.balanceOf(wallet.address);
// -- mine blocks up to block when update should occur
// mine blocks up to block when update should occur
let currentBlockNumber = await harness.mockProvider.getBlockNumber();
while (currentBlockNumber % blockIntervalToUpdate !== 0) {
await mineBlock(harness.mockProvider);
currentBlockNumber = await harness.mockProvider.getBlockNumber();
harness.rerender(wallet.address);
await harness.waitForNextUpdate(defaultBlockWaitOptions);

// -- ensures no update before correct block
// ensures no update before correct block
expect(harness.result.current[0]).be.equal(initialAmountOfTokensInWallet);
}

// -- mine final block
// mine final block
await mineBlock(harness.mockProvider);

// When
// ::When::
harness.rerender(wallet.address);
await harness.waitForValueToChange(() => harness.result.current[0], defaultBlockWaitOptions);

// Then
// ::Then::
const [result] = harness.result.current;
expect(result).be.equal(walletsBalanceAfterTransfer).be.equal(amountOfTokensTransferringToWallet);
});

it('When given option for refetchInterval; then ensures result is not returned before refetchInterval', async () => {
// Given
// -- turn off checkUpdateOptions to allow for lower refetchInterval time
// ::Given::
// turn off checkUpdateOptions to allow for lower refetchInterval time
sandbox = sinon.createSandbox();
sandbox.stub(hookHelpers, 'checkUpdateOptions').returns();

Expand All @@ -125,9 +125,9 @@ describe('useTokenBalance', function () {
from: walletOfERC20TokenSigner.address,
});
const walletsBalanceAfterTransfer = await basicERC20Contract.balanceOf(wallet.address);
// -- ensure mining block doesn't trigger update
// ensure mining block doesn't trigger update
await mineBlock(harness.mockProvider);
// -- ensure doesn't update before refetchInterval time
// ensure doesn't update before refetchInterval time
try {
await harness.waitForValueToChange(() => harness.result.current[0], {
timeout: updateOptions.refetchInterval - 100,
Expand All @@ -139,17 +139,17 @@ describe('useTokenBalance', function () {
expect(harness.result.current[0]).be.equal(initialAmountOfTokensInWallet);
}

// When
// ::When::
harness.rerender(wallet.address);
await harness.waitForValueToChange(() => harness.result.current[0], defaultBlockWaitOptions);

// Then
// ::Then::
const [result] = harness.result.current;
expect(result).be.equal(walletsBalanceAfterTransfer).be.equal(amountOfTokensTransferringToWallet);
});

it('When given option for refetchInterval and blockNumberInterval; then throws error', async () => {
// Given
// ::Given::
const updateOptions = {
refetchInterval: 11_000,
blockNumberInterval: 5,
Expand All @@ -165,7 +165,7 @@ describe('useTokenBalance', function () {
});

it('When given option for refetchInterval < 10000; then throws error', async () => {
// Given
// ::Given::
const updateOptions = {
refetchInterval: 2_000,
blockNumberInterval: undefined,
Expand All @@ -181,7 +181,7 @@ describe('useTokenBalance', function () {
});

it('When given option for blockNumberInterval <= 0; then throws error', async () => {
// Given
// ::Given::
const updateOptions = {
blockNumberInterval: 0,
};
Expand Down

0 comments on commit 7daeca5

Please sign in to comment.