Files
workspace/code/fms/fms-vue/tests/unit/othercompany-utils.spec.js
T
2026-09-15 17:00:29 +08:00

75 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 往来单位分类树的页面级纯函数(仍内联在 views/base/othercompany/index.vue)。
*
* 建树、位置派生、排序号重排、整树重建等通用逻辑已收敛到 @/utils/tree,
* 其测试在 tests/unit/utils.spec.js;本文件只覆盖留在页面里的两个业务判定,
* 它们内联在页面中,因此通过挂载页面组件访问 <script setup> 的内部绑定。
*/
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 {
b_id: 'a',
b_parent_id: null,
b_depth: 0,
b_path: '/a/',
b_name: 'A',
b_xh: 10,
b_canuse: 1,
...over,
}
}
/** 页面实例(分类树纯函数与状态都在它的 setup 里) */
let vm
beforeEach(async () => {
const { wrapper } = await mountPage(Othercompany)
await flushPromises()
vm = wrapper.vm
})
describe('othercompany 分类树工具:排序号与子分类判定', () => {
it('同级最大 b_xh 加 10,无同级时取 10', () => {
const rows = [
category({ b_id: 'a', b_xh: 10 }),
category({ b_id: 'b', b_xh: 30 }),
category({ b_id: 'a1', b_parent_id: 'a', b_xh: 90 }),
]
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 只看直接子分类', () => {
const rows = [
category({ b_id: 'a' }),
category({ b_id: 'a1', b_parent_id: 'a' }),
category({ b_id: 'a1x', b_parent_id: 'a1' }),
]
expect(vm.hasChildCategory(rows, 'a')).toBe(true)
expect(vm.hasChildCategory(rows, 'a1')).toBe(true)
expect(vm.hasChildCategory(rows, 'a1x')).toBe(false)
})
})
describe('othercompany 分类树工具:虚拟根键', () => {
it('与 FmsTree 的 rootKey 约定保持一致', () => {
expect(vm.ALL_CATEGORY_KEY).toBe('__fms_tree_root__')
})
})