dappconnect-vote-poll-sdk/packages/core/test/models/PollInitMsg.test.ts

119 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-08-09 14:00:42 +00:00
import { expect } from 'chai'
2021-08-12 14:37:21 +00:00
import { createSignMsgParams, PollInitMsg } from '../../src/models/PollInitMsg'
2021-08-09 14:00:42 +00:00
import { MockProvider } from 'ethereum-waffle'
import { PollType } from '../../src/types/PollType'
2021-08-12 14:37:21 +00:00
import { BigNumber } from 'ethers'
2021-08-09 14:00:42 +00:00
describe('PollInitMsg', () => {
const provider = new MockProvider()
const [alice] = provider.getWallets()
it('success', async () => {
2021-08-12 14:37:21 +00:00
const poll = await PollInitMsg._createWithSignFunction(
async (params) => params.join(),
alice,
'test',
['one', 'two', 'three'],
PollType.WEIGHTED
)
2021-08-09 14:00:42 +00:00
expect(poll).to.not.be.undefined
2021-08-12 14:37:21 +00:00
if (poll) {
expect(poll.owner).to.eq(alice.address)
2021-08-13 11:41:08 +00:00
expect(poll.endTime).to.eq(poll.timestamp + 100000000)
2021-08-12 14:37:21 +00:00
expect(poll.answers).to.deep.eq(['one', 'two', 'three'])
expect(poll.minToken).to.be.undefined
expect(poll.pollType).to.eq(PollType.WEIGHTED)
expect(poll.question).to.eq('test')
2021-08-09 14:00:42 +00:00
2021-08-12 14:37:21 +00:00
expect(poll.signature).to.eq(
[
poll.owner,
JSON.stringify(
createSignMsgParams({
owner: poll.owner,
2021-08-13 11:41:08 +00:00
timestamp: poll.timestamp,
question: poll.question,
2021-08-12 14:37:21 +00:00
answers: poll.answers,
pollType: poll.pollType,
2021-08-13 11:41:08 +00:00
endTime: poll.endTime,
2021-08-12 14:37:21 +00:00
minToken: poll.minToken,
})
),
].join()
)
}
2021-08-09 14:00:42 +00:00
})
it('success NON_WEIGHTED', async () => {
2021-08-12 14:37:21 +00:00
const poll = await PollInitMsg._createWithSignFunction(
async (params) => params.join(),
2021-08-10 12:55:47 +00:00
alice,
2021-08-09 14:00:42 +00:00
'test',
['one', 'two', 'three'],
PollType.NON_WEIGHTED,
BigNumber.from(123)
)
2021-08-12 14:37:21 +00:00
expect(poll).to.not.be.undefined
2021-08-09 14:00:42 +00:00
expect(poll?.minToken?.toNumber()).to.eq(123)
2021-08-12 14:37:21 +00:00
if (poll) {
expect(poll.signature).to.eq(
[
poll.owner,
JSON.stringify(
createSignMsgParams({
owner: poll.owner,
2021-08-13 11:41:08 +00:00
timestamp: poll.timestamp,
question: poll.question,
2021-08-12 14:37:21 +00:00
answers: poll.answers,
pollType: poll.pollType,
2021-08-13 11:41:08 +00:00
endTime: poll.endTime,
2021-08-12 14:37:21 +00:00
minToken: poll.minToken,
})
),
].join()
)
}
2021-08-09 14:00:42 +00:00
})
it('NON_WEIGHTED no minToken', async () => {
2021-08-12 14:37:21 +00:00
const poll = await PollInitMsg._createWithSignFunction(
async (params) => params.join(),
alice,
'test',
['one', 'two', 'three'],
PollType.NON_WEIGHTED
)
2021-08-09 14:00:42 +00:00
expect(poll?.minToken?.toNumber()).to.eq(1)
2021-08-12 14:37:21 +00:00
expect(poll).to.not.be.undefined
if (poll) {
const msg = createSignMsgParams({
owner: poll.owner,
2021-08-13 11:41:08 +00:00
timestamp: poll.timestamp,
question: poll.question,
2021-08-12 14:37:21 +00:00
answers: poll.answers,
pollType: poll.pollType,
2021-08-13 11:41:08 +00:00
endTime: poll.endTime,
2021-08-12 14:37:21 +00:00
minToken: poll.minToken,
})
2021-08-09 14:00:42 +00:00
2021-08-12 14:37:21 +00:00
expect(poll.signature).to.eq([poll.owner, JSON.stringify(msg)].join())
}
2021-08-09 14:00:42 +00:00
})
it('specific end time', async () => {
2021-08-12 14:37:21 +00:00
const poll = await PollInitMsg._createWithSignFunction(
async () => 'a',
alice,
'test',
['one', 'two', 'three'],
PollType.NON_WEIGHTED,
undefined,
100
)
2021-08-09 14:00:42 +00:00
expect(poll?.endTime).to.eq(100)
})
})