-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdashboard.transactions.$transactionId.$action.test.ts
More file actions
136 lines (119 loc) · 4.06 KB
/
dashboard.transactions.$transactionId.$action.test.ts
File metadata and controls
136 lines (119 loc) · 4.06 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { action } from '../dashboard.transactions.$transactionId.$action'
import { TRANSACTION_STATUS } from '~/models/enum'
import { requireUser } from '~/services/auth.server'
import { getFirstCourse } from '~/models/course'
import { requireCourseAuthor } from '~/utils/permissions'
import {
getTransactionById,
updateTransactionStatus,
} from '~/models/transaction'
import {
activateSubscription,
deactivateSubscription,
} from '~/models/subscription'
vi.mock('~/services/auth.server', () => ({
requireUser: vi.fn(),
}))
vi.mock('~/models/course', () => ({
getFirstCourse: vi.fn(),
}))
vi.mock('~/utils/permissions', () => ({
requireCourseAuthor: vi.fn(),
}))
vi.mock('~/models/transaction', () => ({
getTransactionById: vi.fn(),
updateTransactionStatus: vi.fn(),
}))
vi.mock('~/models/subscription', () => ({
activateSubscription: vi.fn(),
deactivateSubscription: vi.fn(),
}))
function buildRequest(status: string, notes = 'Catatan verifikasi') {
const formData = new URLSearchParams({ status, notes })
return new Request(
'http://localhost:3000/dashboard/transactions/tx-1/verify',
{
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: formData,
}
)
}
async function runAction(status: string) {
return action({
request: buildRequest(status),
params: { transactionId: 'tx-1' },
context: {},
} as never)
}
describe('dashboard.transactions.$transactionId.$action action', () => {
beforeEach(() => {
vi.resetAllMocks()
vi.mocked(requireUser).mockResolvedValue({ id: 'author-1' } as never)
vi.mocked(getFirstCourse).mockResolvedValue({ id: 'course-1' } as never)
vi.mocked(requireCourseAuthor).mockReturnValue(true)
vi.mocked(getTransactionById).mockResolvedValue({
id: 'tx-1',
status: TRANSACTION_STATUS.SUBMITTED,
userId: 'user-1',
} as never)
vi.mocked(updateTransactionStatus).mockResolvedValue({
id: 'tx-1',
status: TRANSACTION_STATUS.VERIFIED,
userId: 'user-1',
} as never)
vi.mocked(activateSubscription).mockResolvedValue({ id: 'sub-1' } as never)
vi.mocked(deactivateSubscription).mockResolvedValue({
id: 'sub-1',
} as never)
})
it('rejects tampered status payloads that are outside allowlist', async () => {
try {
await runAction(TRANSACTION_STATUS.SUBMITTED)
throw new Error('Expected action to throw Response')
} catch (error) {
expect(error).toBeInstanceOf(Response)
const response = error as Response
expect(response.status).toBe(400)
await expect(response.json()).resolves.toBe(
'Status transaksi tidak valid.'
)
}
expect(updateTransactionStatus).not.toHaveBeenCalled()
})
it('rejects forbidden transition from VERIFIED to REJECTED', async () => {
vi.mocked(getTransactionById).mockResolvedValue({
id: 'tx-1',
status: TRANSACTION_STATUS.VERIFIED,
userId: 'user-1',
} as never)
try {
await runAction(TRANSACTION_STATUS.REJECTED)
throw new Error('Expected action to throw Response')
} catch (error) {
expect(error).toBeInstanceOf(Response)
const response = error as Response
expect(response.status).toBe(400)
await expect(response.json()).resolves.toBe(
'Transaksi yang sudah diverifikasi tidak dapat diubah menjadi ditolak.'
)
}
expect(updateTransactionStatus).not.toHaveBeenCalled()
})
it('accepts VERIFIED status and updates transaction', async () => {
const response = await runAction(TRANSACTION_STATUS.VERIFIED)
expect(response).toBeInstanceOf(Response)
if (!(response instanceof Response)) throw new Error('Expected Response')
expect(response.status).toBe(302)
expect(response.headers.get('Location')).toBe(
'/dashboard/transactions/tx-1'
)
expect(updateTransactionStatus).toHaveBeenCalledWith(
'tx-1',
'Catatan verifikasi',
TRANSACTION_STATUS.VERIFIED,
[TRANSACTION_STATUS.SUBMITTED, TRANSACTION_STATUS.REJECTED]
)
})
})