dtwitter_spec.js
1 // our DTwitter contract object to test 2 const DTwitter = require('Embark/contracts/DTwitter'); 3 4 // contract methods we'll be testing 5 const { createAccount, users, owners, userExists, editAccount, tweet } = DTwitter.methods; 6 7 // variables that will be updated in the tests 8 let accounts; 9 10 // set up our config test parameters 11 config({ 12 contracts: { 13 DTwitter: { 14 // would pass constructor args here if needed 15 } 16 } 17 }, (err, theAccounts) => { 18 // this is the list of accounts our node / wallet controls. 19 accounts = theAccounts; 20 }); 21 22 // other test parameters 23 const username = 'testhandle'; 24 const description = 'test description'; 25 const tweetContent = 'test tweet'; 26 27 // Embark exposes a global contract method as an alias 28 // for Mocha.describe 29 contract("DTwitter contract", function () { 30 this.timeout(0); 31 32 33 // it(`should create a dtwitter user '${username}' with description '${description}'`, async function () { 34 it("transaction to create a dtwitter user 'testhandle' with description 'test description' should be successful", async function () { 35 36 // do the create account 37 const createAccountTx = await createAccount(username, description).send(); 38 39 // assert that the transaction was successful 40 assert.equal(createAccountTx.status, true); 41 42 }); 43 44 it("should have created a user 'testhandle'", async function () { 45 46 // get user details from contract 47 const user = await users(web3.utils.keccak256(username)).call(); 48 49 assert.equal(user.username, username); 50 assert.equal(user.description, description); 51 52 }); 53 54 it("should have created an owner for our defaultAccount", async function () { 55 56 // read from the owners mapping 57 const usernameHash = await owners(web3.eth.defaultAccount).call(); 58 59 // check the return value from owners mapping matches 60 assert.equal(usernameHash, web3.utils.keccak256(username)); 61 }); 62 63 it("should know 'testhandle' exists", async function () { 64 const usernameHash = web3.utils.keccak256(username); 65 const exists = await userExists(usernameHash).call(); 66 67 assert.equal(exists, true); 68 }); 69 70 71 it("should be able to edit 'testhandle' user details", async function () { 72 const usernameHash = web3.utils.keccak256(username); 73 const updatedDescription = description + ' edited'; 74 const updatedImageHash = 'QmWvPtv2xVGgdV12cezG7iCQ4hQ52e4ptmFFnBK3gTjnec'; 75 76 await editAccount(usernameHash, updatedDescription, updatedImageHash).send(); 77 78 const updatedUserDetails = await users(usernameHash).call(); 79 80 assert.equal(updatedUserDetails.description, updatedDescription); 81 assert.equal(updatedUserDetails.picture, updatedImageHash); 82 }); 83 84 it("should be able to add a tweet as 'testhandle' and receive it via contract event", async function () { 85 const usernameHash = web3.utils.keccak256(username); 86 87 await tweet(tweetContent).send(); 88 89 DTwitter.events.NewTweet({ 90 filter: { _from: usernameHash }, 91 fromBlock: 1 92 }) 93 .on('data', (event) => { 94 assert.equal(event.returnValues.tweet, tweetContent); 95 }); 96 }); 97 98 });