Skip to content

Commit

Permalink
fix: broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeCap08055 committed Aug 9, 2024
1 parent ff21b38 commit 68295a7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, beforeEach, jest, expect } from '@jest/globals';
import { u32 } from '@polkadot/types';
import { ApiPromise } from '@polkadot/api';
import { BlockchainService } from './blockchain.service';
import { ConfigService } from '../config/config.service';

Expand All @@ -10,8 +10,12 @@ describe('BlockchainService', () => {
beforeEach(async () => {
mockApi = {
createType: jest.fn(),
query: jest.fn(),
};
query: {
capacity: {
currentEpochInfo: jest.fn(),
},
},
} as unknown as ApiPromise;
const configService = {
logger: jest.fn(),
nestConfigService: jest.fn(),
Expand Down Expand Up @@ -39,15 +43,16 @@ describe('BlockchainService', () => {
getDebounceSeconds: jest.fn(),
};
blockchainService = new BlockchainService(configService as unknown as ConfigService);
blockchainService.api = mockApi;
});

describe('getCurrentCapacityEpochStart', () => {
it('should return the current capacity epoch start', async () => {
// Arrange
const expectedEpochStart = mockApi.createType('u32', 123);
const expectedEpochStart = { toNumber: jest.fn(() => 23) };
const currentEpochInfo = { epochStart: expectedEpochStart };

jest.spyOn(blockchainService, 'query').mockResolvedValue(currentEpochInfo);
jest.spyOn(mockApi.query.capacity, 'currentEpochInfo').mockResolvedValue(currentEpochInfo);

// Act
const result = await blockchainService.getCurrentCapacityEpochStart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export class BlockchainService implements OnApplicationBootstrap, OnApplicationS
private logger: Logger;

private readyResolve: (boolean) => void;

private readyReject: (reason: any) => void;

private isReadyPromise = new Promise<boolean>((resolve, reject) => {
this.readyResolve = resolve;
this.readyReject = reject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const setupConfigService = async (envObj: any): Promise<ConfigService> => {
return moduleRef.get<ConfigService>(ConfigService);
};

describe('AccountSericeConfig', () => {
describe('AccountServiceConfig', () => {
const ALL_ENV: { [key: string]: string | undefined } = {
REDIS_URL: undefined,
FREQUENCY_URL: undefined,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Injectable, Logger } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { BlockHash, Hash, SignedBlock } from '@polkadot/types/interfaces';
import { Hash, SignedBlock } from '@polkadot/types/interfaces';
import { BlockchainService } from '#lib/blockchain/blockchain.service';
import { DEFAULT_REDIS_NAMESPACE, getRedisToken, InjectRedis } from '@liaoliaots/nestjs-redis';
import { Redis } from 'ioredis';
import { BlockchainScannerService } from './blockchain-scanner.service';
import { FrameSystemEventRecord } from '@polkadot/types/lookup';
import { BlockchainScannerService } from './blockchain-scanner.service';

const mockRedis = {
provide: getRedisToken(DEFAULT_REDIS_NAMESPACE),
Expand Down Expand Up @@ -38,6 +38,7 @@ Object.defineProperty(mockEmptyBlockHash, 'isEmpty', {
get: jest.fn(() => true),
});
const mockBlockchainService = {
isReady: jest.fn(() => Promise.resolve()),
getBlock: jest.fn((blockHash?: string | Hash) => mockSignedBlock as unknown as SignedBlock),
getBlockHash: jest.fn((blockNumber: number) => (blockNumber > 1 ? mockEmptyBlockHash : mockBlockHash)),
getLatestFinalizedBlockNumber: jest.fn(),
Expand Down

0 comments on commit 68295a7

Please sign in to comment.