Skip to content

Commit dcbee73

Browse files
committed
Fix up tests and build
1 parent 709bbab commit dcbee73

File tree

10 files changed

+286
-452
lines changed

10 files changed

+286
-452
lines changed

src/app/api/articles/[slug]/__tests__/route.test.ts

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
import { NextRequest, NextResponse } from 'next/server';
44
import { jest } from '@jest/globals';
55

6-
// Mock the importContentMetadata function
6+
// Mock the content-handlers functions
77
jest.mock('@/lib/content-handlers', () => ({
8-
importContentMetadata: jest.fn()
8+
getContentItemByDirectorySlug: jest.fn()
99
}));
1010

1111
// 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';
1613

1714
// Mock the route module
15+
const mockGET = jest.fn() as jest.MockedFunction<any>;
1816
jest.mock('../route', () => ({
19-
GET: (...args: any[]) => mockGET(...args)
17+
GET: mockGET
2018
}));
2119

2220
describe('Articles API', () => {
@@ -37,10 +35,8 @@ describe('Articles API', () => {
3735
tags: ['test', 'article']
3836
};
3937

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);
4440

4541
const request = new NextRequest('http://localhost:3000/api/articles/test-article');
4642
const params = { params: Promise.resolve({ slug: 'test-article' }) };
@@ -55,10 +51,8 @@ describe('Articles API', () => {
5551
});
5652

5753
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);
6256

6357
const request = new NextRequest('http://localhost:3000/api/articles/non-existent');
6458
const params = { params: Promise.resolve({ slug: 'non-existent' }) };
@@ -73,13 +67,8 @@ describe('Articles API', () => {
7367
});
7468

7569
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);
8372

8473
const request = new NextRequest('http://localhost:3000/api/articles/error-article');
8574
const params = { params: Promise.resolve({ slug: 'error-article' }) };

src/app/api/articles/__tests__/route.test.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { NextRequest, NextResponse } from 'next/server';
44
import { jest } from '@jest/globals';
55

66
// Mock the route handlers directly
7-
const mockGET = jest.fn();
7+
const mockGET = jest.fn() as jest.MockedFunction<any>;
88

99
// Mock the route module
1010
jest.mock('../[slug]/route', () => ({
11-
GET: (...args) => mockGET(...args)
11+
GET: mockGET
1212
}));
1313

1414
describe('Articles API', () => {
@@ -30,9 +30,8 @@ describe('Articles API', () => {
3030
};
3131

3232
// Setup mock to return article data
33-
mockGET.mockResolvedValue(
34-
NextResponse.json(mockArticle, { status: 200 })
35-
);
33+
const mockResponse = NextResponse.json(mockArticle, { status: 200 });
34+
mockGET.mockResolvedValue(mockResponse);
3635

3736
const request = new NextRequest('http://localhost:3000/api/articles/test-article');
3837
const params = { params: Promise.resolve({ slug: 'test-article' }) };
@@ -48,9 +47,8 @@ describe('Articles API', () => {
4847

4948
it('should return 404 for non-existent article', async () => {
5049
// Setup mock to simulate article not found
51-
mockGET.mockResolvedValue(
52-
NextResponse.json({ error: 'Article not found' }, { status: 404 })
53-
);
50+
const mockResponse = NextResponse.json({ error: 'Article not found' }, { status: 404 });
51+
mockGET.mockResolvedValue(mockResponse);
5452

5553
const request = new NextRequest('http://localhost:3000/api/articles/non-existent');
5654
const params = { params: Promise.resolve({ slug: 'non-existent' }) };

src/app/api/check-purchase/__tests__/route.test.ts

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { NextRequest, NextResponse } from 'next/server';
44
import { jest } from '@jest/globals';
55

66
// Mock the route handlers directly
7-
const mockGET = jest.fn();
7+
const mockGET = jest.fn() as jest.MockedFunction<any>;
88

99
// Mock the route module
1010
jest.mock('../route', () => ({
11-
GET: (...args) => mockGET(...args)
11+
GET: mockGET
1212
}));
1313

1414
describe('Check Purchase API', () => {
@@ -17,62 +17,57 @@ describe('Check Purchase API', () => {
1717
});
1818

1919
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);
2523

2624
const request = new NextRequest('http://localhost:3000/api/check-purchase');
2725

2826
const { GET } = require('../route');
2927
const response = await GET(request);
3028

3129
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' });
3332
expect(mockGET).toHaveBeenCalledTimes(1);
3433
});
3534

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);
4138

42-
const request = new NextRequest('http://localhost:3000/api/check-purchase');
39+
const request = new NextRequest('http://localhost:3000/api/check-purchase?type=blog');
4340

4441
const { GET } = require('../route');
4542
const response = await GET(request);
4643

4744
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' });
4947
expect(mockGET).toHaveBeenCalledTimes(1);
5048
});
5149

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);
5753

5854
const request = new NextRequest('http://localhost:3000/api/check-purchase?slug=test-article');
5955

6056
const { GET } = require('../route');
6157
const response = await GET(request);
6258

6359
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' });
6562
expect(mockGET).toHaveBeenCalledTimes(1);
6663
});
6764

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);
7671

7772
const request = new NextRequest('http://localhost:3000/api/check-purchase?slug=test-article&type=blog');
7873

@@ -81,47 +76,42 @@ describe('Check Purchase API', () => {
8176

8277
expect(response.status).toBe(200);
8378
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+
});
8783
expect(mockGET).toHaveBeenCalledTimes(1);
8884
});
8985

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);
9791

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');
9993

10094
const { GET } = require('../route');
10195
const response = await GET(request);
10296

10397
expect(response.status).toBe(200);
10498
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 });
108100
expect(mockGET).toHaveBeenCalledTimes(1);
109101
});
110102

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);
116106

117107
const request = new NextRequest('http://localhost:3000/api/check-purchase?slug=test-article&type=blog');
118108

119109
const { GET } = require('../route');
120110
const response = await GET(request);
121111

122112
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' });
125115
});
126116
});
127117
});

0 commit comments

Comments
 (0)