Test-Suite.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/dist/src/signers";
  2. import { Contract } from "ethers";
  3. import { ethers } from "hardhat";
  4. import { expect } from "./chai-setup";
  5. describe("Token contract", function () {
  6. // Mocha has four functions that let you hook into the the test runner's
  7. // lifecyle. These are: `before`, `beforeEach`, `after`, `afterEach`.
  8. // They're very useful to setup the environment for tests, and to clean it
  9. // up after they run.
  10. // A common pattern is to declare some variables, and assign them in the
  11. // `before` and `beforeEach` callbacks.
  12. let Token;
  13. let hardhatToken: Contract;
  14. let owner: SignerWithAddress;
  15. let addr1: SignerWithAddress;
  16. let addr2: SignerWithAddress;
  17. let addrs: SignerWithAddress[];
  18. // `beforeEach` will run before each test, re-deploying the contract every
  19. // time. It receives a callback, which can be async.
  20. beforeEach(async function () {
  21. // Get the ContractFactory and Signers here.
  22. Token = await ethers.getContractFactory("NFTToken");
  23. [owner, addr1, addr2, ...addrs] = await ethers.getSigners();
  24. // To deploy our contract, we just have to call Token.deploy() and await
  25. // for it to be deployed(), which happens onces its transaction has been
  26. // mined.
  27. hardhatToken = await Token.deploy();
  28. });
  29. // You can nest describe calls to create subsections.
  30. describe("Deployment", function () {
  31. // `it` is another Mocha function. This is the one you use to define your
  32. // tests. It receives the test name, and a callback function.
  33. // If the callback function is async, Mocha will `await` it.
  34. it("Should set the right owner", async function () {
  35. // Expect receives a value, and wraps it in an Assertion object. These
  36. // objects have a lot of utility methods to assert values.
  37. // This test expects the owner variable stored in the contract to be equal
  38. // to our Signer's owner.
  39. expect(await hardhatToken.owner()).to.equal(owner.address);
  40. });
  41. it("Should Set Initial Token Supply To Zero", async function () {
  42. const factory = await ethers.getContractFactory("NFTToken");
  43. const phd = await factory.deploy();
  44. await phd.deployed();
  45. expect(await phd.totalSupply()).to.equal(0);
  46. });
  47. });
  48. describe("Transactions", function () {
  49. xit("Should transfer tokens between accounts", async function () {
  50. // Transfer 50 tokens from owner to addr1
  51. await hardhatToken.safeTransferFrom(
  52. addr1.address,
  53. addr2.address,
  54. 1
  55. );
  56. const addr1Balance = await hardhatToken.balanceOf(addr1.address);
  57. expect(addr1Balance).to.equal(50);
  58. // Transfer 50 tokens from addr1 to addr2
  59. // We use .connect(signer) to send a transaction from another account
  60. await hardhatToken.connect(addr1).transfer(addr2.address, 50);
  61. const addr2Balance = await hardhatToken.balanceOf(addr2.address);
  62. expect(addr2Balance).to.equal(50);
  63. });
  64. xit("Should fail if sender doesn’t have enough tokens", async function () {
  65. const initialOwnerBalance = await hardhatToken.balanceOf(
  66. owner.address
  67. );
  68. // Try to send 1 token from addr1 (0 tokens) to owner (1000 tokens).
  69. // `require` will evaluate false and revert the transaction.
  70. await expect(
  71. hardhatToken.connect(addr1).transfer(owner.address, 1)
  72. ).to.be.revertedWith("Not enough tokens");
  73. // Owner balance shouldn't have changed.
  74. expect(await hardhatToken.balanceOf(owner.address)).to.equal(
  75. initialOwnerBalance
  76. );
  77. });
  78. xit("Should update balances after transfers", async function () {
  79. const initialOwnerBalance = await hardhatToken.balanceOf(
  80. owner.address
  81. );
  82. // Transfer 100 tokens from owner to addr1.
  83. await hardhatToken.transfer(addr1.address, 100);
  84. // Transfer another 50 tokens from owner to addr2.
  85. await hardhatToken.transfer(addr2.address, 50);
  86. // Check balances.
  87. const finalOwnerBalance = await hardhatToken.balanceOf(
  88. owner.address
  89. );
  90. expect(finalOwnerBalance).to.equal(initialOwnerBalance - 150);
  91. const addr1Balance = await hardhatToken.balanceOf(addr1.address);
  92. expect(addr1Balance).to.equal(100);
  93. const addr2Balance = await hardhatToken.balanceOf(addr2.address);
  94. expect(addr2Balance).to.equal(50);
  95. });
  96. });
  97. });