Add backend and frontend tests and run them in CI
Build and Publish / Build Yale Access Frontend (pull_request) Successful in 1m17s
Build and Publish / Push Yale Access Frontend Docker Image (pull_request) Skipped
Build and Publish / Build Yale Access Backend (pull_request) Successful in 2m12s
Build and Publish / Push Yale Access Backend Docker Image (pull_request) Skipped

- Backend: xUnit integration tests (auth, rate limiting, people, lock codes,
  validation, error handling) using WebApplicationFactory with a fake lock
- Frontend: Vitest + Nuxt test utils for useApi, the auth middleware,
  LoginForm, UserCodeList and PeopleList
- Run both suites in CI before building
This commit is contained in:
LiamPietralla committed 2026-11-16 12:23:32 +11:00
1 parent a9bef7b50d
commit 4fe6f9d7b5
15 files changed
+968

No files matched your search

+101
View File
@@ -0,0 +1,101 @@
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<ReturnType<typeof mountList>>) => {
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<HTMLInputElement>('#add-person input')
await setInput(nameInput!, name)
await setInput(phoneInput!, phone)
await submitForm(inBody<HTMLFormElement>('#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')
})
})