DReddit_spec.js
1 /* global web3, config, contract, assert */ 2 const DReddit = require('Embark/contracts/DReddit'); 3 4 const ipfsHash = 'Qmc5gCcjYypU7y28oCALwfSvxCBskLuPKWpK4qpterKC7z'; 5 let accounts = []; 6 let postId = -1; 7 8 config({ 9 contracts: { 10 DReddit: {} 11 } 12 }, (err, _accounts) => { 13 accounts = _accounts; 14 }); 15 16 contract('DReddit', () => { 17 18 it('should work', () => { 19 assert.ok(true); 20 }); 21 22 it('should be able to create a post and receive it via contract event', async () => { 23 let receipt = await DReddit.methods.create(web3.utils.fromAscii(ipfsHash)).send(); 24 const event = receipt.events.NewPost; 25 postId = event.returnValues.postId; 26 assert.equal(web3.utils.toAscii(event.returnValues.description), ipfsHash); 27 }); 28 29 it ('post should have correct data', async () => { 30 const post = await DReddit.methods.posts(postId).call(); 31 assert.equal(web3.utils.toAscii(post.description), ipfsHash); 32 assert.equal(post.owner, accounts[0]); 33 }); 34 35 it("should not be able to vote in an unexisting post", async () => { 36 const userCanVote = await DReddit.methods.canVote("123").call(); 37 assert.equal(userCanVote, false); 38 }); 39 40 it("should be able to vote in a post if account hasn't voted before", async () => { 41 const userCanVote = await DReddit.methods.canVote(postId).call(); 42 assert.equal(userCanVote, true); 43 }); 44 45 it('should be able to vote in a post', async () => { 46 const receipt = await DReddit.methods.vote(postId, 1).send(); 47 const Vote = receipt.events.Vote; 48 assert.equal(Vote.returnValues.voter, accounts[0]); 49 }); 50 51 it('shouldn\'t be able to vote twice', async () => { 52 try { 53 await DReddit.methods.vote(postId, 1).send(); 54 assert.fail('should have reverted before'); 55 } catch (error){ 56 assert(error.message.search('revert') > -1, 'Revert should happen'); 57 } 58 }); 59 });