Skip to content

Commit

Permalink
chore(rwa): unit tests (#2780)
Browse files Browse the repository at this point in the history
  • Loading branch information
sstraatemans authored Jan 10, 2025
1 parent 4d4d0ee commit 0f0c869
Show file tree
Hide file tree
Showing 25 changed files with 1,462 additions and 228 deletions.
5 changes: 3 additions & 2 deletions packages/apps/rwa-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"lint:fmt": "prettier . --cache --check",
"lint:src": "eslint src",
"start": "next start",
"test": "echo ''",
"test": "vitest run",
"test:coverage": "vitest run --coverage",
"test:watch": "vitest --coverage"
"test:watch": "vitest"
},
"dependencies": {
"@apollo/client": "~3.9.11",
Expand Down Expand Up @@ -51,6 +51,7 @@
"@kadena/pactjs": "workspace:*",
"@kadena/pactjs-cli": "workspace:*",
"@kadena/pactjs-generator": "workspace:*",
"@testing-library/react-hooks": "^8.0.1",
"@types/node": "^20.12.7",
"@types/react": "^18.2.79",
"@types/react-dom": "^18.2.25",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ICSVAccount } from '@/services/batchRegisterIdentity';
import { checkAllowedFileTypes } from '@/utils/checkAllowedFileTypes';
import { csvFileToArray } from '@/utils/csvFileToArray';
import { objectNotValidSchema } from '@/utils/objectNotValidSchema';
import { objectValidSchema } from '@/utils/objectValidSchema';
import { Stack } from '@kadena/kode-ui';
import { useNotifications } from '@kadena/kode-ui/patterns';
import type { DragEventHandler, FC } from 'react';
Expand Down Expand Up @@ -31,7 +31,7 @@ export const DragNDropCSV: FC<IProps> = ({ onResult, resultSchema }) => {
fileReader.onload = function () {
const arr = csvFileToArray<ICSVAccount>(fileReader.result as string);

if (!arr.find(objectNotValidSchema(resultSchema))) {
if (!arr.find(objectValidSchema(resultSchema))) {
addNotification({
intent: 'negative',
label: 'the CSV has the wrong schema',
Expand Down
2 changes: 2 additions & 0 deletions packages/apps/rwa-demo/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export const ADMIN = {
account: 'k:a56eb32f7860bfc4e19ec4d4ac0b2dd6c9a96f3c72d607847416253422a2b1b7',
publicKey: 'a56eb32f7860bfc4e19ec4d4ac0b2dd6c9a96f3c72d607847416253422a2b1b7',
};

export const ALLOWEDFILETYPES = () => ['text/csv'];
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
import { renderHook } from '@testing-library/react-hooks';
import { useTogglePartiallyFreezeTokens } from '../togglePartiallyFreezeTokens';

describe('togglePause hook', () => {
const mocksHook = vi.hoisted(() => {
return {
useAccount: vi.fn().mockReturnValue({
account: {
address: 'k:1',
},
sign: vi.fn(),
isMounted: true,
accountRoles: {
isFreezer: vi.fn().mockReturnValue(false),
},
}),
useAsset: vi.fn().mockReturnValue({
paused: true,
}),
useFreeze: vi.fn().mockReturnValue({
frozen: true,
}),
useTransactions: vi.fn().mockReturnValue({
addTransaction: vi.fn(),
isActiveAccountChangeTx: false,
}),
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('./../freeze', async () => {
const actual = await vi.importActual('./../freeze');
return {
...actual,
useFreeze: mocksHook.useFreeze,
};
});

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

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(() =>
useTogglePartiallyFreezeTokens({ investorAccount: 'k:1' }),
);
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 account is NOT frozen, when contract is NOT paused, when account has role FREEZER, when no activeAccountTx busy', () => {
mocksHook.useAccount.mockImplementation(() => ({
account: {
address: 'k:1',
},
isMounted: true,
accountRoles: {
isFreezer: vi.fn().mockReturnValue(true),
},
}));
mocksHook.useFreeze.mockImplementation(() => ({
...mocksHook.useFreeze.getMockImplementation(),
frozen: false,
}));
mocksHook.useAsset.mockImplementation(() => ({
...mocksHook.useAsset.getMockImplementation(),
paused: false,
}));
mocksHook.useTransactions.mockImplementation(() => ({
...mocksHook.useTransactions.getMockImplementation(),
isActiveAccountChangeTx: false,
}));

const { result } = renderHook(() =>
useTogglePartiallyFreezeTokens({ investorAccount: 'k:1' }),
);

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

it('should return false, when account NOT is mounted', () => {
mocksHook.useAccount.mockImplementation(() => ({
account: undefined,
isMounted: false,
accountRoles: {
isFreezer: vi.fn().mockReturnValue(true),
},
}));

const { result } = renderHook(() =>
useTogglePartiallyFreezeTokens({ investorAccount: 'k:1' }),
);

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

it('should return false, when account is mounted, when account is frozen, when contract is NOT paused, when account has role FREEZER, when no activeAccountTx busy', () => {
mocksHook.useAccount.mockImplementation(() => ({
account: {
address: 'k:1',
},
isMounted: true,
accountRoles: {
isFreezer: vi.fn().mockReturnValue(true),
},
}));
mocksHook.useFreeze.mockImplementation(() => ({
...mocksHook.useFreeze.getMockImplementation(),
frozen: true,
}));
mocksHook.useAsset.mockImplementation(() => ({
...mocksHook.useAsset.getMockImplementation(),
paused: false,
}));
mocksHook.useTransactions.mockImplementation(() => ({
...mocksHook.useTransactions.getMockImplementation(),
isActiveAccountChangeTx: false,
}));

const { result } = renderHook(() =>
useTogglePartiallyFreezeTokens({ investorAccount: 'k:1' }),
);

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

it('should return false, when account is mounted, when account is NOT frozen, when contract is paused, when account has role FREEZER, when no activeAccountTx busy', () => {
mocksHook.useAccount.mockImplementation(() => ({
account: {
address: 'k:1',
},
isMounted: true,
accountRoles: {
isFreezer: vi.fn().mockReturnValue(true),
},
}));
mocksHook.useFreeze.mockImplementation(() => ({
...mocksHook.useFreeze.getMockImplementation(),
frozen: false,
}));
mocksHook.useAsset.mockImplementation(() => ({
...mocksHook.useAsset.getMockImplementation(),
paused: true,
}));
mocksHook.useTransactions.mockImplementation(() => ({
...mocksHook.useTransactions.getMockImplementation(),
isActiveAccountChangeTx: false,
}));

const { result } = renderHook(() =>
useTogglePartiallyFreezeTokens({ investorAccount: 'k:1' }),
);

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

it('should return false, when account is mounted, when account is NOT frozen, when contract is NOT paused, when account has NOT role FREEZER, when no activeAccountTx busy', () => {
mocksHook.useAccount.mockImplementation(() => ({
account: {
address: 'k:1',
},
isMounted: true,
accountRoles: {
isFreezer: vi.fn().mockReturnValue(false),
},
}));
mocksHook.useFreeze.mockImplementation(() => ({
...mocksHook.useFreeze.getMockImplementation(),
frozen: false,
}));
mocksHook.useAsset.mockImplementation(() => ({
...mocksHook.useAsset.getMockImplementation(),
paused: false,
}));
mocksHook.useTransactions.mockImplementation(() => ({
...mocksHook.useTransactions.getMockImplementation(),
isActiveAccountChangeTx: false,
}));

const { result } = renderHook(() =>
useTogglePartiallyFreezeTokens({ investorAccount: 'k:1' }),
);

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

it('should return false, when account is mounted, when account is NOT frozen, when contract is NOT paused, when account has role FREEZER, when activeAccountTx IS busy', () => {
mocksHook.useAccount.mockImplementation(() => ({
account: {
address: 'k:1',
},
isMounted: true,
accountRoles: {
isFreezer: vi.fn().mockReturnValue(true),
},
}));
mocksHook.useFreeze.mockImplementation(() => ({
...mocksHook.useFreeze.getMockImplementation(),
frozen: false,
}));
mocksHook.useAsset.mockImplementation(() => ({
...mocksHook.useAsset.getMockImplementation(),
paused: false,
}));
mocksHook.useTransactions.mockImplementation(() => ({
...mocksHook.useTransactions.getMockImplementation(),
isActiveAccountChangeTx: true,
}));

const { result } = renderHook(() =>
useTogglePartiallyFreezeTokens({ investorAccount: 'k:1' }),
);

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

0 comments on commit 0f0c869

Please sign in to comment.