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
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:
1 parent
a9bef7b50d
commit
4fe6f9d7b5
15 files changed
+968
No files matched your search
@@ -0,0 +1,135 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { mockNuxtImport } from '@nuxt/test-utils/runtime'
|
||||
import UserCodeList from '~/components/UserCodeList.vue'
|
||||
import { UserCodeStatus } from '~/types/yale'
|
||||
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 codes = [
|
||||
{ id: 4, code: '9999', status: UserCodeStatus.ENABLED, isHome: true },
|
||||
{ id: 2, code: '2222', status: UserCodeStatus.ENABLED, isHome: false },
|
||||
{ id: 3, code: '', status: UserCodeStatus.AVAILABLE, isHome: false }
|
||||
]
|
||||
|
||||
const mountList = async () => {
|
||||
const wrapper = await mountInApp(UserCodeList)
|
||||
await settle()
|
||||
return wrapper
|
||||
}
|
||||
|
||||
const rows = (wrapper: Awaited<ReturnType<typeof mountList>>) => wrapper.findAll('tbody tr')
|
||||
const buttonLabels = (row: { findAll: (s: string) => { attributes: (n: string) => string | undefined }[] }) =>
|
||||
row.findAll('button').map(b => b.attributes('aria-label'))
|
||||
|
||||
describe('UserCodeList', () => {
|
||||
beforeEach(() => {
|
||||
api.get.mockReset()
|
||||
api.post.mockReset()
|
||||
api.get.mockImplementation(async (path: string) =>
|
||||
path === '/api/Yale/codes'
|
||||
? { success: true, data: structuredClone(codes) }
|
||||
: { success: true, data: [{ id: 1, name: 'Alex', phoneNumber: '+61400000001' }] })
|
||||
api.post.mockResolvedValue({ success: true, data: true })
|
||||
document.body.innerHTML = ''
|
||||
})
|
||||
|
||||
it('lists the codes sorted by id with their status', async () => {
|
||||
const wrapper = await mountList()
|
||||
const r = rows(wrapper)
|
||||
|
||||
expect(r).toHaveLength(3)
|
||||
expect(r.map(x => x.findAll('td')[0]!.text())).toEqual(['2', '3', '4'])
|
||||
expect(r[0]!.text()).toContain('Enabled')
|
||||
expect(r[1]!.text()).toContain('Available')
|
||||
})
|
||||
|
||||
it('only offers show and edit for the home code, and marks it with the home icon', async () => {
|
||||
const wrapper = await mountList()
|
||||
const home = rows(wrapper)[2]!
|
||||
|
||||
expect(buttonLabels(home)).toEqual(['Show code', 'Edit code'])
|
||||
expect(home.find('[aria-label="Home code"]').exists()).toBe(true)
|
||||
})
|
||||
|
||||
it('disables show, send and delete for an available (unset) code', async () => {
|
||||
const wrapper = await mountList()
|
||||
const available = rows(wrapper)[1]!
|
||||
|
||||
for (const label of ['Show code', 'Send code', 'Delete code']) {
|
||||
expect(available.find(`button[aria-label="${label}"]`).attributes('disabled')).toBeDefined()
|
||||
}
|
||||
expect(available.find('button[aria-label="Edit code"]').attributes('disabled')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('shows the code in a modal', async () => {
|
||||
const wrapper = await mountList()
|
||||
await click(rows(wrapper)[0]!.find('button[aria-label="Show code"]').element)
|
||||
|
||||
expect(document.body.textContent).toContain('User Code #2')
|
||||
expect(document.body.textContent).toContain('2222')
|
||||
})
|
||||
|
||||
it('rejects a code that is not 4-6 digits without calling the API', async () => {
|
||||
const wrapper = await mountList()
|
||||
await click(rows(wrapper)[0]!.find('button[aria-label="Edit code"]').element)
|
||||
|
||||
await setInput(inBody<HTMLInputElement>('input')!, '12')
|
||||
await submitForm(inBody<HTMLFormElement>('#edit-code')!)
|
||||
|
||||
expect(document.body.textContent).toContain('Code must be 4, 5 or 6 digits long')
|
||||
expect(api.post).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('updates a code through the API and reflects it in the table', async () => {
|
||||
const wrapper = await mountList()
|
||||
await click(rows(wrapper)[1]!.find('button[aria-label="Edit code"]').element) // available code #3
|
||||
|
||||
await setInput(inBody<HTMLInputElement>('input')!, '123456')
|
||||
await submitForm(inBody<HTMLFormElement>('#edit-code')!)
|
||||
|
||||
expect(api.post).toHaveBeenCalledExactlyOnceWith('/api/Yale/code/3', '123456')
|
||||
expect(rows(wrapper)[1]!.text()).toContain('Enabled')
|
||||
})
|
||||
|
||||
it('warns before editing the home code', async () => {
|
||||
const wrapper = await mountList()
|
||||
await click(rows(wrapper)[2]!.find('button[aria-label="Edit code"]').element)
|
||||
|
||||
expect(document.body.textContent).toContain('You are about to edit the home code')
|
||||
})
|
||||
|
||||
it('does not change the table when the API rejects the update', async () => {
|
||||
api.post.mockResolvedValue({ success: false, error: 'An error occurred setting the code.' })
|
||||
const wrapper = await mountList()
|
||||
await click(rows(wrapper)[1]!.find('button[aria-label="Edit code"]').element)
|
||||
|
||||
await setInput(inBody<HTMLInputElement>('input')!, '4321')
|
||||
await submitForm(inBody<HTMLFormElement>('#edit-code')!)
|
||||
|
||||
expect(rows(wrapper)[1]!.text()).toContain('Available')
|
||||
})
|
||||
|
||||
it('deletes a code after confirmation and marks it available', async () => {
|
||||
const wrapper = await mountList()
|
||||
await click(rows(wrapper)[0]!.find('button[aria-label="Delete code"]').element) // code #2
|
||||
|
||||
expect(document.body.textContent).toContain('Are you sure you want to delete this code?')
|
||||
expect(api.post).not.toHaveBeenCalled()
|
||||
|
||||
const confirm = [...document.body.querySelectorAll('button')].find(b => b.textContent?.trim() === 'Delete Code')
|
||||
await click(confirm ?? null)
|
||||
|
||||
expect(api.post).toHaveBeenCalledExactlyOnceWith('/api/Yale/code/2/status')
|
||||
expect(rows(wrapper)[0]!.text()).toContain('Available')
|
||||
})
|
||||
|
||||
it('keeps the send button disabled until a person is chosen', async () => {
|
||||
const wrapper = await mountList()
|
||||
await click(rows(wrapper)[0]!.find('button[aria-label="Send code"]').element)
|
||||
|
||||
const send = [...document.body.querySelectorAll('button')].find(b => b.textContent?.trim() === 'Send Code')
|
||||
expect(send?.hasAttribute('disabled')).toBe(true)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user