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

Commit 467c0eb

Browse files
committed
Use new polling util in info action
1 parent fb48991 commit 467c0eb

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/action/info.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* the current block height.
55
*/
66

7-
import { RETRY_DELAY } from '../config';
87
import { observe } from 'mobx';
8+
import { poll } from '../helper';
99
import * as log from './log';
1010

1111
class InfoAction {
@@ -35,14 +35,21 @@ class InfoAction {
3535
this._notification.display({ msg: 'Syncing to chain', wait: true });
3636
log.info(`Syncing to chain ... block height: ${response.block_height}`);
3737
this._store.percentSynced = this.calcPercentSynced(response);
38-
clearTimeout(this.t3);
39-
this.t3 = setTimeout(() => this.getInfo(), RETRY_DELAY);
4038
}
39+
return response.synced_to_chain;
4140
} catch (err) {
4241
log.error('Getting node info failed', err);
4342
}
4443
}
4544

45+
/**
46+
* Poll the getInfo api until synced_to_chain is true.
47+
* @return {Promise<undefined>}
48+
*/
49+
async pollInfo() {
50+
await poll(() => this.getInfo());
51+
}
52+
4653
/**
4754
* A navigation helper called during the app onboarding process. The loader
4855
* screen indicating the syncing progress in displayed until syncing has

test/unit/action/info.spec.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import NavAction from '../../../src/action/nav';
44
import NotificationAction from '../../../src/action/notification';
55
import InfoAction from '../../../src/action/info';
66
import * as logger from '../../../src/action/log';
7-
import { nap } from '../../../src/helper';
87

98
describe('Action Info Unit Tests', () => {
109
let sandbox;
@@ -27,7 +26,6 @@ describe('Action Info Unit Tests', () => {
2726
});
2827

2928
afterEach(() => {
30-
clearTimeout(info.t3);
3129
sandbox.restore();
3230
});
3331

@@ -44,13 +42,12 @@ describe('Action Info Unit Tests', () => {
4442
expect(store.blockHeight, 'to equal', 'some-height');
4543
});
4644

47-
it('should only call once if chain is synced', async () => {
45+
it('should return true if chain is synced', async () => {
4846
grpc.sendCommand.withArgs('getInfo').resolves({
4947
synced_to_chain: true,
5048
});
51-
await info.getInfo();
52-
await nap(30);
53-
expect(grpc.sendCommand.callCount, 'to equal', 1);
49+
const synced = await info.getInfo();
50+
expect(synced, 'to be', true);
5451
});
5552

5653
it('should set percentSynced', async () => {
@@ -63,22 +60,30 @@ describe('Action Info Unit Tests', () => {
6360
expect(store.percentSynced, 'to be within', 0, 1);
6461
});
6562

66-
it('should retry if chain is not synced', async () => {
63+
it('should return false if chain is not synced', async () => {
6764
grpc.sendCommand.withArgs('getInfo').resolves({
6865
synced_to_chain: false,
6966
});
70-
await info.getInfo();
71-
await nap(30);
72-
expect(grpc.sendCommand.callCount, 'to be greater than', 1);
67+
const synced = await info.getInfo();
68+
expect(synced, 'to be', false);
7369
});
7470

75-
it('should retry on failure', async () => {
71+
it('should log error on failure', async () => {
7672
grpc.sendCommand.rejects();
7773
await info.getInfo();
7874
expect(logger.error, 'was called once');
7975
});
8076
});
8177

78+
describe('pollInfo()', () => {
79+
it('should poll wallet balances', async () => {
80+
sandbox.stub(info, 'getInfo');
81+
info.getInfo.onSecondCall().resolves(true);
82+
await info.pollInfo();
83+
expect(info.getInfo, 'was called twice');
84+
});
85+
});
86+
8287
describe('initLoaderSyncing()', () => {
8388
it('should navigate straight to home if synced', async () => {
8489
grpc.sendCommand.withArgs('getInfo').resolves({
@@ -99,7 +104,7 @@ describe('Action Info Unit Tests', () => {
99104
grpc.sendCommand.withArgs('getInfo').resolves({
100105
synced_to_chain: true,
101106
});
102-
await nap(10);
107+
await info.getInfo();
103108
expect(nav.goHome, 'was called once');
104109
});
105110
});

0 commit comments

Comments
 (0)