Skip to content

Commit

Permalink
test: Update tests to conform to version 7.0.0 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jontey committed Nov 27, 2018
1 parent 0b09e2d commit 40b73db
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 414 deletions.
98 changes: 59 additions & 39 deletions src/infrastructure/ApostilleHttp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sortBy } from 'lodash';
import { AccountHttp, Address, AggregateTransaction, Listener, PublicAccount, QueryParams, SignedTransaction, Transaction, TransactionHttp, TransactionInfo, TransferTransaction } from 'nem2-sdk';
import { AccountHttp, Address, AggregateTransaction, Listener, PublicAccount, QueryParams, SignedTransaction, Transaction, TransactionAnnounceResponse, TransactionHttp, TransactionInfo, TransferTransaction } from 'nem2-sdk';
import { filter, mergeMap } from 'rxjs/operators';
import { Errors } from '../types/Errors';

Expand All @@ -13,40 +13,66 @@ export class ApostilleHttp {
this.accountHttp = new AccountHttp(url);
}

public announce(signedTransaction: SignedTransaction): Promise<any> {
return new Promise<any>((resolve, reject) => {
/**
* Generic announce method
*
* @param {SignedTransaction} signedTransaction
* @returns {Promise<TransactionAnnounceResponse>}
* @memberof ApostilleHttp
*/
public announce(signedTransaction: SignedTransaction): Promise<TransactionAnnounceResponse> {
return new Promise<TransactionAnnounceResponse>((resolve, reject) => {
this.transactionHttp.announce(signedTransaction)
.subscribe((x) => resolve(x), (err) => reject(err));
});
}

/**
* Helper method to announce aggregate bonded transaction with lock funds
*
* @param {PublicAccount} cosignatoryAccount
* @param {SignedTransaction} signedAggregateBondedTransaction
* @param {SignedTransaction} signedLockFundsTransaction
* @param {Listener} listener
* @returns {Promise<TransactionAnnounceResponse>}
* @memberof ApostilleHttp
*/
public announceAggregateBonded(
cosignatoryAccount: PublicAccount,
signedAggregateBondedTransaction: SignedTransaction,
signedLockFundsTransaction: SignedTransaction,
listener: Listener): void {

listener.open().then(() => {

this.transactionHttp
.announce(signedLockFundsTransaction)
.subscribe((x) => console.log(x), (err) => console.error(err));

listener
.confirmed(cosignatoryAccount.address)
.pipe(
filter((transaction) => transaction.transactionInfo !== undefined
&& transaction.transactionInfo.hash === signedLockFundsTransaction.hash),
mergeMap((ignored) => this.transactionHttp.announceAggregateBonded(
signedAggregateBondedTransaction)),
)
.subscribe((announcedAggregateBonded) => console.log(announcedAggregateBonded),
(err) => console.error(err));
listener: Listener): Promise<TransactionAnnounceResponse> {

return new Promise<TransactionAnnounceResponse>((resolve, reject) => {
listener.open().then(() => {

// Announce lock funds first
this.transactionHttp
.announce(signedLockFundsTransaction)
.subscribe((x) => console.log(x), (err) => reject(err));

// Watch for lock funds confirmation before announcing aggregate bonded
listener
.confirmed(cosignatoryAccount.address)
.pipe(
filter((transaction) => transaction.transactionInfo !== undefined
&& transaction.transactionInfo.hash === signedLockFundsTransaction.hash),
mergeMap((ignored) => this.transactionHttp.announceAggregateBonded(
signedAggregateBondedTransaction)),
)
.subscribe((announcedAggregateBonded) => {
resolve(announcedAggregateBonded);
}, (err) => {
reject(err);
});
});
});

}

/**
* @description - cheks on chain if there are any transactions announced
* @description - checks on chain if account exists
* @param {PublicAccount} publicAccount
* @returns {Promise<boolean>}
* @memberof ApostilleAccount
*/
Expand All @@ -59,10 +85,10 @@ export class ApostilleHttp {
} else {
// then check transactions
const transactions = await this._transactions(publicAccount);
if (transactions.length) {
if (transactions.length > 0) {
return true;
} else {
return true;
return false;
}
}
} catch (err) {
Expand All @@ -71,7 +97,7 @@ export class ApostilleHttp {
}

/**
* @description - get cosignatories of the account
* @description - get cosignatories of an account
* @returns {Promise<PublicAccount[]>}
* @memberof ApostilleHttp
*/
Expand All @@ -87,15 +113,14 @@ export class ApostilleHttp {
}

/**
* @description - get first transaction
* @static
* @description - Check if account is owned
* @returns {Promise<boolean>}
* @memberof ApostilleHttp
*/
public async isOwned(address: Address): Promise<boolean> {
try {
const cossignatories = await this.getCosignatories(address);
if (cossignatories.length > 0) {
const cosignatories = await this.getCosignatories(address);
if (cosignatories.length > 0) {
return true;
} else {
return false;
Expand Down Expand Up @@ -149,16 +174,11 @@ export class ApostilleHttp {
innerTransactions, ['transactionInfo.index']);
const firstInnerTransaction = sortedInnerTransactions[0];
if (firstInnerTransaction instanceof TransferTransaction) {
resolve (firstInnerTransaction);
} else {
reject (Errors[Errors.CREATION_TRANSACTIONS_NOT_FOUND]);
resolve(firstInnerTransaction);
}
} else {
reject (Errors[Errors.CREATION_TRANSACTIONS_NOT_FOUND]);
}
} else {
reject (Errors[Errors.CREATION_TRANSACTIONS_NOT_FOUND]);
}
reject(Errors[Errors.CREATION_TRANSACTIONS_NOT_FOUND]);
});
});
}
Expand All @@ -169,7 +189,7 @@ export class ApostilleHttp {
* @returns {Promise<Transaction[]>}
* @memberof CertificateHistory
*/
public _fetchAllTransactions(publicAccount: PublicAccount): Promise<Transaction[]> {
public _fetchAllTransactions(publicAccount: PublicAccount): Promise <Transaction[]> {
return new Promise<Transaction[]>(async (resolve, reject) => {
let nextId: string = '';
const pageSize: number = 100;
Expand All @@ -192,7 +212,7 @@ export class ApostilleHttp {

private _unconfirmedTransactions(
publicAccount: PublicAccount,
queryParams?: QueryParams | undefined): Promise<Transaction[]> {
queryParams ?: QueryParams | undefined): Promise<Transaction[]> {

return new Promise<Transaction[]>((resolve, reject) => {
this.accountHttp.unconfirmedTransactions(
Expand All @@ -210,7 +230,7 @@ export class ApostilleHttp {

private _transactions(
publicAccount: PublicAccount,
queryParams?: QueryParams | undefined): Promise<Transaction[]> {
queryParams ?: QueryParams | undefined): Promise<Transaction[]> {

return new Promise<Transaction[]>((resolve, reject) => {
this.accountHttp.transactions(
Expand Down
Loading

0 comments on commit 40b73db

Please sign in to comment.