@@ -11,7 +11,10 @@ import {
1111 getDisplayName ,
1212 getDisplayNameForReactElement ,
1313} from 'react-devtools-shared/src/utils' ;
14- import { format } from 'react-devtools-shared/src/backend/utils' ;
14+ import {
15+ format ,
16+ formatWithStyles ,
17+ } from 'react-devtools-shared/src/backend/utils' ;
1518import {
1619 REACT_SUSPENSE_LIST_TYPE as SuspenseList ,
1720 REACT_STRICT_MODE_TYPE as StrictMode ,
@@ -110,4 +113,67 @@ describe('utils', () => {
110113 expect ( format ( Symbol ( 'abc' ) , 123 ) ) . toEqual ( 'Symbol(abc) 123' ) ;
111114 } ) ;
112115 } ) ;
116+
117+ describe ( 'formatWithStyles' , ( ) => {
118+ it ( 'should format empty arrays' , ( ) => {
119+ expect ( formatWithStyles ( [ ] ) ) . toEqual ( [ ] ) ;
120+ expect ( formatWithStyles ( [ ] , 'gray' ) ) . toEqual ( [ ] ) ;
121+ expect ( formatWithStyles ( undefined ) ) . toEqual ( undefined ) ;
122+ } ) ;
123+
124+ it ( 'should bail out of strings with styles' , ( ) => {
125+ expect (
126+ formatWithStyles ( [ '%ca' , 'color: green' , 'b' , 'c' ] , 'color: gray' ) ,
127+ ) . toEqual ( [ '%ca' , 'color: green' , 'b' , 'c' ] ) ;
128+ } ) ;
129+
130+ it ( 'should format simple strings' , ( ) => {
131+ expect ( formatWithStyles ( [ 'a' ] ) ) . toEqual ( [ 'a' ] ) ;
132+
133+ expect ( formatWithStyles ( [ 'a' , 'b' , 'c' ] ) ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
134+ expect ( formatWithStyles ( [ 'a' ] , 'color: gray' ) ) . toEqual ( [
135+ '%c%s' ,
136+ 'color: gray' ,
137+ 'a' ,
138+ ] ) ;
139+ expect ( formatWithStyles ( [ 'a' , 'b' , 'c' ] , 'color: gray' ) ) . toEqual ( [
140+ '%c%s %s %s' ,
141+ 'color: gray' ,
142+ 'a' ,
143+ 'b' ,
144+ 'c' ,
145+ ] ) ;
146+ } ) ;
147+
148+ it ( 'should format string substituions' , ( ) => {
149+ expect (
150+ formatWithStyles ( [ '%s %s %s' , 'a' , 'b' , 'c' ] , 'color: gray' ) ,
151+ ) . toEqual ( [ '%c%s %s %s' , 'color: gray' , 'a' , 'b' , 'c' ] ) ;
152+
153+ // The last letter isn't gray here but I think it's not a big
154+ // deal, since there is a string substituion but it's incorrect
155+ expect (
156+ formatWithStyles ( [ '%s %s' , 'a' , 'b' , 'c' ] , 'color: gray' ) ,
157+ ) . toEqual ( [ '%c%s %s' , 'color: gray' , 'a' , 'b' , 'c' ] ) ;
158+ } ) ;
159+
160+ it ( 'should support multiple argument types' , ( ) => {
161+ const symbol = Symbol ( 'a' ) ;
162+ expect (
163+ formatWithStyles (
164+ [ 'abc' , 123 , 12.3 , true , { hello : 'world' } , symbol ] ,
165+ 'color: gray' ,
166+ ) ,
167+ ) . toEqual ( [
168+ '%c%s %i %f %s %o %s' ,
169+ 'color: gray' ,
170+ 'abc' ,
171+ 123 ,
172+ 12.3 ,
173+ true ,
174+ { hello : 'world' } ,
175+ symbol ,
176+ ] ) ;
177+ } ) ;
178+ } ) ;
113179} ) ;
0 commit comments