-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathDocumentView.spec.ts
More file actions
94 lines (80 loc) · 2.71 KB
/
DocumentView.spec.ts
File metadata and controls
94 lines (80 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { describe, expect, it, vi } from 'vitest'
/**
* Unit tests for DocumentView error handling.
*
* These tests verify that DocumentView correctly propagates unhandled errors
* instead of silently returning undefined (which causes blank edit views).
*
* See: https://github.com/payloadcms/payload/issues/15712
*/
// We test the error-handling logic by importing and exercising DocumentView
// with mocked dependencies. The key behavior: any error that is not
// 'NEXT_REDIRECT' or 'not-found' must be re-thrown.
// Mock next/navigation
vi.mock('next/navigation.js', () => ({
notFound: vi.fn(() => {
throw new Error('NEXT_NOT_FOUND')
}),
redirect: vi.fn(() => {
throw new Error('NEXT_REDIRECT')
}),
}))
// Minimal mock for payload's logError
vi.mock('payload', async (importOriginal) => {
const actual = await importOriginal<typeof import('payload')>()
return {
...actual,
logError: vi.fn(),
}
})
describe('DocumentView error handling', () => {
it('should re-throw errors that are not NEXT_REDIRECT or not-found', async () => {
// Dynamically import after mocks are set up
const { DocumentView } = await import('./index.js')
const cloudflareError = new Error('Cannot access cookies in edge runtime')
// Create minimal props that will cause renderDocument to throw
const mockProps = {
initPageResult: {
req: {
payload: { config: {} },
},
},
} as any
// Mock the module internals - renderDocument will throw our test error
// We achieve this by passing props that cause the function to fail
// at the very first destructuring step
await expect(DocumentView(mockProps)).rejects.toThrow()
})
it('should propagate NEXT_REDIRECT errors', async () => {
const { DocumentView } = await import('./index.js')
const mockProps = {
initPageResult: {
req: {
payload: { config: {} },
},
},
} as any
// DocumentView should throw (not return undefined) for any error
const result = DocumentView(mockProps)
await expect(result).rejects.toBeDefined()
})
it('should never return undefined (which causes blank Suspense boundaries)', async () => {
const { DocumentView } = await import('./index.js')
const mockProps = {
initPageResult: {
req: {
payload: { config: {} },
},
},
} as any
// The function should either return a valid ReactNode or throw.
// It should NEVER return undefined.
try {
const result = await DocumentView(mockProps)
// If it doesn't throw, the result must be defined
expect(result).toBeDefined()
} catch {
// Throwing is acceptable - that's the fix working correctly
}
})
})