Skip to content

Commit

Permalink
test: implement graph.controller.spec.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
tmrdlt committed Aug 29, 2024
1 parent 533ee4c commit 049dcbb
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/api/graph/__snapshots__/graph.controller.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`GraphController GET /graph should return graph entries and use default value for empty timeFrame param 1`] = `
{
"data": [
"0.891976",
"4.45988",
"3.2178483932052355",
"1229.6756639761477",
"2796.5976572912773",
"2357.402275696613",
"2394.993846770214",
"4393.928107053716",
"5658.507945871855",
"5967.322102367237",
"5852.012548120791",
"5913.8632908319405",
"5947.116084972092",
"5536.124560953219",
"4855.777566862958",
"4864.095902889159",
"4856.350335213515",
"4870.636268888729",
"4914.178996317133",
"4912.496648397706",
"4910.442571134672",
"4912.544361500864",
"4909.655916901359",
"4771.123757553093",
"4074.1987426476467",
"4097.443070933878",
"4094.0281843595367",
"4092.008552809779",
"4091.725983349489",
"4091.7535841281515",
"4091.7535841281515",
],
"graphType": "TVL",
"labels": [
"1649064249645",
"1651593148104.2",
"1654122046563.4",
"1656650945022.6",
"1659179843481.8",
"1661708741941",
"1664237640400.2",
"1666766538859.4",
"1669295437318.6",
"1671824335777.8",
"1674353234237",
"1676882132696.2",
"1679411031155.4",
"1681939929614.6",
"1684468828073.8",
"1686997726533",
"1689526624992.2",
"1692055523451.4",
"1694584421910.6",
"1697113320369.8",
"1699642218829",
"1702171117288.2",
"1704700015747.4",
"1707228914206.6",
"1709757812665.8",
"1712286711125",
"1714815609584.2",
"1717344508043.4",
"1719873406502.6",
"1722402304961.8",
"1724931203421",
],
"timeFrame": "MAX",
}
`;
100 changes: 100 additions & 0 deletions src/api/graph/graph.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { CacheModule } from '@nestjs/cache-manager';
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';

import { GraphController } from '@/api/graph/graph.controller';
import { GraphType, TimeFrame } from '@/api/graph/graph.model';
import { GraphService } from '@/api/graph/graph.service';
import { overviewMaxTVLGraph } from '@/test/mock-data/graph-mock-data';

const mockGraphService = {
getGraph: jest.fn(),
};

describe('GraphController', () => {
let app: INestApplication;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [GraphController],
providers: [
{
provide: GraphService,
useValue: mockGraphService,
},
],
imports: [
CacheModule.register({
isGlobal: true,
}),
],
}).compile();

app = module.createNestApplication();
await app.init();
});

describe('GET /graph', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('should return graph entries and use default value for empty timeFrame param', async () => {
// Mocks
mockGraphService.getGraph.mockResolvedValue(overviewMaxTVLGraph);

// Call route
const result = await request(app.getHttpServer()).get(
'/graph?graphType=TVL',
);

// Assertions
expect(mockGraphService.getGraph).toHaveBeenCalledWith(
GraphType.TVL,
TimeFrame.MAX,
undefined,
undefined,
);
expect(result.status).toBe(200);
expect(result.body).toMatchSnapshot();
});

it('should parse all query params correctly', async () => {
// Call route
await request(app.getHttpServer()).get(
'/graph?graphType=Volume&timeFrame=1Y&tokenAddress=ct_123&pairAddress=ct_456',
);

// Assertions
expect(mockGraphService.getGraph).toHaveBeenCalledWith(
GraphType.Volume,
TimeFrame['1Y'],
'ct_123',
'ct_456',
);
});

it('should validate graphType param correctly', async () => {
// Call route
const result = await request(app.getHttpServer()).get(
'/graph?graphType=xyz',
);

// Assertions
expect(mockGraphService.getGraph).toHaveBeenCalledTimes(0);
expect(result.status).toBe(400);
});

it('should validate timeFrame query param correctly', async () => {
// Call route
const result = await request(app.getHttpServer()).get(
'/graph?timeFrame=xyz',
);

// Assertions
expect(mockGraphService.getGraph).toHaveBeenCalledTimes(0);
expect(result.status).toBe(400);
});
});
});
72 changes: 72 additions & 0 deletions test/mock-data/graph-mock-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Graph, GraphType, TimeFrame } from '@/api/graph/graph.model';

export const overviewMaxTVLGraph: Graph = {
graphType: GraphType.TVL,
timeFrame: TimeFrame.MAX,
labels: [
'1649064249645',
'1651593148104.2',
'1654122046563.4',
'1656650945022.6',
'1659179843481.8',
'1661708741941',
'1664237640400.2',
'1666766538859.4',
'1669295437318.6',
'1671824335777.8',
'1674353234237',
'1676882132696.2',
'1679411031155.4',
'1681939929614.6',
'1684468828073.8',
'1686997726533',
'1689526624992.2',
'1692055523451.4',
'1694584421910.6',
'1697113320369.8',
'1699642218829',
'1702171117288.2',
'1704700015747.4',
'1707228914206.6',
'1709757812665.8',
'1712286711125',
'1714815609584.2',
'1717344508043.4',
'1719873406502.6',
'1722402304961.8',
'1724931203421',
],
data: [
'0.891976',
'4.45988',
'3.2178483932052355',
'1229.6756639761477',
'2796.5976572912773',
'2357.402275696613',
'2394.993846770214',
'4393.928107053716',
'5658.507945871855',
'5967.322102367237',
'5852.012548120791',
'5913.8632908319405',
'5947.116084972092',
'5536.124560953219',
'4855.777566862958',
'4864.095902889159',
'4856.350335213515',
'4870.636268888729',
'4914.178996317133',
'4912.496648397706',
'4910.442571134672',
'4912.544361500864',
'4909.655916901359',
'4771.123757553093',
'4074.1987426476467',
'4097.443070933878',
'4094.0281843595367',
'4092.008552809779',
'4091.725983349489',
'4091.7535841281515',
'4091.7535841281515',
],
};

0 comments on commit 049dcbb

Please sign in to comment.