Skip to content

Commit

Permalink
[RED] add test for payment success page
Browse files Browse the repository at this point in the history
  • Loading branch information
catherineeangel committed May 9, 2024
1 parent 73d9979 commit b1333ae
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 8 deletions.
23 changes: 19 additions & 4 deletions __tests__/payment/payment-details-no-url.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import '@testing-library/jest-dom'
import { render, waitFor } from '@testing-library/react'
import PaymentPage from '@/app/payment/page'
import { useLazyGetTransactionQuery } from '@/redux/api/paymentApi'

jest.mock('next/navigation', () => ({
useSearchParams: jest.fn(() => ({
Expand All @@ -13,10 +14,12 @@ jest.mock('@/redux/api/paymentApi', () => ({
jest.fn(),
{
data: {
package: { name: 'Test Package' },
payment_type: 'Test Payment Type',
payment_merchant: 'Test Payment Merchant',
midtrans_url: null,
transaction_detail: {
package: { name: 'Test Package' },
payment_type: 'Test Payment Type',
payment_merchant: 'Test Payment Merchant',
midtrans_url: null,
},
},
isLoading: false,
},
Expand All @@ -37,4 +40,16 @@ describe('Payment Detail Page No Midtrans Url', () => {
})
expect(continueButton).toBeDisabled()
})
it('shows loading state when query is still loading', async () => {
;(useLazyGetTransactionQuery as jest.Mock).mockReturnValue([
jest.fn(),
{
data: null,
isLoading: true,
},
])
const { queryByTestId } = render(<PaymentPage />)

await waitFor(() => expect(queryByTestId('loader')).toBeInTheDocument())
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ jest.mock('@/redux/api/paymentApi', () => ({
jest.fn(),
{
data: {
package: { name: 'Test Package' },
payment_type: 'Test Payment Type',
payment_merchant: 'Test Payment Merchant',
midtrans_url: 'https://example.com/midtrans',
transaction_detail: {
package: { name: 'Test Package' },
payment_type: 'Test Payment Type',
payment_merchant: 'Test Payment Merchant',
midtrans_url: 'https://example.com/midtrans',
},
},
isLoading: false,
},
Expand Down
76 changes: 76 additions & 0 deletions __tests__/payment/payment-success.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { render, waitFor } from '@testing-library/react'
import '@testing-library/jest-dom'
import PaymentSuccessPage from '@/app/payment/success/page'
import { useLazyGetTransactionQuery } from '@/redux/api/paymentApi'

jest.mock('next/link', () => {
const MockedLink = ({ children, href }: any) => <a href={href}>{children}</a>

MockedLink.displayName = 'MockedNextLink'

return MockedLink
})

jest.mock('next/navigation', () => ({
useSearchParams: jest.fn(() => ({
get: jest.fn(() => 'test_order_id'),
})),
}))

jest.mock('@/redux/api/paymentApi', () => ({
useLazyGetTransactionQuery: jest.fn(),
}))
describe('PaymentSuccess Component', () => {
it('renders the success message when data is loaded', async () => {
;(useLazyGetTransactionQuery as jest.Mock).mockReturnValue([
jest.fn(),
{
data: {
transaction_detail: {
id: 'transaction_id',
package: {
id: 2,
name: 'Premium',
},
midtrans_url: null,
midtrans_transaction_id: 'midtrans_id',
order_id: 'order_id',
price: 10000,
checkout_time: '2024-05-06T14:49:19Z',
expiry_time: '2024-05-06T15:04:19Z',
payment_type: 'qris',
payment_merchant: 'gopay',
status: 'settlement',
},
},
isLoading: false,
},
])

const { getByText, getByRole } = render(<PaymentSuccessPage />)

await waitFor(() => {
expect(getByText('Payment Success')).toBeInTheDocument()
expect(getByText('Premium')).toBeInTheDocument()
const button = getByRole('button', { name: 'Go to transactions' })
expect(button.closest('a')).toHaveAttribute(
'href',
'/payment/transactions/'
)
})
})

it('shows loader when data is still loading', () => {
;(useLazyGetTransactionQuery as jest.Mock).mockReturnValue([
jest.fn(),
{
data: null,
isLoading: true,
},
])

const { getByTestId } = render(<PaymentSuccessPage />)

expect(getByTestId('loader')).toBeInTheDocument()
})
})
3 changes: 3 additions & 0 deletions src/app/payment/success/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function PaymentSuccessPage() {
return <div></div>
}

0 comments on commit b1333ae

Please sign in to comment.