Skip to content

Commit

Permalink
test: add unit tests for api and app service
Browse files Browse the repository at this point in the history
  • Loading branch information
Romsters committed Nov 1, 2023
1 parent 1f92bbf commit b6f3e9f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/api/src/token/token.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,12 @@ describe("TokenService", () => {
expect(repositoryMock.createQueryBuilder).toHaveBeenCalledWith("token");
});

it("returns tokens ordered by blockNumber and logIndex DESC", async () => {
it("returns tokens ordered by liquidity, blockNumber and logIndex DESC", async () => {
await service.findAll(pagingOptions);
expect(queryBuilderMock.orderBy).toBeCalledTimes(1);
expect(queryBuilderMock.orderBy).toHaveBeenCalledWith("token.blockNumber", "DESC");
expect(queryBuilderMock.addOrderBy).toBeCalledTimes(1);
expect(queryBuilderMock.orderBy).toHaveBeenCalledWith("token.liquidity", "DESC");
expect(queryBuilderMock.addOrderBy).toBeCalledTimes(2);
expect(queryBuilderMock.addOrderBy).toHaveBeenCalledWith("token.blockNumber", "DESC");
expect(queryBuilderMock.addOrderBy).toHaveBeenCalledWith("token.logIndex", "DESC");
});

Expand Down
33 changes: 33 additions & 0 deletions packages/worker/src/app.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { CounterService } from "./counter";
import { BatchService } from "./batch";
import { BlockService } from "./block";
import { BlocksRevertService } from "./blocksRevert";
import { TokenOffChainDataSaverService } from "./token/tokenInfo/tokenOffChainDataSaver.service";
import runMigrations from "./utils/runMigrations";
import { BLOCKS_REVERT_DETECTED_EVENT } from "./constants";

Expand All @@ -35,6 +36,7 @@ describe("AppService", () => {
let batchService: BatchService;
let blockService: BlockService;
let blocksRevertService: BlocksRevertService;
let tokenOffChainDataSaverService: TokenOffChainDataSaverService;
let dataSourceMock: DataSource;
let configServiceMock: ConfigService;

Expand All @@ -58,6 +60,10 @@ describe("AppService", () => {
blocksRevertService = mock<BlocksRevertService>({
handleRevert: jest.fn().mockResolvedValue(null),
});
tokenOffChainDataSaverService = mock<TokenOffChainDataSaverService>({
start: jest.fn().mockResolvedValue(null),
stop: jest.fn().mockResolvedValue(null),
});
dataSourceMock = mock<DataSource>();
configServiceMock = mock<ConfigService>({
get: jest.fn().mockReturnValue(false),
Expand Down Expand Up @@ -88,6 +94,10 @@ describe("AppService", () => {
provide: BlocksRevertService,
useValue: blocksRevertService,
},
{
provide: TokenOffChainDataSaverService,
useValue: tokenOffChainDataSaverService,
},
{
provide: DataSource,
useValue: dataSourceMock,
Expand Down Expand Up @@ -156,6 +166,13 @@ describe("AppService", () => {
expect(balancesCleanerService.stop).toBeCalledTimes(1);
});

it("does not start token offchain data saver service by default", async () => {
appService.onModuleInit();
await migrationsRunFinished;
expect(tokenOffChainDataSaverService.start).not.toBeCalled();
appService.onModuleDestroy();
});

it("does not start batches service when disableBatchesProcessing is true", async () => {
(configServiceMock.get as jest.Mock).mockReturnValue(true);
appService.onModuleInit();
Expand All @@ -179,6 +196,15 @@ describe("AppService", () => {
expect(balancesCleanerService.start).not.toBeCalled();
appService.onModuleDestroy();
});

it("starts token offchain data saver service when enableTokenOffChainDataSaver is true", async () => {
(configServiceMock.get as jest.Mock).mockReturnValue(true);
appService.onModuleInit();
await migrationsRunFinished;
expect(tokenOffChainDataSaverService.start).toBeCalledTimes(1);
appService.onModuleDestroy();
expect(tokenOffChainDataSaverService.stop).toBeCalledTimes(1);
});
});

describe("onModuleDestroy", () => {
Expand All @@ -201,6 +227,11 @@ describe("AppService", () => {
appService.onModuleDestroy();
expect(balancesCleanerService.stop).toBeCalledTimes(1);
});

it("stops token offchain data saver service", async () => {
appService.onModuleDestroy();
expect(tokenOffChainDataSaverService.stop).toBeCalledTimes(1);
});
});

describe("Handling blocks revert event", () => {
Expand All @@ -212,13 +243,15 @@ describe("AppService", () => {
expect(batchService.stop).toBeCalledTimes(1);
expect(counterService.stop).toBeCalledTimes(1);
expect(balancesCleanerService.stop).toBeCalledTimes(1);
expect(tokenOffChainDataSaverService.stop).toBeCalledTimes(1);

expect(blocksRevertService.handleRevert).toBeCalledWith(blockNumber);

expect(blockService.start).toBeCalledTimes(2);
expect(batchService.start).toBeCalledTimes(2);
expect(counterService.start).toBeCalledTimes(2);
expect(balancesCleanerService.start).toBeCalledTimes(2);
expect(tokenOffChainDataSaverService.start).toBeCalledTimes(2);

await app.close();
});
Expand Down

0 comments on commit b6f3e9f

Please sign in to comment.