CappedCrowdsale.test.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const { accounts, contract } = require('@openzeppelin/test-environment');
  2. const { BN, ether, expectRevert } = require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. const CappedCrowdsaleImpl = contract.fromArtifact('CappedCrowdsaleImpl');
  5. const SimpleToken = contract.fromArtifact('SimpleToken');
  6. describe('CappedCrowdsale', function () {
  7. const [ wallet ] = accounts;
  8. const rate = new BN('1');
  9. const cap = ether('100');
  10. const lessThanCap = ether('60');
  11. const tokenSupply = new BN('10').pow(new BN('22'));
  12. beforeEach(async function () {
  13. this.token = await SimpleToken.new();
  14. });
  15. it('rejects a cap of zero', async function () {
  16. await expectRevert(CappedCrowdsaleImpl.new(rate, wallet, this.token.address, 0),
  17. 'CappedCrowdsale: cap is 0'
  18. );
  19. });
  20. context('with crowdsale', function () {
  21. beforeEach(async function () {
  22. this.crowdsale = await CappedCrowdsaleImpl.new(rate, wallet, this.token.address, cap);
  23. await this.token.transfer(this.crowdsale.address, tokenSupply);
  24. });
  25. describe('accepting payments', function () {
  26. it('should accept payments within cap', async function () {
  27. await this.crowdsale.send(cap.sub(lessThanCap));
  28. await this.crowdsale.send(lessThanCap);
  29. });
  30. it('should reject payments outside cap', async function () {
  31. await this.crowdsale.send(cap);
  32. await expectRevert(this.crowdsale.send(1), 'CappedCrowdsale: cap exceeded');
  33. });
  34. it('should reject payments that exceed cap', async function () {
  35. await expectRevert(this.crowdsale.send(cap.addn(1)), 'CappedCrowdsale: cap exceeded');
  36. });
  37. });
  38. describe('ending', function () {
  39. it('should not reach cap if sent under cap', async function () {
  40. await this.crowdsale.send(lessThanCap);
  41. expect(await this.crowdsale.capReached()).to.equal(false);
  42. });
  43. it('should not reach cap if sent just under cap', async function () {
  44. await this.crowdsale.send(cap.subn(1));
  45. expect(await this.crowdsale.capReached()).to.equal(false);
  46. });
  47. it('should reach cap if cap sent', async function () {
  48. await this.crowdsale.send(cap);
  49. expect(await this.crowdsale.capReached()).to.equal(true);
  50. });
  51. });
  52. });
  53. });