Files
client-web/src/modules/overseas-warehouse/views/transferCreateForm.test.mjs
T

78 lines
4.5 KiB
JavaScript

import assert from 'node:assert/strict'
import test from 'node:test'
import { readFileSync } from 'node:fs'
import { createRequire } from 'node:module'
import { computed, reactive, ref } from 'vue'
import ts from 'typescript'
const source = readFileSync(new URL('./WarehouseServiceView.vue', import.meta.url), 'utf8')
const { default: Schema } = createRequire(import.meta.resolve('element-plus'))('async-validator')
const ruleSource = source.slice(source.indexOf('const transferFormRules ='), source.indexOf('watch([inventoryTransfer,'))
const code = ts.transpileModule(ruleSource, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText
function harness(patch = {}) {
const form = reactive({ warehouse: 'WH', deliveryMethod: 'WAREHOUSE_PICKUP', address: 'Street 1', pickupContact: 'Contact', pickupPhone: '123456', estimatedArrivalAt: '2026-11-01T12:00:00', relabel: false, ...patch })
const inventoryTransfer = ref(false), transferDestinationType = ref('NONE'), labelingMode = ref('carton')
const rules = new Function('computed', 'inventoryTransfer', 'transferDestinationType', 'labelingMode', 'form', code + '\nreturn transferFormRules')(computed, inventoryTransfer, transferDestinationType, labelingMode, form)
return { form, inventoryTransfer, transferDestinationType, labelingMode, validate: () => new Schema(rules.value).validate(form) }
}
const hasFields = (...fields) => error => {
assert.deepEqual(Object.keys(error.fields).sort(), fields.sort())
return true
}
test('pickup requires contact and phone; customer delivery hides those requirements', async () => {
const state = harness({ pickupContact: ' ', pickupPhone: '' })
await assert.rejects(state.validate(), hasFields('pickupContact', 'pickupPhone'))
state.form.deliveryMethod = 'CUSTOMER_DELIVERY'
await state.validate()
state.form.address = ''
await assert.rejects(state.validate(), hasFields('address'))
})
test('inventory transfer excludes inbound address, arrival and pickup fields', async () => {
const state = harness({ deliveryMethod: '', address: '', estimatedArrivalAt: '', pickupContact: '', pickupPhone: '' })
state.inventoryTransfer.value = true
await state.validate()
state.form.warehouse = ''
await assert.rejects(state.validate(), hasFields('warehouse'))
})
test('destination switch validates only FBA or private warehouse fields currently visible', async () => {
const state = harness()
await state.validate()
state.transferDestinationType.value = 'FBA'
await assert.rejects(state.validate(), hasFields('destination'))
state.transferDestinationType.value = 'PRIVATE'
await assert.rejects(state.validate(), hasFields('privateDestinationAddress', 'privateDestinationPostalCode', 'privateDestinationContact', 'privateDestinationPhone'))
Object.assign(state.form, { privateDestinationAddress: 'Private street', privateDestinationPostalCode: '10000', privateDestinationContact: 'Person', privateDestinationPhone: '123456' })
await state.validate()
state.transferDestinationType.value = 'NONE'
await state.validate()
})
test('relabeling requires its visible fields, with no delivery method for SKU-only mode', async () => {
const state = harness({ relabel: true })
await assert.rejects(state.validate(), hasFields('labelDeadline', 'relabelContact', 'relabelPhone', 'relabelDeliveryMethod', 'relabelDestination'))
Object.assign(state.form, { labelDeadline: '2026-11-01T12:00:00', relabelContact: 'Contact', relabelPhone: '123456', relabelDestination: 'FBA-A' })
state.labelingMode.value = 'sku'
await state.validate()
state.labelingMode.value = 'carton_and_sku'
await assert.rejects(state.validate(), hasFields('relabelDeliveryMethod'))
state.form.relabel = false
await state.validate()
})
test('draft and submit stop before business requests when create validation fails; edit remains unchanged', async () => {
const start = source.indexOf(' if (isTransferCreate.value) {', source.indexOf('async function submit('))
const end = source.indexOf(' const primaryWarehouse =', start)
const code = 'return async function submit(action) {' + source.slice(start, end) + '\nreturn action\n}'
const isTransferCreate = ref(true)
const labelingFormRef = ref({ validate: async () => { throw new Error('required') } })
const submit = new Function('isTransferCreate', 'labelingFormRef', 'nextTick', code)(isTransferCreate, labelingFormRef, async () => {})
assert.equal(await submit('draft'), undefined)
assert.equal(await submit('submit'), undefined)
isTransferCreate.value = false
assert.equal(await submit('draft'), 'draft')
assert.equal(await submit('submit'), 'submit')
})