import {Reflaunt} from "../Reflaunt"; import {execSync} from "child_process"; beforeEach(async function () { execSync('./node_modules/.bin/truffle migrate --network docker --reset'); }); describe('Reflaunt.getMyBalance', function () { test('Should be able to get correct balance', async () => { const reflaunt: Reflaunt = new Reflaunt({ mnemonic: 'blame left final crime wing curious actual light drip trip patient daughter', nodeUrl: 'http://localhost:9988' }); const instance = await reflaunt.loginAs(0); const bal = await instance.getMyBalance(); expect(bal).toEqual(0); await instance.createProduct(0, 0); const balAfter = await instance.getMyBalance(); expect(balAfter).toEqual(1); }); }); describe('Reflaunt.getMyEtherBalance', function () { test('Should be able to get correct ether balance', async () => { const reflaunt: Reflaunt = new Reflaunt({ mnemonic: 'blame left final crime wing curious actual light drip trip patient daughter', nodeUrl: 'http://localhost:9988' }); const instance = await reflaunt.loginAs(0); const bal = await instance.getMyEtherBalance(); await instance.createProduct(0, 0); const balAfter = await instance.getMyEtherBalance(); expect(balAfter).toBeLessThan(bal); }); }); describe('Reflaunt.getMyTransactionCount', function () { test('Should be able to get correct transaction count', async () => { const reflaunt: Reflaunt = new Reflaunt({ mnemonic: 'blame left final crime wing curious actual light drip trip patient daughter', nodeUrl: 'http://localhost:9988' }); const instance = await reflaunt.loginAs(0); const ct = await instance.getMyTransactionCount(); await instance.createProduct(0, 0); const ctAfter = await instance.getMyTransactionCount(); expect(ctAfter).toBeGreaterThan(ct); }); }); describe('Reflaunt.sendEther', function () { test('Should be able to send ether to another account', async () => { const reflaunt: Reflaunt = new Reflaunt({ mnemonic: 'blame left final crime wing curious actual light drip trip patient daughter', nodeUrl: 'http://localhost:9988' }); const instance = await reflaunt.loginAs(0); const anotherInstance = await reflaunt.loginAs(1); const bal = await instance.getMyEtherBalance(); const anotherBal = await anotherInstance.getMyEtherBalance(); await instance.sendEther(1, 1); // cannot use toEqual(bal - 1) as we need to consider the ether spent for gas too expect(await instance.getMyEtherBalance()).toBeLessThan(bal); expect(await anotherInstance.getMyEtherBalance()).toEqual(anotherBal + 1); }); }); describe('Reflaunt.createProduct', function () { test('Should be able to create correct product', async () => { const reflaunt: Reflaunt = new Reflaunt({ mnemonic: 'blame left final crime wing curious actual light drip trip patient daughter', nodeUrl: 'http://localhost:9988' }); const instance = await reflaunt.loginAs(0); const product = await instance.createProduct(1, 1); expect(parseInt(product.receipt.logs[0].args.tokenId)).toEqual(1); }); }); describe('Reflaunt.transferFrom', function () { test('Should be able to transfer product from someont correctly', async () => { const reflaunt: Reflaunt = new Reflaunt({ mnemonic: 'blame left final crime wing curious actual light drip trip patient daughter', nodeUrl: 'http://localhost:9988' }); const instance = await reflaunt.loginAs(0); const ownerInstance = await reflaunt.loginAs(2); const anotherInstance = await reflaunt.loginAs(1); expect(await instance.getMyBalance()).toEqual(0); expect(await ownerInstance.getMyBalance()).toEqual(0); expect(await anotherInstance.getMyBalance()).toEqual(0); await instance.createProduct(2, 0); expect(await instance.getMyBalance()).toEqual(0); expect(await ownerInstance.getMyBalance()).toEqual(1); expect(await anotherInstance.getMyBalance()).toEqual(0); await ownerInstance.transferFrom(2, 1, 0); expect(await instance.getMyBalance()).toEqual(0); expect(await ownerInstance.getMyBalance()).toEqual(0); expect(await anotherInstance.getMyBalance()).toEqual(1); }); }); describe('Reflaunt.addProductCreator', function () { test('Should be able to transfer product from someone correctly', async () => { let reflaunt: Reflaunt = new Reflaunt({ mnemonic: 'blame left final crime wing curious actual light drip trip patient daughter', nodeUrl: 'http://localhost:9988' }); const instance = await reflaunt.loginAs(2); await expect((async () => { const data = await instance.createProduct.bind({ // hack to make sure it doesnt throw during this.removeListener("receipt", handlers.receipt); in // truffle contract removeListener: () => { } },1, 0)(); // hack to throw error when using ganache as it was always resolved with undefined tx // https://github.com/trufflesuite/truffle/issues/2512 if (data.tx === undefined) { throw new Error('tx undefined'); } })()).rejects.toBeTruthy(); const curOwner = await reflaunt.getProductOwner(0); expect(curOwner).toBeNull(); const owner = await reflaunt.loginAs(0); await owner.addProductCreator(2); const product = await instance.createProduct(1, 0); expect(parseInt(product.receipt.logs[0].args.tokenId)).toEqual(0); }); });