3
3
import { NextRequest , NextResponse } from 'next/server' ;
4
4
import { jest } from '@jest/globals' ;
5
5
6
- // Mock the importContentMetadata function
6
+ // Mock the content-handlers functions
7
7
jest . mock ( '@/lib/content-handlers' , ( ) => ( {
8
- importContentMetadata : jest . fn ( )
8
+ getContentItemByDirectorySlug : jest . fn ( )
9
9
} ) ) ;
10
10
11
11
// Import the mocked function
12
- import { importContentMetadata } from '@/lib/content-handlers' ;
13
-
14
- // Mock the route handlers directly
15
- const mockGET = jest . fn ( ) ;
12
+ import { getContentItemByDirectorySlug } from '@/lib/content-handlers' ;
16
13
17
14
// Mock the route module
15
+ const mockGET = jest . fn ( ) as jest . MockedFunction < any > ;
18
16
jest . mock ( '../route' , ( ) => ( {
19
- GET : ( ... args : any [ ] ) => mockGET ( ... args )
17
+ GET : mockGET
20
18
} ) ) ;
21
19
22
20
describe ( 'Articles API' , ( ) => {
@@ -37,10 +35,8 @@ describe('Articles API', () => {
37
35
tags : [ 'test' , 'article' ]
38
36
} ;
39
37
40
- // Setup mock to return article data
41
- mockGET . mockResolvedValue (
42
- NextResponse . json ( mockArticle , { status : 200 } )
43
- ) ;
38
+ const mockResponse = NextResponse . json ( mockArticle , { status : 200 } ) ;
39
+ mockGET . mockResolvedValue ( mockResponse ) ;
44
40
45
41
const request = new NextRequest ( 'http://localhost:3000/api/articles/test-article' ) ;
46
42
const params = { params : Promise . resolve ( { slug : 'test-article' } ) } ;
@@ -55,10 +51,8 @@ describe('Articles API', () => {
55
51
} ) ;
56
52
57
53
it ( 'should return 404 for non-existent article' , async ( ) => {
58
- // Setup mock to simulate article not found
59
- mockGET . mockResolvedValue (
60
- NextResponse . json ( { error : 'Article not found' } , { status : 404 } )
61
- ) ;
54
+ const mockResponse = NextResponse . json ( { error : 'Article not found' } , { status : 404 } ) ;
55
+ mockGET . mockResolvedValue ( mockResponse ) ;
62
56
63
57
const request = new NextRequest ( 'http://localhost:3000/api/articles/non-existent' ) ;
64
58
const params = { params : Promise . resolve ( { slug : 'non-existent' } ) } ;
@@ -73,13 +67,8 @@ describe('Articles API', () => {
73
67
} ) ;
74
68
75
69
it ( 'should handle errors gracefully' , async ( ) => {
76
- // Setup mock to throw an error
77
- mockGET . mockRejectedValue ( new Error ( 'Unexpected error' ) ) ;
78
-
79
- // Setup error handler mock
80
- mockGET . mockResolvedValueOnce (
81
- NextResponse . json ( { error : 'Internal server error' } , { status : 500 } )
82
- ) ;
70
+ const mockResponse = NextResponse . json ( { error : 'Internal server error' } , { status : 500 } ) ;
71
+ mockGET . mockResolvedValue ( mockResponse ) ;
83
72
84
73
const request = new NextRequest ( 'http://localhost:3000/api/articles/error-article' ) ;
85
74
const params = { params : Promise . resolve ( { slug : 'error-article' } ) } ;
0 commit comments