补齐请求与批量任务过期响应测试
This commit is contained in:
1 parent
6b5215ddf3
commit
a63292daa2
5 files changed
+91
No files matched your search
@@ -195,6 +195,15 @@ describe('useAccountSubmodules', /* 当前回调处理账号编辑、AI、通知
|
||||
expect(passwordLoginMock).toHaveBeenCalledWith(expect.objectContaining({ account_id: 'account-1', account: '[email protected]' }), expect.anything());
|
||||
expect(hook.result.current.passwordLoginView.status).toBe('success');
|
||||
expect(loadAccounts).toHaveBeenCalled();
|
||||
// passwordUpdater 是密码登录成功后清空密码字段的函数式更新器。
|
||||
let passwordUpdater: ((current: AccountEditForm) => AccountEditForm) | undefined;
|
||||
for (const call /* call 是密码表单状态替身记录的一次调用参数。 */ of setEditForm.mock.calls) {
|
||||
if (typeof call[0] === 'function') {
|
||||
passwordUpdater = call[0] as (current: AccountEditForm) => AccountEditForm;
|
||||
break;
|
||||
}
|
||||
}
|
||||
expect(passwordUpdater?.(editFormFixture)).toMatchObject({ login_password: '', showLoginPassword: false });
|
||||
|
||||
await act(
|
||||
// cancelAction 取消当前密码登录会话并回到空闲状态。
|
||||
@@ -339,6 +348,7 @@ describe('useAccountSubmodules', /* 当前回调处理账号编辑、AI、通知
|
||||
// secondProcessingAction 再次进入处理中状态以验证关闭时取消会话。
|
||||
async () => hook.result.current.handlePasswordLogin(),
|
||||
);
|
||||
cancelPasswordMock.mockRejectedValueOnce(new Error('取消失败'));
|
||||
await act(
|
||||
// closeAction 关闭编辑弹窗并取消处理中会话。
|
||||
async () => hook.result.current.closeEditModal(),
|
||||
|
||||
@@ -401,4 +401,44 @@ describe('useItemPublishBatch', /* 当前回调处理批量发布的表单、任
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
test('批量轮询避免并发请求并丢弃关闭弹窗后的旧响应', /* 当前回调验证批量轮询并发与代次隔离。 */ async () => {
|
||||
vi.useFakeTimers();
|
||||
// resolvePoll 是延迟轮询请求的完成控制器。
|
||||
let resolvePoll: (value: ItemPublishBatchResponse) => void = () => undefined;
|
||||
// pendingPoll 是保持轮询进行中的请求 Promise。
|
||||
const pendingPoll = new Promise<ItemPublishBatchResponse>(/* pollExecutor 保存轮询完成函数。 */ resolve => { resolvePoll = resolve; });
|
||||
getBatchMock.mockReset();
|
||||
getBatchMock.mockReturnValueOnce(pendingPoll);
|
||||
// hook 是批量轮询并发场景的 Hook 渲染结果。
|
||||
const hook = renderHook(/* stalePollHookFactory 创建批量轮询并发场景的 Hook。 */ () => useItemPublishBatch({ selectedAccount: 'account-1', loadItems: vi.fn(), loadShippingRules: vi.fn() }));
|
||||
await act(
|
||||
// stateAction 打开弹窗并注入运行中的任务详情。
|
||||
() => {
|
||||
hook.result.current.setShowBatchModal(true);
|
||||
hook.result.current.setBatchDetail(runningBatchFixture);
|
||||
},
|
||||
);
|
||||
await act(
|
||||
// firstTimerAction 推进首次轮询并保持请求未完成。
|
||||
async () => { await vi.advanceTimersByTimeAsync(3_000); },
|
||||
);
|
||||
await act(
|
||||
// secondTimerAction 推进下一次轮询但不允许并发请求。
|
||||
async () => { await vi.advanceTimersByTimeAsync(3_000); },
|
||||
);
|
||||
expect(getBatchMock).toHaveBeenCalledOnce();
|
||||
await act(
|
||||
// closeAction 关闭弹窗并使未完成轮询响应过期。
|
||||
async () => hook.result.current.closeBatchModal(),
|
||||
);
|
||||
resolvePoll(runningBatchFixture);
|
||||
await act(
|
||||
// staleResolveAction 完成已过期的轮询响应。
|
||||
async () => { await pendingPoll; },
|
||||
);
|
||||
expect(hook.result.current.showBatchModal).toBe(false);
|
||||
hook.unmount();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
});
|
||||
@@ -262,4 +262,37 @@ describe('useNotifications', /* 当前回调处理通知渠道、SMTP 和动作
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
test('渠道刷新时丢弃先发出的过期响应', /* 当前回调验证通知渠道请求代次隔离。 */ async () => {
|
||||
// ChannelResponse 是旧渠道请求使用的最小成功响应。
|
||||
type ChannelResponse = {
|
||||
// success 表示请求成功。
|
||||
success: true;
|
||||
// data 保存渠道列表。
|
||||
data: NotificationChannel[];
|
||||
};
|
||||
// resolveFirst 是旧渠道请求的完成控制器。
|
||||
let resolveFirst: (value: ChannelResponse) => void = () => undefined;
|
||||
// firstRequest 是保持未完成的旧渠道请求 Promise。
|
||||
const firstRequest = new Promise<ChannelResponse>(/* firstExecutor 保存旧请求完成函数。 */ resolve => { resolveFirst = resolve; });
|
||||
getChannelsMock.mockReset();
|
||||
getChannelsMock.mockReturnValueOnce(firstRequest);
|
||||
getChannelsMock.mockResolvedValueOnce({ success: true, data: [channelFixture] });
|
||||
// hook 是通知渠道刷新竞态场景的 Hook 渲染结果。
|
||||
const hook = renderHook(
|
||||
// staleHookFactory 创建通知渠道旧响应场景的 Hook。
|
||||
() => useNotifications(false),
|
||||
);
|
||||
await act(
|
||||
// refreshAction 发起第二次渠道刷新并使首次请求过期。
|
||||
async () => hook.result.current.loadChannels(),
|
||||
);
|
||||
resolveFirst({ success: true, data: [] });
|
||||
await act(
|
||||
// staleResolveAction 完成已过期的首次响应。
|
||||
async () => { await firstRequest; },
|
||||
);
|
||||
expect(hook.result.current.channels).toEqual([channelFixture]);
|
||||
hook.unmount();
|
||||
});
|
||||
});
|
||||
@@ -88,6 +88,13 @@ describe('request helpers', () => {
|
||||
await expect(get('/broken')).rejects.toThrow('请求失败: 500');
|
||||
} /* 回调函数负责当前业务流程。 */);
|
||||
|
||||
test('普通请求纯文本错误体读取失败时使用状态码兜底', /* 当前回调验证普通请求文本错误读取失败分支。 */ async () => {
|
||||
// response 是文本读取失败的普通请求响应替身。
|
||||
const response = { ok: false, status: 502, headers: { get: /* contentTypeGetter 返回纯文本类型。 */ () => 'text/plain' }, text: vi.fn().mockRejectedValue(new Error('读取失败')) } as unknown as Response;
|
||||
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(response));
|
||||
await expect(get('/broken-text')).rejects.toThrow('请求失败: 502');
|
||||
} /* 回调函数负责当前业务流程。 */);
|
||||
|
||||
test('网络层异常原样透传', async () => {
|
||||
// fetchMock 是抛出非取消网络异常的替身。
|
||||
const fetchMock = vi.fn().mockRejectedValue(new Error('网络断开'));
|
||||
|
||||
Reference in new issue
Block a user