1- import { jest } from "@jest/globals" ;
1+ import { jest } from "@jest/globals" ; // eslint-disable-line import/no-extraneous-dependencies
22import request from "supertest" ;
33import app from "#app" ; // Update this import based on your app's structure
44import connector from "#models/databaseUtil" ; // Update this import
@@ -10,7 +10,7 @@ let server;
1010let agent ;
1111
1212beforeAll ( ( done ) => {
13- server = app . listen ( 5000 , ( ) => {
13+ server = app . listen ( null , ( ) => {
1414 agent = request . agent ( server ) ;
1515 connector . set ( "debug" , false ) ;
1616 done ( ) ;
@@ -19,7 +19,7 @@ beforeAll((done) => {
1919
2020function cleanUp ( callback ) {
2121 PracticalModel
22- . deleteMany ( { } )
22+ . remove ( { } )
2323 . then ( ( ) => {
2424 connector . disconnect ( ( DBerr ) => {
2525 if ( DBerr ) console . log ( "Database disconnect error: " , DBerr ) ;
@@ -45,48 +45,42 @@ describe("Practical API", () => {
4545 cognitiveLevels : [ "L2" , "L3" ] ,
4646 } ) ;
4747
48- expect ( response . status ) . toBe ( 201 ) ;
49- expect ( response . body . res ) . toMatch ( / a d d e d u s e r / ) ;
48+ expect ( response . status ) . toBe ( 200 ) ;
49+ expect ( response . body . res ) . toMatch ( / A d d e d P r a c t i c a l / ) ;
5050 } ) ;
5151
5252 describe ( "after creating a practical" , ( ) => {
5353 let practicalId ;
5454
5555 beforeEach ( async ( ) => {
56- const response = await agent . post ( "/practical/create" ) . send ( {
57- no : 1 ,
58- title : "Sample Practical " ,
59- type : "Experiment " ,
60- hours : 2 ,
61- cognitiveLevels : [ "L2 " , "L3 " ] ,
56+ const id = await agent . post ( "/practical/create" ) . send ( {
57+ no : 2 ,
58+ title : "new practical " ,
59+ type : "fun experiment " ,
60+ hours : 5 ,
61+ cognitiveLevels : [ "L1 " , "L4 " ] ,
6262 } ) ;
63- practicalId = response . body . res . match ( / ( \d + ) / ) [ 0 ] ;
63+ practicalId = JSON . parse ( id . res . text ) . id ;
6464 } ) ;
6565
6666 afterEach ( async ( ) => {
67- await PracticalModel . deleteOne ( { _id : practicalId } ) ;
67+ await PracticalModel . remove ( ) ;
6868 } ) ;
6969
7070 it ( "should list practical entities" , async ( ) => {
71- const response = await agent . get ( "/practical/list" ) ;
71+ const response = await agent . get ( "/practical/list" )
72+ . send ( { title : "new practical" } ) ;
7273 expect ( response . status ) . toBe ( 200 ) ;
7374 expect ( response . body . res ) . toHaveLength ( 1 ) ;
7475 } ) ;
7576
7677 it ( "should update a practical entity" , async ( ) => {
77- const response = await agent . post ( "/practical/update" ) . send ( {
78- id : practicalId ,
78+ const response = await agent . post ( `/practical/update/${ practicalId } ` ) . send ( {
7979 hours : 3 ,
8080 } ) ;
8181
8282 expect ( response . status ) . toBe ( 200 ) ;
83- expect ( response . body . res ) . toMatch ( / u p d a t e d p r a c t i c a l / ) ;
84- } ) ;
85-
86- it ( "should delete a practical entity" , async ( ) => {
87- const response = await agent . post ( `/practical/delete/${ practicalId } ` ) ;
88- expect ( response . status ) . toBe ( 200 ) ;
89- expect ( response . body . res ) . toMatch ( / D e l e t e d p r a c t i c a l / ) ;
83+ expect ( response . body . res ) . toMatch ( / U p d a t e d P r a c t i c a l / ) ;
9084 } ) ;
9185 } ) ;
9286} ) ;
0 commit comments