Skip to content

Commit f7621d7

Browse files
committed
feat(util-dev): add createStubObservable util
hoist from wallet TipTracker tests
1 parent 704b5d6 commit f7621d7

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Observable } from 'rxjs';
2+
3+
/**
4+
* @returns an Observable that proxies subscriptions to observables provided as arguments.
5+
* Arguments are subscribed to in order they are provided.
6+
*/
7+
export const createStubObservable = <T>(...calls: Observable<T>[]) => {
8+
let numCall = 0;
9+
return new Observable<T>((subscriber) => {
10+
const sub = calls[numCall++].subscribe(subscriber);
11+
return () => sub.unsubscribe();
12+
});
13+
};

packages/util-dev/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export * from './util';
77
export * from './createStubStakePoolProvider';
88
export * from './testScheduler';
99
export * from './createStubUtxoProvider';
10+
export * from './createStubObservable';
1011
export * from './createGenericMockServer';
1112
export * from './dataGeneration';
1213
export * from './eraSummaries';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { concat } from 'rxjs';
2+
import { createStubObservable, createTestScheduler } from '../src';
3+
4+
describe('createStubObservable', () => {
5+
it('returns an observable that subscribes to observables provided as arguments in order', () => {
6+
createTestScheduler().run(({ cold, expectObservable, expectSubscriptions }) => {
7+
const a$ = cold('a|');
8+
const b$ = cold('b|');
9+
const target$ = createStubObservable(a$, b$);
10+
expectObservable(concat(target$, target$)).toBe('ab|');
11+
expectSubscriptions(a$.subscriptions).toBe('^!');
12+
expectSubscriptions(b$.subscriptions).toBe('-^!');
13+
});
14+
});
15+
});

packages/wallet/test/services/TipTracker.test.ts

+5-13
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,9 @@ import { ConnectionStatus, TipTracker } from '../../src/services';
33
import { InMemoryDocumentStore } from '../../src/persistence';
44
import { Milliseconds, SyncStatus } from '../../src';
55
import { Observable, firstValueFrom, of } from 'rxjs';
6-
import { createTestScheduler } from '@cardano-sdk/util-dev';
6+
import { createStubObservable, createTestScheduler } from '@cardano-sdk/util-dev';
77
import { dummyLogger } from 'ts-log';
88

9-
const stubObservableProvider = <T>(...calls: Observable<T>[]) => {
10-
let numCall = 0;
11-
return new Observable<T>((subscriber) => {
12-
const sub = calls[numCall++].subscribe(subscriber);
13-
return () => sub.unsubscribe();
14-
});
15-
};
16-
179
const mockTips = {
1810
a: { hash: 'ha' },
1911
b: { hash: 'hb' },
@@ -37,7 +29,7 @@ describe('TipTracker', () => {
3729
it('calls the provider immediately, only emitting distinct values, with throttling', () => {
3830
createTestScheduler().run(({ cold, expectObservable }) => {
3931
const syncStatus: Partial<SyncStatus> = { isSettled$: cold('---a---bc--d|') };
40-
const provider$ = stubObservableProvider<Cardano.Tip>(
32+
const provider$ = createStubObservable<Cardano.Tip>(
4133
cold('-x|', mockTips),
4234
cold('--a|', mockTips),
4335
cold('--b|', mockTips),
@@ -85,7 +77,7 @@ describe('TipTracker', () => {
8577
store.set = jest.fn().mockImplementation(store.set.bind(store));
8678
createTestScheduler().run(({ cold, expectObservable }) => {
8779
const syncStatus: Partial<SyncStatus> = { isSettled$: cold('---a---b|') };
88-
const provider$ = stubObservableProvider<Cardano.Tip>(
80+
const provider$ = createStubObservable<Cardano.Tip>(
8981
cold('-y|', mockTips),
9082
cold('--a|', mockTips),
9183
cold('-ab|', mockTips)
@@ -108,7 +100,7 @@ describe('TipTracker', () => {
108100
it('times out trigger$ with maxPollInterval, then listens for trigger$ again', () => {
109101
createTestScheduler().run(({ cold, hot, expectObservable }) => {
110102
const syncStatus: Partial<SyncStatus> = { isSettled$: hot('10ms a|') };
111-
const provider$ = stubObservableProvider<Cardano.Tip>(
103+
const provider$ = createStubObservable<Cardano.Tip>(
112104
cold('-a|', mockTips),
113105
cold('-b|', mockTips),
114106
cold('-c|', mockTips)
@@ -135,7 +127,7 @@ describe('TipTracker', () => {
135127
d: ConnectionStatus.down,
136128
p: ConnectionStatus.up
137129
});
138-
const provider$ = stubObservableProvider<Cardano.Tip>(cold('x|', mockTips), cold('a|', mockTips));
130+
const provider$ = createStubObservable<Cardano.Tip>(cold('x|', mockTips), cold('a|', mockTips));
139131
const tracker$ = new TipTracker({
140132
connectionStatus$: connectionStatusMock$,
141133
logger,

0 commit comments

Comments
 (0)