@@ -8,6 +8,10 @@ import * as ftTokens from "../src/ft-tokens";
88import * as allTokenBalanceHistory from "../src/all-token-balance-history" ;
99import * as transactionsTransferHistory from "../src/transactions-transfer-history" ;
1010import { 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
1317jest . 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