Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit 8be5784

Browse files
committed
Integrate ipcRenderer open-url event in payment action
1 parent 4265af4 commit 8be5784

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

src/action/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export const invoice = new InvoiceAction(
4343
export const payment = new PaymentAction(store, grpc, transaction, nav, notify);
4444
export const setting = new SettingAction(store, wallet, db);
4545

46+
payment.listenForUrl(ipcRenderer);
47+
4648
//
4749
// Init actions
4850
//

src/action/payment.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PREFIX_URI } from '../config';
2-
import { toSatoshis, toAmount, parseSat } from '../helper';
2+
import { toSatoshis, toAmount, parseSat, isValidUri, nap } from '../helper';
33
import * as log from './log';
44

55
class PaymentAction {
@@ -11,6 +11,21 @@ class PaymentAction {
1111
this._notification = notification;
1212
}
1313

14+
listenForUrl(ipcRenderer) {
15+
ipcRenderer.on('open-url', async (event, url) => {
16+
log.info('open-url', url);
17+
if (!isValidUri(url)) {
18+
return;
19+
}
20+
while (!this._store.lndReady) {
21+
this._tOpenUri = await nap(100);
22+
}
23+
this.init();
24+
this.setAddress({ address: url.replace(PREFIX_URI, '') });
25+
this.checkType();
26+
});
27+
}
28+
1429
init() {
1530
this._store.payment.address = '';
1631
this._store.payment.amount = '';

test/unit/action/payment.spec.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { EventEmitter } from 'events';
12
import { Store } from '../../../src/store';
23
import GrpcAction from '../../../src/action/grpc';
34
import PaymentAction from '../../../src/action/payment';
45
import TransactionAction from '../../../src/action/transaction';
56
import NotificationAction from '../../../src/action/notification';
67
import NavAction from '../../../src/action/nav';
78
import * as logger from '../../../src/action/log';
9+
import { nap } from '../../../src/helper';
810

911
describe('Action Payments Unit Tests', () => {
1012
let store;
@@ -28,9 +30,48 @@ describe('Action Payments Unit Tests', () => {
2830
});
2931

3032
afterEach(() => {
33+
clearTimeout(payment.tOpenUri);
3134
sandbox.restore();
3235
});
3336

37+
describe('listenForUrl()', () => {
38+
let ipcRendererStub;
39+
40+
beforeEach(() => {
41+
ipcRendererStub = new EventEmitter();
42+
payment.listenForUrl(ipcRendererStub);
43+
sandbox.stub(payment, 'init');
44+
sandbox.stub(payment, 'checkType');
45+
});
46+
47+
it('should not navigate to payment view for invalid uri', () => {
48+
const uri = 'invalid-uri';
49+
ipcRendererStub.emit('open-url', 'some-event', uri);
50+
expect(payment.init, 'was not called');
51+
});
52+
53+
it('should navigate to payment view for valid uri and lndReady', () => {
54+
store.lndReady = true;
55+
const uri = 'lightning:lntb100n1pdn2e0app';
56+
ipcRendererStub.emit('open-url', 'some-event', uri);
57+
expect(payment.init, 'was called once');
58+
expect(store.payment.address, 'to equal', 'lntb100n1pdn2e0app');
59+
expect(payment.checkType, 'was called once');
60+
});
61+
62+
it('should wait for lndReady', async () => {
63+
store.lndReady = false;
64+
const uri = 'lightning:lntb100n1pdn2e0app';
65+
ipcRendererStub.emit('open-url', 'some-event', uri);
66+
expect(payment.init, 'was not called');
67+
store.lndReady = true;
68+
await nap(300);
69+
expect(payment.init, 'was called once');
70+
expect(store.payment.address, 'to equal', 'lntb100n1pdn2e0app');
71+
expect(payment.checkType, 'was called once');
72+
});
73+
});
74+
3475
describe('init()', () => {
3576
it('should clear attributes and navigate to payment view', () => {
3677
store.payment.address = 'foo';
@@ -58,6 +99,33 @@ describe('Action Payments Unit Tests', () => {
5899
});
59100
});
60101

102+
describe('checkType()', () => {
103+
beforeEach(() => {
104+
sandbox.stub(payment, 'decodeInvoice');
105+
});
106+
107+
it('should notify if address is empty', async () => {
108+
payment.decodeInvoice.resolves(true);
109+
await payment.checkType();
110+
expect(notification.display, 'was called once');
111+
expect(payment.decodeInvoice, 'was not called');
112+
});
113+
114+
it('should decode successfully', async () => {
115+
store.payment.address = 'some-address';
116+
payment.decodeInvoice.resolves(true);
117+
await payment.checkType();
118+
expect(nav.goPayLightningConfirm, 'was called once');
119+
});
120+
121+
it('should set response to null on error', async () => {
122+
store.payment.address = 'some-address';
123+
payment.decodeInvoice.resolves(false);
124+
await payment.checkType();
125+
expect(nav.goPayBitcoin, 'was called once');
126+
});
127+
});
128+
61129
describe('decodeInvoice()', () => {
62130
it('should decode successfully', async () => {
63131
grpc.sendCommand.withArgs('decodePayReq').resolves({

0 commit comments

Comments
 (0)