@@ -12,7 +12,7 @@ import {formatPlural as formatPluralFn} from '../../src/formatters/plural';
12
12
import { formatList as formatListFn } from '../../src/formatters/list' ;
13
13
import { formatDisplayName as formatDisplayNameFn } from '../../src/formatters/displayName' ;
14
14
import { formatMessage as baseFormatMessage } from '../../src/formatters/message' ;
15
- import { IntlFormatters , defineMessages } from '../../src' ;
15
+ import { IntlFormatters , defineMessages , IntlConfig } from '../../src' ;
16
16
17
17
describe ( 'format API' , ( ) => {
18
18
const { NODE_ENV } = process . env ;
@@ -30,14 +30,14 @@ describe('format API', () => {
30
30
with_named_format : 'It is {now, date, year_only}' ,
31
31
with_html : 'Hello, <b>{name}</b>!' ,
32
32
33
- missing : undefined ,
33
+ missing : undefined as any ,
34
34
empty : '' ,
35
35
invalid : 'invalid {}' ,
36
36
missing_value : 'missing {arg_missing}' ,
37
37
missing_named_format : 'missing {now, date, format_missing}' ,
38
38
ast_simple : parse ( 'hello world' ) ,
39
39
ast_var : parse ( 'hello there, {name}' ) ,
40
- } ,
40
+ } as Record < string , any > ,
41
41
42
42
formats : {
43
43
date : {
@@ -69,7 +69,7 @@ describe('format API', () => {
69
69
} ,
70
70
missing : undefined ,
71
71
} ,
72
- } ,
72
+ } as any ,
73
73
74
74
defaultLocale : 'en' ,
75
75
defaultFormats : { } ,
@@ -178,12 +178,7 @@ describe('format API', () => {
178
178
179
179
it ( 'falls back and warns on invalid Intl.DateTimeFormat options' , ( ) => {
180
180
expect ( formatDate ( 0 , { year : 'invalid' } ) ) . toBe ( '0' ) ;
181
- expect ( config . onError ) . toHaveBeenCalledTimes ( 1 ) ;
182
- expect ( config . onError ) . toHaveBeenCalledWith (
183
- expect . stringMatching (
184
- / \[ R e a c t I n t l \] E r r o r f o r m a t t i n g d a t e .\n R a n g e E r r o r : V a l u e i n v a l i d o u t o f r a n g e f o r ( .* ) o p t i o n s p r o p e r t y y e a r /
185
- )
186
- ) ;
181
+ expect ( config . onError . mock . calls . map ( c => c [ 0 ] . code ) ) . toMatchSnapshot ( ) ;
187
182
} ) ;
188
183
189
184
it ( 'uses configured named formats' , ( ) => {
@@ -217,10 +212,7 @@ describe('format API', () => {
217
212
df = new Intl . DateTimeFormat ( config . locale ) ;
218
213
219
214
expect ( formatDate ( date , { format} ) ) . toBe ( df . format ( date ) ) ;
220
- expect ( config . onError ) . toHaveBeenCalledTimes ( 1 ) ;
221
- expect ( config . onError ) . toHaveBeenCalledWith (
222
- `[React Intl] No date format named: ${ format } `
223
- ) ;
215
+ expect ( config . onError . mock . calls . map ( c => c [ 0 ] . code ) ) . toMatchSnapshot ( ) ;
224
216
} ) ;
225
217
226
218
it ( 'uses time zone specified in options over the one passed through by the provider' , ( ) => {
@@ -310,12 +302,7 @@ describe('format API', () => {
310
302
311
303
it ( 'falls back and warns on invalid Intl.DateTimeFormat options' , ( ) => {
312
304
expect ( formatTime ( 0 , { hour : 'invalid' } ) ) . toBe ( '0' ) ;
313
- expect ( config . onError ) . toHaveBeenCalledTimes ( 1 ) ;
314
- expect ( config . onError ) . toHaveBeenCalledWith (
315
- expect . stringMatching (
316
- / \[ R e a c t I n t l \] E r r o r f o r m a t t i n g t i m e .\n R a n g e E r r o r : V a l u e i n v a l i d o u t o f r a n g e f o r ( .* ) o p t i o n s p r o p e r t y h o u r /
317
- )
318
- ) ;
305
+ expect ( config . onError . mock . calls . map ( c => c [ 0 ] . code ) ) . toMatchSnapshot ( ) ;
319
306
} ) ;
320
307
321
308
it ( 'uses configured named formats' , ( ) => {
@@ -347,10 +334,7 @@ describe('format API', () => {
347
334
const format = 'missing' ;
348
335
349
336
expect ( formatTime ( date , { format} ) ) . toBe ( df . format ( date ) ) ;
350
- expect ( config . onError ) . toHaveBeenCalledTimes ( 1 ) ;
351
- expect ( config . onError ) . toHaveBeenCalledWith (
352
- `[React Intl] No time format named: ${ format } `
353
- ) ;
337
+ expect ( config . onError . mock . calls . map ( c => c [ 0 ] . code ) ) . toMatchSnapshot ( ) ;
354
338
} ) ;
355
339
356
340
it ( 'should set default values' , ( ) => {
@@ -420,12 +404,7 @@ describe('format API', () => {
420
404
421
405
it ( 'falls back and warns when no value is provided' , ( ) => {
422
406
expect ( formatRelativeTime ( ) ) . toBe ( 'undefined' ) ;
423
- expect ( config . onError ) . toHaveBeenCalledTimes ( 1 ) ;
424
- expect ( config . onError ) . toHaveBeenCalledWith (
425
- expect . stringContaining (
426
- '[React Intl] Error formatting relative time.\nRangeError:'
427
- )
428
- ) ;
407
+ expect ( config . onError . mock . calls . map ( c => c [ 0 ] . code ) ) . toMatchSnapshot ( ) ;
429
408
} ) ;
430
409
431
410
it ( 'falls back and warns when a non-finite value is provided' , ( ) => {
@@ -463,12 +442,7 @@ describe('format API', () => {
463
442
464
443
it ( 'falls back and warns on invalid IntlRelativeFormat options' , ( ) => {
465
444
expect ( formatRelativeTime ( 0 , 'invalid' ) ) . toBe ( '0' ) ;
466
- expect ( config . onError ) . toHaveBeenCalledTimes ( 1 ) ;
467
- expect ( config . onError ) . toHaveBeenCalledWith (
468
- expect . stringMatching (
469
- / \[ R e a c t I n t l \] E r r o r f o r m a t t i n g r e l a t i v e t i m e .\n R a n g e E r r o r : I n v a l i d u n i t ( .* ) i n v a l i d /
470
- )
471
- ) ;
445
+ expect ( config . onError . mock . calls . map ( c => c [ 0 ] . code ) ) . toMatchSnapshot ( ) ;
472
446
} ) ;
473
447
474
448
it ( 'uses configured named formats' , ( ) => {
@@ -505,10 +479,7 @@ describe('format API', () => {
505
479
expect ( formatRelativeTime ( - 1 , 'second' , { format} ) ) . toBe (
506
480
rf . format ( - 1 , 'second' )
507
481
) ;
508
- expect ( config . onError ) . toHaveBeenCalledTimes ( 1 ) ;
509
- expect ( config . onError ) . toHaveBeenCalledWith (
510
- `[React Intl] No relative format named: ${ format } `
511
- ) ;
482
+ expect ( config . onError . mock . calls . map ( c => c [ 0 ] . code ) ) . toMatchSnapshot ( ) ;
512
483
} ) ;
513
484
} ) ;
514
485
} ) ;
@@ -562,12 +533,7 @@ describe('format API', () => {
562
533
563
534
it ( 'falls back and warns on invalid Intl.NumberFormat options' , ( ) => {
564
535
expect ( formatNumber ( 0 , { style : 'invalid' } ) ) . toBe ( String ( 0 ) ) ;
565
- expect ( config . onError ) . toHaveBeenCalledTimes ( 1 ) ;
566
- expect ( config . onError ) . toHaveBeenCalledWith (
567
- expect . stringMatching (
568
- / \[ R e a c t I n t l \] E r r o r f o r m a t t i n g n u m b e r .\n R a n g e E r r o r : V a l u e i n v a l i d o u t o f r a n g e f o r ( .* ) o p t i o n s p r o p e r t y s t y l e /
569
- )
570
- ) ;
536
+ expect ( config . onError . mock . calls . map ( c => c [ 0 ] . code ) ) . toMatchSnapshot ( ) ;
571
537
} ) ;
572
538
573
539
it ( 'uses configured named formats' , ( ) => {
@@ -601,10 +567,7 @@ describe('format API', () => {
601
567
nf = new Intl . NumberFormat ( config . locale ) ;
602
568
603
569
expect ( formatNumber ( num , { format} ) ) . toBe ( nf . format ( num ) ) ;
604
- expect ( config . onError ) . toHaveBeenCalledTimes ( 1 ) ;
605
- expect ( config . onError ) . toHaveBeenCalledWith (
606
- `[React Intl] No number format named: ${ format } `
607
- ) ;
570
+ expect ( config . onError . mock . calls . map ( c => c [ 0 ] . code ) ) . toMatchSnapshot ( ) ;
608
571
} ) ;
609
572
} ) ;
610
573
} ) ;
@@ -815,11 +778,10 @@ describe('format API', () => {
815
778
)
816
779
) . toBe ( mf . format ( values ) ) ;
817
780
818
- expect ( config . onError . mock . calls ) . toMatchInlineSnapshot ( `
781
+ expect ( config . onError . mock . calls . map ( c => c [ 0 ] . code ) )
782
+ . toMatchInlineSnapshot ( `
819
783
Array [
820
- Array [
821
- "[React Intl] Missing message: \\"missing\\" for locale: \\"fr\\", using default message as fallback.",
822
- ],
784
+ "MISSING_TRANSLATION",
823
785
]
824
786
` ) ;
825
787
} ) ;
@@ -839,14 +801,11 @@ describe('format API', () => {
839
801
)
840
802
) . toBe ( id ) ;
841
803
842
- expect ( config . onError . mock . calls ) . toMatchInlineSnapshot ( `
804
+ expect ( config . onError . mock . calls . map ( c => c [ 0 ] . code ) )
805
+ . toMatchInlineSnapshot ( `
843
806
Array [
844
- Array [
845
- "[React Intl] Missing message: \\"missing\\" for locale: \\"en\\"",
846
- ],
847
- Array [
848
- "[React Intl] Cannot format message: \\"missing\\", using message id as fallback.",
849
- ],
807
+ "MISSING_TRANSLATION",
808
+ "DATE_FORMAT_ERROR",
850
809
]
851
810
` ) ;
852
811
} ) ;
@@ -867,12 +826,7 @@ describe('format API', () => {
867
826
)
868
827
) . toBe ( mf . format ( values ) ) ;
869
828
870
- expect ( config . onError ) . toHaveBeenCalledTimes ( 1 ) ;
871
- expect ( config . onError ) . toHaveBeenCalledWith (
872
- expect . stringContaining (
873
- `[React Intl] Error formatting message: "${ id } " for locale: "${ locale } ", using default message as fallback.`
874
- )
875
- ) ;
829
+ expect ( config . onError . mock . calls . map ( c => c [ 0 ] . code ) ) . toMatchSnapshot ( ) ;
876
830
} ) ;
877
831
878
832
it ( 'formats `defaultMessage` when message has missing values' , ( ) => {
@@ -891,12 +845,7 @@ describe('format API', () => {
891
845
)
892
846
) . toBe ( mf . format ( values ) ) ;
893
847
894
- expect ( config . onError ) . toHaveBeenCalledTimes ( 1 ) ;
895
- expect ( config . onError ) . toHaveBeenCalledWith (
896
- expect . stringContaining (
897
- `[React Intl] Error formatting message: "${ id } " for locale: "${ locale } ", using default message as fallback.`
898
- )
899
- ) ;
848
+ expect ( config . onError . mock . calls . map ( c => c [ 0 ] . code ) ) . toMatchSnapshot ( ) ;
900
849
} ) ;
901
850
902
851
it ( 'returns message source when message and `defaultMessage` have formatting errors' , ( ) => {
@@ -909,23 +858,7 @@ describe('format API', () => {
909
858
defaultMessage : messages . invalid ,
910
859
} )
911
860
) . toBe ( messages [ id ] ) ;
912
-
913
- expect ( config . onError ) . toHaveBeenCalledTimes ( 3 ) ;
914
- expect ( config . onError ) . toHaveBeenCalledWith (
915
- expect . stringContaining (
916
- `[React Intl] Error formatting message: "${ id } " for locale: "${ locale } "`
917
- )
918
- ) ;
919
- expect ( config . onError ) . toHaveBeenCalledWith (
920
- expect . stringContaining (
921
- `[React Intl] Error formatting the default message for: "${ id } "`
922
- )
923
- ) ;
924
- expect ( config . onError ) . toHaveBeenCalledWith (
925
- expect . stringContaining (
926
- `[React Intl] Cannot format message: "${ id } ", using message source as fallback.`
927
- )
928
- ) ;
861
+ expect ( config . onError . mock . calls . map ( c => c [ 0 ] . code ) ) . toMatchSnapshot ( ) ;
929
862
} ) ;
930
863
931
864
it ( 'returns message source when formatting error and missing `defaultMessage`' , ( ) => {
@@ -938,18 +871,7 @@ describe('format API', () => {
938
871
defaultMessage : messages . missing ,
939
872
} )
940
873
) . toBe ( messages [ id ] ) ;
941
-
942
- expect ( config . onError ) . toHaveBeenCalledTimes ( 2 ) ;
943
- expect ( config . onError ) . toHaveBeenCalledWith (
944
- expect . stringContaining (
945
- `[React Intl] Error formatting message: "${ id } " for locale: "${ locale } "`
946
- )
947
- ) ;
948
- expect ( config . onError ) . toHaveBeenCalledWith (
949
- expect . stringContaining (
950
- `[React Intl] Cannot format message: "${ id } ", using message source as fallback.`
951
- )
952
- ) ;
874
+ expect ( config . onError . mock . calls . map ( c => c [ 0 ] . code ) ) . toMatchSnapshot ( ) ;
953
875
} ) ;
954
876
955
877
it ( 'returns `defaultMessage` source when formatting errors and missing message' , ( ) => {
@@ -965,44 +887,19 @@ describe('format API', () => {
965
887
} )
966
888
) . toBe ( messages . invalid ) ;
967
889
968
- expect ( config . onError ) . toHaveBeenCalledTimes ( 3 ) ;
969
- expect ( config . onError ) . toHaveBeenCalledWith (
970
- expect . stringContaining (
971
- `[React Intl] Missing message: "${ id } " for locale: "${ locale } ", using default message as fallback.`
972
- )
973
- ) ;
974
- expect ( config . onError ) . toHaveBeenCalledWith (
975
- expect . stringContaining (
976
- `[React Intl] Error formatting the default message for: "${ id } "`
977
- )
978
- ) ;
979
- expect ( config . onError ) . toHaveBeenCalledWith (
980
- expect . stringContaining (
981
- `[React Intl] Cannot format message: "${ id } ", using message source as fallback.`
982
- )
983
- ) ;
890
+ expect ( config . onError . mock . calls . map ( c => c [ 0 ] . code ) ) . toMatchSnapshot ( ) ;
984
891
} ) ;
985
892
986
893
it ( 'returns message `id` when message and `defaultMessage` are missing' , ( ) => {
987
894
const id = 'missing' ;
988
895
989
896
expect ( formatMessage ( { id : id } ) ) . toBe ( id ) ;
990
897
991
- expect ( config . onError ) . toHaveBeenCalledTimes ( 2 ) ;
992
- expect ( config . onError ) . toHaveBeenCalledWith (
993
- expect . stringContaining (
994
- `[React Intl] Missing message: "${ id } " for locale: "${ config . locale } "`
995
- )
996
- ) ;
997
- expect ( config . onError ) . toHaveBeenCalledWith (
998
- expect . stringContaining (
999
- `[React Intl] Cannot format message: "${ id } ", using message id as fallback.`
1000
- )
1001
- ) ;
898
+ expect ( config . onError . mock . calls . map ( c => c [ 0 ] . code ) ) . toMatchSnapshot ( ) ;
1002
899
} ) ;
1003
900
1004
901
it ( 'returns message `id` when message and `defaultMessage` are empty' , ( ) => {
1005
- const { locale , messages} = config ;
902
+ const { messages} = config ;
1006
903
const id = 'empty' ;
1007
904
1008
905
expect (
@@ -1012,17 +909,7 @@ describe('format API', () => {
1012
909
} )
1013
910
) . toBe ( id ) ;
1014
911
1015
- expect ( config . onError ) . toHaveBeenCalledTimes ( 2 ) ;
1016
- expect ( config . onError ) . toHaveBeenCalledWith (
1017
- expect . stringContaining (
1018
- `[React Intl] Missing message: "${ id } " for locale: "${ locale } "`
1019
- )
1020
- ) ;
1021
- expect ( config . onError ) . toHaveBeenCalledWith (
1022
- expect . stringContaining (
1023
- `[React Intl] Cannot format message: "${ id } ", using message id as fallback.`
1024
- )
1025
- ) ;
912
+ expect ( config . onError . mock . calls . map ( c => c [ 0 ] . code ) ) . toMatchSnapshot ( ) ;
1026
913
} ) ;
1027
914
} ) ;
1028
915
} ) ;
0 commit comments