Users who leave the payment step can return later and still see what they ordered. Unpaid orders get a clear path back to Swish checkout. - Add letterText to frontend Order type - Show beställnings-ID, message, and formatted date on each order card - Add "Betala nu" link to payment route for pending_payment orders - Extend OrdersPage unit tests and order-history e2e for pay-later flow
220 lines
6.9 KiB
TypeScript
220 lines
6.9 KiB
TypeScript
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
|
import { mount } from '@vue/test-utils'
|
|
import { createRouter, createMemoryHistory } from 'vue-router'
|
|
import { createPinia } from 'pinia'
|
|
import OrdersPage from '@/pages/OrdersPage.vue'
|
|
|
|
function mockFetchResponse(status: number, body: unknown) {
|
|
return Promise.resolve({
|
|
ok: status >= 200 && status < 300,
|
|
status,
|
|
json: () => Promise.resolve(body),
|
|
})
|
|
}
|
|
|
|
function createTestRouter() {
|
|
return createRouter({
|
|
history: createMemoryHistory(),
|
|
routes: [
|
|
{ path: '/orders', name: 'orders', component: OrdersPage },
|
|
{
|
|
path: '/betalning/:orderId',
|
|
name: 'payment',
|
|
component: { template: '<div>Payment</div>' },
|
|
},
|
|
{ path: '/', name: 'home', component: { template: '<div>Home</div>' } },
|
|
],
|
|
})
|
|
}
|
|
|
|
function mountPage() {
|
|
const router = createTestRouter()
|
|
const pinia = createPinia()
|
|
router.push('/orders')
|
|
return {
|
|
router,
|
|
wrapper: mount(OrdersPage, {
|
|
global: { plugins: [router, pinia] },
|
|
}),
|
|
}
|
|
}
|
|
|
|
const mockOrders = [
|
|
{
|
|
id: 'c1eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
|
|
plate: 'ABC123',
|
|
letterText: 'Hej fin bil!',
|
|
status: 'sent',
|
|
trackingId: 'PN123456789',
|
|
createdAt: '2026-05-11T12:00:00Z',
|
|
},
|
|
{
|
|
id: 'c2eebc99-9c0b-4ef8-bb6d-6bb9bd380a12',
|
|
plate: 'DEF456',
|
|
letterText: 'Vill köpa din bil.',
|
|
status: 'pending_payment',
|
|
trackingId: null,
|
|
createdAt: '2026-05-14T13:00:00Z',
|
|
},
|
|
]
|
|
|
|
describe('OrdersPage', () => {
|
|
beforeEach(() => {
|
|
localStorage.clear()
|
|
globalThis.fetch = vi.fn()
|
|
vi.mocked(globalThis.fetch).mockResolvedValue(
|
|
mockFetchResponse(200, mockOrders),
|
|
)
|
|
})
|
|
|
|
it('renders heading and subtitle', async () => {
|
|
const { wrapper } = mountPage()
|
|
await new Promise((r) => setTimeout(r, 50))
|
|
expect(wrapper.text()).toContain('Mina beställningar')
|
|
expect(wrapper.text()).toContain(
|
|
'Här kan du se dina tidigare beställningar',
|
|
)
|
|
})
|
|
|
|
it('shows loading state initially', async () => {
|
|
globalThis.fetch = vi.fn().mockImplementation(() => new Promise(() => {}))
|
|
const { wrapper } = mountPage()
|
|
expect(wrapper.text()).toContain('Laddar beställningar...')
|
|
})
|
|
|
|
it('fetches orders from API on mount', async () => {
|
|
mountPage()
|
|
await new Promise((r) => setTimeout(r, 50))
|
|
expect(globalThis.fetch).toHaveBeenCalledWith(
|
|
'/api/orders',
|
|
expect.objectContaining({ headers: expect.any(Object) }),
|
|
)
|
|
})
|
|
|
|
it('renders order cards with plate numbers', async () => {
|
|
const { wrapper } = mountPage()
|
|
await new Promise((r) => setTimeout(r, 50))
|
|
expect(wrapper.text()).toContain('ABC123')
|
|
expect(wrapper.text()).toContain('DEF456')
|
|
})
|
|
|
|
it('renders Swedish status labels', async () => {
|
|
const { wrapper } = mountPage()
|
|
await new Promise((r) => setTimeout(r, 50))
|
|
expect(wrapper.text()).toContain('Skickat')
|
|
expect(wrapper.text()).toContain('Väntar på betalning')
|
|
})
|
|
|
|
it('renders tracking link when trackingId exists', async () => {
|
|
const { wrapper } = mountPage()
|
|
await new Promise((r) => setTimeout(r, 50))
|
|
const link = wrapper.find('a[href*="postnord"]')
|
|
expect(link.exists()).toBe(true)
|
|
expect(link.text()).toContain('PN123456789')
|
|
expect(link.attributes('target')).toBe('_blank')
|
|
})
|
|
|
|
it('does not render tracking link when trackingId is null', async () => {
|
|
const ordersWithoutTracking = [
|
|
{
|
|
id: 'c2eebc99-9c0b-4ef8-bb6d-6bb9bd380a12',
|
|
plate: 'DEF456',
|
|
letterText: 'Test',
|
|
status: 'pending_payment',
|
|
trackingId: null,
|
|
createdAt: '2026-05-14T13:00:00Z',
|
|
},
|
|
]
|
|
vi.mocked(globalThis.fetch).mockResolvedValue(
|
|
mockFetchResponse(200, ordersWithoutTracking),
|
|
)
|
|
const { wrapper } = mountPage()
|
|
await new Promise((r) => setTimeout(r, 50))
|
|
const link = wrapper.find('a[href*="postnord"]')
|
|
expect(link.exists()).toBe(false)
|
|
})
|
|
|
|
it('renders order id and message', async () => {
|
|
const { wrapper } = mountPage()
|
|
await new Promise((r) => setTimeout(r, 50))
|
|
expect(wrapper.text()).toContain('Beställnings-ID')
|
|
expect(wrapper.text()).toContain('c1eebc99-9c0b-4ef8-bb6d-6bb9bd380a11')
|
|
expect(wrapper.text()).toContain('Meddelande')
|
|
expect(wrapper.text()).toContain('Hej fin bil!')
|
|
expect(wrapper.text()).toContain('Vill köpa din bil.')
|
|
})
|
|
|
|
it('renders formatted date', async () => {
|
|
const { wrapper } = mountPage()
|
|
await new Promise((r) => setTimeout(r, 50))
|
|
expect(wrapper.text()).toContain('2026')
|
|
})
|
|
|
|
it('shows empty state when no orders', async () => {
|
|
vi.mocked(globalThis.fetch).mockResolvedValue(mockFetchResponse(200, []))
|
|
const { wrapper } = mountPage()
|
|
await new Promise((r) => setTimeout(r, 50))
|
|
expect(wrapper.text()).toContain('Inga beställningar ännu')
|
|
})
|
|
|
|
it('shows error state on API failure', async () => {
|
|
vi.mocked(globalThis.fetch).mockResolvedValue(
|
|
mockFetchResponse(500, { message: 'Internal server error' }),
|
|
)
|
|
const { wrapper } = mountPage()
|
|
await new Promise((r) => setTimeout(r, 50))
|
|
expect(wrapper.text()).toContain('Kunde inte hämta beställningar')
|
|
})
|
|
|
|
it('applies correct badge class for status', async () => {
|
|
const { wrapper } = mountPage()
|
|
await new Promise((r) => setTimeout(r, 50))
|
|
const badges = wrapper.findAll('.badge')
|
|
expect(badges[0].classes()).toContain('badge--success')
|
|
expect(badges[1].classes()).toContain('badge--muted')
|
|
})
|
|
|
|
it('shows pay button only for pending payment orders', async () => {
|
|
const { wrapper } = mountPage()
|
|
await new Promise((r) => setTimeout(r, 50))
|
|
|
|
const payLinks = wrapper.findAll('.orders__pay-btn')
|
|
expect(payLinks).toHaveLength(1)
|
|
expect(payLinks[0].text()).toBe('Betala nu')
|
|
|
|
const href = payLinks[0].attributes('href')
|
|
expect(href).toContain('c2eebc99-9c0b-4ef8-bb6d-6bb9bd380a12')
|
|
expect(href).toContain('plate=DEF456')
|
|
})
|
|
|
|
it('does not show pay button for paid or sent orders', async () => {
|
|
const { wrapper } = mountPage()
|
|
await new Promise((r) => setTimeout(r, 50))
|
|
|
|
const sentCard = wrapper
|
|
.findAll('.orders__card')
|
|
.find((card) => card.text().includes('ABC123'))
|
|
expect(sentCard?.find('.orders__pay-btn').exists()).toBe(false)
|
|
})
|
|
|
|
it('renders processing status correctly', async () => {
|
|
const ordersWithProcessing = [
|
|
{
|
|
id: 'c4eebc99-9c0b-4ef8-bb6d-6bb9bd380a14',
|
|
plate: 'XYZ123',
|
|
letterText: 'Processing message',
|
|
status: 'processing',
|
|
trackingId: null,
|
|
createdAt: '2026-05-15T10:00:00Z',
|
|
},
|
|
]
|
|
vi.mocked(globalThis.fetch).mockResolvedValue(
|
|
mockFetchResponse(200, ordersWithProcessing),
|
|
)
|
|
const { wrapper } = mountPage()
|
|
await new Promise((r) => setTimeout(r, 50))
|
|
expect(wrapper.text()).toContain('Hanteras')
|
|
const badge = wrapper.find('.badge')
|
|
expect(badge.classes()).toContain('badge--primary')
|
|
})
|
|
})
|