11/* eslint-disable react/display-name */
2- import React from 'react'
32import { render } from '@testing-library/react'
4- import Interpolate , { SYNTAX_I18NEXT } from '../src'
3+ import React from 'react'
4+ import Interpolate , { SYNTAX_I18NEXT , type InterpolateProps } from '../src'
55
6- Interpolate . defaultProps = {
7- graceful : false ,
6+ afterEach ( ( ) => {
7+ jest . restoreAllMocks ( )
8+ } )
9+
10+ const suppressConsole = ( ) => {
11+ jest . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => undefined )
12+ jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => undefined )
813}
914
10- const surpressConsole = ( ) => {
11- const w = jest . spyOn ( console , 'warn' ) . mockImplementation ( )
12- const e = jest . spyOn ( console , 'error' ) . mockImplementation ( )
15+ type RenderSuccessProps = InterpolateProps & {
16+ expected : string
17+ }
1318
14- return ( ) => {
15- w . mockRestore ( )
16- e . mockRestore ( )
17- }
19+ type RenderErrorProps = InterpolateProps & {
20+ expectedError : string
1821}
1922
2023describe ( 'Interpolate' , ( ) => {
21- function renderTest ( { expected, ...props } ) {
22- const { container } = render ( < Interpolate { ...props } /> )
24+ function renderTest ( { expected, graceful = false , ...props } : RenderSuccessProps ) {
25+ const { container } = render ( < Interpolate { ...props } graceful = { graceful } /> )
2326 expect ( container . innerHTML ) . toEqual ( expected )
2427 }
2528
2629 test ( 'when no mapping is provide' , ( ) => {
27- const restore = surpressConsole ( ) // Interpolate will output warning when no mapping is provided
30+ suppressConsole ( )
2831
2932 renderTest ( {
3033 string : '<h1>hello <b>{name}</b></h1><br/>. welcome to todoist' ,
3134 expected : '<h1>hello <b>{name}</b></h1><br>. welcome to todoist' ,
3235 } )
33-
34- restore ( )
3536 } )
3637
3738 test ( 'tag mapping' , ( ) =>
3839 renderTest ( {
3940 string : '<h1>hello <b>steven</b></h1>. welcome to todoist' ,
4041 mapping : {
41- b : ( child ) => < i > { child } </ i > ,
42- h1 : ( child ) => < h2 > { child } </ h2 > ,
42+ b : ( children ) => < i > { children } </ i > ,
43+ h1 : ( children ) => < h2 > { children } </ h2 > ,
4344 } ,
4445 expected : '<h2>hello <i>steven</i></h2>. welcome to todoist' ,
4546 } ) )
@@ -77,25 +78,24 @@ describe('Interpolate', () => {
7778 renderTest ( {
7879 string : '<h1>hello <b>{name}</b></h1>.<br/> welcome to todoist' ,
7980 mapping : {
80- h1 : ( child ) => < h2 > { child } </ h2 > ,
81- b : ( child ) => < i > { child } </ i > ,
81+ h1 : ( children ) => < h2 > { children } </ h2 > ,
82+ b : ( children ) => < i > { children } </ i > ,
8283 name : 'steven' ,
8384 br : < hr /> ,
8485 } ,
8586 expected : '<h2>hello <i>steven</i></h2>.<hr> welcome to todoist' ,
8687 } ) )
8788
8889 test ( 'combination of mapping with function component' , ( ) => {
89- // eslint-disable-next-line
90- const Subheader = ( { children } ) => {
90+ const Subheader = ( { children } : React . PropsWithChildren ) => {
9191 return < h2 className = "subheader" > { children } </ h2 >
9292 }
9393
9494 renderTest ( {
9595 string : '<h1>hello <b>{name}</b></h1>.<br/> welcome to todoist' ,
9696 mapping : {
97- h1 : ( child ) => < Subheader > { child } </ Subheader > ,
98- b : ( child ) => < i > { child } </ i > ,
97+ h1 : ( children ) => < Subheader > { children } </ Subheader > ,
98+ b : ( children ) => < i > { children } </ i > ,
9999 name : 'steven' ,
100100 br : < hr /> ,
101101 } ,
@@ -124,28 +124,26 @@ describe('Interpolate', () => {
124124 'hello <script>window.xss = 1</script><script>window.xss = 1</script> welcome to todoist' ,
125125 } )
126126
127- expect ( window . css ) . toBeUndefined ( )
127+ expect ( ( window as typeof window & { xss ?: number } ) . xss ) . toBeUndefined ( )
128128 } )
129129
130130 test ( 'when graceful flag is on and string contains syntax error, interpolate should return the original string and should not throw error' , ( ) => {
131- const restore = surpressConsole ( )
131+ suppressConsole ( )
132132
133133 renderTest ( {
134134 string : '</h1>' ,
135135 expected : '</h1>' ,
136136 graceful : true ,
137137 } )
138-
139- restore ( )
140138 } )
141139
142140 test ( 'using SYNTAX_I18NEXT' , ( ) => {
143141 renderTest ( {
144142 syntax : SYNTAX_I18NEXT ,
145143 string : '<0>hello <b>{{name}}</b></0>.<br/> welcome to todoist' ,
146144 mapping : {
147- 0 : ( child ) => < h2 className = "subheader" > { child } </ h2 > ,
148- b : ( child ) => < i > { child } </ i > ,
145+ 0 : ( children ) => < h2 className = "subheader" > { children } </ h2 > ,
146+ b : ( children ) => < i > { children } </ i > ,
149147 name : 'steven' ,
150148 br : < hr /> ,
151149 } ,
@@ -155,17 +153,15 @@ describe('Interpolate', () => {
155153} )
156154
157155describe ( 'Interpolate: error cases' , ( ) => {
158- let restore
159- beforeAll ( ( ) => {
160- restore = surpressConsole ( )
161- } )
162- afterAll ( ( ) => {
163- restore ( )
156+ beforeEach ( ( ) => {
157+ suppressConsole ( )
164158 } )
165159
166- function renderTest ( { expectedError, ...props } ) {
160+ function renderTest ( { expectedError, graceful = false , ...props } : RenderErrorProps ) {
167161 return ( ) => {
168- expect ( ( ) => render ( < Interpolate { ...props } /> ) ) . toThrow ( expectedError )
162+ expect ( ( ) => render ( < Interpolate { ...props } graceful = { graceful } /> ) ) . toThrow (
163+ expectedError ,
164+ )
169165 }
170166 }
171167
0 commit comments