Skip to content

Commit

Permalink
Filter user account's address from quick connect suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelwedler committed Apr 15, 2021
1 parent f5a50b3 commit 18615f1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { amountToDecimal } from 'app/utils/amount.converter';
import BigNumber from 'bignumber.js';
import { ClipboardModule } from 'ngx-clipboard';
import { clickElement, mockInput } from 'testing/interaction-helper';
import { createSuggestedConnections, createToken } from 'testing/test-data';
import { createTestSuggestedConnections, createToken } from 'testing/test-data';
import { TestProviders } from 'testing/test-providers';
import { ConnectionSelectorComponent } from './connection-selector.component';

Expand All @@ -40,7 +40,7 @@ describe('ConnectionSelectorComponent', () => {
balance: new BigNumber('10000000000000000000'),
});
const totalAmount = new BigNumber('3000000000000000000');
const suggestions = createSuggestedConnections();
const suggestions = createTestSuggestedConnections();

function initComponentForFakeAsync() {
fixture.detectChanges();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ import {
createAddress,
createChannel,
createSettings,
createSuggestedConnections,
createSuggestedConnection,
createTestSuggestedConnections,
createToken,
} from 'testing/test-data';
import { TestProviders } from 'testing/test-providers';
Expand All @@ -59,7 +60,7 @@ describe('QuickConnectDialogComponent', () => {
let component: QuickConnectDialogComponent;
let fixture: ComponentFixture<QuickConnectDialogComponent>;

const suggestions = createSuggestedConnections();
const suggestions = createTestSuggestedConnections();
const token = createToken({
decimals: 0,
balance: new BigNumber(1000),
Expand All @@ -69,6 +70,7 @@ describe('QuickConnectDialogComponent', () => {
},
});
const tokenNetworkAddress = createAddress();
const raidenAddress = createAddress();
const settings = createSettings();
const totalAmountInput = '70';
const totalAmountValue = amountFromDecimal(
Expand All @@ -84,6 +86,9 @@ describe('QuickConnectDialogComponent', () => {
spyOn(raidenService, 'getTokenNetworkAddress').and.returnValue(
of(tokenNetworkAddress)
);
spyOnProperty(raidenService, 'raidenAddress').and.returnValue(
raidenAddress
);
settingsSubject = new BehaviorSubject(settings);
spyOn(raidenService, 'getSettings').and.returnValue(
settingsSubject.asObservable()
Expand Down Expand Up @@ -260,6 +265,19 @@ describe('QuickConnectDialogComponent', () => {
expect(component.suggestions).toEqual(suggestions.slice(1));
}));

it('should filter the suggestions by the address of the user account', fakeAsync(() => {
const suggestedConnections = [
createSuggestedConnection({ address: raidenAddress }),
createSuggestedConnection(),
];
initComponentForFakeAsync(suggestedConnections);

expect(component.suggestions.length).toEqual(1);
expect(component.suggestions).toEqual(
suggestedConnections.slice(1)
);
}));

it('should show an error if there are no suggestions', fakeAsync(() => {
initComponentForFakeAsync([]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@ import { ChannelPollingService } from 'app/services/channel-polling.service';
import { RaidenService } from 'app/services/raiden.service';
import { TokenPollingService } from 'app/services/token-polling.service';
import BigNumber from 'bignumber.js';
import {
combineLatest,
EMPTY,
Observable,
Subject,
throwError,
zip,
} from 'rxjs';
import { combineLatest, EMPTY, Observable, Subject, zip } from 'rxjs';
import {
catchError,
delay,
Expand Down Expand Up @@ -160,14 +153,16 @@ export class QuickConnectDialogComponent implements OnInit, OnDestroy {
zip(suggestions$, existingChannels$)
.pipe(
map(([suggestions, channels]) =>
suggestions.filter(
(suggestion) =>
!channels.find(
(channel) =>
channel.partner_address ===
suggestion.address
)
)
suggestions.filter((suggestion) => {
const channelExisting = !!channels.find(
(channel) =>
channel.partner_address === suggestion.address
);
const isOwnAddress =
suggestion.address ===
this.raidenService.raidenAddress;
return !channelExisting && !isOwnAddress;
})
),
catchError((error, caught) => {
this.loading = false;
Expand Down
21 changes: 13 additions & 8 deletions src/testing/test-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,23 @@ export function createContractsInfo(obj: any = {}): ContractsInfo {
return Object.assign(contracts, obj);
}

export function createSuggestedConnections(
export function createSuggestedConnection(obj: any = {}): SuggestedConnection {
const suggestedConnection: SuggestedConnection = {
address: createAddress(),
score: BigNumber.random(19).times(10 ** 19),
centrality: BigNumber.random(21),
uptime: BigNumber.random(5).times(10 ** 5),
capacity: BigNumber.random(19).times(10 ** 19),
};
return Object.assign(suggestedConnection, obj);
}

export function createTestSuggestedConnections(
count: number = 5
): SuggestedConnection[] {
const suggestions: SuggestedConnection[] = [];
for (let i = 0; i < count; i++) {
suggestions.push({
address: createAddress(),
score: BigNumber.random(19).times(10 ** 19),
centrality: BigNumber.random(21),
uptime: BigNumber.random(5).times(10 ** 5),
capacity: BigNumber.random(19).times(10 ** 19),
});
suggestions.push(createSuggestedConnection());
}
suggestions.sort((a, b) => a.score.minus(b.score).toNumber());
return suggestions;
Expand Down

0 comments on commit 18615f1

Please sign in to comment.