@@ -3,59 +3,52 @@ import * as appHandler from "./route";
3
3
import { expect , test } from "@jest/globals" ;
4
4
// import { authMock } from "@/test/authMock";
5
5
import { validateSession , invalidateSession } from "@/test/util/authMockUtils" ;
6
- import { manyUnclaimedItems } from "@/test/util/dbMockUtils" ;
6
+ import { fillDbMockWithManyUnclaimedItems } from "@/test/util/dbMockUtils" ;
7
7
import { dbMock } from "@/test/dbMock" ;
8
- import { UnclaimedItem } from "@prisma/client" ;
9
8
10
9
test ( "Should return 401 for invalid session" , async ( ) => {
11
- await testApiHandler ( {
12
- appHandler,
13
- async test ( { fetch } ) {
14
- invalidateSession ( ) ;
15
-
16
- const res = await fetch ( { method : "GET" } ) ;
17
- await expect ( res . status ) . toBe ( 401 ) ;
18
- const json = await res . json ( ) ;
19
- await expect ( json ) . toEqual ( { message : "Session required" } ) ;
20
- }
21
- } )
10
+ await testApiHandler ( {
11
+ appHandler,
12
+ async test ( { fetch } ) {
13
+ invalidateSession ( ) ;
14
+
15
+ const res = await fetch ( { method : "GET" } ) ;
16
+ await expect ( res . status ) . toBe ( 401 ) ;
17
+ const json = await res . json ( ) ;
18
+ await expect ( json ) . toEqual ( { message : "Session required" } ) ;
19
+ } ,
20
+ } ) ;
22
21
} ) ;
23
22
24
23
test ( "Should return 200 for valid session" , async ( ) => {
25
- await testApiHandler ( {
26
- appHandler,
27
- async test ( { fetch } ) {
28
- validateSession ( ) ;
29
-
30
- const res = await fetch ( { method : "GET" } ) ;
31
- await expect ( res . status ) . toBe ( 200 ) ;
32
- }
33
- } )
24
+ await testApiHandler ( {
25
+ appHandler,
26
+ async test ( { fetch } ) {
27
+ validateSession ( "ADMIN" ) ;
28
+
29
+ const res = await fetch ( { method : "GET" } ) ;
30
+ await expect ( res . status ) . toBe ( 200 ) ;
31
+ } ,
32
+ } ) ;
34
33
} ) ;
35
34
36
35
test ( "Should give correct database queries" , async ( ) => {
37
- await testApiHandler ( {
38
- appHandler,
39
- async test ( { fetch } ) {
40
- // Create mock data
41
- dbMock . unclaimedItem . create ( { data : { id : 1 , name : "Test Item 1" , quantity : 1 , expirationDate : new Date ( ) } } ) ;
42
- const items : UnclaimedItem [ ] = await manyUnclaimedItems ( 3 ) ;
43
- // Convert items expirationDate to ISO-8601 string
44
- const expectedRet = items . map ( item => {
45
- return { ...item , expirationDate : item . expirationDate ?. toISOString ( ) || "" }
46
- } ) ;
47
-
48
- validateSession ( ) ;
49
-
50
- const res = await fetch ( { method : "GET" } ) ;
51
- await expect ( res . status ) . toBe ( 200 ) ;
52
-
53
- // Check that the database was queried
54
- await expect ( dbMock . unclaimedItem . findMany ) . toHaveBeenCalledTimes ( 1 ) ;
55
-
56
- // Check that the response json was written correctly
57
- const json = await res . json ( ) ;
58
- await expect ( json [ "unclaimedItems" ] ) . toEqual ( expectedRet ) ;
59
- }
60
- } )
61
- } ) ;
36
+ await testApiHandler ( {
37
+ appHandler,
38
+ async test ( { fetch } ) {
39
+ await fillDbMockWithManyUnclaimedItems ( 3 ) ;
40
+ validateSession ( "ADMIN" ) ;
41
+
42
+ const res = await fetch ( { method : "GET" } ) ;
43
+ await expect ( res . status ) . toBe ( 200 ) ;
44
+
45
+ // Check that the response json was written correctly
46
+ const expectedRet = {
47
+ numberOfItems : 3 ,
48
+ unclaimedItems : await dbMock . unclaimedItem . findMany ( ) ,
49
+ } ;
50
+ const json = await res . json ( ) ;
51
+ await expect ( json ) . toEqual ( JSON . parse ( JSON . stringify ( expectedRet ) ) ) ; // Needed to stringify and parse because the expiration field would cause an error because Date != ISOstring
52
+ } ,
53
+ } ) ;
54
+ } ) ;
0 commit comments