Skip to content

Commit 36d810c

Browse files
add unit tests
1 parent 1c1c10d commit 36d810c

3 files changed

Lines changed: 176 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { render, screen } from '@testing-library/react';
2+
3+
import { QuoteAttachment } from './QuoteAttachment';
4+
5+
jest.mock('../../hooks/useMaxMessageParseSize', () => ({
6+
useMaxMessageParseSize: () => 100,
7+
}));
8+
9+
jest.mock('@rocket.chat/ui-contexts', () => ({
10+
useUserPreference: () => true,
11+
}));
12+
13+
jest.mock('../../../../hooks/useTimeAgo', () => ({
14+
useTimeAgo: () => (date: Date) => date.toISOString(),
15+
}));
16+
17+
jest.mock('../../MessageContentBody', () => ({
18+
__esModule: true,
19+
default: () => <div data-testid='message-content-body' />,
20+
}));
21+
22+
const baseAttachment = {
23+
author_name: 'User',
24+
author_icon: '',
25+
ts: new Date(),
26+
text: 'short text',
27+
md: [{ type: 'PARAGRAPH', value: [{ type: 'PLAIN_TEXT', value: 'short text' }] }],
28+
};
29+
30+
describe('QuoteAttachment', () => {
31+
it('renders MessageContentBody when text length is within maxMessageParseSize', () => {
32+
render(<QuoteAttachment attachment={baseAttachment as any} />);
33+
expect(screen.getByTestId('message-content-body')).toBeInTheDocument();
34+
});
35+
36+
it('renders plain text when text exceeds maxMessageParseSize', () => {
37+
const longText = 'a'.repeat(101);
38+
const attachment = { ...baseAttachment, text: longText };
39+
40+
render(<QuoteAttachment attachment={attachment as any} />);
41+
42+
expect(screen.queryByTestId('message-content-body')).not.toBeInTheDocument();
43+
});
44+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { renderHook } from '@testing-library/react';
2+
3+
import { useNormalizedMessage } from './useNormalizedMessage';
4+
5+
const mockParseMessageTextToAstMarkdown = jest.fn((msg: any, ..._args: any[]) => msg);
6+
7+
jest.mock('../list/MessageListContext', () => ({
8+
useMessageListKatex: () => null,
9+
useMessageListAutoTranslate: () => ({
10+
showAutoTranslate: () => false,
11+
autoTranslateLanguage: '',
12+
}),
13+
useMessageListShowColors: () => false,
14+
}));
15+
16+
jest.mock('../../../lib/parseMessageTextToAstMarkdown', () => ({
17+
parseMessageTextToAstMarkdown: (msg: any, ...args: any[]) => mockParseMessageTextToAstMarkdown(msg, ...args),
18+
}));
19+
20+
jest.mock('../../../views/room/MessageList/hooks/useAutoLinkDomains', () => ({ useAutoLinkDomains: () => [] }));
21+
22+
const baseMessage = {
23+
_id: 'msg1',
24+
rid: 'room1',
25+
u: { _id: 'u1', username: 'user', name: 'User' },
26+
ts: new Date(),
27+
_updatedAt: new Date(),
28+
};
29+
30+
describe('useNormalizedMessage', () => {
31+
beforeEach(() => {
32+
mockParseMessageTextToAstMarkdown.mockClear();
33+
});
34+
35+
it('should skip parsing and returns PARAGRAPH node when msg exceeds maxMessageParseSize', () => {
36+
const longMsg = 'a'.repeat(101);
37+
const message = { ...baseMessage, msg: longMsg };
38+
39+
const { result } = renderHook(() => useNormalizedMessage(message as any, 100));
40+
41+
expect(mockParseMessageTextToAstMarkdown).not.toHaveBeenCalled();
42+
expect(result.current.md).toEqual([
43+
{
44+
type: 'PARAGRAPH',
45+
value: [{ type: 'PLAIN_TEXT', value: longMsg }],
46+
},
47+
]);
48+
});
49+
50+
it('should call parseMessageTextToAstMarkdown when msg is within maxMessageParseSize', () => {
51+
const message = { ...baseMessage, msg: 'Hello world' };
52+
53+
renderHook(() => useNormalizedMessage(message as any, 100));
54+
55+
expect(mockParseMessageTextToAstMarkdown).toHaveBeenCalledWith(message, expect.anything(), expect.anything());
56+
});
57+
58+
it('should preserve attachments when bypassing parsing due to size', () => {
59+
const longMsg = 'a'.repeat(101);
60+
const attachments = [{ type: 'quote', text: 'quoted' }];
61+
const message = { ...baseMessage, msg: longMsg, attachments };
62+
63+
const { result } = renderHook(() => useNormalizedMessage(message as any, 100));
64+
65+
expect(mockParseMessageTextToAstMarkdown).not.toHaveBeenCalled();
66+
expect(result.current.attachments).toBe(attachments);
67+
});
68+
});
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { renderHook } from '@testing-library/react';
2+
3+
import { useMessageBody } from './useMessageBody';
4+
5+
const mockParseMessageTextToAstMarkdown = jest.fn();
6+
7+
jest.mock('./useAutoLinkDomains', () => ({
8+
useAutoLinkDomains: () => [],
9+
}));
10+
11+
jest.mock('../../../../components/message/list/MessageListContext', () => ({
12+
useMessageListAutoTranslate: () => ({
13+
showAutoTranslate: () => false,
14+
autoTranslateLanguage: '',
15+
}),
16+
}));
17+
18+
jest.mock('../../../../lib/parseMessageTextToAstMarkdown', () => ({
19+
parseMessageTextToAstMarkdown: (msg: any, ...args: any[]) => mockParseMessageTextToAstMarkdown(msg, ...args),
20+
}));
21+
22+
const baseMessage = {
23+
_id: 'msg1',
24+
rid: 'room1',
25+
u: { _id: 'u1', username: 'user', name: 'User' },
26+
ts: new Date(),
27+
_updatedAt: new Date(),
28+
};
29+
30+
describe('useMessageBody', () => {
31+
beforeEach(() => {
32+
mockParseMessageTextToAstMarkdown.mockClear();
33+
});
34+
35+
it('should return raw msg and skips parsing when msg exceeds maxMessageParseSize', () => {
36+
const longMsg = 'a'.repeat(101);
37+
const message = { ...baseMessage, msg: longMsg, md: [{ type: 'PARAGRAPH', value: [] }] };
38+
39+
const { result } = renderHook(() => useMessageBody(message as any, 100));
40+
41+
expect(result.current).toBe(longMsg);
42+
expect(mockParseMessageTextToAstMarkdown).not.toHaveBeenCalled();
43+
});
44+
45+
it('should call parser when message has md and is within maxMessageParseSize', () => {
46+
const md = [{ type: 'PARAGRAPH', value: [] }];
47+
const message = { ...baseMessage, msg: 'Hello world', md };
48+
mockParseMessageTextToAstMarkdown.mockReturnValue({ ...message, md });
49+
50+
const { result } = renderHook(() => useMessageBody(message as any, 100));
51+
52+
expect(mockParseMessageTextToAstMarkdown).toHaveBeenCalledWith(message, expect.anything(), expect.anything());
53+
expect(result.current).toBe(md);
54+
});
55+
56+
it('should return raw msg without parsing when message has no md', () => {
57+
const message = { ...baseMessage, msg: 'Hello world' };
58+
59+
const { result } = renderHook(() => useMessageBody(message as any, 100));
60+
61+
expect(mockParseMessageTextToAstMarkdown).not.toHaveBeenCalled();
62+
expect(result.current).toBe('Hello world');
63+
});
64+
});

0 commit comments

Comments
 (0)