Skip to content

Commit

Permalink
test: Improve overall unit test coverage to above 90% 🧪
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldev5 authored and dudo50 committed Oct 4, 2024
1 parent de7c1ca commit aeb32bb
Show file tree
Hide file tree
Showing 162 changed files with 5,500 additions and 736 deletions.
3 changes: 2 additions & 1 deletion apps/playground/src/components/TransferInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import TransferInfoForm, { FormValues } from "./TransferInfoForm";
import OutputAlert from "./OutputAlert";
import { getTransferInfo } from "@paraspell/sdk";
import { fetchFromApi } from "../utils/submitUsingApi";
import { replaceBigInt } from "../utils/replaceBigInt";

const TransferInfo = () => {
const { selectedAccount } = useWallet();
Expand Down Expand Up @@ -79,7 +80,7 @@ const TransferInfo = () => {

try {
const output = await getQueryResult(formValues);
setOutput(JSON.stringify(output, null, 2));
setOutput(JSON.stringify(output, replaceBigInt, 2));
openOutputAlert();
closeAlert();
} catch (e) {
Expand Down
6 changes: 0 additions & 6 deletions apps/playground/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import ReactDOM from "react-dom/client";
import App from "./App";
import WalletProvider from "./providers/WalletProvider";

(BigInt.prototype as unknown as { toJSON: () => string }).toJSON = function (
this: bigint
) {
return this.toString();
};

ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<WalletProvider>
Expand Down
2 changes: 2 additions & 0 deletions apps/playground/src/utils/replaceBigInt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const replaceBigInt = (_key: string, value: unknown) =>
typeof value === 'bigint' ? value.toString() : value;
3 changes: 3 additions & 0 deletions apps/visualizator-be/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
"**/*.(t|j)s",
"!**/*.resolver.ts"
],
"coveragePathIgnorePatterns": [
".module.ts"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
Expand Down
49 changes: 49 additions & 0 deletions apps/visualizator-be/src/channels/channel.entity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Channel } from './channel.entity';

describe('Channel Entity', () => {
it('should create a Channel entity with the correct fields', () => {
const channel = new Channel();
channel.id = 1;
channel.sender = 101;
channel.recipient = 202;
channel.status = 'active';
channel.transfer_count = 5;
channel.message_count = 15;
channel.active_at = Date.now();
channel.proposed_max_capacity = 1000;
channel.proposed_max_message_size = 256;

expect(channel.id).toBe(1);
expect(channel.sender).toBe(101);
expect(channel.recipient).toBe(202);
expect(channel.status).toBe('active');
expect(channel.transfer_count).toBe(5);
expect(channel.message_count).toBe(15);
expect(channel.active_at).toBeGreaterThan(0);
expect(channel.proposed_max_capacity).toBe(1000);
expect(channel.proposed_max_message_size).toBe(256);
});

it('should handle nullable or optional fields correctly', () => {
const channel = new Channel();
channel.id = 2;
channel.sender = 103;
channel.recipient = 204;
channel.status = 'pending';
channel.transfer_count = 0;
channel.message_count = 0;
channel.active_at = 0;
channel.proposed_max_capacity = 500;
channel.proposed_max_message_size = 128;

expect(channel.id).toBe(2);
expect(channel.sender).toBe(103);
expect(channel.recipient).toBe(204);
expect(channel.status).toBe('pending');
expect(channel.transfer_count).toBe(0);
expect(channel.message_count).toBe(0);
expect(channel.active_at).toBe(0);
expect(channel.proposed_max_capacity).toBe(500);
expect(channel.proposed_max_message_size).toBe(128);
});
});
19 changes: 10 additions & 9 deletions apps/visualizator-be/src/channels/channel.entity.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
import { ObjectType, Field, Int } from '@nestjs/graphql';
import { ObjectType, Field } from '@nestjs/graphql';
import { returnInt } from '../utils/graphql.utils';
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@ObjectType()
@Entity('channels')
export class Channel {
@Field(() => Int)
@Field(returnInt)
@PrimaryGeneratedColumn()
id: number;

@Field(() => Int)
@Field(returnInt)
@Column()
sender: number;

@Field(() => Int)
@Field(returnInt)
@Column()
recipient: number;

@Field()
@Column()
status: string;

@Field(() => Int)
@Field(returnInt)
@Column()
transfer_count: number;

@Field(() => Int)
@Field(returnInt)
@Column()
message_count: number;

@Field(() => Int)
@Field(returnInt)
@Column('bigint')
active_at: number;

@Field(() => Int)
@Field(returnInt)
@Column()
proposed_max_capacity: number;

@Field(() => Int)
@Field(returnInt)
@Column()
proposed_max_message_size: number;
}
30 changes: 30 additions & 0 deletions apps/visualizator-be/src/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app/app.module';

jest.mock('@nestjs/core', () => ({
NestFactory: {
create: jest.fn(),
},
}));

describe('Application Bootstrap', () => {
let mockApp: { listen: jest.Mock };

beforeAll(() => {
mockApp = {
listen: jest.fn(),
};

(NestFactory.create as jest.Mock).mockResolvedValue(mockApp);
});

it('should bootstrap the application and listen on the correct port', async () => {
const { bootstrap } = await import('./main');

await bootstrap();

expect(() => NestFactory.create(AppModule, { cors: true })).not.toThrow();

expect(mockApp.listen).toHaveBeenCalledWith(4201);
});
});
5 changes: 3 additions & 2 deletions apps/visualizator-be/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app/app.module';

async function bootstrap() {
export const bootstrap = async () => {
const options = { cors: true };
const app = await NestFactory.create(AppModule, options);
await app.listen(4201);
}
};

void bootstrap();
47 changes: 47 additions & 0 deletions apps/visualizator-be/src/messages/message.entity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ID } from '@nestjs/graphql';
import { Message, returnID, returnAssetArray, Asset } from './message.entity';

describe('Message Entity', () => {
it('should create a Message entity with the correct fields', () => {
const assets: Asset[] = [
{
enum_key: 'key',
asset_module: 'module',
amount: '1000',
decimals: 6,
symbol: 'TOKEN',
},
];

const message = new Message();
message.message_hash = 'hash';
message.origin_event_index = 'event_1';
message.from_account_id = 'account_1';
message.origin_para_id = 1000;
message.origin_block_timestamp = Date.now();
message.relayed_block_timestamp = Date.now();
message.block_num = 1;
message.status = 'pending';
message.relayed_event_index = 'event_2';
message.dest_event_index = 'event_3';
message.dest_para_id = 2000;
message.to_account_id = 'account_2';
message.confirm_block_timestamp = Date.now();
message.extrinsic_index = 'extrinsic_1';
message.relayed_extrinsic_index = 'extrinsic_2';
message.dest_extrinsic_index = 'extrinsic_3';
message.child_para_id = 3000;
message.child_dest = 'child_dest';
message.protocol = 'protocol_1';
message.message_type = 'type_1';
message.unique_id = 'unique_1';
message.xcm_version = 2;
message.assets = assets;

expect(message.message_hash).toBe('hash');
expect(message.assets[0].symbol).toBe('TOKEN');
expect(message.xcm_version).toBe(2);
expect(returnID()).toBe(ID);
expect(returnAssetArray()).toStrictEqual([Asset]);
});
});
28 changes: 16 additions & 12 deletions apps/visualizator-be/src/messages/message.entity.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { ObjectType, Field, ID, Int } from '@nestjs/graphql';
import { ObjectType, Field, ID } from '@nestjs/graphql';
import { returnInt } from '../utils/graphql.utils';
import { Entity, Column, PrimaryColumn } from 'typeorm';

export const returnID = () => ID;
export const returnAssetArray = () => [Asset];

@ObjectType()
@Entity('messages')
export class Message {
@Field(() => ID)
@Field(returnID)
@PrimaryColumn()
message_hash: string;

Expand All @@ -16,19 +20,19 @@ export class Message {
@Column()
from_account_id: string;

@Field(() => Int)
@Field(returnInt)
@Column()
origin_para_id: number;

@Field(() => Int)
@Field(returnInt)
@Column('bigint')
origin_block_timestamp: number;

@Field(() => Int)
@Field(returnInt)
@Column('bigint')
relayed_block_timestamp: number;

@Field(() => Int)
@Field(returnInt)
@Column('bigint')
block_num: number;

Expand All @@ -44,15 +48,15 @@ export class Message {
@Column()
dest_event_index: string;

@Field(() => Int)
@Field(returnInt)
@Column()
dest_para_id: number;

@Field()
@Column()
to_account_id: string;

@Field(() => Int)
@Field(returnInt)
@Column('bigint')
confirm_block_timestamp: number;

Expand All @@ -68,7 +72,7 @@ export class Message {
@Column()
dest_extrinsic_index: string;

@Field(() => Int)
@Field(returnInt)
@Column()
child_para_id: number;

Expand All @@ -88,11 +92,11 @@ export class Message {
@Column()
unique_id: string;

@Field(() => Int)
@Field(returnInt)
@Column()
xcm_version: number;

@Field(() => [Asset])
@Field(returnAssetArray)
@Column('jsonb')
assets: Asset[];
}
Expand All @@ -108,7 +112,7 @@ export class Asset {
@Field()
amount: string;

@Field(() => Int)
@Field(returnInt)
decimals: number;

@Field()
Expand Down
44 changes: 44 additions & 0 deletions apps/visualizator-be/src/messages/messages.module.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { MessageService } from './messages.service';
import { MessageResolver } from './messages.resolver';
import { Message } from './message.entity';

describe('MessageModule', () => {
let module: TestingModule;
let messageService: MessageService;
let messageResolver: MessageResolver;

beforeAll(async () => {
const mockRepository = {
find: jest.fn(),
save: jest.fn(),
};

module = await Test.createTestingModule({
providers: [
MessageService,
MessageResolver,
{
provide: getRepositoryToken(Message),
useValue: mockRepository,
},
],
}).compile();

messageService = module.get<MessageService>(MessageService);
messageResolver = module.get<MessageResolver>(MessageResolver);
});

it('should compile the module', () => {
expect(module).toBeDefined();
});

it('should provide MessageService', () => {
expect(messageService).toBeDefined();
});

it('should provide MessageResolver', () => {
expect(messageResolver).toBeDefined();
});
});
Loading

0 comments on commit aeb32bb

Please sign in to comment.