|
| 1 | +import { Client } from '../src/mintlayer-connect-sdk'; |
| 2 | +import fetchMock from 'jest-fetch-mock'; |
| 3 | + |
| 4 | +import { addresses, utxos } from './__mocks__/accounts/account_01' |
| 5 | + |
| 6 | +beforeEach(() => { |
| 7 | + fetchMock.resetMocks(); |
| 8 | + |
| 9 | + // эмуляция window.mojito |
| 10 | + (window as any).mojito = { |
| 11 | + isExtension: true, |
| 12 | + connect: jest.fn().mockResolvedValue(addresses), |
| 13 | + restore: jest.fn().mockResolvedValue(addresses), |
| 14 | + disconnect: jest.fn().mockResolvedValue(undefined), |
| 15 | + request: jest.fn().mockResolvedValue('signed-transaction'), |
| 16 | + }; |
| 17 | + |
| 18 | + // API /chain/tip |
| 19 | + fetchMock.mockIf('https://api-server-lovelace.mintlayer.org/api/v2/chain/tip', async () => { |
| 20 | + return { |
| 21 | + body: JSON.stringify({ height: 200000 }), |
| 22 | + }; |
| 23 | + }); |
| 24 | + |
| 25 | + // API /account |
| 26 | + fetchMock.mockIf('https://api.mintini.app/account', async () => { |
| 27 | + return { |
| 28 | + body: JSON.stringify({ |
| 29 | + utxos: utxos, |
| 30 | + }), |
| 31 | + }; |
| 32 | + }); |
| 33 | +}); |
| 34 | + |
| 35 | +test('preview utxo after transfer', async () => { |
| 36 | + const client = await Client.create({ network: 'testnet', autoRestore: false }); |
| 37 | + |
| 38 | + await client.connect(); |
| 39 | + |
| 40 | + const transaction = await client.buildTransaction({ |
| 41 | + type: 'Transfer', |
| 42 | + params: { |
| 43 | + to: 'tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc', |
| 44 | + amount: 10, |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + const { spent, created } = client.previewUtxoChange(transaction); |
| 49 | + |
| 50 | + expect(spent.length).toBe(2); |
| 51 | + expect(spent[0].outpoint.source_id).toBe('af3b5fad20f6f97eb210934e942176f7f7d0f70423590659ee0e0217053a7cab'); |
| 52 | + expect(spent[0].outpoint.index).toBe(1); |
| 53 | + expect(created.length).toBe(2); |
| 54 | + expect(created[0].outpoint.source_id).toBe('05d9ec811338b736816f64965f17844cdbf45711f0892534f3f7408344052bcc'); |
| 55 | + expect(created[0].outpoint.index).toBe(0); |
| 56 | + expect(created[1].utxo.destination).toBe('tmt1q9mfg7d6ul2nt5yhmm7l7r6wwyqkd822rymr83uc'); |
| 57 | + expect(created[1].utxo.value.amount.decimal).toBe('10'); |
| 58 | + expect(created[1].outpoint.source_id).toBe('05d9ec811338b736816f64965f17844cdbf45711f0892534f3f7408344052bcc'); |
| 59 | + expect(created[1].outpoint.index).toBe(1); |
| 60 | + expect(created[1].utxo.type).toBe('Transfer'); |
| 61 | + expect(created[1].utxo.value.type).toBe('Coin'); |
| 62 | + expect(created[1].utxo.value.amount.atoms).toBe('1701805604300000'); |
| 63 | +}); |
0 commit comments