form-picker-popup.test.mjs 1.72 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createRequire } from 'node:module'

const require = createRequire(import.meta.url)
const {
	resolvePickerItemKey,
	normalizePickerValues,
	buildPickerOptionRows
} = require('../components/form-picker-popup/picker-value.js')

describe('form-picker-popup picker-value', () => {
	it('IT-PICKER-001: normalizePickerValues coerces scalar to single-item array', () => {
		assert.deepEqual(normalizePickerValues('4s-001'), ['4s-001'])
		assert.deepEqual(normalizePickerValues(['4s-001', '4s-002']), ['4s-001', '4s-002'])
		assert.deepEqual(normalizePickerValues(null), [])
		assert.deepEqual(normalizePickerValues(''), [])
	})

	it('IT-PICKER-002: buildPickerOptionRows marks active for current value', () => {
		const options = [
			{ label: '员工A', value: '4s-001' },
			{ label: '员工B', value: '4s-002' }
		]
		const rows = buildPickerOptionRows(options, '4s-002')
		assert.equal(rows.length, 2)
		assert.equal(rows[0].active, false)
		assert.equal(rows[1].active, true)
	})

	it('IT-PICKER-003: normalizePickerValues resolves object array to ids not [object Object]', () => {
		const staff = {
			id: '982873a3744e44fbb439432e51edadef',
			name: '林永健test003',
			phone: '17875511831'
		}
		assert.equal(resolvePickerItemKey(staff), '982873a3744e44fbb439432e51edadef')
		assert.deepEqual(normalizePickerValues([staff]), [
			'982873a3744e44fbb439432e51edadef'
		])
		assert.ok(
			!normalizePickerValues([staff]).some(item => item.includes('[object Object]'))
		)
		const options = [{ label: '林永健test003', value: '982873a3744e44fbb439432e51edadef' }]
		const rows = buildPickerOptionRows(options, [staff])
		assert.equal(rows[0].active, true)
	})
})