Votes.test.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const { expectRevert, BN } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { getChainId } = require('../../helpers/chainid');
  4. const { clockFromReceipt } = require('../../helpers/time');
  5. const { shouldBehaveLikeVotes } = require('./Votes.behavior');
  6. const MODES = {
  7. blocknumber: artifacts.require('$VotesMock'),
  8. timestamp: artifacts.require('$VotesTimestampMock'),
  9. };
  10. contract('Votes', function (accounts) {
  11. const [account1, account2, account3] = accounts;
  12. for (const [mode, artifact] of Object.entries(MODES)) {
  13. describe(`vote with ${mode}`, function () {
  14. beforeEach(async function () {
  15. this.name = 'My Vote';
  16. this.votes = await artifact.new(this.name, '1');
  17. });
  18. it('starts with zero votes', async function () {
  19. expect(await this.votes.getTotalSupply()).to.be.bignumber.equal('0');
  20. });
  21. describe('performs voting operations', function () {
  22. beforeEach(async function () {
  23. this.tx1 = await this.votes.$_mint(account1, 1);
  24. this.tx2 = await this.votes.$_mint(account2, 1);
  25. this.tx3 = await this.votes.$_mint(account3, 1);
  26. this.tx1.timepoint = await clockFromReceipt[mode](this.tx1.receipt);
  27. this.tx2.timepoint = await clockFromReceipt[mode](this.tx2.receipt);
  28. this.tx3.timepoint = await clockFromReceipt[mode](this.tx3.receipt);
  29. });
  30. it('reverts if block number >= current block', async function () {
  31. await expectRevert(this.votes.getPastTotalSupply(this.tx3.timepoint + 1), 'Votes: future lookup');
  32. });
  33. it('delegates', async function () {
  34. await this.votes.delegate(account3, account2);
  35. expect(await this.votes.delegates(account3)).to.be.equal(account2);
  36. });
  37. it('returns total amount of votes', async function () {
  38. expect(await this.votes.getTotalSupply()).to.be.bignumber.equal('3');
  39. });
  40. });
  41. describe('performs voting workflow', function () {
  42. beforeEach(async function () {
  43. this.chainId = await getChainId();
  44. this.account1 = account1;
  45. this.account2 = account2;
  46. this.account1Delegatee = account2;
  47. this.NFT0 = new BN('10000000000000000000000000');
  48. this.NFT1 = new BN('10');
  49. this.NFT2 = new BN('20');
  50. this.NFT3 = new BN('30');
  51. });
  52. // includes EIP6372 behavior check
  53. shouldBehaveLikeVotes(mode);
  54. });
  55. });
  56. }
  57. });