Help understand JavaScript unit test

Hi,
I got the code from the following link:
quick guide

it("Test balance after deposit", function() {
        return SimpleBank.deployed().then(function(instance) {
            sb = instance;
            return sb.deposit({ from: accounts[0], value: web3.utils.toWei('10', 'ether') });
        }).then(function(tx_receipt) {
            return sb.getBalance({ from: accounts[0] });
        }).then(function(x) {
            assert.equal(web3.utils.toWei('10', 'ether'), x, "Wrong balance");
        }).then(function() {
            return sb.getBalance({ from: accounts[1] });
        }).then(function(x) {
            assert.equal(0, x, "Wrong balance");
        });
    });
});

==

Somebody please provide me line by line explanation of above code, I would highly appreciate it. I have problem with the keyword ‘function()’, it is used with different arguments and sometimes with no arguments. deposit() is defined in the contract with no arguments, but in the above code deposit() is used i.e. its arguments as called in other language are very strange , I have not seen it in any other language. What is the purpose of tx_receipt? We have ‘return’ statement at the start of the first bracket which then starts another block then sb is initialized, then again a ‘return’ statement but this time hooked to toWei(…)which is connected to ‘then’ and then we have another block and so on . Each time the block starts with 'then function() ’ but argument of ‘function’ can be different, two times, it is ‘x’, please guide me.
Zulfi.

:computer: Environment

:memo:Details

:1234: Code to reproduce

Emmm, as a matter of fact, there is no parameter for the deposit,

sb.deposit({ from: accounts[0], value: web3.utils.toWei('10', 'ether') });

cause it uses {}, and this is a fixed usage, the keyword from means who is the caller, the value means how many ethers who you like to transfer when call this function. If you want to pass a parameter for a function, you should write like following:

ERC20Token.transfer(recipientAddress, amount).

that is:

ERC20Token.transfer(recipientAddress, amount, {from: accounts[0]}).
2 Likes

Hi,
Thanks a lot. Kindly tell me about the ‘function’ command

God blesses you.

Zulfi.