- 分公司类型模板:超管配置隐藏路由/菜单 + 中文文案覆盖,仅对挂类型的分公司生效(后端 service/routes、前端 Provider/守卫/配置页) - 超管模板 iframe 真还原预览:28 个业务页注册、白名单点选编辑、空/示例数据 fixture、白名单漂移检测工具 - 商品/SKU 下拉远程搜索:服务端关键词搜索 + 前端 useRemoteOptionSearch/SearchableSelect 接入(订单/采购/商品等页) - 主系统联调:子系统主动解绑主系统授权(幂等重试/本地兜底)、追加修改 price_amend 视图代理与 amend-price 转发 - 修复:仓内加工重复建单幂等(已完成需求单 guard、order 任务一商品一单 guard) - 修复:出入库记录只取前 100 条 → 服务端分页 + 真实总数 - 其它:库存/订单若干修复、分公司管理响应式布局、i18n 文案、设计文档与回归测试
155 lines
7.1 KiB
TypeScript
155 lines
7.1 KiB
TypeScript
// @vitest-environment jsdom
|
||
import { act, renderHook, waitFor } from '@testing-library/react';
|
||
import { describe, expect, it, vi } from 'vitest';
|
||
import { useRemoteOptionSearch, type RemoteOption } from './useRemoteOptionSearch';
|
||
|
||
/** T2:远程候选 hook —— 初始/防抖/过期响应/回显/canCreate/codes。 */
|
||
type Option = RemoteOption & { unit?: string | null };
|
||
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
||
|
||
function deferred<T>() {
|
||
let resolve!: (value: T) => void;
|
||
const promise = new Promise<T>((res) => {
|
||
resolve = res;
|
||
});
|
||
return { promise, resolve };
|
||
}
|
||
|
||
describe('useRemoteOptionSearch', () => {
|
||
it('初始只发一次请求(limit=20、q 空);输入防抖只发最后一次', async () => {
|
||
const fetcher = vi.fn(async (_params: { q: string; limit: number; codes: string[] }) => [{ value: 'A', label: 'A' }]);
|
||
const { result } = renderHook(() =>
|
||
useRemoteOptionSearch<Option>({ fetcher, initialLimit: 20, debounceMs: 10 })
|
||
);
|
||
await waitFor(() => expect(fetcher).toHaveBeenCalledTimes(1));
|
||
expect(fetcher.mock.calls[0][0]).toEqual({ q: '', limit: 20, codes: [] });
|
||
await waitFor(() => expect(result.current.options).toHaveLength(1));
|
||
|
||
act(() => result.current.setQuery('黄'));
|
||
act(() => result.current.setQuery('黄玉'));
|
||
act(() => result.current.setQuery('黄玉米'));
|
||
await sleep(25);
|
||
expect(fetcher).toHaveBeenCalledTimes(2);
|
||
expect(fetcher.mock.calls[1][0].q).toBe('黄玉米');
|
||
});
|
||
|
||
it('旧响应晚到不覆盖新结果(generation 守卫)', async () => {
|
||
const first = deferred<Option[]>();
|
||
const second = deferred<Option[]>();
|
||
const fetcher = vi
|
||
.fn<(params: { q: string; limit: number; codes: string[] }) => Promise<Option[]>>()
|
||
.mockImplementationOnce(() => first.promise)
|
||
.mockImplementationOnce(() => second.promise);
|
||
const { result } = renderHook(() => useRemoteOptionSearch<Option>({ fetcher, debounceMs: 10 }));
|
||
act(() => result.current.setQuery('旧'));
|
||
await sleep(25);
|
||
act(() => result.current.setQuery('新'));
|
||
await sleep(25);
|
||
await act(async () => {
|
||
second.resolve([{ value: 'NEW', label: '新商品' }]);
|
||
await sleep(0);
|
||
});
|
||
await waitFor(() => expect(result.current.options[0]?.value).toBe('NEW'));
|
||
await act(async () => {
|
||
first.resolve([{ value: 'OLD', label: '旧商品' }]);
|
||
await sleep(0);
|
||
});
|
||
expect(result.current.options[0]?.value).toBe('NEW');
|
||
});
|
||
|
||
it('已选不在结果时合成 selectedOption;在结果时直接用结果行', async () => {
|
||
const fetcher = vi.fn(async (_params: { q: string; limit: number; codes: string[] }) => [{ value: 'A', label: '商品A' }]);
|
||
const { result, rerender } = renderHook(
|
||
(props: { selectedValue?: string; selectedLabel?: string }) =>
|
||
useRemoteOptionSearch<Option>({
|
||
fetcher,
|
||
debounceMs: 10,
|
||
selectedValue: props.selectedValue,
|
||
selectedLabel: props.selectedLabel,
|
||
}),
|
||
{ initialProps: { selectedValue: 'X', selectedLabel: '旧商品X' } }
|
||
);
|
||
expect(result.current.selectedOption).toMatchObject({ value: 'X', label: '旧商品X' });
|
||
rerender({ selectedValue: 'A', selectedLabel: 'A' });
|
||
await waitFor(() => expect(result.current.options).toHaveLength(1));
|
||
expect(result.current.selectedOption).toMatchObject({ value: 'A', label: '商品A' });
|
||
});
|
||
|
||
it('canCreate 仅在「有关键词 + 非 loading + 无候选」时为 true', async () => {
|
||
const pending = deferred<Option[]>();
|
||
const fetcher = vi
|
||
.fn<(params: { q: string; limit: number; codes: string[] }) => Promise<Option[]>>()
|
||
.mockImplementationOnce(() => pending.promise)
|
||
.mockResolvedValue([{ value: 'A', label: 'A' }]);
|
||
const { result } = renderHook(() => useRemoteOptionSearch<Option>({ fetcher, debounceMs: 10 }));
|
||
expect(result.current.canCreate).toBe(false);
|
||
act(() => result.current.setQuery('不存在的商品'));
|
||
await sleep(25);
|
||
expect(result.current.loading).toBe(true);
|
||
expect(result.current.canCreate).toBe(false);
|
||
await act(async () => {
|
||
pending.resolve([]);
|
||
await sleep(0);
|
||
});
|
||
await waitFor(() => expect(result.current.loading).toBe(false));
|
||
expect(result.current.canCreate).toBe(true);
|
||
|
||
act(() => result.current.setQuery('有结果'));
|
||
await waitFor(() => expect(result.current.options).toHaveLength(1), { timeout: 2000 });
|
||
expect(result.current.canCreate).toBe(false);
|
||
});
|
||
|
||
it('T5:输入变化立即作废在途请求(防抖等待期旧响应也不落地)', async () => {
|
||
const first = deferred<Option[]>();
|
||
const fetcher = vi
|
||
.fn<(params: { q: string; limit: number; codes: string[] }) => Promise<Option[]>>()
|
||
.mockImplementationOnce(() => first.promise)
|
||
.mockResolvedValue([{ value: 'NEW', label: '新结果' }]);
|
||
const { result } = renderHook(() => useRemoteOptionSearch<Option>({ fetcher, debounceMs: 30 }));
|
||
await waitFor(() => expect(fetcher).toHaveBeenCalledTimes(1));
|
||
act(() => result.current.setQuery('新'));
|
||
// 防抖尚未到期(<30ms)时旧响应返回:必须被立即丢弃
|
||
await act(async () => {
|
||
first.resolve([{ value: 'STALE', label: '旧结果' }]);
|
||
await sleep(0);
|
||
});
|
||
expect(result.current.options).toHaveLength(0);
|
||
await waitFor(() => expect(result.current.options[0]?.value).toBe('NEW'));
|
||
});
|
||
|
||
it('T4:fetcher reject → error 有值、canCreate=false(接口报错绝不显示新建)', async () => {
|
||
const fetcher = vi.fn(async (_params: { q: string; limit: number; codes: string[] }) => {
|
||
throw new Error('网络错误:服务暂不可用(501)');
|
||
});
|
||
const { result } = renderHook(() => useRemoteOptionSearch<Option>({ fetcher, debounceMs: 10 }));
|
||
act(() => result.current.setQuery('黄玉米棒'));
|
||
await waitFor(() => expect(result.current.error).toBeTruthy());
|
||
await waitFor(() => expect(result.current.loading).toBe(false));
|
||
expect(result.current.error).toContain('501');
|
||
expect(result.current.options).toHaveLength(0);
|
||
expect(result.current.canCreate).toBe(false);
|
||
});
|
||
|
||
it('codes 回显:每次请求都附带已选 codes', async () => {
|
||
const fetcher = vi.fn(async (_params: { q: string; limit: number; codes: string[] }) => [] as Option[]);
|
||
renderHook(() =>
|
||
useRemoteOptionSearch<Option>({ fetcher, debounceMs: 10, selectedCodes: ['SKU-OLD-1', 'SKU-OLD-2'] })
|
||
);
|
||
await waitFor(() => expect(fetcher).toHaveBeenCalled());
|
||
expect(fetcher.mock.calls[0][0].codes).toEqual(['SKU-OLD-1', 'SKU-OLD-2']);
|
||
});
|
||
});
|
||
|
||
|
||
describe('T6-N7 防抖窗口 canCreate', () => {
|
||
it('setQuery 后立即 loading=true、canCreate=false(无需等请求启动)', async () => {
|
||
const fetcher = vi.fn(async (_p: { q: string; limit: number; codes: string[] }) => [] as Option[]);
|
||
const { result } = renderHook(() => useRemoteOptionSearch<Option>({ fetcher, debounceMs: 50 }));
|
||
await waitFor(() => expect(result.current.loading).toBe(false));
|
||
act(() => result.current.setQuery('黄玉米棒'));
|
||
// 防抖窗口内(尚未发起请求)
|
||
expect(result.current.loading).toBe(true);
|
||
expect(result.current.canCreate).toBe(false);
|
||
});
|
||
});
|