Skip to content

Commit 07fc7cf

Browse files
Add tests
1 parent f17078c commit 07fc7cf

File tree

2 files changed

+75
-6
lines changed

2 files changed

+75
-6
lines changed

src/components/Message/__tests__/QuotedMessage.test.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import '@testing-library/jest-dom';
22
import { render, screen } from '@testing-library/react';
33
import { toHaveNoViolations } from 'jest-axe';
44
import React from 'react';
5+
import { nanoid } from 'nanoid';
56
import { axe } from '../../../../axe-helper';
67

78
import {
@@ -106,13 +107,46 @@ describe('QuotedMessage', () => {
106107
expect(results).toHaveNoViolations();
107108
});
108109

109-
it('should rendered text', async () => {
110-
const { container, queryByTestId, queryByText } = await renderQuotedMessage({
110+
it('renders proper markdown (through default renderText fn)', async () => {
111+
const messageText = 'hey @John Cena';
112+
const { container, findByTestId, findByText, queryByTestId } =
113+
await renderQuotedMessage({
114+
customProps: {
115+
message: {
116+
quoted_message: {
117+
mentioned_users: [{ id: 'john', name: 'John Cena' }],
118+
text: messageText,
119+
},
120+
},
121+
},
122+
});
123+
124+
expect(await findByText('@John Cena')).toHaveAttribute('data-user-id');
125+
expect((await findByTestId('quoted-message-text')).textContent).toEqual(messageText);
126+
expect(queryByTestId(quotedAttachmentListTestId)).not.toBeInTheDocument();
127+
const results = await axe(container);
128+
expect(results).toHaveNoViolations();
129+
});
130+
131+
it('uses custom renderText fn if provided', async () => {
132+
const messageText = nanoid();
133+
const fn = jest
134+
.fn()
135+
.mockReturnValue(<div data-testid={messageText}>{messageText}</div>);
136+
137+
const { container, findByTestId, queryByTestId } = await renderQuotedMessage({
111138
customProps: {
112-
message: { quoted_message: { text: quotedText } },
139+
message: {
140+
quoted_message: {
141+
text: messageText,
142+
},
143+
},
144+
renderText: fn,
113145
},
114146
});
115-
expect(queryByText(quotedText)).toBeInTheDocument();
147+
148+
expect(fn).toHaveBeenCalled();
149+
expect((await findByTestId('quoted-message-text')).textContent).toEqual(messageText);
116150
expect(queryByTestId(quotedAttachmentListTestId)).not.toBeInTheDocument();
117151
const results = await axe(container);
118152
expect(results).toHaveNoViolations();

src/components/MessageInput/__tests__/MessageInput.test.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
initClientWithChannels,
2727
} from '../../../mock-builders';
2828
import { generatePoll } from '../../../mock-builders/generator/poll';
29+
import { QuotedMessagePreview } from '../QuotedMessagePreview';
2930

3031
expect.extend(toHaveNoViolations);
3132

@@ -1520,8 +1521,10 @@ describe(`MessageInputFlat only`, () => {
15201521
});
15211522
};
15221523

1523-
const initQuotedMessagePreview = async (message) => {
1524-
await waitFor(() => expect(screen.queryByText(message.text)).not.toBeInTheDocument());
1524+
const initQuotedMessagePreview = async () => {
1525+
await waitFor(() =>
1526+
expect(screen.queryByTestId('quoted-message-preview')).not.toBeInTheDocument(),
1527+
);
15251528

15261529
const quoteButton = await screen.findByText(/^reply$/i);
15271530
await waitFor(() => expect(quoteButton).toBeInTheDocument());
@@ -1550,6 +1553,38 @@ describe(`MessageInputFlat only`, () => {
15501553
await quotedMessagePreviewIsDisplayedCorrectly(mainListMessage);
15511554
});
15521555

1556+
it('renders proper markdown (through default renderText fn)', async () => {
1557+
const m = generateMessage({
1558+
mentioned_users: [{ id: 'john', name: 'John Cena' }],
1559+
text: 'hey @John Cena',
1560+
user,
1561+
});
1562+
await renderComponent({ messageContextOverrides: { message: m } });
1563+
await initQuotedMessagePreview(m);
1564+
1565+
expect(await screen.findByText('@John Cena')).toHaveAttribute('data-user-id');
1566+
});
1567+
1568+
it('uses custom renderText fn if provided', async () => {
1569+
const m = generateMessage({
1570+
text: nanoid(),
1571+
user,
1572+
});
1573+
const fn = jest.fn().mockReturnValue(<div data-testid={m.text}>{m.text}</div>);
1574+
await renderComponent({
1575+
channelProps: {
1576+
QuotedMessagePreview: (props) => (
1577+
<QuotedMessagePreview {...props} renderText={fn} />
1578+
),
1579+
},
1580+
messageContextOverrides: { message: m },
1581+
});
1582+
await initQuotedMessagePreview(m);
1583+
1584+
expect(fn).toHaveBeenCalled();
1585+
expect(await screen.findByTestId(m.text)).toBeInTheDocument();
1586+
});
1587+
15531588
it('is updated on original message update', async () => {
15541589
const { channel, client } = await renderComponent();
15551590
await initQuotedMessagePreview(mainListMessage);

0 commit comments

Comments
 (0)