// test/Token.test.js
const Token = artifacts.require("Token");
const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
contract("Token", function(accounts) {
const [owner, recipient, spender] = accounts;
const name = "MyToken";
const symbol = "MTK";
const initialSupply = new BN('1000000000000000000000000');
beforeEach(async function() {
this.token = await Token.new(name, symbol);
});
describe("token attributes", function() {
it("has the correct name", async function() {
expect(await this.token.name()).to.equal(name);
});
it("has the correct symbol", async function() {
expect(await this.token.symbol()).to.equal(symbol);
});
it("has 18 decimals", async function() {
expect(await this.token.decimals()).to.be.bignumber.equal('18');
});
});
describe("minting", function() {
it("owner can mint tokens", async function() {
const amount = new BN('50');
const receipt = await this.token.mint(recipient, amount, { from: owner });
expectEvent(receipt, 'Transfer', {
from: constants.ZERO_ADDRESS,
to: recipient,
value: amount
});
expect(await this.token.balanceOf(recipient))
.to.be.bignumber.equal(amount);
});
it("non-owner cannot mint tokens", async function() {
const amount = new BN('50');
await expectRevert(
this.token.mint(recipient, amount, { from: spender }),
"Ownable: caller is not the owner"
);
});
});
describe("transfers", function() {
beforeEach(async function() {
await this.token.mint(owner, initialSupply);
});
it("allows token transfers", async function() {
const amount = new BN('100');
const receipt = await this.token.transfer(recipient, amount, { from: owner });
expectEvent(receipt, 'Transfer', {
from: owner,
to: recipient,
value: amount
});
expect(await this.token.balanceOf(recipient))
.to.be.bignumber.equal(amount);
});
it("reverts when trying to transfer more than balance", async function() {
const balance = await this.token.balanceOf(owner);
await expectRevert(
this.token.transfer(recipient, balance.addn(1), { from: owner }),
"ERC20: transfer amount exceeds balance"
);
});
});
});