embark/dapps/tests/app/test/simple_storage_spec.js

81 lines
2.6 KiB
JavaScript
Raw Normal View History

/*global contract, config, it, assert, web3*/
const SimpleStorage = artifacts.require('SimpleStorage');
2018-06-06 17:54:20 +00:00
let accounts;
const {Utils} = artifacts.require('EmbarkJS');
2018-01-15 14:51:45 +00:00
2018-06-01 14:48:29 +00:00
config({
contracts: {
deploy: {
"SimpleStorage": {
args: [100],
onDeploy: (dependencies) => {
return dependencies.contracts.SimpleStorage.methods.setRegistar(dependencies.contracts.SimpleStorage.options.address).send();
}
}
2018-06-01 14:48:29 +00:00
}
}
2018-06-06 17:54:20 +00:00
}, (err, theAccounts) => {
accounts = theAccounts;
2018-06-01 14:48:29 +00:00
});
2018-01-15 14:51:45 +00:00
contract("SimpleStorage", function() {
2018-01-05 20:10:47 +00:00
this.timeout(0);
2018-01-15 14:51:45 +00:00
it("should set constructor value", async function() {
2018-04-17 18:48:31 +00:00
let result = await SimpleStorage.methods.storedData().call();
2018-06-01 14:48:29 +00:00
assert.strictEqual(parseInt(result, 10), 100);
2017-02-10 00:38:02 +00:00
});
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();
});
2017-02-10 00:38:02 +00:00
});
2018-06-06 14:39:02 +00:00
it("should set to self address", async function() {
2018-06-06 14:39:02 +00:00
let result = await SimpleStorage.methods.registar().call();
assert.strictEqual(result, SimpleStorage.options.address);
});
it('should have the right defaultAccount', function() {
2018-06-06 17:54:20 +00:00
assert.strictEqual(accounts[0], web3.eth.defaultAccount);
2018-06-06 14:39:02 +00:00
});
it("should alias contract address", function() {
2018-06-06 16:47:16 +00:00
assert.strictEqual(SimpleStorage.options.address, SimpleStorage.address);
});
it('listens to events', async function() {
const promise = new Promise((resolve) => {
SimpleStorage.once("EventOnSet2", (error, result) => {
assert.strictEqual(error, null);
assert.strictEqual(parseInt(result.returnValues.setValue, 10), 150);
resolve();
});
2018-06-12 15:38:25 +00:00
});
await SimpleStorage.methods.set2(150).send();
// execute the same method/value twice as a workaround for getting this test
// to pass with --node=embark. The cause of the issue and how the workaround
// works is still unknown.
await SimpleStorage.methods.set2(150).send();
return promise;
2018-06-12 15:38:25 +00:00
});
it('asserts event triggered', async function() {
const tx = await SimpleStorage.methods.set2(160).send();
assert.eventEmitted(tx, 'EventOnSet2', {passed: true, message: "hi", setValue: "160"});
});
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');
});
2017-02-10 00:38:02 +00:00
});