embark/dapps/tests/app/test/simple_storage_spec.js
Jonathan Rainville a3b52676fc Fix part of the test app and add new test util functions (#1977)
* fix: fix tests hanging because the console is not started

* fix(@embark/proxy): send back errors correctly to the client

Code originally by @emizzle and fixed by me

* feat(@embark/test-runner): add assert.reverts to test reverts

* fix: make test app actually run its test and not hang

* fix(@embark/proxy): fix listening to contract event in the proxy

* feat(@embark/test-runner): add assertion for events being triggered

* docs(@embark/site): add docs for the new assert functions

* feat(@embark/test-runner): add increaseTime util function to globals

* docs(@embark/site): add docs for increaseTime
2019-10-17 14:39:25 -04:00

71 lines
2.2 KiB
JavaScript

/*global contract, config, it, assert, web3*/
const SimpleStorage = require('Embark/contracts/SimpleStorage');
let accounts;
const {Utils} = require('Embark/EmbarkJS');
config({
contracts: {
deploy: {
"SimpleStorage": {
args: [100],
onDeploy: ["SimpleStorage.methods.setRegistar('$SimpleStorage').send()"]
}
}
}
}, (err, theAccounts) => {
accounts = theAccounts;
});
contract("SimpleStorage", function() {
this.timeout(0);
it("should set constructor value", async function() {
let result = await SimpleStorage.methods.storedData().call();
assert.strictEqual(parseInt(result, 10), 100);
});
it("set storage value", function(done) {
Utils.secureSend(web3, SimpleStorage.methods.set(150), {}, false, async function(err) {
if (err) {
return done(err);
}
let result = await SimpleStorage.methods.get().call();
assert.strictEqual(parseInt(result, 10), 150);
done();
});
});
it("should set to self address", async function() {
let result = await SimpleStorage.methods.registar().call();
assert.strictEqual(result, SimpleStorage.options.address);
});
it('should have the right defaultAccount', function() {
assert.strictEqual(accounts[0], web3.eth.defaultAccount);
});
it("should alias contract address", function() {
assert.strictEqual(SimpleStorage.options.address, SimpleStorage.address);
});
it('listens to events', function(done) {
SimpleStorage.once('EventOnSet2', async function(error, _result) {
assert.strictEqual(error, null);
let result = await SimpleStorage.methods.get().call();
assert.strictEqual(parseInt(result, 10), 150);
done(error);
});
SimpleStorage.methods.set2(150).send();
});
it('asserts event triggered', async function() {
const tx = await SimpleStorage.methods.set2(160).send();
assert.eventEmitted(tx, 'EventOnSet2', {passed: true, message: "hi"});
});
it("should revert with a value lower than 5", async function() {
await assert.reverts(SimpleStorage.methods.set3(2), {from: web3.eth.defaultAccount}, 'Returned error: VM Exception while processing transaction: revert Value needs to be higher than 5');
});
});