dappconnect-vote-poll-sdk/packages/core/test/utils/proto/PollInit.test.ts

78 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-08-09 12:53:39 +00:00
import { expect } from 'chai'
2021-08-09 14:00:42 +00:00
import { PollType } from '../../../src/types/PollType'
2021-08-09 12:53:39 +00:00
import PollInit from '../../../src/utils/proto/PollInit'
import { BigNumber } from 'ethers'
2021-08-09 14:00:42 +00:00
import { PollInitMsg } from '../../../src/models/PollInitMsg'
2021-08-10 12:55:47 +00:00
import { MockProvider } from 'ethereum-waffle'
2021-08-09 12:53:39 +00:00
describe('PollInit', () => {
2021-08-10 12:55:47 +00:00
const provider = new MockProvider()
const [alice] = provider.getWallets()
2021-08-09 12:53:39 +00:00
it('success', async () => {
2021-08-12 14:37:21 +00:00
const data = await PollInitMsg._createWithSignFunction(
async () => '0x01',
alice,
'whats up',
['ab', 'cd', 'ef'],
PollType.WEIGHTED
)
expect(data).to.not.be.undefined
if (data) {
const payload = PollInit.encode(data)
expect(payload).to.not.be.undefined
if (payload) {
expect(PollInit.decode(payload, new Date(data.timestamp), () => data.owner)).to.deep.eq(data)
}
2021-08-09 12:53:39 +00:00
}
})
it('random decode', async () => {
2021-08-10 12:55:47 +00:00
expect(PollInit.decode(new Uint8Array([12, 12, 3, 32, 31, 212, 31, 32, 23]), new Date(10))).to.be.undefined
2021-08-09 12:53:39 +00:00
})
it('random data', async () => {
expect(PollInit.encode({ sadf: '0x0' } as unknown as PollInitMsg)).to.be.undefined
})
it('NON_WEIGHTED init', async () => {
2021-08-12 14:37:21 +00:00
const data = await PollInitMsg._createWithSignFunction(
async () => '0x01',
2021-08-10 12:55:47 +00:00
alice,
'whats up',
['ab', 'cd', 'ef'],
PollType.NON_WEIGHTED,
BigNumber.from(10)
)
2021-08-12 14:37:21 +00:00
expect(data).to.not.be.undefined
if (data) {
const payload = PollInit.encode(data)
expect(payload).to.not.be.undefined
if (payload) {
expect(PollInit.decode(payload, new Date(data.timestamp), () => data.owner)).to.deep.eq(data)
}
2021-08-09 12:53:39 +00:00
}
})
it('NON_WEIGHTED no min token', async () => {
2021-08-12 14:37:21 +00:00
const data = await PollInitMsg._createWithSignFunction(
async () => '0x01',
alice,
'whats up',
['ab', 'cd', 'ef'],
PollType.NON_WEIGHTED
)
expect(data).to.not.be.undefined
if (data) {
const payload = PollInit.encode(data)
2021-08-09 12:53:39 +00:00
2021-08-12 14:37:21 +00:00
expect(payload).to.not.be.undefined
if (payload) {
expect(PollInit.decode(payload, new Date(data.timestamp), () => data.owner)).to.deep.eq({
...data,
minToken: BigNumber.from(1),
})
}
2021-08-10 12:55:47 +00:00
}
2021-08-09 12:53:39 +00:00
})
})