Skip to content

Commit

Permalink
set createcontract isAllowed option and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sstraatemans committed Jan 17, 2025
1 parent d01250d commit dac771b
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 6 deletions.
9 changes: 8 additions & 1 deletion packages/apps/rwa-demo/src/app/(app)/assets/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Confirmation } from '@/components/Confirmation/Confirmation';
import { SideBarBreadcrumbs } from '@/components/SideBarBreadcrumbs/SideBarBreadcrumbs';
import { useAccount } from '@/hooks/account';
import { useAsset } from '@/hooks/asset';
import { useCreateContract } from '@/hooks/createContract';
import { MonoAdd, MonoDelete, MonoFindInPage } from '@kadena/kode-icons';
import { Button } from '@kadena/kode-ui';
import {
Expand All @@ -26,6 +27,7 @@ import { useState } from 'react';
const Assets = () => {
const { account } = useAccount();
const { assets, removeAsset, setAsset, getAsset } = useAsset();
const { isAllowed } = useCreateContract();
const { addNotification } = useNotifications();
const [openSide, setOpenSide] = useState(false);
const { setIsRightAsideExpanded, isRightAsideExpanded } = useLayout();
Expand Down Expand Up @@ -72,7 +74,12 @@ const Assets = () => {
actions={
<AssetFormScreen
trigger={
<Button variant="outlined" isCompact endVisual={<MonoAdd />}>
<Button
isDisabled={!isAllowed}
variant="outlined"
isCompact
endVisual={<MonoAdd />}
>
Add Asset
</Button>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const AssetStepperForm: FC<IProps> = ({ handleDone }) => {
const [step, setStep] = useState<number>(STEPS.START);
const { addAsset, setAsset } = useAsset();
const { data: namespace } = useGetPrincipalNamespace();
const { submit: submitContract } = useCreateContract();
const { submit: submitContract, isAllowed } = useCreateContract();
const [error, setError] = useState('');
const [isLoading, setIsLoading] = useState(false);
const router = useRouter();
Expand Down Expand Up @@ -96,6 +96,7 @@ export const AssetStepperForm: FC<IProps> = ({ handleDone }) => {
<Text bold>or</Text>
</Stack>
<Button
isDisabled={!isAllowed}
variant="outlined"
onPress={async () => {
setStep(STEPS.CREATE_CONTRACT);
Expand Down Expand Up @@ -151,7 +152,10 @@ export const AssetStepperForm: FC<IProps> = ({ handleDone }) => {
{isLoading ? (
<TransactionPendingIcon />
) : (
<Button isDisabled={!isValid || isLoading} type="submit">
<Button
isDisabled={!isValid || isLoading || !isAllowed}
type="submit"
>
Create the contract
</Button>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { renderHook } from '@testing-library/react-hooks';
import { useBatchTransferTokens } from '../batchTransferTokens';
import { useGetInvestors } from '../getInvestors';

describe('batchTransferTokens hook', () => {
const mocksHook = vi.hoisted(() => {
Expand Down
98 changes: 98 additions & 0 deletions packages/apps/rwa-demo/src/hooks/__tests__/createContract.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { renderHook } from '@testing-library/react-hooks';
import { useCreateContract } from '../createContract';

describe('createContract hook', () => {
const mocksHook = vi.hoisted(() => {
return {
useAccount: vi.fn().mockReturnValue({
account: {
address: 'k:he-man',
},
sign: vi.fn(),
isMounted: true,
isGasPayable: false,
}),

useTransactions: vi.fn().mockReturnValue({
addTransaction: vi.fn(),
}),
useNotifications: vi.fn().mockReturnValue({
addNotification: vi.fn(),
}),
};
});

beforeEach(async () => {
vi.mock('./../account', async () => {
const actual = await vi.importActual('./../account');
return {
...actual,
useAccount: mocksHook.useAccount,
};
});

vi.mock('./../transactions', async () => {
const actual = await vi.importActual('./../transactions');
return {
...actual,
useTransactions: mocksHook.useTransactions,
};
});

vi.mock('@kadena/kode-ui/patterns', async () => {
const actual = await vi.importActual('@kadena/kode-ui/patterns');
return {
...actual,
useNotifications: mocksHook.useNotifications,
};
});
});

afterEach(() => {
vi.clearAllMocks();
});

it('should return the correct properties', async () => {
const { result } = renderHook(() => useCreateContract());
expect(result.current.hasOwnProperty('isAllowed')).toBe(true);
expect(result.current.hasOwnProperty('submit')).toBe(true);
});

describe('isAllowed', () => {
it('should return true, when account is Mounted, when gasisPayable', () => {
mocksHook.useAccount.mockImplementation(() => ({
...mocksHook.useAccount.getMockImplementation(),
isMounted: true,
isGasPayable: true,
}));

const { result } = renderHook(() => useCreateContract());

expect(result.current.isAllowed).toBe(true);
});

it('should return false, when account is NOT Mounted, when gasisPayable', () => {
mocksHook.useAccount.mockImplementation(() => ({
...mocksHook.useAccount.getMockImplementation(),
isMounted: false,
isGasPayable: true,
}));

const { result } = renderHook(() => useCreateContract());

expect(result.current.isAllowed).toBe(false);
});

it('should return false, when account is Mounted, when NOT gasisPayable', () => {
mocksHook.useAccount.mockImplementation(() => ({
...mocksHook.useAccount.getMockImplementation(),
isMounted: true,
isGasPayable: false,
}));

const { result } = renderHook(() => useCreateContract());

expect(result.current.isAllowed).toBe(false);
});
});
});
11 changes: 9 additions & 2 deletions packages/apps/rwa-demo/src/hooks/createContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import type { IAddContractProps } from '@/services/createContract';
import { createContract } from '@/services/createContract';
import { getClient } from '@/utils/client';
import { useNotifications } from '@kadena/kode-ui/patterns';
import { useEffect, useState } from 'react';
import { useAccount } from './account';
import { useTransactions } from './transactions';

export const useCreateContract = () => {
const { account, sign } = useAccount();
const { account, isMounted, sign, isGasPayable } = useAccount();
const [isAllowed, setIsAllowed] = useState(false);
const { addTransaction } = useTransactions();
const { addNotification } = useNotifications();

Expand Down Expand Up @@ -54,5 +56,10 @@ export const useCreateContract = () => {
}
};

return { submit };
useEffect(() => {
if (!isMounted) return;
setIsAllowed(isGasPayable);
}, [isMounted, isGasPayable]);

return { submit, isAllowed };
};

0 comments on commit dac771b

Please sign in to comment.