Skip to content

Commit 4cae86d

Browse files
committed
add tests
1 parent 4f81739 commit 4cae86d

File tree

2 files changed

+72
-21
lines changed

2 files changed

+72
-21
lines changed

src/server.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -276,29 +276,29 @@ app.get(
276276
}
277277
);
278278

279-
app.get(
280-
"/api/ft-token-price",
281-
async (req: Request, res: Response) => {
282-
try {
283-
const { account_id } = req.query;
284-
285-
const contract = account_id === 'near' ? 'wrap.near' : account_id;
286-
const { data } = await axios.get(
287-
`https://api.nearblocks.io/v1/fts/${contract}`,
288-
{
289-
headers: {
290-
Authorization: `Bearer ${process.env.NEARBLOCKS_API_KEY}`,
291-
},
292-
}
293-
);
294-
const contractData = data?.contracts?.[0];
295-
return res.send({ price:parseFloat(contractData.price) || 0 });
296-
} catch (error) {
297-
console.error("Error fetching data:", error);
298-
return res.status(500).send({ error: "An error occurred" });
279+
app.get("/api/ft-token-price", async (req: Request, res: Response) => {
280+
try {
281+
const { account_id } = req.query;
282+
if (!account_id) {
283+
return res.status(400).send({ error: "account_id is required" });
299284
}
285+
286+
const contract = account_id === "near" ? "wrap.near" : account_id;
287+
const { data } = await axios.get(
288+
`https://api.nearblocks.io/v1/fts/${contract}`,
289+
{
290+
headers: {
291+
Authorization: `Bearer ${process.env.NEARBLOCKS_API_KEY}`,
292+
},
293+
}
294+
);
295+
const contractData = data?.contracts?.[0];
296+
return res.send({ price: parseFloat(contractData.price) || 0 });
297+
} catch (error) {
298+
console.error("Error fetching data:", error);
299+
return res.status(500).send({ error: "An error occurred" });
300300
}
301-
);
301+
});
302302

303303
// Start the server
304304
if (process.env.NODE_ENV !== "test") {

test/server.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import * as ftTokens from "../src/ft-tokens";
88
import * as allTokenBalanceHistory from "../src/all-token-balance-history";
99
import * as transactionsTransferHistory from "../src/transactions-transfer-history";
1010
import { tokens } from "../src/constants/tokens";
11+
import axios from "axios";
12+
13+
jest.mock("axios");
14+
const mockedAxios = axios as jest.Mocked<typeof axios>;
1115

1216
// Mock all external dependencies
1317
jest.mock("../src/prisma", () => ({
@@ -231,4 +235,51 @@ describe("API Endpoints", () => {
231235
expect(response.status).toBe(400);
232236
});
233237
});
238+
239+
describe("GET /api/ft-token-price", () => {
240+
it("should return token price of near", async () => {
241+
const mockPrice = 2;
242+
const mockResponse = {
243+
data: {
244+
contracts: [{ price: mockPrice.toString() }],
245+
},
246+
};
247+
248+
mockedAxios.get.mockResolvedValue(mockResponse);
249+
250+
const response = await request(app)
251+
.get("/api/ft-token-price")
252+
.query({ account_id: "near" });
253+
254+
expect(response.status).toBe(200);
255+
expect(response.body).toEqual({ price: mockPrice });
256+
257+
expect(axios.get).toHaveBeenCalledWith(
258+
"https://api.nearblocks.io/v1/fts/wrap.near",
259+
expect.objectContaining({
260+
headers: expect.objectContaining({
261+
Authorization: expect.stringContaining("Bearer"),
262+
}),
263+
})
264+
);
265+
});
266+
267+
it("should return 400 if account_id is missing", async () => {
268+
const response = await request(app).get("/api/ft-token-price");
269+
270+
expect(response.status).toBe(400);
271+
expect(response.body).toEqual({ error: "account_id is required" });
272+
});
273+
274+
it("should return 500 if axios request fails", async () => {
275+
mockedAxios.get.mockRejectedValue(new Error("API error"));
276+
277+
const response = await request(app)
278+
.get("/api/ft-token-price")
279+
.query({ account_id: "near" });
280+
281+
expect(response.status).toBe(500);
282+
expect(response.body).toEqual({ error: "An error occurred" });
283+
});
284+
});
234285
});

0 commit comments

Comments
 (0)