383 lines
20 KiB
TypeScript
383 lines
20 KiB
TypeScript
/**
|
||
* 京东分类管理对话框(JdCategoryManageDialog)的挂载契约。
|
||
*
|
||
* 2026-11-17 用户口径「京东分类管理并到京东数据库分类编辑里,做一个搜索和添加 删除
|
||
* 编辑 分类名称即可」:V204 独立子菜单页(JdCategoryManagerPage,已删)整体并入本对话框,
|
||
* 从京东数据库页工具栏「分类管理」打开。原页面契约逐条承接:当前数据权限分组的
|
||
* 分类建/改名/删除;新增名称搜索(本地过滤)。商品归类不在这里(去「批量编辑分类」)。
|
||
* 权限口径(V203)不变:editable 才渲染写控件;同组普通号只读可看。
|
||
*
|
||
* 跑法:`npx vitest run tests/mount/jd-category-manage-dialog.mount.test.ts`
|
||
*/
|
||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
|
||
import { createPinia, setActivePinia, type Pinia } from 'pinia'
|
||
|
||
import JdCategoryManageDialog from '@/pages/jd/JdCategoryManageDialog.vue'
|
||
import { useAdminSessionStore } from '@/stores/admin-session'
|
||
|
||
const jdApi = vi.hoisted(() => ({
|
||
fetchJdProductGroups: vi.fn(),
|
||
createJdProductGroup: vi.fn(),
|
||
updateJdProductGroup: vi.fn(),
|
||
deleteJdProductGroup: vi.fn(),
|
||
}))
|
||
|
||
const groupApi = vi.hoisted(() => ({
|
||
createShopGroup: vi.fn(),
|
||
}))
|
||
|
||
const confirmMock = vi.hoisted(() => vi.fn())
|
||
|
||
vi.mock('@/pages/jd/jd-product-api.ts', () => jdApi)
|
||
vi.mock('@/pages/account/shop-group-api.ts', () => groupApi)
|
||
vi.mock('@/components/admin-confirm', () => ({ openAdminConfirm: confirmMock }))
|
||
|
||
/** 分组 id 常量:默认作用域就这一个可见且可写的组(组长档形状)。 */
|
||
const GROUP_A = 11
|
||
|
||
function group(id: number, name: string, productCount = 0, sortOrder = 0) {
|
||
return { id, name, ownerGroupId: GROUP_A, sortOrder, productCount }
|
||
}
|
||
|
||
let pinia: Pinia
|
||
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
pinia = createPinia()
|
||
setActivePinia(pinia)
|
||
// 空态直建分组(2026-11-17 用户口径「admin 权限最大」)需要当前登录人做组长。
|
||
useAdminSessionStore().user = { id: 9, username: 'admin', role: 'super_admin' } as never
|
||
jdApi.fetchJdProductGroups.mockResolvedValue([])
|
||
groupApi.createShopGroup.mockResolvedValue({ id: 21, name: '京东-团队', ownerGroupId: 21, sortOrder: 0, productCount: 0 })
|
||
confirmMock.mockResolvedValue(true)
|
||
})
|
||
|
||
async function mountDialog(editable = true): Promise<VueWrapper<Element>> {
|
||
const wrapper = mount(JdCategoryManageDialog, {
|
||
global: { plugins: [pinia] },
|
||
props: {
|
||
visible: true,
|
||
ownerGroupId: GROUP_A,
|
||
ownerGroupName: '甲组',
|
||
editable,
|
||
},
|
||
})
|
||
await flushPromises()
|
||
await flushPromises()
|
||
return wrapper
|
||
}
|
||
|
||
it('批量删除分类:全选当前页、确认只移除分类、刷新一次;只读模式无入口', async () => {
|
||
jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3), group(8, '裤装', 1)])
|
||
jdApi.deleteJdProductGroup.mockResolvedValue(undefined)
|
||
const wrapper = await mountDialog()
|
||
await wrapper.get('.option-head input[type="checkbox"]').setValue(true)
|
||
await wrapper.get('.batch-delete-actions .btn-danger').trigger('click')
|
||
await flushPromises()
|
||
expect(confirmMock).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining('商品本身不会被删除') }))
|
||
expect(jdApi.deleteJdProductGroup.mock.calls).toEqual([[7], [8]])
|
||
expect(jdApi.fetchJdProductGroups).toHaveBeenCalledTimes(2)
|
||
expect(wrapper.emitted('changed')).toHaveLength(1)
|
||
expect(wrapper.get('.batch-delete-actions .btn-danger').text()).toBe('批量删除(0)')
|
||
await wrapper.setProps({ editable: false })
|
||
expect(wrapper.find('.batch-delete-actions').exists()).toBe(false)
|
||
expect(wrapper.find('.batch-delete-checkbox').exists()).toBe(false)
|
||
wrapper.unmount()
|
||
})
|
||
|
||
describe('京东分类管理对话框(V204 独立页 2026-11-17 并入京东数据库页)', () => {
|
||
it('打开即按当前分组取分类(ownerGroupId 必带);标题不带分组名(2026-11-17 用户口径「不要显示周丽娥」)', async () => {
|
||
jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)])
|
||
const wrapper = await mountDialog()
|
||
expect(jdApi.fetchJdProductGroups).toHaveBeenCalledWith(GROUP_A)
|
||
expect(wrapper.get('.manage-modal').text()).toContain('分类管理')
|
||
expect(wrapper.find('.modal-sub').exists()).toBe(false, '标题后缀(· 分组名)已删,不得长回来')
|
||
expect(wrapper.get('.manage-modal').text()).not.toContain('甲组', '分组名不进标题')
|
||
expect(wrapper.findAll('.option-row')).toHaveLength(1)
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('新建分类:落当前分组归属(ownerGroupId 必带),成功后清输入重取并通知父页刷新', async () => {
|
||
jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)])
|
||
const wrapper = await mountDialog()
|
||
await wrapper.get('.group-create input').setValue('上新区')
|
||
await wrapper.findAll('button').find((b) => b.text() === '新建')!.trigger('click')
|
||
await flushPromises()
|
||
expect(jdApi.createJdProductGroup).toHaveBeenCalledWith({ name: '上新区', sortOrder: 0, ownerGroupId: GROUP_A })
|
||
expect((wrapper.get('.group-create input').element as HTMLInputElement).value).toBe('')
|
||
expect(jdApi.fetchJdProductGroups).toHaveBeenCalledTimes(2, '成功后重取分类列表')
|
||
expect(wrapper.emitted('changed'), '写成功必须 emit changed 让父页刷筛选下拉').toBeTruthy()
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('搜索:按名称包含本地过滤(不发额外请求),命中后新建会清关键字', async () => {
|
||
jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3), group(8, '裤装', 1), group(9, '配饰', 0)])
|
||
const wrapper = await mountDialog()
|
||
const callsBefore = jdApi.fetchJdProductGroups.mock.calls.length
|
||
await wrapper.get('.search-input').setValue('裙')
|
||
await flushPromises()
|
||
expect(wrapper.findAll('.option-row')).toHaveLength(1, '「裙」只命中裙装')
|
||
expect(wrapper.findAll('.option-row')[0].text()).toContain('裙装')
|
||
expect(jdApi.fetchJdProductGroups.mock.calls.length).toBe(callsBefore, '搜索是本地过滤,不另发请求')
|
||
expect(wrapper.text()).not.toContain('裤装', '不匹配项不出现')
|
||
|
||
await wrapper.get('.group-create input').setValue('上新区')
|
||
await wrapper.findAll('button').find((b) => b.text() === '新建')!.trigger('click')
|
||
await flushPromises()
|
||
expect((wrapper.get('.search-input').element as HTMLInputElement).value).toBe('',
|
||
'新建成功清关键字(新分类不被旧搜索藏住)')
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('搜索无命中给显式空态文案(不是空列表静默)', async () => {
|
||
jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)])
|
||
const wrapper = await mountDialog()
|
||
await wrapper.get('.search-input').setValue('不存在的分类')
|
||
expect(wrapper.get('.manage-empty').text()).toBe('没有匹配的分类')
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('分页:每页 6 条、翻页只换当前页、改搜索回第一页(2026-11-17 用户口径「分类这也要加分页」)', async () => {
|
||
jdApi.fetchJdProductGroups.mockResolvedValue(
|
||
Array.from({ length: 11 }, (_, index) => group(100 + index, `分类${index + 1}`)),
|
||
)
|
||
const wrapper = await mountDialog()
|
||
expect(wrapper.findAll('.option-row')).toHaveLength(6, '第一页只渲染 6 行')
|
||
expect(wrapper.text()).not.toContain('分类7', '第 7 条以后不在第一页')
|
||
const pagerText = wrapper.get('.manage-pager').text()
|
||
expect(pagerText).toContain('第 1 / 2 页')
|
||
expect(pagerText).toContain('共 11 个分类', '总数取搜索命中集,不是当前页行数')
|
||
|
||
await wrapper.findAll('.manage-pager button').find((b) => b.text() === '下一页')!.trigger('click')
|
||
await flushPromises()
|
||
expect(wrapper.findAll('.option-row')).toHaveLength(5, '第二页剩 5 行')
|
||
expect(wrapper.text()).toContain('分类11')
|
||
expect(wrapper.get('.manage-pager').text()).toContain('第 2 / 2 页')
|
||
|
||
await wrapper.get('.search-input').setValue('分类1')
|
||
await flushPromises()
|
||
expect(wrapper.find('.manage-pager').exists()).toBe(false, '命中 3 条不足一页 ⇒ 不渲染页码条')
|
||
expect(wrapper.findAll('.option-row')).toHaveLength(3, '搜索回第一页(不是停在旧页码的空白处)')
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('分页:一屏装得下就不出页码条(不白占一行)', async () => {
|
||
jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3), group(8, '裤装', 1)])
|
||
const wrapper = await mountDialog()
|
||
expect(wrapper.findAll('.option-row')).toHaveLength(2)
|
||
expect(wrapper.find('.manage-pager').exists()).toBe(false)
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('搜索与新建行同属 manage-bar 的一排(搜索在左、新建在右,2026-11-17 用户口径「弄成一排呗」)', async () => {
|
||
jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)])
|
||
const wrapper = await mountDialog()
|
||
const bar = wrapper.get('.manage-bar')
|
||
const children = Array.from(bar.element.children)
|
||
expect(children).toHaveLength(2, '一排只有两件:搜索框、新建行(新建行内=输入框+新建按钮)')
|
||
expect(children[0].classList.contains('search-input')).toBe(true, '搜索框在左')
|
||
expect(children[1].classList.contains('group-create')).toBe(true, '新建行在右')
|
||
expect(children[1].children).toHaveLength(2, '新建行只有输入框+按钮(排序输入 11-15 已下线,不得长出第三个框)')
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('改名:行内编辑后按 (id, {name, sortOrder}) 提交并 emit changed', async () => {
|
||
jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)])
|
||
const wrapper = await mountDialog()
|
||
await wrapper.findAll('button').find((b) => b.text() === '改名')!.trigger('click')
|
||
await wrapper.get('.rename-input').setValue('长裙')
|
||
await wrapper.findAll('button').find((b) => b.text() === '保存')!.trigger('click')
|
||
await flushPromises()
|
||
expect(jdApi.updateJdProductGroup).toHaveBeenCalledWith(7, { name: '长裙', sortOrder: 0 })
|
||
expect(wrapper.emitted('changed')).toBeTruthy()
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('删除:二次确认文案点名商品数(删除不删商品,只解挂归属),确认后调端点重取', async () => {
|
||
jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)])
|
||
const wrapper = await mountDialog()
|
||
await wrapper.findAll('button').find((b) => b.text() === '删除')!.trigger('click')
|
||
await flushPromises()
|
||
expect(confirmMock).toHaveBeenCalledTimes(1)
|
||
const options = confirmMock.mock.calls[0][0]
|
||
expect(options.title).toBe('删除分类')
|
||
expect(options.message).toContain('确定删除分类「裙装」吗')
|
||
expect(options.message).toContain('3 个商品回到未分类', '文案点名该分类下的商品数')
|
||
expect(options.danger).toBe(true)
|
||
expect(jdApi.deleteJdProductGroup).toHaveBeenCalledWith(7)
|
||
expect(jdApi.fetchJdProductGroups).toHaveBeenCalledTimes(2, '删除成功后重取列表')
|
||
expect(wrapper.emitted('changed')).toBeTruthy()
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('删除确认点取消就不发请求(防误删第一道闸)', async () => {
|
||
confirmMock.mockResolvedValue(false)
|
||
jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)])
|
||
const wrapper = await mountDialog()
|
||
await wrapper.findAll('button').find((b) => b.text() === '删除')!.trigger('click')
|
||
await flushPromises()
|
||
expect(confirmMock).toHaveBeenCalledTimes(1)
|
||
expect(jdApi.deleteJdProductGroup).not.toHaveBeenCalled()
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('只读档(editable=false):无新建行、无搜索仍可用、无改名/删除按钮', async () => {
|
||
const wrapper = await mountDialog(false)
|
||
expect(wrapper.find('.group-create').exists()).toBe(false, '只读账号不渲染新建行')
|
||
expect(wrapper.findAll('button').some((b) => b.text() === '改名')).toBe(false)
|
||
expect(wrapper.findAll('button').some((b) => b.text() === '删除')).toBe(false)
|
||
expect(wrapper.get('.search-input').exists()).toBe(true, '搜索是读面,只读档保留')
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('不属于任何数据权限分组(ownerGroupId<=0):不发取数请求,空态直说', async () => {
|
||
const wrapper = mount(JdCategoryManageDialog, {
|
||
global: { plugins: [pinia] },
|
||
props: { visible: true, ownerGroupId: 0, ownerGroupName: '', editable: false },
|
||
})
|
||
await flushPromises()
|
||
await flushPromises()
|
||
expect(jdApi.fetchJdProductGroups).not.toHaveBeenCalled()
|
||
expect(wrapper.get('.manage-empty').text()).toBe('你还不属于任何数据权限分组')
|
||
expect(wrapper.find('.group-create').exists()).toBe(false)
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('本组暂无分类:可写档文案引导新建,只读档直说', async () => {
|
||
const writable = await mountDialog(true)
|
||
expect(writable.get('.manage-empty').text()).toBe('本组暂无分类,先新建一个')
|
||
writable.unmount()
|
||
|
||
const readonly = await mountDialog(false)
|
||
expect(readonly.get('.manage-empty').text()).toBe('本组暂无分类')
|
||
readonly.unmount()
|
||
})
|
||
|
||
it('加载失败:红条可见且重试真能重跑(取数页硬规矩)', async () => {
|
||
jdApi.fetchJdProductGroups.mockRejectedValueOnce(new Error('上游 502'))
|
||
const wrapper = mount(JdCategoryManageDialog, {
|
||
global: { plugins: [pinia] },
|
||
props: { visible: true, ownerGroupId: GROUP_A, ownerGroupName: '甲组', editable: true },
|
||
})
|
||
await flushPromises()
|
||
await flushPromises()
|
||
const bar = wrapper.find('.load-failure-bar')
|
||
expect(bar.exists()).toBe(true, '失败态必须可见(不是一闪而过的 toast)')
|
||
expect(bar.text()).toContain('分类列表加载失败')
|
||
expect(bar.text()).toContain('上游 502', '后端/网络原因要进文案')
|
||
jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)])
|
||
await bar.find('button').trigger('click')
|
||
await flushPromises()
|
||
await flushPromises()
|
||
expect(wrapper.find('.load-failure-bar').exists()).toBe(false, '重试成功后红条消失')
|
||
expect(wrapper.text()).toContain('裙装')
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('页面职责边界(反向钉):归类不在本对话框——不出现批量归类/删除勾选入口', async () => {
|
||
jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)])
|
||
const wrapper = await mountDialog()
|
||
expect(wrapper.findAll('button').some((b) => b.text() === '批量编辑分类')).toBe(false, '归类入口在京东数据库页')
|
||
expect(wrapper.text()).not.toContain('批量编辑分类',
|
||
'指路小字已随 2026-11-17 用户口径「小字不需要」删除,弹窗里不该再出现这句')
|
||
expect(jdApi.deleteJdProductGroup).not.toHaveBeenCalled()
|
||
wrapper.unmount()
|
||
})
|
||
})
|
||
|
||
/** 平台超管空态直建分组(2026-11-17 用户口径「admin 权限最大」):京东侧还没有任何
|
||
* 数据权限分组时(平台闸上线后 jd 档组全空,B117-02 前置未做),平台超管在弹窗里
|
||
* 直接建第一个京东档分组,不再死在「你还不属于任何数据权限分组」。 */
|
||
describe('平台超管空态直建京东档分组', () => {
|
||
function mountEmpty(platformSuperAdmin: boolean): VueWrapper<Element> {
|
||
return mount(JdCategoryManageDialog, {
|
||
global: { plugins: [pinia] },
|
||
props: { visible: true, ownerGroupId: 0, ownerGroupName: '', editable: false, platformSuperAdmin },
|
||
})
|
||
}
|
||
|
||
it('平台超管空态:出直建表单(不是死路文案),提交走分组管理同一条 POST 且 platform 恒 jd、组长=当前登录人', async () => {
|
||
const wrapper = mountEmpty(true)
|
||
await flushPromises()
|
||
await flushPromises()
|
||
expect(jdApi.fetchJdProductGroups).not.toHaveBeenCalled()
|
||
expect(wrapper.get('.manage-empty').find('[aria-label="新分组名称"]').exists()).toBe(true,
|
||
'空态直建表单就在空态区里(引导语小字已删,表单本身还在)')
|
||
|
||
const input = wrapper.get('[aria-label="新分组名称"]')
|
||
await input.setValue('京东-团队')
|
||
await wrapper.findAll('button').find((b) => b.text() === '新建分组')!.trigger('click')
|
||
await flushPromises()
|
||
await flushPromises()
|
||
expect(groupApi.createShopGroup).toHaveBeenCalledTimes(1)
|
||
expect(groupApi.createShopGroup).toHaveBeenCalledWith('京东-团队', [], 9, 'jd')
|
||
expect(wrapper.emitted('group-created'), '建组成功即通知父页重取作用域').toBeTruthy()
|
||
expect((input.element as HTMLInputElement).value).toBe('', '成功后清输入')
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('没有当前登录人就不发请求并给可见错误(组长缺了这单没法建)', async () => {
|
||
useAdminSessionStore().user = null
|
||
const wrapper = mountEmpty(true)
|
||
await flushPromises()
|
||
await wrapper.get('[aria-label="新分组名称"]').setValue('京东-团队')
|
||
await wrapper.findAll('button').find((b) => b.text() === '新建分组')!.trigger('click')
|
||
await flushPromises()
|
||
expect(groupApi.createShopGroup).not.toHaveBeenCalled()
|
||
expect(wrapper.text()).toContain('无法获取当前登录用户')
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('建组失败:错误进红条、不发 group-created(父页作用域不动)', async () => {
|
||
groupApi.createShopGroup.mockRejectedValueOnce(new Error('分组名称已存在'))
|
||
const wrapper = mountEmpty(true)
|
||
await flushPromises()
|
||
await wrapper.get('[aria-label="新分组名称"]').setValue('京东-团队')
|
||
await wrapper.findAll('button').find((b) => b.text() === '新建分组')!.trigger('click')
|
||
await flushPromises()
|
||
await flushPromises()
|
||
expect(wrapper.text()).toContain('分组名称已存在')
|
||
expect(wrapper.emitted('group-created')).toBeFalsy()
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('父页把 ownerGroupId 翻到新组后:直建表单消失、按新组重取分类(不用关弹窗重开)', async () => {
|
||
const wrapper = mountEmpty(true)
|
||
await flushPromises()
|
||
await wrapper.get('[aria-label="新分组名称"]').setValue('京东-团队')
|
||
await wrapper.findAll('button').find((b) => b.text() === '新建分组')!.trigger('click')
|
||
await flushPromises()
|
||
const emittedBefore = wrapper.emitted('group-created')?.length ?? 0
|
||
await wrapper.setProps({ ownerGroupId: 21, ownerGroupName: '京东-团队', editable: true })
|
||
await flushPromises()
|
||
await flushPromises()
|
||
expect(jdApi.fetchJdProductGroups).toHaveBeenLastCalledWith(21)
|
||
expect(jdApi.fetchJdProductGroups).toHaveBeenCalledWith(21)
|
||
expect(wrapper.find('[aria-label="新分组名称"]').exists()).toBe(false, '有组了直建口子就关')
|
||
expect(wrapper.emitted('group-created')?.length).toBe(emittedBefore, '翻组重取不再重复通知父页')
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('非平台超管的空态维持原状:死路文案照旧、无直建表单(口子不开给组长/公司超管)', async () => {
|
||
const wrapper = mountEmpty(false)
|
||
await flushPromises()
|
||
expect(wrapper.get('.manage-empty').text()).toBe('你还不属于任何数据权限分组')
|
||
expect(wrapper.find('[aria-label="新分组名称"]').exists()).toBe(false)
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('平台超管但已有分组(ownerGroupId>0):直建口子不出现(完整分组管理归「分组管理」页)', async () => {
|
||
jdApi.fetchJdProductGroups.mockResolvedValue([group(7, '裙装', 3)])
|
||
const wrapper = mount(JdCategoryManageDialog, {
|
||
global: { plugins: [pinia] },
|
||
props: { visible: true, ownerGroupId: GROUP_A, ownerGroupName: '甲组', editable: true, platformSuperAdmin: true },
|
||
})
|
||
await flushPromises()
|
||
await flushPromises()
|
||
expect(wrapper.find('[aria-label="新分组名称"]').exists()).toBe(false)
|
||
expect(wrapper.find('.group-bootstrap').exists()).toBe(false, '有组时直建表单整体不渲染')
|
||
wrapper.unmount()
|
||
})
|
||
})
|