Files
zyt/admin/tests/first-visit-patient-actions.test.cjs
T

77 lines
4.0 KiB
JavaScript

const assert = require('node:assert/strict')
const path = require('node:path')
const test = require('node:test')
const { buildSync } = require('esbuild')
const { outputFiles } = buildSync({
entryPoints: [path.join(__dirname, '../src/views/first_visit/my_patients/patient-actions.ts')],
write: false, format: 'cjs', platform: 'node'
})
const mod = { exports: {} }
new Function('module', 'exports', outputFiles[0].text)(mod, mod.exports)
const { patientActionLayout } = mod.exports
const none = { edit: false, read: false, book: false, assign: false }
const all = { edit: true, read: true, book: true, assign: true }
const booked = { assistant_id: '12', appointment_id: '82', appointment_status: '1', appointment_doctor_id: '7', has_id_card: '0' }
const unbooked = { assistant_id: 0, appointment_id: 0, has_id_card: '1' }
const keys = actions => actions.map(a => a.key)
const allKeys = group => keys([...group.inline, ...group.more])
test('an empty list or a scope-only role does not reserve an action column', () => {
assert.equal(patientActionLayout([], all, 857, true).width, 0)
const layout = patientActionLayout([booked, unbooked], none, 857, true)
assert.equal(layout.width, 0)
assert.deepEqual(allKeys(layout.byRow.get(booked)), [])
})
test('read-only and assignment roles get compact direct actions; use the longest real row in either order', () => {
const access = { ...none, assign: true }
const widthFor = rows => patientActionLayout(rows, access, 857, true).width
const small = widthFor([unbooked])
const large = widthFor([booked])
assert.ok(small > 0 && small < large && large < 120)
for (const rows of [[unbooked, booked], [booked, unbooked]]) assert.equal(widthFor(rows), large)
const layout = patientActionLayout([booked, unbooked], { ...access, read: true }, 601, true)
assert.deepEqual(layout.byRow.get(booked).inline.map(a => a.label), ['查看', '重新指派'])
assert.deepEqual(layout.byRow.get(unbooked).inline.map(a => a.label), ['查看', '指派'])
assert.deepEqual(layout.byRow.get(booked).more, [])
})
test('high-frequency actions stay direct, and every authorized row action stays reachable at all widths', () => {
const expected = ['edit', 'book', 'assign', 'fillId', 'qr', 'cancel']
for (const width of [0, 280, 390, 601, 857, 1017]) {
const layout = patientActionLayout([unbooked, booked], all, width, true)
const group = layout.byRow.get(booked)
assert.deepEqual(keys(group.inline), width >= 720 ? ['edit', 'book'] : ['edit'])
assert.deepEqual(allKeys(group), expected)
assert.deepEqual(allKeys(layout.byRow.get(unbooked)), ['edit', 'book', 'assign'])
assert.ok(layout.width < 200)
if (width < 355) assert.equal(layout.fixPatient, false)
if (layout.fixActions) assert.ok(width - layout.width - 190 >= 165)
if (width === 601) assert.ok(layout.fixActions && layout.fixPatient)
}
})
test('booking menus follow row status; no QR or cancel action leaks through other permissions', () => {
const access = { ...none, book: true }
for (const [overrides, wanted] of [
[{}, ['book', 'qr', 'cancel']],
[{ appointment_doctor_id: 0 }, ['book', 'cancel']],
[{ appointment_status: '4' }, ['book', 'cancel']],
[{ appointment_status: '3' }, ['book']],
[{ appointment_id: '0' }, ['book']]
]) {
const row = { ...booked, ...overrides }
assert.deepEqual(allKeys(patientActionLayout([row], access, 857, true).byRow.get(row)), wanted)
assert.deepEqual(allKeys(patientActionLayout([row], { ...none, edit: true, read: true }, 857, true).byRow.get(row)), ['edit', 'fillId'])
}
})
test('classic keeps both pinned columns and every eligible action inline even on narrow screens', () => {
const layout = patientActionLayout([booked], all, 280, false)
assert.deepEqual(keys(layout.byRow.get(booked).inline), ['edit', 'book', 'assign', 'fillId', 'qr', 'cancel'])
assert.deepEqual(layout.byRow.get(booked).more, [])
assert.equal(layout.fixActions && layout.fixPatient, true)
})