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