From 2b2a1876c5f5f669e4491fff5e77a9691797a9db Mon Sep 17 00:00:00 2001 From: Anton Lykhoyda Date: Thu, 18 Apr 2024 15:29:10 +0200 Subject: [PATCH] update test --- .../fungible/fungible-token-transfer.ts | 11 +++-- packages/widget/src/context/config.ts | 3 -- .../transfers/fungible-token-transfer.ts | 29 +++++++++----- .../fungible/fungible-token-transfer.test.ts | 40 +++++++++++++++++++ 4 files changed, 64 insertions(+), 19 deletions(-) diff --git a/packages/widget/src/components/transfer/fungible/fungible-token-transfer.ts b/packages/widget/src/components/transfer/fungible/fungible-token-transfer.ts index 5172718d..09ff8b47 100644 --- a/packages/widget/src/components/transfer/fungible/fungible-token-transfer.ts +++ b/packages/widget/src/components/transfer/fungible/fungible-token-transfer.ts @@ -45,12 +45,11 @@ export class FungibleTokenTransfer extends BaseComponent { connectedCallback(): void { super.connectedCallback(); - void this.transferController.init( - this.environment!, - this.whitelistedSourceNetworks, - this.whitelistedDestinationNetworks, - this.whitelistedSourceResources - ); + void this.transferController.init(this.environment!, { + whitelistedSourceNetworks: this.whitelistedSourceNetworks, + whitelistedDestinationNetworks: this.whitelistedDestinationNetworks, + whitelistedSourceResources: this.whitelistedSourceResources + }); } private onClick = (): void => { diff --git a/packages/widget/src/context/config.ts b/packages/widget/src/context/config.ts index b14db7fa..569cbfcb 100644 --- a/packages/widget/src/context/config.ts +++ b/packages/widget/src/context/config.ts @@ -12,9 +12,6 @@ export interface ConfigContext { walletConnectOptions?: WalletConnectOptions; appMetaData?: AppMetadata; walletModules?: WalletInit[]; - whitelistedSourceNetworks?: string[]; - whitelistedDestinationNetworks?: string[]; - whitelistedSourceResources?: string[]; } export const configContext = createContext( diff --git a/packages/widget/src/controllers/transfers/fungible-token-transfer.ts b/packages/widget/src/controllers/transfers/fungible-token-transfer.ts index 72a59d29..7332a2fb 100644 --- a/packages/widget/src/controllers/transfers/fungible-token-transfer.ts +++ b/packages/widget/src/controllers/transfers/fungible-token-transfer.ts @@ -173,7 +173,6 @@ export class FungibleTokenTransferController implements ReactiveController { * {@link Config} from `@buildwithsygma/sygma-sdk-core` * and emits a {@link SdkInitializedEvent} * @returns {void} - * @param retryMs */ async retryInitSdk(retryMs = 100): Promise { try { @@ -207,26 +206,36 @@ export class FungibleTokenTransferController implements ReactiveController { async init( env: Environment, - whitelistedSourceNetworks?: string[], - whitelistedDestinationNetworks?: string[], - whitelistedSourceResources?: string[] + options?: { + whitelistedSourceNetworks?: string[]; + whitelistedDestinationNetworks?: string[]; + whitelistedSourceResources?: string[]; + } ): Promise { this.host.requestUpdate(); this.env = env; - this.whitelistedSourceNetworks = whitelistedSourceNetworks; - this.whitelistedDestinationNetworks = whitelistedDestinationNetworks; - this.whitelistedSourceResources = whitelistedSourceResources; + + this.whitelistedSourceNetworks = options?.whitelistedSourceNetworks; + this.whitelistedDestinationNetworks = + options?.whitelistedDestinationNetworks; + this.whitelistedSourceResources = options?.whitelistedSourceResources; await this.retryInitSdk(); this.supportedSourceNetworks = this.config .getDomains() .filter((network) => - this.filterWhitelistedNetworks(whitelistedSourceNetworks, network) + this.filterWhitelistedNetworks( + options?.whitelistedSourceNetworks, + network + ) ); this.supportedDestinationNetworks = this.config .getDomains() .filter((network) => - this.filterWhitelistedNetworks(whitelistedDestinationNetworks, network) + this.filterWhitelistedNetworks( + options?.whitelistedDestinationNetworks, + network + ) ); this.host.requestUpdate(); } @@ -460,7 +469,7 @@ export class FungibleTokenTransferController implements ReactiveController { return this.whitelistedSourceResources.includes(route.resource.symbol!); }) .map((route) => route.resource); - + console.log('this.supportedResources', this.supportedResources); void this.buildTransactions(); this.host.requestUpdate(); }; diff --git a/packages/widget/tests/unit/components/transfer/fungible/fungible-token-transfer.test.ts b/packages/widget/tests/unit/components/transfer/fungible/fungible-token-transfer.test.ts index c0726917..7802ae30 100644 --- a/packages/widget/tests/unit/components/transfer/fungible/fungible-token-transfer.test.ts +++ b/packages/widget/tests/unit/components/transfer/fungible/fungible-token-transfer.test.ts @@ -114,6 +114,22 @@ describe('Fungible token Transfer', function () { }); it('should filter whitelisted networks and resources', async () => { + function containsWhitelistedData( + data: T[], + whitelist: string[], + dataExtractor: (item: T) => string, + errorMessageContext: string + ): void { + assert.isNotEmpty(data, 'Data must not be empty.'); + data.forEach((item) => { + const key = dataExtractor(item); + assert.isTrue( + whitelist.includes(key), + `${key} expected to be whitelisted in ${errorMessageContext}` + ); + }); + } + const whitelistedSourceNetworks = ['cronos']; const whitelistedDestinationNetworks = ['sepolia']; const whitelistedResources = ['ERC20LRTest']; @@ -142,6 +158,23 @@ describe('Fungible token Transfer', function () { }) ); + await fungibleTransfer.updateComplete; + await aTimeout(1000); + + containsWhitelistedData( + fungibleTransfer.transferController.supportedSourceNetworks, + whitelistedSourceNetworks, + (network) => network.name, + 'transfer controller for source networks' + ); + + containsWhitelistedData( + fungibleTransfer.transferController.supportedDestinationNetworks, + whitelistedDestinationNetworks, + (network) => network.name, + 'transfer controller for destination networks' + ); + // Set Source and Destination Networks fungibleTransfer.transferController.onSourceNetworkSelected(cronosNetwork); fungibleTransfer.transferController.onDestinationNetworkSelected( @@ -160,6 +193,13 @@ describe('Fungible token Transfer', function () { // Wait for sdk init await aTimeout(1000); + containsWhitelistedData( + fungibleTransfer.transferController.supportedResources, + whitelistedResources, + (resource) => resource.symbol!, + 'transfer controller for resources' + ); + assert.isTrue( whitelistedSourceNetworks.includes( sygmaSourceNetwork.networks?.[0]?.name