Skip to content

Commit

Permalink
🐛 fix: 解决受控情况下,有些 Chats 数据依然不被受控的问题 (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
ONLY-yours authored Apr 15, 2024
1 parent e803eea commit 6196aba
Show file tree
Hide file tree
Showing 6 changed files with 1,032 additions and 952 deletions.
1,965 changes: 1,018 additions & 947 deletions src/ProChat/__test__/__snapshots__/demo.test.tsx.snap

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/ProChat/__test__/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ describe('ProChat', () => {
it('input area render worked', () => {
const wrapper = render(
<ProChat
renderInputArea={() => {
inputAreaRender={() => {
return <div>RenderInputArea</div>;
}}
/>,
);
expect(wrapper.getByText('RenderInputArea')).toBeInTheDocument();
expect(wrapper.getByText('RenderInputArea')).toBeTruthy();
});

it('i18n worked', () => {
const app = render(<ProChat locale="en-US" />);
const text = gLocaleObject('en-US');
expect(app.queryByPlaceholderText(text.placeholder)).toBeInTheDocument();
expect(app.queryByPlaceholderText(text.placeholder)).toBeTruthy();
});
});
3 changes: 2 additions & 1 deletion src/ProChat/demos/control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import { ChatMessage, ProChat } from '@ant-design/pro-chat';
import { useTheme } from 'antd-style';
import { useState } from 'react';

import { example } from '../mocks/basic';
import { MockResponse } from '../mocks/streamResponse';

export default () => {
const theme = useTheme();

const [chats, setChats] = useState<ChatMessage<Record<string, any>>[]>();
const [chats, setChats] = useState<ChatMessage<Record<string, any>>[]>(example.chats);

return (
<div style={{ background: theme.colorBgLayout }}>
Expand Down
3 changes: 3 additions & 0 deletions src/ProChat/mocks/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export const example = {
id: 'ZGxiX2p4',
role: 'user',
updateAt: 1697862243540,
extra: {
test: 'Test Extra',
},
},
{
content: '昨天的当天是明天的昨天。',
Expand Down
2 changes: 2 additions & 0 deletions src/ProChat/store/selectors/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const currentChats = (s: ChatStore): ChatMessage[] => {
// 针对新助手添加初始化时的自定义消息
export const currentChatsWithGuideMessage = (s: ChatStore): ChatMessage[] => {
const data = currentChats(s);

// TODO: need topic inject
const isBrandNewChat = data.length === 0;
if (!isBrandNewChat) return data;
Expand All @@ -85,6 +86,7 @@ export const currentChatsWithGuideMessage = (s: ChatStore): ChatMessage[] => {
meta: s.assistantMeta,
role: 'hello',
updateAt: Date.now(),
...data,
} as ChatMessage;

return [emptyInboxGuideMessage];
Expand Down
5 changes: 4 additions & 1 deletion tests/demo.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { cleanup, render } from '@testing-library/react';
import { theme } from 'antd';
import { glob } from 'glob';
import path from 'path';
import { afterEach, beforeEach, describe, it, vi } from 'vitest';
import { resetMockDate, setMockDate } from './utils';

theme.defaultConfig.hashed = false;
// 特殊情况略过 snapshot 的文件
const NotSnapshotFileList = ['renderInputArea.tsx'];
const NotSnapshotFileList = ['renderInputArea.tsx', 'float-drawer.tsx'];

function demoTest(component: string) {
beforeEach(() => {
theme.defaultConfig.hashed = false;
process.env.NODE_ENV = 'TEST';
setMockDate('2020-07-15T05:20:00.795');
});
Expand Down

0 comments on commit 6196aba

Please sign in to comment.