Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/action/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const grpc = new GrpcAction(store, ipc);
export const notify = new NotificationAction(store, nav);
export const wallet = new WalletAction(store, grpc, db, nav, notify);
export const info = new InfoAction(store, grpc, nav, notify);
export const transaction = new TransactionAction(store, grpc, nav);
export const transaction = new TransactionAction(store, grpc, nav, notify);
export const channel = new ChannelAction(store, grpc, nav, notify);
export const invoice = new InvoiceAction(store, grpc, nav, notify, Clipboard);
export const payment = new PaymentAction(store, grpc, nav, notify);
Expand Down
22 changes: 20 additions & 2 deletions src/action/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import * as log from './log';
import { parseDate, parseSat, toHex } from '../helper';

class TransactionAction {
constructor(store, grpc, nav) {
constructor(store, grpc, nav, notification) {
this._store = store;
this._grpc = grpc;
this._nav = nav;
this._notification = notification;
}

/**
Expand Down Expand Up @@ -133,12 +134,29 @@ class TransactionAction {
async subscribeInvoices() {
const stream = this._grpc.sendStreamCommand('subscribeInvoices');
await new Promise((resolve, reject) => {
stream.on('data', () => this.update());
stream.on('data', invoice => this._receiveInvoice(invoice));
stream.on('end', resolve);
stream.on('error', reject);
stream.on('status', status => log.info(`Invoices update: ${status}`));
});
}

//
// Helper functions
//

async _receiveInvoice(invoice) {
await this.update();
if (!invoice.settled) return;
const { computedTransactions, unitLabel } = this._store;
let inv = computedTransactions.find(tx => tx.id === toHex(invoice.r_hash));
this._notification.display({
type: 'success',
msg: `Invoice success: received ${inv.amountLabel} ${unitLabel}`,
handler: () => this.select({ item: inv }),
handlerLbl: 'View details',
});
}
}

export default TransactionAction;
2 changes: 1 addition & 1 deletion stories/screen-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ sinon.stub(wallet, 'checkSeed');
sinon.stub(wallet, 'checkNewPassword');
sinon.stub(wallet, 'checkPassword');
sinon.stub(wallet, 'getExchangeRate');
const transaction = new TransactionAction(store, grpc, nav);
const transaction = new TransactionAction(store, grpc, nav, notify);
sinon.stub(transaction, 'update');
const invoice = new InvoiceAction(store, grpc, nav, notify, Clipboard);
sinon.stub(invoice, 'generateUri');
Expand Down
4 changes: 2 additions & 2 deletions test/integration/action/action-integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('Action Integration Tests', function() {
grpc1 = new GrpcAction(store1, ipc1);
info1 = new InfoAction(store1, grpc1, nav1, notify1);
wallet1 = new WalletAction(store1, grpc1, db1, nav1, notify1);
transactions1 = new TransactionAction(store1, grpc1, nav1);
transactions1 = new TransactionAction(store1, grpc1, nav1, notify1);
channels1 = new ChannelAction(store1, grpc1, nav1, notify1);
invoice1 = new InvoiceAction(store1, grpc1, nav1, notify1);
payments1 = new PaymentAction(store1, grpc1, nav1, notify1);
Expand All @@ -161,7 +161,7 @@ describe('Action Integration Tests', function() {
grpc2 = new GrpcAction(store2, ipc2);
info2 = new InfoAction(store2, grpc2, nav2, notify2);
wallet2 = new WalletAction(store2, grpc2, db2, nav2, notify2);
transactions2 = new TransactionAction(store2, grpc2, nav2);
transactions2 = new TransactionAction(store2, grpc2, nav2, notify2);
channels2 = new ChannelAction(store2, grpc2, nav2, notify2);
invoice2 = new InvoiceAction(store2, grpc2, nav2, notify2);
payments2 = new PaymentAction(store2, grpc2, nav2, notify2);
Expand Down
32 changes: 30 additions & 2 deletions test/unit/action/transaction.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { Store } from '../../../src/store';
import GrpcAction from '../../../src/action/grpc';
import TransactionAction from '../../../src/action/transaction';
import NavAction from '../../../src/action/nav';
import NotificationAction from '../../../src/action/notification';
import * as logger from '../../../src/action/log';

describe('Action Transactions Unit Tests', () => {
let store;
let sandbox;
let grpc;
let nav;
let notification;
let transaction;

beforeEach(() => {
Expand All @@ -18,7 +20,8 @@ describe('Action Transactions Unit Tests', () => {
require('../../../src/config').RETRY_DELAY = 1;
grpc = sinon.createStubInstance(GrpcAction);
nav = sinon.createStubInstance(NavAction);
transaction = new TransactionAction(store, grpc, nav);
notification = sinon.createStubInstance(NotificationAction);
transaction = new TransactionAction(store, grpc, nav, notification);
});

afterEach(() => {
Expand Down Expand Up @@ -210,13 +213,38 @@ describe('Action Transactions Unit Tests', () => {
});

it('should update invoices on data event', async () => {
onStub.withArgs('data').yields();
onStub.withArgs('data').yields({});
onStub.withArgs('end').yields();
grpc.sendStreamCommand
.withArgs('subscribeInvoices')
.returns({ on: onStub });
await transaction.subscribeInvoices();
expect(transaction.update, 'was called once');
});

it('should notify the user on settled invoice', async () => {
store.computedTransactions = [{ id: 'cdab' }];
onStub.withArgs('data').yields({
settled: true,
r_hash: Buffer.from('cdab', 'hex'),
});
onStub.withArgs('end').yields();
grpc.sendStreamCommand
.withArgs('subscribeInvoices')
.returns({ on: onStub });
await transaction.subscribeInvoices();
expect(notification.display, 'was called once');
});

it('should not notify the user on an unsettled invoice', async () => {
onStub.withArgs('data').yields({ settled: false });
onStub.withArgs('end').yields();
grpc.sendStreamCommand
.withArgs('subscribeInvoices')
.returns({ on: onStub });
await transaction.subscribeInvoices();
expect(transaction.update, 'was called once');
expect(notification.display, 'was not called');
});

it('should reject in case of error', async () => {
Expand Down