11import React from 'react' ;
22import { render , fireEvent } from '@testing-library/react-native' ;
3+ import type { Json } from '@metamask/utils' ;
34import ContentfulRichText from './ContentfulRichText' ;
45import 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
5042const 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} ) ;
0 commit comments