import {Reflaunt} from "../Reflaunt"; import {container} from "tsyringe"; import {ConfigurationService} from "../services/ConfigurationService"; import {execSync} from "child_process"; describe('Reflaunt.constructor', function () { test('Should be able to inject', () => { const reflaunt: Reflaunt = new Reflaunt({ mnemonic: 'blame left final crime wing curious actual light drip trip patient daughter', nodeUrl: 'http://localhost:9988' }); expect(reflaunt.configurationService.config).toEqual({ mnemonic: 'blame left final crime wing curious actual light drip trip patient daughter', nodeUrl: 'http://localhost:9988' }); expect(container.resolve(ConfigurationService).config).toEqual({ mnemonic: 'blame left final crime wing curious actual light drip trip patient daughter', nodeUrl: 'http://localhost:9988' }); }) }); describe('Reflaunt.getWallet', function () { test('Should be able to get correct wallet', () => { const reflaunt: Reflaunt = new Reflaunt({ mnemonic: 'blame left final crime wing curious actual light drip trip patient daughter', nodeUrl: 'http://localhost:9988' }); const wallet = reflaunt.getWallet(1); expect(wallet.address).toEqual('0x26c6ba91ab880e34365a82da8c94153a9284e4e2'); expect(wallet.privateKey).toEqual('ffc2a24dfb610daef396ef9533b52960d233a585b0b1c2c06155e55509a86382'); }); test('Should be able to high index wallet', () => { const reflaunt: Reflaunt = new Reflaunt({ mnemonic: 'blame left final crime wing curious actual light drip trip patient daughter', nodeUrl: 'http://localhost:9988' }); const wallet = reflaunt.getWallet(1000000000); expect(wallet.address).toEqual('0xf50ecc90a3816e853850ba5f7229b24e5209d7a2'); expect(wallet.privateKey).toEqual('72fe65e4e4a545635ae8938b3245adac4f9b2907771f4c66c3fa5f391cfa19b3'); }); test('Should be able to timestamp wallet', () => { const reflaunt: Reflaunt = new Reflaunt({ mnemonic: 'blame left final crime wing curious actual light drip trip patient daughter', nodeUrl: 'http://localhost:9988' }); const wallet = reflaunt.getWallet(10000000000); expect(wallet.address).toEqual('0x9d3309c96054fe8798b791a22470066ccb8a0b26'); expect(wallet.privateKey).toEqual('4eae8a45bd03a48b2e44c0220c4c584358cdfe007bbe7c6929c51dce1ef6adc3'); }); }); describe('Instance needed', function () { let reflaunt: Reflaunt; beforeEach(async function () { execSync('./node_modules/.bin/truffle migrate --network docker --reset'); reflaunt = new Reflaunt({ mnemonic: 'blame left final crime wing curious actual light drip trip patient daughter', nodeUrl: 'http://localhost:9988' }); }); describe('Reflaunt.getAllTransferEvents', function () { test('Should be able to get all past events', async () => { const events = await reflaunt.getAllTransferEvents({}); expect(events.length).toEqual(0); const instance = await reflaunt.loginAs(0); await instance.createProduct(0,0, {}); const eventsAfter = await reflaunt.getAllTransferEvents({}); expect(eventsAfter.length).toEqual(1); }); }); describe('Reflaunt.getTransferEventsOfProduct', function () { test('Should be able to get all transfer events of a product', async () => { const events = await reflaunt.getTransferEventsOfProduct(0, {}); expect(events.length).toEqual(0); const instance = await reflaunt.loginAs(0); await instance.createProduct(0,1, {}); const eventsAfter = await reflaunt.getTransferEventsOfProduct(0,{}); expect(eventsAfter.length).toEqual(0); await instance.createProduct(0,0, {}); const eventsAfter2 = await reflaunt.getTransferEventsOfProduct(0,{}); expect(eventsAfter2.length).toEqual(1); }); }); describe('Reflaunt.getProductOwner', function () { test('should be able to get correct owner', async () => { const owner = await reflaunt.getProductOwner(1); expect(owner).toBeNull(); const instance = await reflaunt.loginAs(0); await instance.createProduct(0, 0, {}); const ownerAfter = await reflaunt.getProductOwner(0); expect(ownerAfter).toEqual("0x34AAb7A2b6800963a3012BbF54970A383a79a94F"); }) }); describe('Reflaunt.getProductMetadata', function () { test('Should be able to get product metadata', async () => { const metadata = await reflaunt.getProductMetadata(1); expect(metadata).toBeNull(); const instance = await reflaunt.loginAs(0); await instance.createProduct(0, 0); const ownerAfter = await reflaunt.getProductMetadata(0); expect(ownerAfter.indexOf('random_url_')).toEqual(0); }) }); describe('Reflaunt.watchTransferEvent', function () { let onData, onFail; beforeEach(() => { onData = jest.fn(); onFail = jest.fn(); }); test('Should watch event correctly', async () => { const event = await reflaunt.watchTransferEvent(onData, onFail); expect(onData).not.toHaveBeenCalled(); expect(onFail).not.toHaveBeenCalled(); const instance = await reflaunt.loginAs(0); await instance.createProduct(0,0); expect(onData).toHaveBeenCalledTimes(1); expect(onData).toHaveBeenCalledWith(expect.objectContaining({ eventName: "Transfer", data: { fromAddress: null, isNew: true, productId: 0, toAddress: "0x34AAb7A2b6800963a3012BbF54970A383a79a94F" } })); }) }) });