1+ const request = require ( 'supertest' ) ;
2+ const { app } = require ( '../index' ) ;
3+
4+ jest . mock ( 'fs/promises' , ( ) => ( {
5+ appendFile : jest . fn ( ( ) => Promise . resolve ( ) ) ,
6+ readFile : jest . fn ( ( ) => Promise . resolve ( '10;SERVICE_ID-abcde;\n20;SERVICE_ID-fghij;\n' ) ) ,
7+ } ) ) ;
8+
9+ jest . mock ( 'fs' , ( ) => ( {
10+ readdirSync : jest . fn ( ( path ) => {
11+ if ( path === './' ) return [ 'app.js' , 'package.json' , 'data' ] ;
12+ if ( path === './data' ) return [ 'results.csv' ] ;
13+ return [ ] ;
14+ } ) ,
15+ } ) ) ;
16+
17+ describe ( 'Express App Tests' , ( ) => {
18+ let server ;
19+
20+ beforeAll ( ( done ) => {
21+ server = app . listen ( 0 , done ) ;
22+ } ) ;
23+ afterAll ( ( done ) => {
24+ server . close ( done ) ;
25+ } ) ;
26+
27+ test ( 'GET /app1/ should return hello and serviceId' , async ( ) => {
28+ const res = await request ( server ) . get ( '/app1/' ) ;
29+ expect ( res . statusCode ) . toEqual ( 200 ) ;
30+ expect ( res . headers [ 'content-type' ] ) . toMatch ( / a p p l i c a t i o n \/ j s o n / ) ;
31+ expect ( res . body . result ) . toEqual ( 'hello' ) ;
32+ expect ( res . body ) . toHaveProperty ( 'serviceId' ) ;
33+ } ) ;
34+
35+ test ( 'GET /app1/add with valid body should return sum and serviceId' , async ( ) => {
36+ const res = await request ( server )
37+ . get ( '/app1/add' )
38+ . send ( { firstValue : 5 , secondValue : 3 } )
39+ . set ( 'Content-Type' , 'application/json' ) ;
40+
41+ expect ( res . statusCode ) . toEqual ( 200 ) ;
42+ expect ( res . body . result ) . toEqual ( 8 ) ;
43+ expect ( res . body ) . toHaveProperty ( 'serviceId' ) ;
44+ expect ( require ( 'fs/promises' ) . appendFile ) . toHaveBeenCalledWith (
45+ './data/results.csv' ,
46+ expect . stringContaining ( '8;SERVICE_ID-' ) ,
47+ ) ;
48+ } ) ;
49+
50+ test ( 'GET /app1/add without body should return 400' , async ( ) => {
51+ const res = await request ( server ) . get ( '/app1/add' ) ; // Без .send()
52+ expect ( res . statusCode ) . toEqual ( 400 ) ;
53+ expect ( res . text ) . toEqual ( 'body is required' ) ;
54+ } ) ;
55+
56+ test ( 'GET /app1/add with missing firstValue should return 400' , async ( ) => {
57+ const res = await request ( server )
58+ . get ( '/app1/add' )
59+ . send ( { secondValue : 3 } )
60+ . set ( 'Content-Type' , 'application/json' ) ;
61+ expect ( res . statusCode ) . toEqual ( 400 ) ;
62+ expect ( res . text ) . toEqual ( 'body is required' ) ;
63+ } ) ;
64+
65+ test ( 'GET /app1/getResults should return results from file' , async ( ) => {
66+ const res = await request ( server ) . get ( '/app1/getResults' ) ;
67+ expect ( res . statusCode ) . toEqual ( 200 ) ;
68+ expect ( res . body . results ) . toEqual ( '10;SERVICE_ID-abcde;\n20;SERVICE_ID-fghij;\n' ) ;
69+ expect ( res . body ) . toHaveProperty ( 'serviceId' ) ;
70+ // Перевіряємо, що readFile був викликаний
71+ expect ( require ( 'fs/promises' ) . readFile ) . toHaveBeenCalledWith (
72+ './data/results.csv' ,
73+ { encoding : 'utf-8' }
74+ ) ;
75+ } ) ;
76+
77+ test ( 'GET /app1/getDir should return directory contents and path' , async ( ) => {
78+ const res = await request ( server ) . get ( '/app1/getDir' ) ;
79+ expect ( res . statusCode ) . toEqual ( 200 ) ;
80+ expect ( res . body . files ) . toEqual ( [ 'app.js' , 'package.json' , 'data' ] ) ;
81+ expect ( res . body . dataContent ) . toEqual ( [ 'results.csv' ] ) ;
82+ expect ( res . body ) . toHaveProperty ( 'myPath' ) ;
83+ expect ( res . body ) . toHaveProperty ( 'serviceId' ) ;
84+ } ) ;
85+ } ) ;
0 commit comments