From 25277d0975ba10501295a7eeffde35153610d4c9 Mon Sep 17 00:00:00 2001 From: danielailie Date: Tue, 5 Dec 2023 18:45:29 +0200 Subject: [PATCH] Add missing tests --- .../marketplaces/marketplaces.service.ts | 7 +- .../tests/marketplaces.service.spec.ts | 101 ++++++++++++++++-- .../blockchain-events/nft-events.consumer.ts | 2 +- 3 files changed, 100 insertions(+), 10 deletions(-) diff --git a/src/modules/marketplaces/marketplaces.service.ts b/src/modules/marketplaces/marketplaces.service.ts index c6fdd8f92..2ee825c5b 100644 --- a/src/modules/marketplaces/marketplaces.service.ts +++ b/src/modules/marketplaces/marketplaces.service.ts @@ -85,13 +85,14 @@ export class MarketplacesService { return externalMarketplaces.map((m) => m.address); } - async getDisableMarketplacesAddreses(): Promise { + async getDisabledMarketplacesAddreses(): Promise { let allMarketplaces = await this.getAllMarketplaces(); - const externalMarketplaces = allMarketplaces?.items?.filter((m) => m.state === MarketplaceState.Disable); + const disabledMarketplaces = allMarketplaces?.items?.filter((m) => m.state === MarketplaceState.Disable); - return externalMarketplaces.map((m) => m.address); + return disabledMarketplaces.map((m) => m.address); } + async getMarketplacesAddreses(): Promise { let allMarketplaces = await this.getAllMarketplaces(); diff --git a/src/modules/marketplaces/tests/marketplaces.service.spec.ts b/src/modules/marketplaces/tests/marketplaces.service.spec.ts index 790030a8f..e6f0898d1 100644 --- a/src/modules/marketplaces/tests/marketplaces.service.spec.ts +++ b/src/modules/marketplaces/tests/marketplaces.service.spec.ts @@ -34,8 +34,15 @@ describe('Marketplaces Service', () => { type: MarketplaceTypeEnum.Internal, state: MarketplaceState.Enable, }), + new MarketplaceEntity({ + address: 'disabledAddress', + name: 'name', + key: 'test', + type: MarketplaceTypeEnum.External, + state: MarketplaceState.Disable, + }), ], - 2, + 3, ]; beforeEach(async () => { @@ -92,6 +99,7 @@ describe('Marketplaces Service', () => { address: 'address', name: 'name', key: 'xoxno', + state: MarketplaceState.Enable, type: MarketplaceTypeEnum.External, }), new Marketplace({ @@ -99,9 +107,17 @@ describe('Marketplaces Service', () => { name: 'name2', key: 'common', type: MarketplaceTypeEnum.Internal, + state: MarketplaceState.Enable, + }), + new Marketplace({ + address: 'disabledAddress', + name: 'name', + key: 'test', + type: MarketplaceTypeEnum.External, + state: MarketplaceState.Disable, }), ], - count: 2, + count: 3, }); cacheService.getAllMarketplaces = jest.fn().mockReturnValueOnce( @@ -115,6 +131,7 @@ describe('Marketplaces Service', () => { expect(result).toMatchObject(expectedResult); }); + it('when filters by marketplaceKey and marketplaceAddress returns list with one item', async () => { const cacheService = module.get(MarketplacesCachingService); @@ -398,7 +415,7 @@ describe('Marketplaces Service', () => { describe('getMarketplacesAddreses', () => { it('returns list of addresses for all marketplaces', async () => { const cacheService = module.get(MarketplacesCachingService); - const expectedResult = ['address', 'address2']; + const expectedResult = ['address', 'address2', 'disabledAddress']; cacheService.getAllMarketplaces = jest.fn().mockReturnValueOnce( new CollectionType({ @@ -591,19 +608,43 @@ describe('Marketplaces Service', () => { const expectedResult = new CollectionType({ items: [ new Marketplace({ + acceptedPaymentIdentifiers: null, address: 'address', - name: 'name', + iconUrl: 'https://media.elrond.com/markets/xoxno.svg', + id: undefined, key: 'xoxno', + lastIndexTimestamp: undefined, + name: 'name', + state: MarketplaceState.Enable, type: MarketplaceTypeEnum.External, + url: undefined, }), new Marketplace({ + acceptedPaymentIdentifiers: null, address: 'address2', - name: 'name2', + iconUrl: 'https://media.elrond.com/markets/metaspace.svg', + id: undefined, key: 'common', + lastIndexTimestamp: undefined, + name: 'name2', + state: MarketplaceState.Enable, type: MarketplaceTypeEnum.Internal, + url: undefined, + }), + new Marketplace({ + acceptedPaymentIdentifiers: null, + address: 'disabledAddress', + iconUrl: 'https://media.elrond.com/markets/test.svg', + id: undefined, + key: 'test', + lastIndexTimestamp: undefined, + name: 'name', + state: MarketplaceState.Disable, + type: MarketplaceTypeEnum.External, + url: undefined, }), ], - count: 2, + count: 3, }); persistenceService.getMarketplaces = jest.fn().mockReturnValueOnce([inputMarketplaces, inputCount]); @@ -991,4 +1032,52 @@ describe('Marketplaces Service', () => { expect(expectedResult).toBeTruthy(); }); }); + + describe('getDisableMarketplacesAddreses', () => { + it('returns list of addresses of disabled marketplaces', async () => { + const cacheService = module.get(MarketplacesCachingService); + const expectedResult = ['disabledAddress']; + cacheService.getAllMarketplaces = jest.fn().mockReturnValueOnce( + new CollectionType({ + items: inputMarketplaces, + count: inputCount, + }), + ); + + const result = await service.getDisabledMarketplacesAddreses(); + + expect(result).toMatchObject(expectedResult); + }); + + it('when no expernal marketplace exists returns empty array', async () => { + const cacheService = module.get(MarketplacesCachingService); + + const expectedResult = []; + cacheService.getAllMarketplaces = jest.fn().mockReturnValueOnce( + new CollectionType({ + items: [ + new Marketplace({ + address: 'address', + name: 'name', + key: 'xoxno', + type: MarketplaceTypeEnum.Internal, + state: MarketplaceState.Enable, + }), + new Marketplace({ + address: 'address2', + name: 'name2', + key: 'common', + type: MarketplaceTypeEnum.Internal, + state: MarketplaceState.Enable, + }), + ], + count: 2, + }), + ); + + const result = await service.getDisabledMarketplacesAddreses(); + + expect(result).toMatchObject(expectedResult); + }); + }); }); diff --git a/src/modules/rabbitmq/blockchain-events/nft-events.consumer.ts b/src/modules/rabbitmq/blockchain-events/nft-events.consumer.ts index 2869afbf0..f19b684ef 100644 --- a/src/modules/rabbitmq/blockchain-events/nft-events.consumer.ts +++ b/src/modules/rabbitmq/blockchain-events/nft-events.consumer.ts @@ -36,7 +36,7 @@ export class NftEventsConsumer { if (nftAuctionEvents?.events) { const internalMarketplaces = await this.marketplaceService.getInternalMarketplacesAddreses(); const externalMarketplaces = await this.marketplaceService.getExternalMarketplacesAddreses(); - const disabledMarketplaces = await this.marketplaceService.getDisableMarketplacesAddreses(); + const disabledMarketplaces = await this.marketplaceService.getDisabledMarketplacesAddreses(); console.log({ disabledMarketplaces: JSON.stringify(disabledMarketplaces) }); const disabledMarketplacesEvents = nftAuctionEvents?.events?.filter(