bolts/frontend/tests/user-file.spec.ts

138 lines
5.0 KiB
TypeScript

import { expect, type Page, test } from '@playwright/test'
async function useAdminSession (page: Page) {
await page.addInitScript(() => {
sessionStorage.setItem('bolts-session', JSON.stringify({
username: 'admin',
role: 'admin',
token: 'admin-token',
}))
})
}
test('admin opens a user file and views a linked entity record', async ({ page }) => {
let projectTitle = 'Website refresh'
const projects: Record<string, unknown>[] = [{ id: 42, title: 'Website refresh', owner: 7 }]
const tasks: Record<string, unknown>[] = []
const projectResponse = () => ({
entity: {
id: 3,
name: 'Project',
identifier: 'project',
fields: [
{ id: 11, name: 'Title', identifier: 'title', type: 'TEXT' },
{ id: 12, name: 'Owner', identifier: 'owner', type: 'USER' },
],
},
values: { id: 42, title: projectTitle, owner: 7 },
children: [{
entity: {
id: 4,
name: 'Task',
identifier: 'task',
fields: [
{ id: 20, name: 'Description', identifier: 'description', type: 'TEXT' },
{
id: 21,
name: 'Project',
identifier: 'project',
type: 'RELATIONSHIP',
targetEntityId: 3,
},
],
},
relationshipField: {
id: 21,
name: 'Project',
identifier: 'project',
type: 'RELATIONSHIP',
targetEntityId: 3,
},
records: tasks,
}],
})
await useAdminSession(page)
await page.route('**/api/users', route => route.fulfill({
json: [{ id: 7, username: 'alex', role: 'user' }],
}))
await page.route('**/api/users/7/file', route => route.fulfill({
json: {
user: { id: 7, username: 'alex', role: 'user' },
linkedEntities: [{
entity: {
id: 3,
name: 'Project',
identifier: 'project',
fields: [
{ id: 11, name: 'Title', identifier: 'title', type: 'TEXT' },
{ id: 12, name: 'Owner', identifier: 'owner', type: 'USER' },
],
},
relationshipField: { id: 12, name: 'Owner', identifier: 'owner', type: 'USER' },
records: projects,
}],
},
}))
await page.route('**/api/users/7/children/Project/12', async route => {
const values = new URLSearchParams(route.request().postData() ?? '')
const created = { id: 43, title: values.get('title'), owner: 7 }
projects.push(created)
await route.fulfill({
status: 201,
json: { entity: projectResponse().entity, values: created, children: [] },
})
})
await page.route('**/api/users/7/entities/Project/records/42', async route => {
if (route.request().method() === 'PATCH') {
const values = new URLSearchParams(route.request().postData() ?? '')
projectTitle = values.get('title') ?? projectTitle
}
await route.fulfill({ json: projectResponse() })
})
await page.route('**/api/users/7/entities/Project/records/42/children/Task/21', async route => {
const values = new URLSearchParams(route.request().postData() ?? '')
const created = { id: 55, description: values.get('description'), project: 42 }
tasks.push(created)
await route.fulfill({
status: 201,
json: {
entity: projectResponse().children.at(0)!.entity,
values: created,
children: [],
},
})
})
await page.goto('/users')
await page.getByRole('link', { name: 'alex' }).click()
await expect(page.getByRole('heading', { name: 'alex' })).toBeVisible()
await expect(page.getByRole('heading', { name: 'Project' })).toBeVisible()
await expect(page.getByRole('row').filter({ hasText: 'Website refresh' })).toContainText('7')
await page.getByRole('button', { name: 'Add Project' }).click()
const projectForm = page.getByRole('form', { name: 'Add Project' })
await projectForm.getByLabel('Title').fill('Mobile application')
await projectForm.getByRole('button', { name: 'Add Project' }).click()
await expect(page.getByRole('row').filter({ hasText: 'Mobile application' })).toContainText('7')
await page.getByRole('link', { name: 'View Project record 42' }).click()
await expect(page).toHaveURL('/users/7/entities/Project/records/42')
await expect(page.getByRole('heading', { name: 'Project #42' })).toBeVisible()
await expect(page.getByText('Website refresh')).toBeVisible()
await expect(page.getByText('Owner')).toBeVisible()
await page.getByRole('button', { name: 'Edit record' }).click()
const editForm = page.getByRole('form', { name: 'Edit record' })
await editForm.getByLabel('Title').fill('Mobile application')
await editForm.getByRole('button', { name: 'Save changes' }).click()
await expect(page.getByText('Mobile application')).toBeVisible()
await page.getByRole('button', { name: 'Add Task' }).click()
const childForm = page.getByRole('form', { name: 'Add Task' })
await childForm.getByLabel('Description').fill('Prepare launch')
await childForm.getByRole('button', { name: 'Add Task' }).click()
await expect(page.getByRole('row').filter({ hasText: 'Prepare launch' })).toContainText('42')
})