Skip to content

Commit

Permalink
Add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Dec 5, 2023
1 parent 5d30c3c commit 25277d0
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/modules/marketplaces/marketplaces.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ export class MarketplacesService {
return externalMarketplaces.map((m) => m.address);
}

async getDisableMarketplacesAddreses(): Promise<string[]> {
async getDisabledMarketplacesAddreses(): Promise<string[]> {
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<string[]> {
let allMarketplaces = await this.getAllMarketplaces();

Expand Down
101 changes: 95 additions & 6 deletions src/modules/marketplaces/tests/marketplaces.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -92,16 +99,25 @@ describe('Marketplaces Service', () => {
address: 'address',
name: 'name',
key: 'xoxno',
state: MarketplaceState.Enable,
type: MarketplaceTypeEnum.External,
}),
new Marketplace({
address: 'address2',
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(
Expand All @@ -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>(MarketplacesCachingService);

Expand Down Expand Up @@ -398,7 +415,7 @@ describe('Marketplaces Service', () => {
describe('getMarketplacesAddreses', () => {
it('returns list of addresses for all marketplaces', async () => {
const cacheService = module.get<MarketplacesCachingService>(MarketplacesCachingService);
const expectedResult = ['address', 'address2'];
const expectedResult = ['address', 'address2', 'disabledAddress'];

cacheService.getAllMarketplaces = jest.fn().mockReturnValueOnce(
new CollectionType({
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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>(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>(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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 25277d0

Please sign in to comment.