|
| 1 | +import { renderHook } from '@testing-library/react-hooks'; |
| 2 | +import { useBatchTransferTokens } from '../batchTransferTokens'; |
| 3 | +import { useGetInvestors } from '../getInvestors'; |
| 4 | + |
| 5 | +describe('batchTransferTokens hook', () => { |
| 6 | + const mocksHook = vi.hoisted(() => { |
| 7 | + return { |
| 8 | + useAsset: vi.fn().mockReturnValue({ |
| 9 | + paused: true, |
| 10 | + }), |
| 11 | + useFreeze: vi.fn().mockReturnValue({ |
| 12 | + frozen: false, |
| 13 | + }), |
| 14 | + useGetInvestors: vi.fn().mockReturnValue({ |
| 15 | + data: [ |
| 16 | + { accountName: 'k:he-man', alias: 'he-man' }, |
| 17 | + { accountName: 'k:skeletor', alias: 'skeletor' }, |
| 18 | + ], |
| 19 | + }), |
| 20 | + useAccount: vi.fn().mockReturnValue({ |
| 21 | + account: { |
| 22 | + address: 'k:he-man', |
| 23 | + }, |
| 24 | + sign: vi.fn(), |
| 25 | + isMounted: true, |
| 26 | + isInvestor: true, |
| 27 | + balance: 0, |
| 28 | + }), |
| 29 | + |
| 30 | + useTransactions: vi.fn().mockReturnValue({ |
| 31 | + addTransaction: vi.fn(), |
| 32 | + }), |
| 33 | + useNotifications: vi.fn().mockReturnValue({ |
| 34 | + addNotification: vi.fn(), |
| 35 | + }), |
| 36 | + }; |
| 37 | + }); |
| 38 | + |
| 39 | + beforeEach(async () => { |
| 40 | + vi.mock('./../account', async () => { |
| 41 | + const actual = await vi.importActual('./../account'); |
| 42 | + return { |
| 43 | + ...actual, |
| 44 | + useAccount: mocksHook.useAccount, |
| 45 | + }; |
| 46 | + }); |
| 47 | + |
| 48 | + vi.mock('./../freeze', async () => { |
| 49 | + const actual = await vi.importActual('./../freeze'); |
| 50 | + return { |
| 51 | + ...actual, |
| 52 | + useFreeze: mocksHook.useFreeze, |
| 53 | + }; |
| 54 | + }); |
| 55 | + |
| 56 | + vi.mock('./../getInvestors', async () => { |
| 57 | + const actual = await vi.importActual('./../getInvestors'); |
| 58 | + return { |
| 59 | + ...actual, |
| 60 | + useGetInvestors: mocksHook.useGetInvestors, |
| 61 | + }; |
| 62 | + }); |
| 63 | + |
| 64 | + vi.mock('./../asset', async () => { |
| 65 | + const actual = await vi.importActual('./../asset'); |
| 66 | + return { |
| 67 | + ...actual, |
| 68 | + useAsset: mocksHook.useAsset, |
| 69 | + }; |
| 70 | + }); |
| 71 | + |
| 72 | + vi.mock('./../transactions', async () => { |
| 73 | + const actual = await vi.importActual('./../transactions'); |
| 74 | + return { |
| 75 | + ...actual, |
| 76 | + useTransactions: mocksHook.useTransactions, |
| 77 | + }; |
| 78 | + }); |
| 79 | + |
| 80 | + vi.mock('@kadena/kode-ui/patterns', async () => { |
| 81 | + const actual = await vi.importActual('@kadena/kode-ui/patterns'); |
| 82 | + return { |
| 83 | + ...actual, |
| 84 | + useNotifications: mocksHook.useNotifications, |
| 85 | + }; |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + afterEach(() => { |
| 90 | + vi.clearAllMocks(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should return the correct properties', async () => { |
| 94 | + const { result } = renderHook(() => useBatchTransferTokens()); |
| 95 | + expect(result.current.hasOwnProperty('isAllowed')).toBe(true); |
| 96 | + expect(result.current.hasOwnProperty('submit')).toBe(true); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('isAllowed', () => { |
| 100 | + it('should return true, when account is Mounted when contract is NOT paused,when account is not frozen, when account isInvestor', () => { |
| 101 | + mocksHook.useAccount.mockImplementation(() => ({ |
| 102 | + ...mocksHook.useAccount.getMockImplementation(), |
| 103 | + isMounted: true, |
| 104 | + isInvestor: true, |
| 105 | + })); |
| 106 | + |
| 107 | + mocksHook.useAsset.mockImplementation(() => ({ |
| 108 | + ...mocksHook.useAsset.getMockImplementation(), |
| 109 | + paused: false, |
| 110 | + })); |
| 111 | + |
| 112 | + mocksHook.useFreeze.mockImplementation(() => ({ |
| 113 | + ...mocksHook.useFreeze.getMockImplementation(), |
| 114 | + frozen: false, |
| 115 | + })); |
| 116 | + |
| 117 | + const { result } = renderHook(() => useBatchTransferTokens()); |
| 118 | + |
| 119 | + expect(result.current.isAllowed).toBe(true); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should return false, when account is NOT Mounted when contract is NOT paused,when account is not frozen, when account isInvestor', () => { |
| 123 | + mocksHook.useAccount.mockImplementation(() => ({ |
| 124 | + ...mocksHook.useAccount.getMockImplementation(), |
| 125 | + isMounted: false, |
| 126 | + isInvestor: true, |
| 127 | + })); |
| 128 | + |
| 129 | + mocksHook.useAsset.mockImplementation(() => ({ |
| 130 | + ...mocksHook.useAsset.getMockImplementation(), |
| 131 | + paused: false, |
| 132 | + })); |
| 133 | + |
| 134 | + mocksHook.useFreeze.mockImplementation(() => ({ |
| 135 | + ...mocksHook.useFreeze.getMockImplementation(), |
| 136 | + frozen: false, |
| 137 | + })); |
| 138 | + |
| 139 | + const { result } = renderHook(() => useBatchTransferTokens()); |
| 140 | + |
| 141 | + expect(result.current.isAllowed).toBe(false); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should return false, when account is Mounted when contract is paused,when account is not frozen, when account isInvestor', () => { |
| 145 | + mocksHook.useAccount.mockImplementation(() => ({ |
| 146 | + ...mocksHook.useAccount.getMockImplementation(), |
| 147 | + isMounted: true, |
| 148 | + isInvestor: true, |
| 149 | + })); |
| 150 | + |
| 151 | + mocksHook.useAsset.mockImplementation(() => ({ |
| 152 | + ...mocksHook.useAsset.getMockImplementation(), |
| 153 | + paused: true, |
| 154 | + })); |
| 155 | + |
| 156 | + mocksHook.useFreeze.mockImplementation(() => ({ |
| 157 | + ...mocksHook.useFreeze.getMockImplementation(), |
| 158 | + frozen: false, |
| 159 | + })); |
| 160 | + |
| 161 | + const { result } = renderHook(() => useBatchTransferTokens()); |
| 162 | + |
| 163 | + expect(result.current.isAllowed).toBe(false); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should return false, when account is Mounted when contract is NOT paused,when account is frozen, when account isInvestor', () => { |
| 167 | + mocksHook.useAccount.mockImplementation(() => ({ |
| 168 | + ...mocksHook.useAccount.getMockImplementation(), |
| 169 | + isMounted: true, |
| 170 | + isInvestor: true, |
| 171 | + })); |
| 172 | + |
| 173 | + mocksHook.useAsset.mockImplementation(() => ({ |
| 174 | + ...mocksHook.useAsset.getMockImplementation(), |
| 175 | + paused: false, |
| 176 | + })); |
| 177 | + |
| 178 | + mocksHook.useFreeze.mockImplementation(() => ({ |
| 179 | + ...mocksHook.useFreeze.getMockImplementation(), |
| 180 | + frozen: true, |
| 181 | + })); |
| 182 | + |
| 183 | + const { result } = renderHook(() => useBatchTransferTokens()); |
| 184 | + |
| 185 | + expect(result.current.isAllowed).toBe(false); |
| 186 | + }); |
| 187 | + |
| 188 | + it('should return false, when account is Mounted when contract is NOT paused,when account is NOT frozen, when account NOT isInvestor', () => { |
| 189 | + mocksHook.useAccount.mockImplementation(() => ({ |
| 190 | + ...mocksHook.useAccount.getMockImplementation(), |
| 191 | + isMounted: true, |
| 192 | + isInvestor: false, |
| 193 | + })); |
| 194 | + |
| 195 | + mocksHook.useAsset.mockImplementation(() => ({ |
| 196 | + ...mocksHook.useAsset.getMockImplementation(), |
| 197 | + paused: false, |
| 198 | + })); |
| 199 | + |
| 200 | + mocksHook.useFreeze.mockImplementation(() => ({ |
| 201 | + ...mocksHook.useFreeze.getMockImplementation(), |
| 202 | + frozen: false, |
| 203 | + })); |
| 204 | + |
| 205 | + const { result } = renderHook(() => useBatchTransferTokens()); |
| 206 | + |
| 207 | + expect(result.current.isAllowed).toBe(false); |
| 208 | + }); |
| 209 | + }); |
| 210 | +}); |
0 commit comments