store-staff-count.test.mjs 5.78 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 {
	STORE_STAFF_LIST_FIELD_KEY,
	STORE_STAFF_COUNT_FIELD_KEY,
	countStoreStaffSelection,
	resolveStoreStaffCount,
	isStoreStaffCountEqual,
	shouldSyncStoreStaffCount,
	shouldDisableStoreStaffCount,
	isStoreStaffListFieldVisible,
	patchStoreStaffCountFieldInViewState
} = require('../utils/store-staff-count.js')
const dailyReportPageMethods = require('../pages/behaviors/dailyReportPageMethods.js')

describe('store-staff-count', () => {
	it('IT-STORE-STAFF-COUNT-001: object array counts as selection length', () => {
		const values = {
			[STORE_STAFF_LIST_FIELD_KEY]: [
				{ id: 'u-1', name: '张三' },
				{ id: 'u-2', name: '李四' }
			]
		}
		assert.equal(countStoreStaffSelection(values), 2)
		assert.equal(resolveStoreStaffCount(values), 2)
	})

	it('IT-STORE-STAFF-COUNT-002: empty selection yields 0', () => {
		assert.equal(countStoreStaffSelection({ [STORE_STAFF_LIST_FIELD_KEY]: [] }), 0)
		assert.equal(countStoreStaffSelection({}), 0)
	})

	it('IT-STORE-STAFF-COUNT-003: shouldSync false when F2-1 not visible', () => {
		const viewState = {
			fixedFields: [{ key: STORE_STAFF_COUNT_FIELD_KEY, type: 'numberInput' }],
			sectionGroups: []
		}
		assert.equal(isStoreStaffListFieldVisible(viewState), false)
		assert.equal(
			shouldSyncStoreStaffCount({
				isTodayReport: true,
				formDisabled: false,
				viewState
			}),
			false
		)
	})

	it('IT-STORE-STAFF-COUNT-004: shouldSync true when F2-1 visible and today editable', () => {
		const viewState = {
			fixedFields: [
				{ key: STORE_STAFF_LIST_FIELD_KEY, type: 'multipleSelect' },
				{ key: STORE_STAFF_COUNT_FIELD_KEY, type: 'numberInput' }
			],
			sectionGroups: []
		}
		assert.equal(isStoreStaffListFieldVisible(viewState), true)
		assert.equal(
			shouldSyncStoreStaffCount({
				isTodayReport: true,
				formDisabled: false,
				viewState
			}),
			true
		)
		assert.equal(
			shouldSyncStoreStaffCount({
				isTodayReport: false,
				formDisabled: false,
				viewState
			}),
			false
		)
	})

	it('IT-STORE-STAFF-COUNT-005: isStoreStaffCountEqual treats 0 as filled', () => {
		assert.equal(isStoreStaffCountEqual(0, 0), true)
		assert.equal(isStoreStaffCountEqual(null, 0), true)
		assert.equal(isStoreStaffCountEqual(1, 2), false)
	})

	const visibleViewState = {
		fixedFields: [
			{ key: STORE_STAFF_LIST_FIELD_KEY, type: 'multipleSelect' },
			{ key: STORE_STAFF_COUNT_FIELD_KEY, type: 'numberInput' }
		],
		sectionGroups: []
	}

	it('IT-STORE-STAFF-DIS-001: shouldDisable true when F2-1 has selection', () => {
		assert.equal(
			shouldDisableStoreStaffCount({
				isTodayReport: true,
				formDisabled: false,
				viewState: visibleViewState,
				values: {
					[STORE_STAFF_LIST_FIELD_KEY]: [
						{ id: 'u-1', name: '张三' },
						{ id: 'u-2', name: '李四' }
					]
				}
			}),
			true
		)
	})

	it('IT-STORE-STAFF-DIS-002: shouldDisable false when F2-1 empty', () => {
		assert.equal(
			shouldDisableStoreStaffCount({
				isTodayReport: true,
				formDisabled: false,
				viewState: visibleViewState,
				values: { [STORE_STAFF_LIST_FIELD_KEY]: [] }
			}),
			false
		)
	})

	it('IT-STORE-STAFF-DIS-003: shouldDisable false when not today or F2-1 hidden', () => {
		assert.equal(
			shouldDisableStoreStaffCount({
				isTodayReport: false,
				formDisabled: false,
				viewState: visibleViewState,
				values: {
					[STORE_STAFF_LIST_FIELD_KEY]: [{ id: 'u-1', name: '张三' }]
				}
			}),
			false
		)
		assert.equal(
			shouldDisableStoreStaffCount({
				isTodayReport: true,
				formDisabled: false,
				viewState: {
					fixedFields: [{ key: STORE_STAFF_COUNT_FIELD_KEY, type: 'numberInput' }],
					sectionGroups: []
				},
				values: {
					[STORE_STAFF_LIST_FIELD_KEY]: [{ id: 'u-1', name: '张三' }]
				}
			}),
			false
		)
	})

	it('IT-STORE-STAFF-PATCH-001: patch sets F2-2 disabled when F2-1 has selection', () => {
		const viewState = {
			fixedFields: [
				{ key: STORE_STAFF_LIST_FIELD_KEY, type: 'multipleSelect' },
				{ key: STORE_STAFF_COUNT_FIELD_KEY, type: 'numberInput' }
			],
			values: {
				[STORE_STAFF_LIST_FIELD_KEY]: [{ id: 'u-1', name: '张三' }]
			}
		}
		const patched = patchStoreStaffCountFieldInViewState(viewState, {
			isTodayReport: true,
			formDisabled: false
		})
		const f22 = patched.fixedFields.find(field => field.key === STORE_STAFF_COUNT_FIELD_KEY)
		assert.equal(f22.disabled, true)
	})

	it('IT-STORE-STAFF-PATCH-002: patch clears F2-2 disabled when F2-1 empty', () => {
		const viewState = {
			fixedFields: [
				{ key: STORE_STAFF_LIST_FIELD_KEY, type: 'multipleSelect' },
				{ key: STORE_STAFF_COUNT_FIELD_KEY, type: 'numberInput', disabled: true }
			],
			values: { [STORE_STAFF_LIST_FIELD_KEY]: [] }
		}
		const patched = patchStoreStaffCountFieldInViewState(viewState, {
			isTodayReport: true,
			formDisabled: false
		})
		const f22 = patched.fixedFields.find(field => field.key === STORE_STAFF_COUNT_FIELD_KEY)
		assert.equal(f22.disabled, false)
	})

	it('IT-STORE-STAFF-PATCH-003: applyEngineView refreshFixedFields updates fixedFields when structure unchanged', () => {
		const calls = []
		const page = {
			data: {
				fixedFields: [{ key: STORE_STAFF_COUNT_FIELD_KEY, type: 'numberInput' }],
				groupFields: [],
				groupActiveIndex: {}
			},
			getFormDisabled() {
				return false
			},
			setData(data) {
				calls.push(data)
			}
		}
		dailyReportPageMethods.applyEngineView.call(
			page,
			{
				fixedFields: [
					{ key: STORE_STAFF_COUNT_FIELD_KEY, type: 'numberInput', disabled: true }
				],
				values: {},
				groupRowsCache: {}
			},
			{ structureChanged: false, refreshFixedFields: true }
		)
		assert.equal(calls.length, 1)
		assert.equal(calls[0].fixedFields[0].disabled, true)
		assert.equal(calls[0].sectionGroups, undefined)
	})
})