1- import mongoose from 'mongoose' ;
2- import { MongoMemoryServer } from 'mongodb-memory-server' ;
31import { jest } from '@jest/globals' ;
4- import databaseUtil from '#models/databaseUtil' ;
5- import app from '#app' ;
2+ import request from 'supertest' ;
3+ import app from '#app' ; // Update this import based on your app's structure
4+ import connector from '#models/databaseUtil' ; // Update this import
5+ import infrastructureModel from '#models/infrastructure' ; // Update this import
66
7- import InfrastructureFunctions from '#models/infrastructure' ;
8-
9- const request = require ( 'supertest' ) ;
107jest . mock ( '#util' ) ;
11- let mongoServer ;
8+
129let server ;
1310let agent ;
11+
1412beforeAll ( ( done ) => {
1513 server = app . listen ( 5000 , ( ) => {
1614 agent = request . agent ( server ) ;
@@ -20,113 +18,78 @@ beforeAll((done) => {
2018} ) ;
2119
2220function cleanUp ( callback ) {
23- accreditationModel . remove ( { name : 'xyz' } ) . then ( ( ) => {
24- connector . disconnect ( ( DBerr ) => {
25- if ( DBerr ) console . log ( 'Database dissconnnect error: ' , DBerr ) ;
26- server . close ( ( serverErr ) => {
27- if ( serverErr ) console . log ( serverErr ) ;
28- callback ( ) ;
21+ infrastructureModel
22+ . remove ( {
23+ name : 'Building A' ,
24+ type : 'Office' ,
25+ wing : 'East' ,
26+ floor : 3 ,
27+ capacity : 100 ,
28+ } )
29+ . then ( ( ) => {
30+ connector . disconnect ( ( DBerr ) => {
31+ if ( DBerr ) console . log ( 'Database disconnect error: ' , DBerr ) ;
32+ server . close ( ( serverErr ) => {
33+ if ( serverErr ) console . log ( serverErr ) ;
34+ callback ( ) ;
35+ } ) ;
2936 } ) ;
3037 } ) ;
31- } ) ;
3238}
3339
3440afterAll ( ( done ) => {
3541 cleanUp ( done ) ;
3642} ) ;
3743
38- describe ( 'Infrastructure API Endpoints' , ( ) => {
39- // ... (beforeEach, beforeAll, afterAll, etc.)
40-
41- test ( 'Read Infrastructure' , async ( ) => {
42- await InfrastructureFunctions . create (
43- 'Building A' ,
44- 'Office' ,
45- 'East Wing' ,
46- 2 ,
47- 100
48- ) ;
49- await InfrastructureFunctions . create (
50- 'Building B' ,
51- 'Residential' ,
52- 'West Wing' ,
53- 1 ,
54- 50
55- ) ;
56-
57- const response = await request ( app ) . get ( '/api/infrastructures' ) ; // Adjust the API endpoint
58-
59- expect ( response . status ) . toBe ( 200 ) ;
60- expect ( response . body ) . toHaveLength ( 2 ) ;
61- expect ( response . body [ 0 ] . name ) . toBe ( 'Building A' ) ;
62- expect ( response . body [ 1 ] . name ) . toBe ( 'Building B' ) ;
63- } ) ;
64-
65- test ( 'Update Infrastructure' , async ( ) => {
66- const createdInfrastructure = await InfrastructureFunctions . create (
67- 'Building A' ,
68- 'Office' ,
69- 'East Wing' ,
70- 2 ,
71- 100
72- ) ;
73-
74- const response = await request ( app )
75- . put ( `/api/infrastructures/${ createdInfrastructure . _id } ` ) // Adjust the API endpoint
76- . send ( { capacity : 150 } ) ;
77-
78- expect ( response . status ) . toBe ( 200 ) ;
79- expect ( response . body . capacity ) . toBe ( 150 ) ;
80- } ) ;
81-
82- test ( 'Remove Infrastructure' , async ( ) => {
83- const createdInfrastructure = await InfrastructureFunctions . create (
84- 'Building A' ,
85- 'Office' ,
86- 'East Wing' ,
87- 2 ,
88- 100
89- ) ;
90-
91- const response = await request ( app ) . delete (
92- `/api/infrastructures/${ createdInfrastructure . _id } `
93- ) ; // Adjust the API endpoint
94-
95- expect ( response . status ) . toBe ( 200 ) ;
96- expect ( response . body ) . toMatchObject ( {
44+ describe ( 'Infrastructure API' , ( ) => {
45+ it ( 'should create infrastructure' , async ( ) => {
46+ const response = await agent . post ( '/infrastructure/add' ) . send ( {
9747 name : 'Building A' ,
9848 type : 'Office' ,
99- wing : 'East Wing ' ,
100- floor : 2 ,
49+ wing : 'East' ,
50+ floor : 3 ,
10151 capacity : 100 ,
10252 } ) ;
10353
104- const infrastructuresResponse = await request ( app ) . get (
105- '/api/infrastructures'
106- ) ;
107- expect ( infrastructuresResponse . body ) . toHaveLength ( 0 ) ;
54+ expect ( response . status ) . toBe ( 200 ) ;
55+ expect ( response . body . res ) . toMatch ( / a d d e d u s e r / ) ;
10856 } ) ;
10957
110- test ( 'Get Infrastructure by ID' , async ( ) => {
111- const createdInfrastructure = await InfrastructureFunctions . create (
112- 'Building A' ,
113- 'Office' ,
114- 'East Wing' ,
115- 2 ,
116- 100
117- ) ;
58+ describe ( 'after adding infrastructure' , ( ) => {
59+ beforeEach ( async ( ) => {
60+ await agent . post ( '/infrastructure/add' ) . send ( {
61+ name : 'Building A' ,
62+ type : 'Office' ,
63+ wing : 'East' ,
64+ floor : 3 ,
65+ capacity : 100 ,
66+ } ) ;
67+ } ) ;
68+
69+ afterEach ( async ( ) => {
70+ await infrastructureModel . remove ( {
71+ name : 'Building A' ,
72+ type : 'Office' ,
73+ wing : 'East' ,
74+ floor : 3 ,
75+ capacity : 100 ,
76+ } ) ;
77+ } ) ;
11878
119- const response = await request ( app ) . get (
120- `/api/infrastructures/${ createdInfrastructure . _id } `
121- ) ; // Adjust the API endpoint
79+ it ( 'should read infrastructure' , async ( ) => {
80+ const response = await agent
81+ . post ( '/infrastructure/list' )
82+ . send ( { name : 'Building A' } ) ;
83+ expect ( response . body . res ) . not . toBeNull ( ) ;
84+ } ) ;
12285
123- expect ( response . status ) . toBe ( 200 ) ;
124- expect ( response . body ) . toMatchObject ( {
125- name : 'Building A' ,
126- type : 'Office' ,
127- wing : 'East Wing' ,
128- floor : 2 ,
129- capacity : 100 ,
86+ it ( 'should update infrastructure' , async ( ) => {
87+ const response = await agent
88+ . post ( '/infrastructure/update' )
89+ . send ( { name : 'Building A' } , { capacity : 150 } ) ;
90+
91+ expect ( response . status ) . toBe ( 200 ) ;
92+ expect ( response . body . res ) . toMatch ( / u p d a t e d i n f r a s t r u c t u r e / ) ;
13093 } ) ;
13194 } ) ;
13295} ) ;
0 commit comments