108 lines
4.2 KiB
TypeScript
108 lines
4.2 KiB
TypeScript
/**
|
||
* 用户管理页 × 平台 tab(V206)的挂载契约:
|
||
* 账号域三页两 tab 共用组件,列表请求必须带 platform=当前平台;切 tab 重取数;
|
||
* 「上架上限」列仅京东侧渲染(用户口径「不需要在亚马逊那边」),文案 0=不限/null=-。
|
||
* 不用 readFileSync(source-pin-ratchet 对台账外新文件带读点即红),全部走真实挂载断言。
|
||
*
|
||
* 跑法:`npx vitest run tests/mount/users-page-platform.mount.test.ts`
|
||
*/
|
||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||
import { flushPromises, mount } from '@vue/test-utils'
|
||
import { createPinia, setActivePinia, type Pinia } from 'pinia'
|
||
|
||
import UsersPage from '@/pages/account/UsersPage.vue'
|
||
import { usePlatformTabStore } from '@/stores/platform-tab'
|
||
import { useAdminSessionStore } from '@/stores/admin-session'
|
||
|
||
const usersApi = vi.hoisted(() => ({
|
||
fetchUserList: vi.fn(),
|
||
deleteUser: vi.fn(),
|
||
setUserDisabled: vi.fn(),
|
||
}))
|
||
|
||
vi.mock('@/api/users', () => usersApi)
|
||
vi.mock('@/api/companies', () => ({ fetchCompanies: vi.fn().mockResolvedValue([]) }))
|
||
vi.mock('@/pages/account/user-menu-auth-api', () => ({ loadUserMenuAuth: vi.fn() }))
|
||
|
||
function pageOf(total = 0) {
|
||
return {
|
||
items: [
|
||
{ id: 7, username: '京东号', role: 'normal', platform: 'jd' },
|
||
{ id: 8, username: '存量号', role: 'admin', jdPublishQuota: 0 },
|
||
{ id: 9, username: '老号', role: 'normal', jdPublishQuota: 800 },
|
||
{ id: 10, username: '没配号', role: 'normal' },
|
||
],
|
||
total,
|
||
page: 1,
|
||
pageSize: 10,
|
||
}
|
||
}
|
||
|
||
let pinia: Pinia
|
||
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
window.localStorage.removeItem('admin-platform-tab')
|
||
pinia = createPinia()
|
||
setActivePinia(pinia)
|
||
const session = useAdminSessionStore()
|
||
session.user = { id: 1, username: 'root', role: 'super_admin' }
|
||
usersApi.fetchUserList.mockResolvedValue(pageOf())
|
||
})
|
||
|
||
describe('UsersPage × 平台 tab(V206)', () => {
|
||
it('亚马逊 tab(默认):列表请求带 platform=amazon,无「上架上限」列', async () => {
|
||
const wrapper = mount(UsersPage)
|
||
await flushPromises()
|
||
expect(usersApi.fetchUserList).toHaveBeenCalledTimes(1)
|
||
const params = usersApi.fetchUserList.mock.calls[0][0] as Record<string, unknown>
|
||
expect(params.platform).toBe('amazon')
|
||
expect(params).toHaveProperty('companyId')
|
||
const headers = wrapper.findAll('thead th').map((th) => th.text())
|
||
expect(headers).not.toContain('上架上限')
|
||
// 亚马逊侧表头 9 列(存量表头,quota 列不渲染)
|
||
expect(headers.length).toBe(9)
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('切到京东 tab:重取数并带 platform=jd,「上架上限」列出现,文案 0=不限/null=-', async () => {
|
||
const wrapper = mount(UsersPage)
|
||
await flushPromises()
|
||
const platformTab = usePlatformTabStore()
|
||
platformTab.setActive('jd')
|
||
await flushPromises()
|
||
|
||
expect(usersApi.fetchUserList).toHaveBeenCalledTimes(2)
|
||
const params = usersApi.fetchUserList.mock.calls[1][0] as Record<string, unknown>
|
||
expect(params.platform).toBe('jd')
|
||
|
||
const headers = wrapper.findAll('thead th').map((th) => th.text())
|
||
expect(headers).toContain('上架上限')
|
||
expect(headers.length).toBe(10)
|
||
// quota 单元格:0=不限(存量号 8),null/缺省=-(京东号 7、没配号 10),800 原样
|
||
const quotaCells = wrapper.findAll('tbody tr').map((tr) => {
|
||
const tds = tr.findAll('td')
|
||
// 上架上限列在第 5 列(ID/用户名/角色/所属管理员/到期时间 之后)
|
||
return tds[5]?.text()
|
||
})
|
||
expect(quotaCells).toEqual(['-', '不限', '800', '-'])
|
||
wrapper.unmount()
|
||
})
|
||
|
||
it('切回亚马逊 tab:再次重取数且列消失(两 tab 各看各的数据面)', async () => {
|
||
const wrapper = mount(UsersPage)
|
||
await flushPromises()
|
||
const platformTab = usePlatformTabStore()
|
||
platformTab.setActive('jd')
|
||
await flushPromises()
|
||
platformTab.setActive('amazon')
|
||
await flushPromises()
|
||
|
||
expect(usersApi.fetchUserList).toHaveBeenCalledTimes(3)
|
||
const params = usersApi.fetchUserList.mock.calls[2][0] as Record<string, unknown>
|
||
expect(params.platform).toBe('amazon')
|
||
expect(wrapper.findAll('thead th').map((th) => th.text())).not.toContain('上架上限')
|
||
wrapper.unmount()
|
||
})
|
||
})
|