Skip to content
This repository has been archived by the owner on Feb 25, 2024. It is now read-only.

Commit

Permalink
Add End-To-End tests using supertest
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldev5 committed Aug 10, 2023
1 parent 4205c83 commit 0b97d4b
Show file tree
Hide file tree
Showing 16 changed files with 492 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
ignorePatterns: ['.eslintrc.js', 'test/app.e2e-spec.ts'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ jobs:
- run: pnpm lint
- run: pnpm build
- run: pnpm test
- run: pnpm test:e2e
- uses: codecov/codecov-action@v3
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
"test:e2e": "jest --config ./test/jest-e2e.json --forceExit",
"test:e2e:watch": "jest --watch --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
Expand Down
6 changes: 2 additions & 4 deletions src/app.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';

describe('AppController', () => {
let appController: AppController;

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();

appController = app.get<AppController>(AppController);
});

describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
it('should return undefined because it redirects to XCM API Github repo"', () => {
expect(appController.root()).toBeUndefined();
});
});
});
11 changes: 4 additions & 7 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { Controller, Get, Redirect } from '@nestjs/common';

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
getHello(): string {
return this.appService.getHello();
}
@Redirect('https://github.com/paraspell/xcm-api', 301)
// eslint-disable-next-line @typescript-eslint/no-empty-function
root() {}
}
6 changes: 0 additions & 6 deletions src/app.module.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppModule } from './app.module';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { XTransferModule } from './x-transfer/x-transfer.module';
import { AssetsModule } from './assets/assets.module';
import { ChannelsModule } from './channels/channels.module';
Expand All @@ -25,11 +24,6 @@ describe('AppModule', () => {
expect(controllers).toBeDefined();
});

it('should have the correct providers', () => {
const providers = appModule.get<AppService>(AppService);
expect(providers).toBeDefined();
});

it('should have the correct module imports', () => {
const xTransferModule = appModule.select(XTransferModule);
const assetsModule = appModule.select(AssetsModule);
Expand Down
2 changes: 0 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { XTransferModule } from './x-transfer/x-transfer.module';
import { AssetsModule } from './assets/assets.module';
import { ChannelsModule } from './channels/channels.module';
Expand All @@ -9,6 +8,5 @@ import { PalletsModule } from './pallets/pallets.module';
@Module({
imports: [XTransferModule, AssetsModule, ChannelsModule, PalletsModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
8 changes: 0 additions & 8 deletions src/app.service.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/assets/assets.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('AssetsController', () => {
const mockResult = '1';
jest.spyOn(assetsService, 'getAssetId').mockReturnValue(mockResult);

const result = controller.getAssetId(node, symbol);
const result = controller.getAssetId(node, { symbol });

expect(result).toBe(mockResult);
expect(assetsService.getAssetId).toHaveBeenCalledWith(node, symbol);
Expand Down Expand Up @@ -156,7 +156,7 @@ describe('AssetsController', () => {
const mockResult = 18;
jest.spyOn(assetsService, 'getDecimals').mockReturnValue(mockResult);

const result = controller.getDecimals(node, symbol);
const result = controller.getDecimals(node, { symbol });

expect(result).toBe(mockResult);
expect(assetsService.getDecimals).toHaveBeenCalledWith(node, symbol);
Expand All @@ -170,7 +170,7 @@ describe('AssetsController', () => {
.spyOn(assetsService, 'hasSupportForAsset')
.mockReturnValue(mockResult);

const result = controller.hasSupportForAsset(node, symbol);
const result = controller.hasSupportForAsset(node, { symbol });

expect(result).toBe(mockResult);
expect(assetsService.hasSupportForAsset).toHaveBeenCalledWith(
Expand Down
17 changes: 9 additions & 8 deletions src/assets/assets.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Controller, Get, Param } from '@nestjs/common';
import { Controller, Get, Param, Query } from '@nestjs/common';
import { AssetsService } from './assets.service';
import { isNumeric } from 'src/utils';
import { isNumeric } from '../utils';
import { SymbolDto } from './dto/SymbolDto';

@Controller('assets')
export class AssetsController {
Expand All @@ -20,8 +21,8 @@ export class AssetsController {
return this.assetsService.getAssetsObject(nodeOrParaId);
}

@Get(':node/id/:symbol')
getAssetId(@Param('node') node: string, @Param('symbol') symbol: string) {
@Get(':node/id')
getAssetId(@Param('node') node: string, @Query() { symbol }: SymbolDto) {
return this.assetsService.getAssetId(node, symbol);
}

Expand All @@ -45,15 +46,15 @@ export class AssetsController {
return this.assetsService.getAllAssetsSymbols(node);
}

@Get(':node/decimals/:symbol')
getDecimals(@Param('node') node: string, @Param('symbol') symbol: string) {
@Get(':node/decimals')
getDecimals(@Param('node') node: string, @Query() { symbol }: SymbolDto) {
return this.assetsService.getDecimals(node, symbol);
}

@Get(':node/has-support/:symbol')
@Get(':node/has-support')
hasSupportForAsset(
@Param('node') node: string,
@Param('symbol') symbol: string,
@Query() { symbol }: SymbolDto,
) {
return this.assetsService.hasSupportForAsset(node, symbol);
}
Expand Down
4 changes: 2 additions & 2 deletions src/assets/assets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
getTNode,
hasSupportForAsset,
} from '@paraspell/sdk';
import { validateNode } from 'src/utils';
import { validateNode } from '../utils';

@Injectable()
export class AssetsService {
Expand Down Expand Up @@ -58,7 +58,7 @@ export class AssetsService {
getDecimals(node: string, symbol: string) {
validateNode(node);
const decimals = getAssetDecimals(node as TNode, symbol);
if (!decimals) {
if (decimals === null) {
throw new NotFoundException(`Decimals for currency ${symbol} not found.`);
}
return decimals;
Expand Down
6 changes: 6 additions & 0 deletions src/assets/dto/SymbolDto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { IsNotEmpty } from 'class-validator';

export class SymbolDto {
@IsNotEmpty()
symbol: string;
}
2 changes: 1 addition & 1 deletion src/pallets/pallets.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { TNode, getDefaultPallet, getSupportedPallets } from '@paraspell/sdk';
import { validateNode } from 'src/utils';
import { validateNode } from '../utils';

@Injectable()
export class PalletsService {
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@ export const getNodeRelayChainWsUrl = (destinationNode: TNode) => {
const KUSAMA_WS = 'wss://rpc.polkadot.io';
return symbol === 'DOT' ? POLKADOT_WS : KUSAMA_WS;
};

export const determineWsUrl = (fromNode?: TNode, destinationNode?: TNode) => {
return fromNode
? findWsUrlByNode(fromNode)
: getNodeRelayChainWsUrl(destinationNode);
};
12 changes: 1 addition & 11 deletions src/x-transfer/x-transfer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,7 @@ import {
TSerializedApiCall,
} from '@paraspell/sdk';
import { XTransferDto } from './dto/XTransferDto';
import {
createApiInstance,
findWsUrlByNode,
getNodeRelayChainWsUrl,
} from 'src/utils';

const determineWsUrl = (fromNode?: TNode, destinationNode?: TNode) => {
return fromNode
? findWsUrlByNode(fromNode)
: getNodeRelayChainWsUrl(destinationNode);
};
import { createApiInstance, determineWsUrl } from '../utils';

@Injectable()
export class XTransferService {
Expand Down
Loading

0 comments on commit 0b97d4b

Please sign in to comment.