1+ import request from "supertest" ;
2+ import { jest } from "@jest/globals" ;
3+ import app from "#app" ;
4+ import paperModel from "#models/paper" ;
5+ import connector from "#models/databaseUtil" ;
6+
7+ jest . mock ( "#util" ) ;
8+
9+ let server ;
10+ let agent ;
11+
12+ beforeAll ( ( done ) => {
13+ server = app . listen ( null , ( ) => {
14+ agent = request . agent ( server ) ;
15+ connector . set ( "debug" , false ) ;
16+ done ( ) ;
17+ } ) ;
18+ } ) ;
19+
20+ function cleanUp ( callback ) {
21+ paperModel . remove (
22+ {
23+ answerSheetID : "asd123" ,
24+ exam : "64fc3c8bde9fa947ea1f412f" ,
25+ student : "64fc3c8bde9fa947ea1f412f" ,
26+ checkedBy : "64fc3c8bde9fa947ea1f412f" ,
27+ mark : 100 ,
28+ } ,
29+ )
30+ . then ( ( ) => {
31+ connector . disconnect ( ( DBerr ) => {
32+ if ( DBerr ) console . log ( "Database disconnect error: " , DBerr ) ;
33+ server . close ( ( serverErr ) => {
34+ if ( serverErr ) console . log ( serverErr ) ;
35+ callback ( ) ;
36+ } ) ;
37+ } ) ;
38+ } ) ;
39+ }
40+
41+ afterAll ( ( done ) => {
42+ cleanUp ( done ) ;
43+ } ) ;
44+
45+ describe ( "Paper CRUD" , ( ) => {
46+ it ( "should create paper with associated exam, student and faculty" , async ( ) => {
47+ const response = await agent . post ( "/paper/add" ) . send (
48+ {
49+ answerSheetID : "asd123" ,
50+ exam : "64fc3c8bde9fa947ea1f412f" ,
51+ student : "64fc3c8bde9fa947ea1f412f" ,
52+ checkedBy : "64fc3c8bde9fa947ea1f412f" ,
53+ mark : 100 ,
54+ } ,
55+ ) ;
56+
57+ expect ( response . status ) . toBe ( 200 ) ;
58+ expect ( response . headers [ "content-type" ] ) . toMatch ( / j s o n / ) ;
59+ } ) ;
60+
61+ let id ;
62+ beforeEach ( async ( ) => {
63+ id = await agent . post ( "/paper/add" ) . send (
64+ {
65+ answerSheetID : "asd123" ,
66+ exam : "64fc3c8bde9fa947ea1f412f" ,
67+ student : "64fc3c8bde9fa947ea1f412f" ,
68+ checkedBy : "64fc3c8bde9fa947ea1f412f" ,
69+ mark : 100 ,
70+ } ,
71+ ) ;
72+ id = JSON . parse ( id . res . text ) . id ;
73+ } ) ;
74+
75+ afterEach ( async ( ) => {
76+ await paperModel . remove (
77+ {
78+ answerSheetID : "asd123" ,
79+ exam : "64fc3c8bde9fa947ea1f412f" ,
80+ student : "64fc3c8bde9fa947ea1f412f" ,
81+ checkedBy : "64fc3c8bde9fa947ea1f412f" ,
82+ mark : 100 ,
83+ } ,
84+ ) ;
85+ } ) ;
86+
87+ it ( "should read paper" , async ( ) => {
88+ const response = await agent
89+ . get ( "/paper/list" )
90+ . send (
91+ {
92+ answerSheetID : "asd123" ,
93+ exam : "64fc3c8bde9fa947ea1f412f" ,
94+ student : "64fc3c8bde9fa947ea1f412f" ,
95+ checkedBy : "64fc3c8bde9fa947ea1f412f" ,
96+ mark : 100 ,
97+ } ,
98+ ) ;
99+ expect ( response . body . res ) . not . toBeNull ( ) ;
100+ } ) ;
101+
102+ it ( "should update paper" , async ( ) => {
103+ const response = await agent
104+ . post ( `/paper/update/${ id } ` )
105+ . send (
106+ {
107+ answerSheetID : "asd123" ,
108+ exam : "64fc3c8bde9fa947ea1f412f" ,
109+ student : "64fc3c8bde9fa947ea1f412f" ,
110+ checkedBy : "64fc3c8bde9fa947ea1f412f" ,
111+ mark : 100 ,
112+ } ,
113+ ) ;
114+ expect ( response . status ) . toBe ( 200 ) ;
115+ expect ( response . body . res ) . toMatch ( / P a p e r u p d a t e d / ) ;
116+ } ) ;
117+ } ) ;
0 commit comments