feat(projects): list + create API and creatable ProjectAutocomplete (T4)
- GET /api/projects returns the shared global list; POST /api/projects creates a project, deduped case-insensitively (F3). - Repository find-or-create trims and matches on lower(name); a unique index on lower(name) enforces the same rule at the DB (replacing the case- sensitive column unique constraint) — migration 0001. - ProjectAutocomplete: creatable autocomplete that lists projects, offers to create an unknown name inline, and selects it without reload. - Integration test (dedupe/list against Postgres) + component test.
This commit is contained in:
93
tests/project-autocomplete.nuxt.spec.ts
Normal file
93
tests/project-autocomplete.nuxt.spec.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
// @vitest-environment nuxt
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
||||
import { mountSuspended, registerEndpoint } from '@nuxt/test-utils/runtime'
|
||||
import { flushPromises } from '@vue/test-utils'
|
||||
import { defineEventHandler, readBody } from 'h3'
|
||||
import ProjectAutocomplete from '../app/components/ProjectAutocomplete.vue'
|
||||
|
||||
interface Row {
|
||||
id: string
|
||||
name: string
|
||||
}
|
||||
|
||||
let store: Row[]
|
||||
|
||||
beforeEach(() => {
|
||||
store = [
|
||||
{ id: '1', name: 'Acme' },
|
||||
{ id: '2', name: 'Globex' },
|
||||
]
|
||||
// One endpoint backing both list (GET) and creatable autocomplete (POST),
|
||||
// deduping case-insensitively like the real API.
|
||||
registerEndpoint(
|
||||
'/api/projects',
|
||||
defineEventHandler(async (event) => {
|
||||
if (event.method === 'GET') return store
|
||||
const { name } = await readBody<{ name: string }>(event)
|
||||
const trimmed = name.trim()
|
||||
const existing = store.find(
|
||||
(p) => p.name.toLowerCase() === trimmed.toLowerCase(),
|
||||
)
|
||||
if (existing) return existing
|
||||
const created = { id: String(store.length + 1), name: trimmed }
|
||||
store.push(created)
|
||||
return created
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
describe('ProjectAutocomplete', () => {
|
||||
it('lists the existing projects when opened', async () => {
|
||||
const wrapper = await mountSuspended(ProjectAutocomplete)
|
||||
await flushPromises()
|
||||
|
||||
await wrapper.find('[data-test="project-input"]').trigger('focus')
|
||||
const names = wrapper
|
||||
.findAll('[data-test="project-option"]')
|
||||
.map((el) => el.text())
|
||||
expect(names).toContain('Acme')
|
||||
expect(names).toContain('Globex')
|
||||
})
|
||||
|
||||
it('offers to create a project that does not exist yet', async () => {
|
||||
const wrapper = await mountSuspended(ProjectAutocomplete)
|
||||
await flushPromises()
|
||||
|
||||
const input = wrapper.find('[data-test="project-input"]')
|
||||
await input.setValue('Initech')
|
||||
expect(wrapper.find('[data-test="project-create"]').exists()).toBe(true)
|
||||
})
|
||||
|
||||
it('does not offer to create a name that already exists (case-insensitive)', async () => {
|
||||
const wrapper = await mountSuspended(ProjectAutocomplete)
|
||||
await flushPromises()
|
||||
|
||||
await wrapper.find('[data-test="project-input"]').setValue('acme')
|
||||
expect(wrapper.find('[data-test="project-create"]').exists()).toBe(false)
|
||||
})
|
||||
|
||||
it('persists a new project, selects it, and makes it selectable without reload', async () => {
|
||||
const wrapper = await mountSuspended(ProjectAutocomplete)
|
||||
await flushPromises()
|
||||
|
||||
await wrapper.find('[data-test="project-input"]').setValue('Initech')
|
||||
await wrapper.find('[data-test="project-create"]').trigger('click')
|
||||
|
||||
// Selected and emitted to the parent (the POST resolves asynchronously).
|
||||
await vi.waitFor(() => {
|
||||
expect(wrapper.emitted('update:modelValue')).toBeTruthy()
|
||||
})
|
||||
await flushPromises()
|
||||
const emitted = wrapper.emitted('update:modelValue')!
|
||||
expect(emitted.at(-1)![0]).toMatchObject({ name: 'Initech' })
|
||||
|
||||
// Persisted to the store and now selectable without reload — reopening the
|
||||
// list (the create flow closes it) shows the new project.
|
||||
expect(store.map((p) => p.name)).toContain('Initech')
|
||||
await wrapper.find('[data-test="project-input"]').trigger('focus')
|
||||
const names = wrapper
|
||||
.findAll('[data-test="project-option"]')
|
||||
.map((el) => el.text())
|
||||
expect(names).toContain('Initech')
|
||||
})
|
||||
})
|
||||
70
tests/projects.spec.ts
Normal file
70
tests/projects.spec.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest'
|
||||
import { setupTestDb, truncateAll, type TestDb } from './helpers/db'
|
||||
import { createProject, listProjects } from '../server/db/repositories/projects'
|
||||
|
||||
let db: TestDb
|
||||
let pool: { end: () => Promise<void> }
|
||||
|
||||
beforeAll(async () => {
|
||||
const setup = await setupTestDb()
|
||||
db = setup.db
|
||||
pool = setup.pool
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await pool.end()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
await truncateAll(db)
|
||||
})
|
||||
|
||||
describe('projects repository', () => {
|
||||
it('lists nothing when no projects exist', async () => {
|
||||
expect(await listProjects(db)).toEqual([])
|
||||
})
|
||||
|
||||
it('creates a project and lists it', async () => {
|
||||
const created = await createProject(db, 'Acme')
|
||||
expect(created.id).toBeDefined()
|
||||
expect(created.name).toBe('Acme')
|
||||
|
||||
const list = await listProjects(db)
|
||||
expect(list).toHaveLength(1)
|
||||
expect(list[0]!.name).toBe('Acme')
|
||||
})
|
||||
|
||||
it('dedupes case-insensitively — "Acme" then "acme" yields one project', async () => {
|
||||
const first = await createProject(db, 'Acme')
|
||||
const second = await createProject(db, 'acme')
|
||||
|
||||
expect(second.id).toBe(first.id)
|
||||
expect(second.name).toBe('Acme') // keeps the original casing
|
||||
expect(await listProjects(db)).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('trims surrounding whitespace and dedupes against the trimmed name', async () => {
|
||||
const first = await createProject(db, 'Globex')
|
||||
const second = await createProject(db, ' globex ')
|
||||
|
||||
expect(second.id).toBe(first.id)
|
||||
expect(await listProjects(db)).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('rejects a blank name', async () => {
|
||||
await expect(createProject(db, ' ')).rejects.toThrow()
|
||||
expect(await listProjects(db)).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('lists projects alphabetically by name', async () => {
|
||||
await createProject(db, 'Zebra')
|
||||
await createProject(db, 'Acme')
|
||||
await createProject(db, 'Monarch')
|
||||
|
||||
expect((await listProjects(db)).map((p) => p.name)).toEqual([
|
||||
'Acme',
|
||||
'Monarch',
|
||||
'Zebra',
|
||||
])
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user