@@ -4,11 +4,11 @@ import { NextRequest, NextResponse } from 'next/server';
4
4
import { jest } from '@jest/globals' ;
5
5
6
6
// Mock the route handlers directly
7
- const mockGET = jest . fn ( ) ;
7
+ const mockGET = jest . fn ( ) as jest . MockedFunction < any > ;
8
8
9
9
// Mock the route module
10
10
jest . mock ( '../route' , ( ) => ( {
11
- GET : ( ... args ) => mockGET ( ... args )
11
+ GET : mockGET
12
12
} ) ) ;
13
13
14
14
describe ( 'Check Purchase API' , ( ) => {
@@ -17,62 +17,57 @@ describe('Check Purchase API', () => {
17
17
} ) ;
18
18
19
19
describe ( 'GET /api/check-purchase' , ( ) => {
20
- it ( 'should return 401 for unauthenticated users' , async ( ) => {
21
- // Setup mock to simulate unauthorized response
22
- mockGET . mockResolvedValue (
23
- NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
24
- ) ;
20
+ it ( 'should return 401 for unauthorized request' , async ( ) => {
21
+ const mockResponse = NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } ) ;
22
+ mockGET . mockResolvedValue ( mockResponse ) ;
25
23
26
24
const request = new NextRequest ( 'http://localhost:3000/api/check-purchase' ) ;
27
25
28
26
const { GET } = require ( '../route' ) ;
29
27
const response = await GET ( request ) ;
30
28
31
29
expect ( response . status ) . toBe ( 401 ) ;
32
- expect ( await response . json ( ) ) . toEqual ( { error : 'Unauthorized' } ) ;
30
+ const data = await response . json ( ) ;
31
+ expect ( data ) . toEqual ( { error : 'Unauthorized' } ) ;
33
32
expect ( mockGET ) . toHaveBeenCalledTimes ( 1 ) ;
34
33
} ) ;
35
34
36
- it ( 'should return 400 if slug is missing' , async ( ) => {
37
- // Setup mock to simulate bad request response
38
- mockGET . mockResolvedValue (
39
- NextResponse . json ( { error : 'Missing slug parameter' } , { status : 400 } )
40
- ) ;
35
+ it ( 'should return 400 for missing slug parameter' , async ( ) => {
36
+ const mockResponse = NextResponse . json ( { error : 'Missing slug parameter' } , { status : 400 } ) ;
37
+ mockGET . mockResolvedValue ( mockResponse ) ;
41
38
42
- const request = new NextRequest ( 'http://localhost:3000/api/check-purchase' ) ;
39
+ const request = new NextRequest ( 'http://localhost:3000/api/check-purchase?type=blog ' ) ;
43
40
44
41
const { GET } = require ( '../route' ) ;
45
42
const response = await GET ( request ) ;
46
43
47
44
expect ( response . status ) . toBe ( 400 ) ;
48
- expect ( await response . json ( ) ) . toEqual ( { error : 'Missing slug parameter' } ) ;
45
+ const data = await response . json ( ) ;
46
+ expect ( data ) . toEqual ( { error : 'Missing slug parameter' } ) ;
49
47
expect ( mockGET ) . toHaveBeenCalledTimes ( 1 ) ;
50
48
} ) ;
51
49
52
- it ( 'should return 400 if type is missing' , async ( ) => {
53
- // Setup mock to simulate bad request response
54
- mockGET . mockResolvedValue (
55
- NextResponse . json ( { error : 'Missing type parameter' } , { status : 400 } )
56
- ) ;
50
+ it ( 'should return 400 for missing type parameter' , async ( ) => {
51
+ const mockResponse = NextResponse . json ( { error : 'Missing type parameter' } , { status : 400 } ) ;
52
+ mockGET . mockResolvedValue ( mockResponse ) ;
57
53
58
54
const request = new NextRequest ( 'http://localhost:3000/api/check-purchase?slug=test-article' ) ;
59
55
60
56
const { GET } = require ( '../route' ) ;
61
57
const response = await GET ( request ) ;
62
58
63
59
expect ( response . status ) . toBe ( 400 ) ;
64
- expect ( await response . json ( ) ) . toEqual ( { error : 'Missing type parameter' } ) ;
60
+ const data = await response . json ( ) ;
61
+ expect ( data ) . toEqual ( { error : 'Missing type parameter' } ) ;
65
62
expect ( mockGET ) . toHaveBeenCalledTimes ( 1 ) ;
66
63
} ) ;
67
64
68
- it ( 'should return purchase status for authenticated users' , async ( ) => {
69
- // Setup mock to simulate successful response
70
- mockGET . mockResolvedValue (
71
- NextResponse . json ( {
72
- purchased : true ,
73
- purchaseDate : '2023-01-01T00:00:00Z'
74
- } , { status : 200 } )
75
- ) ;
65
+ it ( 'should return purchase data for valid request' , async ( ) => {
66
+ const mockResponse = NextResponse . json ( {
67
+ purchased : true ,
68
+ purchaseDate : '2023-01-01T00:00:00Z'
69
+ } , { status : 200 } ) ;
70
+ mockGET . mockResolvedValue ( mockResponse ) ;
76
71
77
72
const request = new NextRequest ( 'http://localhost:3000/api/check-purchase?slug=test-article&type=blog' ) ;
78
73
@@ -81,47 +76,42 @@ describe('Check Purchase API', () => {
81
76
82
77
expect ( response . status ) . toBe ( 200 ) ;
83
78
const data = await response . json ( ) ;
84
- expect ( data ) . toHaveProperty ( 'purchased' ) ;
85
- expect ( data . purchased ) . toBe ( true ) ;
86
- expect ( data ) . toHaveProperty ( 'purchaseDate' ) ;
79
+ expect ( data ) . toEqual ( {
80
+ purchased : true ,
81
+ purchaseDate : '2023-01-01T00:00:00Z'
82
+ } ) ;
87
83
expect ( mockGET ) . toHaveBeenCalledTimes ( 1 ) ;
88
84
} ) ;
89
85
90
- it ( 'should return not purchased status when user has not purchased the content' , async ( ) => {
91
- // Setup mock to simulate not purchased response
92
- mockGET . mockResolvedValue (
93
- NextResponse . json ( {
94
- purchased : false
95
- } , { status : 200 } )
96
- ) ;
86
+ it ( 'should return false for non-purchased content' , async ( ) => {
87
+ const mockResponse = NextResponse . json ( {
88
+ purchased : false
89
+ } , { status : 200 } ) ;
90
+ mockGET . mockResolvedValue ( mockResponse ) ;
97
91
98
- const request = new NextRequest ( 'http://localhost:3000/api/check-purchase?slug=unpurchased -article&type=blog' ) ;
92
+ const request = new NextRequest ( 'http://localhost:3000/api/check-purchase?slug=free -article&type=blog' ) ;
99
93
100
94
const { GET } = require ( '../route' ) ;
101
95
const response = await GET ( request ) ;
102
96
103
97
expect ( response . status ) . toBe ( 200 ) ;
104
98
const data = await response . json ( ) ;
105
- expect ( data ) . toHaveProperty ( 'purchased' ) ;
106
- expect ( data . purchased ) . toBe ( false ) ;
107
- expect ( data ) . not . toHaveProperty ( 'purchaseDate' ) ;
99
+ expect ( data ) . toEqual ( { purchased : false } ) ;
108
100
expect ( mockGET ) . toHaveBeenCalledTimes ( 1 ) ;
109
101
} ) ;
110
102
111
- it ( 'should handle database errors gracefully' , async ( ) => {
112
- // Setup mock to simulate server error
113
- mockGET . mockResolvedValue (
114
- NextResponse . json ( { error : 'Database error' } , { status : 500 } )
115
- ) ;
103
+ it ( 'should handle database errors' , async ( ) => {
104
+ const mockResponse = NextResponse . json ( { error : 'Database error' } , { status : 500 } ) ;
105
+ mockGET . mockResolvedValue ( mockResponse ) ;
116
106
117
107
const request = new NextRequest ( 'http://localhost:3000/api/check-purchase?slug=test-article&type=blog' ) ;
118
108
119
109
const { GET } = require ( '../route' ) ;
120
110
const response = await GET ( request ) ;
121
111
122
112
expect ( response . status ) . toBe ( 500 ) ;
123
- expect ( await response . json ( ) ) . toEqual ( { error : 'Database error' } ) ;
124
- expect ( mockGET ) . toHaveBeenCalledTimes ( 1 ) ;
113
+ const data = await response . json ( ) ;
114
+ expect ( data ) . toEqual ( { error : 'Database error' } ) ;
125
115
} ) ;
126
116
} ) ;
127
117
} ) ;
0 commit comments