Skip to content

Commit 6d115a6

Browse files
committed
Fix bug
1 parent 7ca7c30 commit 6d115a6

2 files changed

Lines changed: 45 additions & 33 deletions

File tree

app/components/UI/Rewards/components/ContentfulRichText/ContentfulRichText.test.tsx

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { render, fireEvent } from '@testing-library/react-native';
3+
import type { Json } from '@metamask/utils';
34
import ContentfulRichText from './ContentfulRichText';
45
import Routes from '../../../../../constants/navigation/Routes';
56

@@ -17,38 +18,29 @@ jest.mock('@metamask/design-system-twrnc-preset', () => ({
1718
useTailwind: () => ({ style: (...args: unknown[]) => args }),
1819
}));
1920

20-
interface RichTextNode {
21-
nodeType: string;
22-
data: Record<string, unknown>;
23-
content?: RichTextNode[];
24-
value?: string;
25-
marks?: { type: string }[];
26-
}
21+
type RichTextNode = Record<string, Json>;
2722

28-
const makeDoc = (...content: RichTextNode[]) => ({
29-
nodeType: 'document' as const,
23+
const makeDoc = (...content: RichTextNode[]): Json => ({
24+
nodeType: 'document',
3025
data: {},
3126
content,
3227
});
3328

34-
const paragraph = (...children: RichTextNode[]) => ({
35-
nodeType: 'paragraph' as const,
29+
const paragraph = (...children: RichTextNode[]): RichTextNode => ({
30+
nodeType: 'paragraph',
3631
data: {},
3732
content: children,
3833
});
3934

40-
const text = (
41-
value: string,
42-
marks: { type: string }[] = [],
43-
): RichTextNode => ({
44-
nodeType: 'text' as const,
35+
const text = (value: string, marks: { type: string }[] = []): RichTextNode => ({
36+
nodeType: 'text',
4537
value,
46-
marks,
38+
marks: marks as Json[],
4739
data: {},
4840
});
4941

5042
const hyperlink = (uri: string, linkText: string): RichTextNode => ({
51-
nodeType: 'hyperlink' as const,
43+
nodeType: 'hyperlink',
5244
data: { uri },
5345
content: [text(linkText)],
5446
});
@@ -60,7 +52,7 @@ describe('ContentfulRichText', () => {
6052

6153
it('returns null for invalid document', () => {
6254
const { toJSON } = render(
63-
<ContentfulRichText document={null as unknown} testID="rt" />,
55+
<ContentfulRichText document={null as unknown as Json} testID="rt" />,
6456
);
6557
expect(toJSON()).toBeNull();
6658
});
@@ -77,15 +69,15 @@ describe('ContentfulRichText', () => {
7769
const { getByText } = render(
7870
<ContentfulRichText document={doc} testID="rt" />,
7971
);
80-
expect(getByText('Hello world')).toBeDefined();
72+
expect(getByText('Hello world')).toBeOnTheScreen();
8173
});
8274

8375
it('renders bold text', () => {
8476
const doc = makeDoc(paragraph(text('bold text', [{ type: 'bold' }])));
8577
const { getByText } = render(
8678
<ContentfulRichText document={doc} testID="rt" />,
8779
);
88-
expect(getByText('bold text')).toBeDefined();
80+
expect(getByText('bold text')).toBeOnTheScreen();
8981
});
9082

9183
it('renders a hyperlink and opens in-app browser on press', () => {
@@ -116,8 +108,8 @@ describe('ContentfulRichText', () => {
116108
const { getByText } = render(
117109
<ContentfulRichText document={doc} testID="rt" />,
118110
);
119-
expect(getByText('First paragraph')).toBeDefined();
120-
expect(getByText('Second paragraph')).toBeDefined();
111+
expect(getByText('First paragraph')).toBeOnTheScreen();
112+
expect(getByText('Second paragraph')).toBeOnTheScreen();
121113
});
122114

123115
it('renders an unordered list with bullets', () => {
@@ -140,8 +132,8 @@ describe('ContentfulRichText', () => {
140132
const { getByText, getAllByText } = render(
141133
<ContentfulRichText document={doc} testID="rt" />,
142134
);
143-
expect(getByText('Item one')).toBeDefined();
144-
expect(getByText('Item two')).toBeDefined();
135+
expect(getByText('Item one')).toBeOnTheScreen();
136+
expect(getByText('Item two')).toBeOnTheScreen();
145137
expect(getAllByText('• ').length).toBe(2);
146138
});
147139

@@ -165,8 +157,8 @@ describe('ContentfulRichText', () => {
165157
const { getByText } = render(
166158
<ContentfulRichText document={doc} testID="rt" />,
167159
);
168-
expect(getByText('1. ')).toBeDefined();
169-
expect(getByText('2. ')).toBeDefined();
160+
expect(getByText('1. ')).toBeOnTheScreen();
161+
expect(getByText('2. ')).toBeOnTheScreen();
170162
});
171163

172164
it('renders a heading', () => {
@@ -178,14 +170,28 @@ describe('ContentfulRichText', () => {
178170
const { getByText } = render(
179171
<ContentfulRichText document={doc} testID="rt" />,
180172
);
181-
expect(getByText('Title')).toBeDefined();
173+
expect(getByText('Title')).toBeOnTheScreen();
182174
});
183175

184176
it('sets the testID on the container', () => {
185177
const doc = makeDoc(paragraph(text('test')));
186178
const { getByTestId } = render(
187179
<ContentfulRichText document={doc} testID="my-rich-text" />,
188180
);
189-
expect(getByTestId('my-rich-text')).toBeDefined();
181+
expect(getByTestId('my-rich-text')).toBeOnTheScreen();
182+
});
183+
184+
it('renders a text node that has no marks property', () => {
185+
const doc = makeDoc(
186+
paragraph({
187+
nodeType: 'text',
188+
value: 'no marks',
189+
data: {},
190+
}),
191+
);
192+
const { getByText } = render(
193+
<ContentfulRichText document={doc} testID="rt" />,
194+
);
195+
expect(getByText('no marks')).toBeOnTheScreen();
190196
});
191197
});

app/components/UI/Rewards/components/ContentfulRichText/ContentfulRichText.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ interface ContentfulRichTextProps {
5959
testID?: string;
6060
}
6161

62-
function isDocument(
63-
value: unknown,
64-
): value is {
62+
function isDocument(value: unknown): value is {
6563
nodeType: 'document';
6664
data: Record<string, unknown>;
6765
content: RichTextNode[];
@@ -79,7 +77,11 @@ function isDocument(
7977
function isTextNode(
8078
node: RichTextNode,
8179
): node is RichTextNode & { value: string; marks: RichTextMark[] } {
82-
return node.nodeType === 'text';
80+
return (
81+
node.nodeType === 'text' &&
82+
typeof node.value === 'string' &&
83+
Array.isArray(node.marks)
84+
);
8385
}
8486

8587
/**
@@ -150,6 +152,10 @@ const ContentfulRichText: React.FC<ContentfulRichTextProps> = ({
150152
return renderMarkedText(child.value, child.marks, childKey);
151153
}
152154

155+
if (child.nodeType === 'text' && typeof child.value === 'string') {
156+
return <Fragment key={childKey}>{child.value}</Fragment>;
157+
}
158+
153159
if (child.nodeType === INLINE_TYPES.HYPERLINK) {
154160
const uri = (child.data as { uri?: string }).uri ?? '';
155161
return (

0 commit comments

Comments
 (0)