Skip to content

Commit eb463a0

Browse files
committed
add deleteInvestor hook unit tests
1 parent dac771b commit eb463a0

File tree

2 files changed

+309
-7
lines changed

2 files changed

+309
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
import { renderHook } from '@testing-library/react-hooks';
2+
import { useDeleteInvestor } from '../deleteInvestor';
3+
4+
describe('deleteInvestor hook', () => {
5+
const mocksHook = vi.hoisted(() => {
6+
return {
7+
useGetInvestorBalance: vi.fn().mockReturnValue({
8+
data: 0,
9+
}),
10+
useAsset: vi.fn().mockReturnValue({
11+
paused: true,
12+
}),
13+
useAccount: vi.fn().mockReturnValue({
14+
account: {
15+
address: 'k:1',
16+
},
17+
sign: vi.fn(),
18+
isMounted: true,
19+
isOwner: false,
20+
accountRoles: {
21+
isAgentAdmin: vi.fn().mockReturnValue(false),
22+
},
23+
}),
24+
25+
useTransactions: vi.fn().mockReturnValue({
26+
addTransaction: vi.fn(),
27+
isActiveAccountChangeTx: false,
28+
}),
29+
useNotifications: vi.fn().mockReturnValue({
30+
addNotification: vi.fn(),
31+
}),
32+
};
33+
});
34+
35+
beforeEach(async () => {
36+
vi.mock('./../account', async () => {
37+
const actual = await vi.importActual('./../account');
38+
return {
39+
...actual,
40+
useAccount: mocksHook.useAccount,
41+
};
42+
});
43+
44+
vi.mock('./../getInvestorBalance', async () => {
45+
const actual = await vi.importActual('./../getInvestorBalance');
46+
return {
47+
...actual,
48+
useGetInvestorBalance: mocksHook.useGetInvestorBalance,
49+
};
50+
});
51+
52+
vi.mock('./../asset', async () => {
53+
const actual = await vi.importActual('./../asset');
54+
return {
55+
...actual,
56+
useAsset: mocksHook.useAsset,
57+
};
58+
});
59+
60+
vi.mock('./../transactions', async () => {
61+
const actual = await vi.importActual('./../transactions');
62+
return {
63+
...actual,
64+
useTransactions: mocksHook.useTransactions,
65+
};
66+
});
67+
68+
vi.mock('@kadena/kode-ui/patterns', async () => {
69+
const actual = await vi.importActual('@kadena/kode-ui/patterns');
70+
return {
71+
...actual,
72+
useNotifications: mocksHook.useNotifications,
73+
};
74+
});
75+
});
76+
77+
afterEach(() => {
78+
vi.clearAllMocks();
79+
});
80+
81+
it('should return the correct properties', async () => {
82+
const { result } = renderHook(() =>
83+
useDeleteInvestor({ investorAccount: 'k:1' }),
84+
);
85+
expect(result.current.hasOwnProperty('isAllowed')).toBe(true);
86+
expect(result.current.hasOwnProperty('submit')).toBe(true);
87+
expect(result.current.hasOwnProperty('notAllowedReason')).toBe(true);
88+
});
89+
90+
describe('isAllowed', () => {
91+
it('should return true, when account is mounted, when contract is NOT paused, when account has role ADMIN, when account is NOT owner, when investorbalance is empty, when no activeAccountTx busy', () => {
92+
mocksHook.useAccount.mockImplementation(() => ({
93+
account: {
94+
address: 'k:he-man',
95+
},
96+
isOwner: false,
97+
isMounted: true,
98+
accountRoles: {
99+
isAgentAdmin: vi.fn().mockReturnValue(true),
100+
},
101+
}));
102+
mocksHook.useGetInvestorBalance.mockImplementation(() => ({
103+
...mocksHook.useGetInvestorBalance.getMockImplementation(),
104+
data: 0,
105+
}));
106+
mocksHook.useAsset.mockImplementation(() => ({
107+
...mocksHook.useAsset.getMockImplementation(),
108+
paused: false,
109+
}));
110+
mocksHook.useTransactions.mockImplementation(() => ({
111+
...mocksHook.useTransactions.getMockImplementation(),
112+
isActiveAccountChangeTx: false,
113+
}));
114+
115+
const { result } = renderHook(() =>
116+
useDeleteInvestor({ investorAccount: 'k:1' }),
117+
);
118+
119+
expect(result.current.isAllowed).toBe(true);
120+
});
121+
122+
it('should return true, when account is mounted, when contract is NOT paused, when account has NOT role ADMIN, when account is owner, when investorbalance is empty, when no activeAccountTx busy', () => {
123+
mocksHook.useAccount.mockImplementation(() => ({
124+
account: {
125+
address: 'k:he-man',
126+
},
127+
isOwner: true,
128+
isMounted: true,
129+
accountRoles: {
130+
isAgentAdmin: vi.fn().mockReturnValue(false),
131+
},
132+
}));
133+
mocksHook.useGetInvestorBalance.mockImplementation(() => ({
134+
...mocksHook.useGetInvestorBalance.getMockImplementation(),
135+
data: 0,
136+
}));
137+
mocksHook.useAsset.mockImplementation(() => ({
138+
...mocksHook.useAsset.getMockImplementation(),
139+
paused: false,
140+
}));
141+
mocksHook.useTransactions.mockImplementation(() => ({
142+
...mocksHook.useTransactions.getMockImplementation(),
143+
isActiveAccountChangeTx: false,
144+
}));
145+
146+
const { result } = renderHook(() =>
147+
useDeleteInvestor({ investorAccount: 'k:1' }),
148+
);
149+
150+
expect(result.current.isAllowed).toBe(true);
151+
});
152+
153+
it('should return false, when account is NOT mounted, when contract is NOT paused, when account is owner, when investorbalance is empty, when no activeAccountTx busy', () => {
154+
mocksHook.useAccount.mockImplementation(() => ({
155+
account: {
156+
address: 'k:he-man',
157+
},
158+
isOwner: true,
159+
isMounted: false,
160+
accountRoles: {
161+
isAgentAdmin: vi.fn().mockReturnValue(false),
162+
},
163+
}));
164+
mocksHook.useGetInvestorBalance.mockImplementation(() => ({
165+
...mocksHook.useGetInvestorBalance.getMockImplementation(),
166+
data: 0,
167+
}));
168+
mocksHook.useAsset.mockImplementation(() => ({
169+
...mocksHook.useAsset.getMockImplementation(),
170+
paused: false,
171+
}));
172+
mocksHook.useTransactions.mockImplementation(() => ({
173+
...mocksHook.useTransactions.getMockImplementation(),
174+
isActiveAccountChangeTx: false,
175+
}));
176+
177+
const { result } = renderHook(() =>
178+
useDeleteInvestor({ investorAccount: 'k:1' }),
179+
);
180+
181+
expect(result.current.isAllowed).toBe(false);
182+
});
183+
184+
it('should return false, when account is mounted, when contract is paused, when account is owner, when investorbalance is empty, when no activeAccountTx busy', () => {
185+
mocksHook.useAccount.mockImplementation(() => ({
186+
account: {
187+
address: 'k:he-man',
188+
},
189+
isOwner: true,
190+
isMounted: true,
191+
accountRoles: {
192+
isAgentAdmin: vi.fn().mockReturnValue(false),
193+
},
194+
}));
195+
mocksHook.useGetInvestorBalance.mockImplementation(() => ({
196+
...mocksHook.useGetInvestorBalance.getMockImplementation(),
197+
data: 0,
198+
}));
199+
mocksHook.useAsset.mockImplementation(() => ({
200+
...mocksHook.useAsset.getMockImplementation(),
201+
paused: true,
202+
}));
203+
mocksHook.useTransactions.mockImplementation(() => ({
204+
...mocksHook.useTransactions.getMockImplementation(),
205+
isActiveAccountChangeTx: false,
206+
}));
207+
208+
const { result } = renderHook(() =>
209+
useDeleteInvestor({ investorAccount: 'k:1' }),
210+
);
211+
212+
expect(result.current.isAllowed).toBe(false);
213+
});
214+
215+
it('should return false, when account is mounted, when contract is NOT paused, when account is NOT owner or agentadmin, when investorbalance is empty, when no activeAccountTx busy', () => {
216+
mocksHook.useAccount.mockImplementation(() => ({
217+
account: {
218+
address: 'k:he-man',
219+
},
220+
isOwner: false,
221+
isMounted: true,
222+
accountRoles: {
223+
isAgentAdmin: vi.fn().mockReturnValue(false),
224+
},
225+
}));
226+
mocksHook.useGetInvestorBalance.mockImplementation(() => ({
227+
...mocksHook.useGetInvestorBalance.getMockImplementation(),
228+
data: 0,
229+
}));
230+
mocksHook.useAsset.mockImplementation(() => ({
231+
...mocksHook.useAsset.getMockImplementation(),
232+
paused: false,
233+
}));
234+
mocksHook.useTransactions.mockImplementation(() => ({
235+
...mocksHook.useTransactions.getMockImplementation(),
236+
isActiveAccountChangeTx: false,
237+
}));
238+
239+
const { result } = renderHook(() =>
240+
useDeleteInvestor({ investorAccount: 'k:1' }),
241+
);
242+
243+
expect(result.current.isAllowed).toBe(false);
244+
});
245+
246+
it('should return false, when account is mounted, when contract is NOT paused, when account is owner, when investorbalance is not empty, when no activeAccountTx busy', () => {
247+
mocksHook.useAccount.mockImplementation(() => ({
248+
account: {
249+
address: 'k:he-man',
250+
},
251+
isOwner: true,
252+
isMounted: true,
253+
accountRoles: {
254+
isAgentAdmin: vi.fn().mockReturnValue(false),
255+
},
256+
}));
257+
mocksHook.useGetInvestorBalance.mockImplementation(() => ({
258+
...mocksHook.useGetInvestorBalance.getMockImplementation(),
259+
data: 1,
260+
}));
261+
mocksHook.useAsset.mockImplementation(() => ({
262+
...mocksHook.useAsset.getMockImplementation(),
263+
paused: false,
264+
}));
265+
mocksHook.useTransactions.mockImplementation(() => ({
266+
...mocksHook.useTransactions.getMockImplementation(),
267+
isActiveAccountChangeTx: false,
268+
}));
269+
270+
const { result } = renderHook(() =>
271+
useDeleteInvestor({ investorAccount: 'k:1' }),
272+
);
273+
274+
expect(result.current.isAllowed).toBe(false);
275+
});
276+
277+
it('should return false, when account is mounted, when contract is NOT paused, when account is owner, when investorbalance is empty, when there IS activeAccountTx busy', () => {
278+
mocksHook.useAccount.mockImplementation(() => ({
279+
account: {
280+
address: 'k:he-man',
281+
},
282+
isOwner: true,
283+
isMounted: true,
284+
accountRoles: {
285+
isAgentAdmin: vi.fn().mockReturnValue(false),
286+
},
287+
}));
288+
mocksHook.useGetInvestorBalance.mockImplementation(() => ({
289+
...mocksHook.useGetInvestorBalance.getMockImplementation(),
290+
data: 0,
291+
}));
292+
mocksHook.useAsset.mockImplementation(() => ({
293+
...mocksHook.useAsset.getMockImplementation(),
294+
paused: false,
295+
}));
296+
mocksHook.useTransactions.mockImplementation(() => ({
297+
...mocksHook.useTransactions.getMockImplementation(),
298+
isActiveAccountChangeTx: true,
299+
}));
300+
301+
const { result } = renderHook(() =>
302+
useDeleteInvestor({ investorAccount: 'k:1' }),
303+
);
304+
305+
expect(result.current.isAllowed).toBe(false);
306+
});
307+
});
308+
});

packages/apps/rwa-demo/src/hooks/deleteInvestor.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export const useDeleteInvestor = ({
6262
const result =
6363
!paused &&
6464
(accountRoles.isAgentAdmin() || isOwner) &&
65+
!isActiveAccountChangeTx &&
6566
balance !== undefined &&
6667
balance <= 0;
6768

@@ -71,14 +72,7 @@ export const useDeleteInvestor = ({
7172
`There is still a balance of ${balance} tokens on this account`,
7273
);
7374
}
74-
return;
7575
}
76-
77-
setIsAllowed(
78-
!paused &&
79-
(accountRoles.isAgentAdmin() || isOwner) &&
80-
!isActiveAccountChangeTx,
81-
);
8276
}, [
8377
paused,
8478
isOwner,

0 commit comments

Comments
 (0)