7299166e96
范围:src 里原生 <select> 由 36 处清零(含全站共享分页条),15 个页面 + 1 个组件。
换法统一:class="admin-filter"(main.css 皮肤 42px/9px/#f8fbfd/focus ring)+
aria-label 定位 + el-option 显式 label;「全部 XX」空值行与 value 形状逐字保留
('' / null / String(id) / '1'/'0' / `${source}|${deviceId}` 等,下游查询参数与后端枚举不动)。
- OldPagination:每页条数下拉换 el-select(局部 import,挂载测试单 mount 也能渲染),壳压回分页条 30px/8px 档
- account 三页(菜单类型/密钥状态/分组)+ GroupsPage/UsersPage 清死 select 规则
- asin 三页(国家/弹窗分组):弹窗分组用 placeholder+clearable+:value-on-clear 钉回 null,守 number|null 契约
- patent 页(国家/分组)
- records 两页(来源/用户/终端覆盖):终端覆盖 value 仍 source|deviceId
- tasks 四页 + 任务看板(业务/状态/类型/来源/店铺/分组/场景):BrandGating/DuplicateCheck/KeywordUsage 三页本地是 34px 紧凑档,
故把 el-select 壳压回本地 34px/7px 与同行输入框齐高(否则高 8px 错位)
测试:mount 全量 21 文件 164 passed;node --test 1924 例 1922 passed(2 红为在册:
date-range-order 环境红、company-config-entry-super-only 由 ee613db0 引入与本次无关);
vue-tsc rc=0。新增 account-select-el / records-select-el 两支换壳契约(值形状 + 空值行 + 点开→点选项)。
205 lines
6.5 KiB
TypeScript
205 lines
6.5 KiB
TypeScript
/**
|
||
* 切公司/快速改筛选时,旧响应不得覆盖新结果(CR-27)。
|
||
*
|
||
* 跑真组件 + 真 pinia store,API 用可控 deferred 顶掉:
|
||
* 先让新请求落地,再让旧请求后返回,断言页面仍显示新结果。
|
||
*
|
||
* 跑法:`npx vitest run tests/mount/stale-response-drop.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 { ElOption, ElSelect } from 'element-plus'
|
||
|
||
import KeywordUsagePage from '@/pages/tasks/KeywordUsagePage.vue'
|
||
import BrandGatingPanel from '@/pages/tasks/BrandGatingPanel.vue'
|
||
import { useAdminSessionStore } from '@/stores/admin-session'
|
||
import { useCompanyTabStore } from '@/stores/company-tab'
|
||
|
||
const keywordApi = vi.hoisted(() => ({
|
||
fetchKeywordUsage: vi.fn(),
|
||
fetchKeywordUsageDeletes: vi.fn(),
|
||
fetchKeywordUsageOverview: vi.fn(),
|
||
}))
|
||
const gatingApi = vi.hoisted(() => ({
|
||
fetchBrandGatingPage: vi.fn(),
|
||
fetchBrandGatingStats: vi.fn(),
|
||
deleteBrandGating: vi.fn(),
|
||
importBrandGatingText: vi.fn(),
|
||
submitBrandGating: vi.fn(),
|
||
updateBrandGatingStatus: vi.fn(),
|
||
}))
|
||
|
||
vi.mock('@/pages/tasks/keyword-usage-api.ts', () => keywordApi)
|
||
vi.mock('@/pages/tasks/brand-gating-api.ts', () => gatingApi)
|
||
|
||
interface Deferred<T> {
|
||
promise: Promise<T>
|
||
resolve: (value: T) => void
|
||
}
|
||
|
||
function deferred<T>(): Deferred<T> {
|
||
let resolve!: (value: T) => void
|
||
const promise = new Promise<T>((done) => {
|
||
resolve = done
|
||
})
|
||
return { promise, resolve }
|
||
}
|
||
|
||
const loadingDirective = { mounted() {}, updated() {} }
|
||
|
||
function mountPage(component: Parameters<typeof mount>[0], pinia: Pinia): VueWrapper {
|
||
return mount(component, {
|
||
global: {
|
||
// 筛选下拉 2026-11-17 换成 el-select(原生 select 弹层是直角原生物,与亚马逊页不统一),
|
||
// 两支页面都要把 EP 组件注册进来,否则渲染成未解析标签。
|
||
plugins: [pinia, ElSelect, ElOption],
|
||
directives: { loading: loadingDirective },
|
||
},
|
||
})
|
||
}
|
||
|
||
function usagePage(keyword: string) {
|
||
return {
|
||
total: 1,
|
||
rows: [{
|
||
userName: 'alice',
|
||
country: '英国',
|
||
keyword,
|
||
scene: '复制关键词',
|
||
time: '2026-10-25 10:00:00',
|
||
}],
|
||
counts: { copy: 1, copyExisting: 1, copyDeleted: 0, add: 0 },
|
||
}
|
||
}
|
||
|
||
function gatingPage(brand: string) {
|
||
return {
|
||
total: 1,
|
||
rows: [{
|
||
id: 1,
|
||
brand,
|
||
asin: '',
|
||
country: '英国',
|
||
gatingType: '品牌gating',
|
||
status: 1,
|
||
source: 'MANUAL',
|
||
shopName: '',
|
||
remark: '',
|
||
createdAt: '2026-10-25 10:00:00',
|
||
updatedAt: '',
|
||
disabledAt: '',
|
||
}],
|
||
}
|
||
}
|
||
|
||
describe('CR-27:切换期间晚到的响应必须丢弃', () => {
|
||
let pinia: Pinia
|
||
|
||
beforeEach(() => {
|
||
vi.clearAllMocks()
|
||
pinia = createPinia()
|
||
setActivePinia(pinia)
|
||
useAdminSessionStore().user = { id: 1, username: 'root', role: 'super_admin' }
|
||
// 只有超管才带 company_id;company tab 的 activeId 直接改,不依赖公司目录接口
|
||
useCompanyTabStore().setActiveId(1)
|
||
keywordApi.fetchKeywordUsageOverview.mockResolvedValue({
|
||
stats: { used: 0, exist: 0, deletedKw: 0, added: 0, totalUse: 0, deletes: 0 },
|
||
users: [],
|
||
countries: [],
|
||
})
|
||
keywordApi.fetchKeywordUsageDeletes.mockResolvedValue({
|
||
total: 0,
|
||
rows: [],
|
||
counts: { copy: 0, copyExisting: 0, copyDeleted: 0, add: 0 },
|
||
})
|
||
gatingApi.fetchBrandGatingStats.mockResolvedValue({
|
||
total: 0,
|
||
active: 0,
|
||
disabled: 0,
|
||
brands: 0,
|
||
byType: [],
|
||
byCountry: [],
|
||
bySource: [],
|
||
})
|
||
})
|
||
|
||
it('关键词使用记录:切公司后旧公司响应后到,页面仍显示新公司数据', async () => {
|
||
const calls: Array<{
|
||
companyId?: number | null
|
||
pageSize?: number
|
||
deferred: Deferred<ReturnType<typeof usagePage>>
|
||
}> = []
|
||
keywordApi.fetchKeywordUsage.mockImplementation((params: { companyId?: number | null; pageSize?: number }) => {
|
||
const pending = deferred<ReturnType<typeof usagePage>>()
|
||
calls.push({ companyId: params.companyId, pageSize: params.pageSize, deferred: pending })
|
||
return pending.promise
|
||
})
|
||
|
||
const wrapper = mountPage(KeywordUsagePage, pinia)
|
||
await flushPromises()
|
||
expect(calls).toHaveLength(1)
|
||
expect(calls[0].companyId).toBe(1)
|
||
expect(calls[0].pageSize).toBe(10)
|
||
|
||
useCompanyTabStore().setActiveId(2)
|
||
await flushPromises()
|
||
expect(calls).toHaveLength(2)
|
||
expect(calls[1].companyId).toBe(2)
|
||
|
||
calls[1].deferred.resolve(usagePage('beta-keyword'))
|
||
await flushPromises()
|
||
expect(wrapper.text()).toContain('beta-keyword')
|
||
|
||
// 旧公司的响应晚到:序号闸必须把它丢掉
|
||
calls[0].deferred.resolve(usagePage('alpha-keyword'))
|
||
await flushPromises()
|
||
expect(wrapper.text()).toContain('beta-keyword')
|
||
expect(wrapper.text()).not.toContain('alpha-keyword')
|
||
})
|
||
|
||
it('品牌 Gating:快速改筛选时旧筛选响应后到,列表仍显示新筛选结果', async () => {
|
||
const calls: Array<{
|
||
keyword: string
|
||
deferred: Deferred<ReturnType<typeof gatingPage>>
|
||
}> = []
|
||
gatingApi.fetchBrandGatingPage.mockImplementation((filter: { keyword: string }) => {
|
||
const pending = deferred<ReturnType<typeof gatingPage>>()
|
||
calls.push({ keyword: filter.keyword, deferred: pending })
|
||
return pending.promise
|
||
})
|
||
|
||
const wrapper = mountPage(BrandGatingPanel, pinia)
|
||
await flushPromises()
|
||
expect(calls).toHaveLength(1)
|
||
calls[0].deferred.resolve(gatingPage('initial-brand'))
|
||
await flushPromises()
|
||
expect(wrapper.text()).toContain('initial-brand')
|
||
|
||
const input = wrapper.get('input[placeholder="搜索品牌 / ASIN"]')
|
||
const queryButton = wrapper.findAll('button').find((button) => button.text() === '查询')
|
||
expect(queryButton, '页面里应有「查询」按钮').toBeDefined()
|
||
|
||
await input.setValue('old')
|
||
await queryButton!.trigger('click')
|
||
await flushPromises()
|
||
expect(calls).toHaveLength(2)
|
||
expect(calls[1].keyword).toBe('old')
|
||
|
||
await input.setValue('new')
|
||
await queryButton!.trigger('click')
|
||
await flushPromises()
|
||
expect(calls).toHaveLength(3)
|
||
expect(calls[2].keyword).toBe('new')
|
||
|
||
calls[2].deferred.resolve(gatingPage('new-brand'))
|
||
await flushPromises()
|
||
expect(wrapper.text()).toContain('new-brand')
|
||
|
||
calls[1].deferred.resolve(gatingPage('old-brand'))
|
||
await flushPromises()
|
||
expect(wrapper.text()).toContain('new-brand')
|
||
expect(wrapper.text()).not.toContain('old-brand')
|
||
})
|
||
})
|