import { beforeEach, describe, expect, it, vi } from 'vitest' import { mockNuxtImport } from '@nuxt/test-utils/runtime' import PeopleList from '~/components/PeopleList.vue' import { click, inBody, mountInApp, setInput, settle, submitForm } from './helpers' const { api } = vi.hoisted(() => ({ api: { get: vi.fn(), post: vi.fn(), delete: vi.fn() } })) mockNuxtImport('useApi', () => () => api) const mountList = async () => { const wrapper = await mountInApp(PeopleList) await settle() return wrapper } const openAddModal = async (wrapper: Awaited>) => { const add = wrapper.findAll('button').find(b => b.text().includes('Add Person')) await click(add!.element) } const fillAndSubmit = async (name: string, phone: string) => { const [nameInput, phoneInput] = document.body.querySelectorAll('#add-person input') await setInput(nameInput!, name) await setInput(phoneInput!, phone) await submitForm(inBody('#add-person')!) } describe('PeopleList', () => { beforeEach(() => { api.get.mockReset() api.post.mockReset() api.delete.mockReset() api.get.mockResolvedValue({ success: true, data: [{ id: 1, name: 'Alex', phoneNumber: '+61400000001' }] }) document.body.innerHTML = '' }) it('lists the people', async () => { const wrapper = await mountList() const row = wrapper.find('tbody tr') expect(row.text()).toContain('Alex') expect(row.text()).toContain('+61400000001') }) it('shows an empty state', async () => { api.get.mockResolvedValue({ success: true, data: [] }) const wrapper = await mountList() expect(wrapper.text()).toContain('No people yet.') }) it('requires a name and an international phone number before saving', async () => { const wrapper = await mountList() await openAddModal(wrapper) await fillAndSubmit('', '0400 000 000') expect(document.body.textContent).toContain('Name is required') expect(document.body.textContent).toContain('Use international format') expect(api.post).not.toHaveBeenCalled() }) it('adds a person and shows them in the table', async () => { api.post.mockResolvedValue({ success: true, data: { id: 2, name: 'Sam', phoneNumber: '+61400000002' } }) const wrapper = await mountList() await openAddModal(wrapper) await fillAndSubmit('Sam', '+61400000002') expect(api.post).toHaveBeenCalledExactlyOnceWith('/api/People', { id: 0, name: 'Sam', phoneNumber: '+61400000002' }) expect(wrapper.findAll('tbody tr')).toHaveLength(2) expect(wrapper.text()).toContain('Sam') }) it('keeps the list unchanged when the API rejects the new person', async () => { api.post.mockResolvedValue({ success: false, error: 'nope' }) const wrapper = await mountList() await openAddModal(wrapper) await fillAndSubmit('Sam', '+61400000002') expect(wrapper.findAll('tbody tr')).toHaveLength(1) }) it('deletes a person', async () => { api.delete.mockResolvedValue({ success: true, data: { id: 1 } }) const wrapper = await mountList() await click(wrapper.find('button[aria-label="Delete Alex"]').element) expect(api.delete).toHaveBeenCalledExactlyOnceWith('/api/People/1') expect(wrapper.text()).not.toContain('+61400000001') }) it('keeps the person when the delete fails', async () => { api.delete.mockResolvedValue({ success: false, error: 'Person not found.' }) const wrapper = await mountList() await click(wrapper.find('button[aria-label="Delete Alex"]').element) expect(wrapper.text()).toContain('Alex') }) })