Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Oct 25, 2023
1 parent 34e63f3 commit fa4e938
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/modules/marketplaces/marketplaces.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ export class MarketplacesService {
}
return removedCollection ? true : false;
} catch (error) {
console.log({ error });
this.logger.error('An error has occured while remove whitelist for collection', {
path: this.whitelistCollectionOnMarketplace.name,
collection: request?.collection,
Expand Down
60 changes: 59 additions & 1 deletion src/modules/marketplaces/tests/marketplaces.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { MarketplaceCollectionEntity, MarketplaceEntity } from 'src/db/marketpla
import { MarketplaceTypeEnum } from '../models/MarketplaceType.enum';
import { MarketplaceFilters } from '../models/Marketplace.Filter';
import { Marketplace } from '../models';
import { WhitelistCollectionRequest } from '../models/requests/WhitelistCollectionOnMarketplaceRequest';
import { RemoveWhitelistCollectionRequest, WhitelistCollectionRequest } from '../models/requests/WhitelistCollectionOnMarketplaceRequest';
import { BadRequestError } from 'src/common/models/errors/bad-request-error';
import { Logger } from '@nestjs/common';
import { WhitelistMarketplaceRequest } from '../models/requests/WhitelistMarketplaceRequest';
Expand Down Expand Up @@ -738,6 +738,64 @@ describe('Marketplaces Service', () => {
});
});

describe('removeWhitelistCollection', () => {
it('when marketplace not found throws error', async () => {
const persistenceService = module.get<PersistenceService>(PersistenceService);

persistenceService.getMarketplaceByKey = jest.fn().mockReturnValueOnce(null);
persistenceService.getCollectionByKeyAndCollection = jest.fn().mockReturnValueOnce(new MarketplaceCollectionEntity());

await expect(service.removeWhitelistCollection(new RemoveWhitelistCollectionRequest())).rejects.toThrowError(BadRequestError);
});

it('when collection not found throws error', async () => {
const persistenceService = module.get<PersistenceService>(PersistenceService);

persistenceService.getMarketplaceByKey = jest.fn().mockReturnValueOnce(inputMarketplace[0]);
persistenceService.getCollectionByKeyAndCollection = jest.fn().mockReturnValueOnce(null);

await expect(service.removeWhitelistCollection(new RemoveWhitelistCollectionRequest())).rejects.toThrowError(BadRequestError);
});

it('when marketplace exists and delete fails returns false', async () => {
const persistenceService = module.get<PersistenceService>(PersistenceService);

persistenceService.getMarketplaceByKey = jest.fn().mockReturnValueOnce(inputMarketplace[0]);
persistenceService.getCollectionByKeyAndCollection = jest.fn().mockReturnValueOnce(new MarketplaceCollectionEntity());

persistenceService.deleteMarketplaceCollection = jest.fn(() => {
throw new Error();
});

const expectedResult = await service.removeWhitelistCollection(
new RemoveWhitelistCollectionRequest({ marketplaceKey: 'xoxno', collection: 'identifier' }),
);

expect(expectedResult).toBeFalsy();
});

it('when marketplace exists and save is succesfull returns true', async () => {
const persistenceService = module.get<PersistenceService>(PersistenceService);
const eventPublisher = module.get<CacheEventsPublisherService>(CacheEventsPublisherService);

eventPublisher.publish = jest.fn();
persistenceService.getMarketplaceByKey = jest.fn().mockReturnValueOnce(inputMarketplace[0]);
persistenceService.getCollectionByKeyAndCollection = jest.fn().mockReturnValueOnce(new MarketplaceCollectionEntity());

persistenceService.deleteMarketplaceCollection = jest.fn().mockReturnValueOnce(
new MarketplaceCollectionEntity({
collectionIdentifier: 'collection',
marketplaces: [inputMarketplace[0]],
}),
);
const expectedResult = await service.removeWhitelistCollection(
new RemoveWhitelistCollectionRequest({ marketplaceKey: 'xoxno', collection: 'identifier' }),
);

expect(expectedResult).toBeTruthy();
});
});

describe('whitelistMarketplace', () => {
it('when marketplace key exists throws error', async () => {
const persistenceService = module.get<PersistenceService>(PersistenceService);
Expand Down

0 comments on commit fa4e938

Please sign in to comment.