Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: better blockchain api mocking #255

Merged
merged 11 commits into from
Apr 30, 2020
34 changes: 16 additions & 18 deletions src/attestation/Attestation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ describe('Attestation', () => {
const identityAlice = Identity.buildFromURI('//Alice')
const identityBob = Identity.buildFromURI('//Bob')

const Blockchain = require('../blockchain/Blockchain').default
const blockchainApi = require('../blockchainApiConnection/BlockchainApiConnection')
.__mocked_api

const rawCType: ICType['schema'] = {
$id: 'http://example.com/ctype-1',
Expand Down Expand Up @@ -50,16 +51,15 @@ describe('Attestation', () => {
)

it('stores attestation', async () => {
Blockchain.api.query.attestation.attestations = jest.fn(() => {
const tuple = new Option(
blockchainApi.query.attestation.attestations.mockReturnValue(
new Option(
Tuple,
new Tuple(
[Text, AccountId, Text, Bool],
[testCType.hash, identityAlice.address, undefined, false]
)
)
return Promise.resolve(tuple)
})
)

const attestation: Attestation = Attestation.fromRequestAndPublicIdentity(
requestForAttestation,
Expand All @@ -69,9 +69,9 @@ describe('Attestation', () => {
})

it('verify attestations not on chain', async () => {
Blockchain.api.query.attestation.attestations = jest.fn(() => {
return Promise.resolve(new Option(Tuple))
})
blockchainApi.query.attestation.attestations.mockReturnValue(
new Option(Tuple)
)

const attestation: Attestation = Attestation.fromAttestation({
claimHash: requestForAttestation.rootHash,
Expand All @@ -84,18 +84,16 @@ describe('Attestation', () => {
})

it('verify attestation revoked', async () => {
Blockchain.api.query.attestation.attestations = jest.fn(() => {
return Promise.resolve(
new Option(
Tuple,
new Tuple(
// Attestations: claim-hash -> (ctype-hash, account, delegation-id?, revoked)
[Text, AccountId, Text, Bool],
[testCType.hash, identityAlice.address, undefined, true]
)
blockchainApi.query.attestation.attestations.mockReturnValue(
new Option(
Tuple,
new Tuple(
// Attestations: claim-hash -> (ctype-hash, account, delegation-id?, revoked)
[Text, AccountId, Text, Bool],
[testCType.hash, identityAlice.address, undefined, true]
)
)
})
)

const attestation: Attestation = Attestation.fromRequestAndPublicIdentity(
requestForAttestation,
Expand Down
23 changes: 12 additions & 11 deletions src/balance/Balance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ import { listenToBalanceChanges, makeTransfer } from './Balance.chain'
jest.mock('../blockchainApiConnection/BlockchainApiConnection')

describe('Balance', () => {
const blockchain = require('../blockchain/Blockchain').default

blockchain.api.query.balances.freeBalance = jest.fn((accountAddress, cb) => {
if (cb) {
setTimeout(() => {
cb(new BN(42))
}, 1)
const blockchainApi = require('../blockchainApiConnection/BlockchainApiConnection')
.__mocked_api

blockchainApi.query.balances.freeBalance = jest.fn(
async (accountAddress, cb) => {
if (cb) {
setTimeout(() => {
cb(new BN(42))
}, 1)
}
return new BN(12)
}
return new BN(12)
})
)

it('should listen to balance changes', async done => {
const bob = Identity.buildFromURI('//Bob')
Expand All @@ -33,8 +36,6 @@ describe('Balance', () => {
expect(currentBalance.toString()).toEqual('12')
})

blockchain.__mockResultHash = '123'

it('should make transfer', async () => {
const alice = Identity.buildFromURI('//Alice')
const bob = Identity.buildFromURI('//Bob')
Expand Down
107 changes: 0 additions & 107 deletions src/blockchain/__mocks__/Blockchain.ts

This file was deleted.

Loading