1+ const request = require ( 'supertest' ) ;
2+ const app = require ( './app' ) ;
3+
4+ describe ( 'API Endpoints' , ( ) => {
5+ describe ( 'GET /health' , ( ) => {
6+ test ( 'should return health status' , async ( ) => {
7+ const response = await request ( app )
8+ . get ( '/health' )
9+ . expect ( 200 ) ;
10+
11+ expect ( response . body ) . toEqual ( { status : 'ok' } ) ;
12+ } ) ;
13+ } ) ;
14+
15+ describe ( 'POST /calculate-discount' , ( ) => {
16+ test ( 'should calculate discount correctly' , async ( ) => {
17+ const response = await request ( app )
18+ . post ( '/calculate-discount' )
19+ . send ( { price : 100 , percentage : 20 } )
20+ . expect ( 200 ) ;
21+
22+ expect ( response . body ) . toEqual ( {
23+ original : 100 ,
24+ discount : 20 ,
25+ final : 80
26+ } ) ;
27+ } ) ;
28+
29+ test ( 'should return 400 for invalid input' , async ( ) => {
30+ const response = await request ( app )
31+ . post ( '/calculate-discount' )
32+ . send ( { price : '100' , percentage : 20 } )
33+ . expect ( 400 ) ;
34+
35+ expect ( response . body ) . toHaveProperty ( 'error' ) ;
36+ } ) ;
37+
38+ test ( 'should return 400 for invalid percentage' , async ( ) => {
39+ const response = await request ( app )
40+ . post ( '/calculate-discount' )
41+ . send ( { price : 100 , percentage : 150 } )
42+ . expect ( 400 ) ;
43+
44+ expect ( response . body ) . toHaveProperty ( 'error' ) ;
45+ } ) ;
46+ } ) ;
47+ } ) ;
0 commit comments