Skip to content

Commit

Permalink
✅ test: fix test warning
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Dec 15, 2023
1 parent 4fee0b1 commit 05b6e4c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
30 changes: 15 additions & 15 deletions src/store/chat/actions/enhance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,23 @@ describe('ChatEnhanceAction', () => {
const messageContent = 'Hello World';
const detectedLang = 'en-US';

// 设置初始消息状态
useChatStore.setState({
messages: [
{
id: messageId,
content: messageContent,
createdAt: Date.now(),
updatedAt: Date.now(),
role: 'user',
sessionId: 'test',
topicId: 'test',
meta: {},
},
],
act(() => {
useChatStore.setState({
messages: [
{
id: messageId,
content: messageContent,
createdAt: Date.now(),
updatedAt: Date.now(),
role: 'user',
sessionId: 'test',
topicId: 'test',
meta: {},
},
],
});
});

// 模拟语言检测和翻译结果
(chatService.fetchPresetTaskResult as Mock).mockImplementation(({ params }) => {
if (params === chainLangDetect(messageContent)) {
return Promise.resolve(detectedLang);
Expand Down
5 changes: 3 additions & 2 deletions src/store/chat/actions/message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,9 @@ describe('chatMessage actions', () => {
const toggleChatLoadingSpy = vi.spyOn(result.current, 'toggleChatLoading');
const abortController = new AbortController();

// 设置模拟 abortController
useChatStore.setState({ abortController });
act(() => {
useChatStore.setState({ abortController });
});

await act(async () => {
result.current.stopGenerateMessage();
Expand Down
16 changes: 12 additions & 4 deletions src/store/chat/actions/topic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ describe('topic action', () => {
describe('saveToTopic', () => {
it('should not create a topic if there are no messages', async () => {
const { result } = renderHook(() => useChatStore());
useChatStore.setState({ messages: [] });
act(() => {
useChatStore.setState({ messages: [] });
});

const createTopicSpy = vi.spyOn(topicService, 'createTopic');

Expand All @@ -98,7 +100,9 @@ describe('topic action', () => {
it('should create a topic and bind messages to it', async () => {
const { result } = renderHook(() => useChatStore());
const messages = [{ id: 'message1' }, { id: 'message2' }] as ChatMessage[];
useChatStore.setState({ messages, activeId: 'session-id' });
act(() => {
useChatStore.setState({ messages, activeId: 'session-id' });
});

const createTopicSpy = vi
.spyOn(topicService, 'createTopic')
Expand Down Expand Up @@ -133,8 +137,10 @@ describe('topic action', () => {
it('should call mutate to refresh topics', async () => {
const { result } = renderHook(() => useChatStore());
const activeId = 'test-session-id';
useChatStore.setState({ activeId });

act(() => {
useChatStore.setState({ activeId });
});
// Mock the mutate function to resolve immediately

await act(async () => {
Expand All @@ -148,8 +154,10 @@ describe('topic action', () => {
it('should handle errors during refreshing topics', async () => {
const { result } = renderHook(() => useChatStore());
const activeId = 'test-session-id';
useChatStore.setState({ activeId });

act(() => {
useChatStore.setState({ activeId });
});
// Mock the mutate function to throw an error
// 设置模拟错误
(mutate as Mock).mockImplementation(() => {
Expand Down

0 comments on commit 05b6e4c

Please sign in to comment.