20260914223013

This commit is contained in:
oneao committed 2026-09-14 22:30:13 +08:00
1 parent 549c8bd2eb
commit 63514476d7
60 files changed
+6478 -2004

No files matched your search

@@ -1,21 +1,24 @@
/**
* 往来单位分类树页面级纯函数(views/base/othercompany/utils.js)。
* 往来单位分类树页面级纯函数。
*
* 页面单文件自包含后(分类树逻辑内联在 views/base/othercompany/index.vue),
* 纯函数不再单独导出,测试通过挂载页面组件访问 <script setup> 的内部绑定。
*
* 重点是 buildCategoryTree 不能污染原始行:buildTree 会就地往行对象上挂 children,
* 若直接在 categories 的行上构建,重复构建会累积出重复子节点。
*/
import { describe, expect, it } from 'vitest'
import {
ALL_CATEGORY_KEY,
addRowUpdate,
applyCategoryPosition,
buildCategoryTree,
findNodePosition,
hasChildCategory,
nextSiblingXh,
rebuildTreePositions,
renumberSiblings,
} from '@/views/base/othercompany/utils'
import { describe, expect, it, beforeEach, vi } from 'vitest'
import { flushPromises } from '@vue/test-utils'
import Othercompany from '@/views/base/othercompany/index.vue'
import { mountPage } from '../helpers/app.js'
vi.mock('@/services/api', () => ({
loadDataApi: vi.fn(() => Promise.resolve({ data: [], code: 0, success: true })),
pageDataApi: vi.fn(() =>
Promise.resolve({ data: { rows: [], total: 0 }, code: 0, success: true }),
),
saveObjectApi: vi.fn(() => Promise.resolve({ code: 0, success: true })),
}))
function category(over = {}) {
return {
@@ -30,6 +33,15 @@ function category(over = {}) {
}
}
/** 页面实例(分类树纯函数与状态都在它的 setup 里) */
let vm
beforeEach(async () => {
const { wrapper } = await mountPage(Othercompany)
await flushPromises()
vm = wrapper.vm
})
describe('othercompany 分类树工具:buildCategoryTree', () => {
it('按 b_parent_id 建树并按 b_xh 排序', () => {
const rows = [
@@ -38,7 +50,7 @@ describe('othercompany 分类树工具:buildCategoryTree', () => {
category({ b_id: 'a1', b_parent_id: 'a', b_depth: 1, b_path: '/a/a1/', b_name: 'A1' }),
]
const tree = buildCategoryTree(rows)
const tree = vm.buildCategoryTree(rows)
expect(tree.map((node) => node.b_id)).toEqual(['a', 'b'])
expect(tree[0].children.map((node) => node.b_id)).toEqual(['a1'])
@@ -50,11 +62,11 @@ describe('othercompany 分类树工具:buildCategoryTree', () => {
category({ b_id: 'a1', b_parent_id: 'a', b_depth: 1, b_path: '/a/a1/' }),
]
buildCategoryTree(rows)
vm.buildCategoryTree(rows)
// buildTree 的 children 挂在浅拷贝上,原始行保持干净
expect(rows[0].children).toBeUndefined()
const tree = buildCategoryTree(rows)
const tree = vm.buildCategoryTree(rows)
expect(tree[0].children).toHaveLength(1)
expect(tree[0].children[0].b_id).toBe('a1')
})
@@ -63,13 +75,13 @@ describe('othercompany 分类树工具:buildCategoryTree', () => {
describe('othercompany 分类树工具:位置派生', () => {
it('根节点的 b_depth 为 0、b_path 为 /编码/', () => {
const row = { b_id: 'a' }
applyCategoryPosition(row, null)
vm.applyCategoryPosition(row, null)
expect(row).toMatchObject({ b_depth: 0, b_path: '/a/' })
})
it('子节点继承父级路径并递增层级', () => {
const row = { b_id: 'a1' }
applyCategoryPosition(row, { b_depth: 2, b_path: '/a/b/' })
vm.applyCategoryPosition(row, { b_depth: 2, b_path: '/a/b/' })
expect(row).toMatchObject({ b_depth: 3, b_path: '/a/b/a1/' })
})
})
@@ -82,10 +94,10 @@ describe('othercompany 分类树工具:排序号与子分类判定', () => {
category({ b_id: 'a1', b_parent_id: 'a', b_xh: 90 }),
]
expect(nextSiblingXh(rows, null)).toBe(40)
expect(nextSiblingXh(rows, 'a')).toBe(100)
expect(nextSiblingXh(rows, 'not-exist')).toBe(10)
expect(nextSiblingXh([], null)).toBe(10)
expect(vm.nextSiblingXh(rows, null)).toBe(40)
expect(vm.nextSiblingXh(rows, 'a')).toBe(100)
expect(vm.nextSiblingXh(rows, 'not-exist')).toBe(10)
expect(vm.nextSiblingXh([], null)).toBe(10)
})
it('hasChildCategory 只看直接子分类', () => {
@@ -95,9 +107,9 @@ describe('othercompany 分类树工具:排序号与子分类判定', () => {
category({ b_id: 'a1x', b_parent_id: 'a1' }),
]
expect(hasChildCategory(rows, 'a')).toBe(true)
expect(hasChildCategory(rows, 'a1')).toBe(true)
expect(hasChildCategory(rows, 'a1x')).toBe(false)
expect(vm.hasChildCategory(rows, 'a')).toBe(true)
expect(vm.hasChildCategory(rows, 'a1')).toBe(true)
expect(vm.hasChildCategory(rows, 'a1x')).toBe(false)
})
})
@@ -107,7 +119,7 @@ describe('othercompany 分类树工具:重排与重建位置', () => {
const b = { b_id: 'b', b_xh: 10 }
const updated = []
renumberSiblings([a, b], updated)
vm.renumberSiblings([a, b], updated)
expect([a.b_xh, b.b_xh]).toEqual([10, 20])
expect(updated).toEqual([
@@ -127,7 +139,7 @@ describe('othercompany 分类树工具:重排与重建位置', () => {
]
const updated = []
rebuildTreePositions(tree, updated)
vm.rebuildTreePositions(tree, updated)
expect(tree[0].children[0]).toMatchObject({ b_depth: 1, b_path: '/a/a1/' })
// 根节点本来就是 0 / '/a/',不该产生一条无意义的 update
@@ -136,9 +148,9 @@ describe('othercompany 分类树工具:重排与重建位置', () => {
it('addRowUpdate 按 b_id 合并字段,避免同节点多条 update', () => {
const updated = []
addRowUpdate(updated, { b_id: 'a', b_xh: 20 })
addRowUpdate(updated, { b_id: 'a', b_parent_id: 'p' })
addRowUpdate(updated, { b_id: 'b', b_xh: 10 })
vm.addRowUpdate(updated, { b_id: 'a', b_xh: 20 })
vm.addRowUpdate(updated, { b_id: 'a', b_parent_id: 'p' })
vm.addRowUpdate(updated, { b_id: 'b', b_xh: 10 })
expect(updated).toEqual([
{ b_id: 'a', b_xh: 20, b_parent_id: 'p' },
@@ -149,16 +161,16 @@ describe('othercompany 分类树工具:重排与重建位置', () => {
it('findNodePosition 返回所在父数组与索引', () => {
const tree = [{ b_id: 'a', children: [{ b_id: 'a1' }, { b_id: 'a2' }] }]
expect(findNodePosition(tree, 'a')).toMatchObject({ index: 0 })
const found = findNodePosition(tree, 'a2')
expect(vm.findNodePosition(tree, 'a')).toMatchObject({ index: 0 })
const found = vm.findNodePosition(tree, 'a2')
expect(found.index).toBe(1)
expect(found.list.map((node) => node.b_id)).toEqual(['a1', 'a2'])
expect(findNodePosition(tree, 'not-exist')).toBe(null)
expect(vm.findNodePosition(tree, 'not-exist')).toBe(null)
})
})
describe('othercompany 分类树工具:虚拟根键', () => {
it('与 FmsTree 的 rootKey 约定保持一致', () => {
expect(ALL_CATEGORY_KEY).toBe('__fms_tree_root__')
expect(vm.ALL_CATEGORY_KEY).toBe('__fms_tree_root__')
})
})