@@ -5,48 +5,70 @@ import {
55 LocalizationProvider ,
66 Localized
77} from "../esm/index" ;
8+ import { FluentBundle , FluentResource } from "@fluent/bundle" ;
89
910describe ( "Localized - validation" , ( ) => {
10- test ( "inside of a LocalizationProvider" , ( ) => {
11+ function createValidBundle ( ) {
12+ const validBundle = new FluentBundle ( ) ;
13+ validBundle . addResource (
14+ new FluentResource ( "example-id = Example localized text\n" )
15+ ) ;
16+ return validBundle ;
17+ }
18+
19+ test ( "renders with no errors or warnings when correctly created inside of a LocalizationProvider" , ( ) => {
1120 const renderer = TestRenderer . create (
12- < LocalizationProvider l10n = { new ReactLocalization ( [ ] ) } >
13- < Localized >
21+ < LocalizationProvider l10n = { new ReactLocalization ( [ createValidBundle ( ) ] ) } >
22+ < Localized id = "example-id" >
1423 < div />
1524 </ Localized >
1625 </ LocalizationProvider >
1726 ) ;
1827
19- expect ( renderer . toJSON ( ) ) . toMatchInlineSnapshot ( `<div />` ) ;
28+ expect ( renderer . toJSON ( ) ) . toMatchInlineSnapshot ( `
29+ <div>
30+ Example localized text
31+ </div>
32+ ` ) ;
2033 } ) ;
2134
22- test ( "outside of a LocalizationProvider" , ( ) => {
23- const renderer = TestRenderer . create (
24- < Localized >
25- < div />
26- </ Localized >
35+ test ( "throws an error when placed outside of a LocalizationProvider" , ( ) => {
36+ jest . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
37+
38+ expect ( ( ) => {
39+ TestRenderer . create (
40+ < Localized id = "example-id" >
41+ < div />
42+ </ Localized >
43+ ) ;
44+ } ) . toThrowErrorMatchingInlineSnapshot (
45+ `"The <Localized /> component was not properly wrapped in a <LocalizationProvider />."`
2746 ) ;
2847
29- expect ( renderer . toJSON ( ) ) . toMatchInlineSnapshot ( `<div />` ) ;
48+ // React also does a console.error.
49+ expect ( console . error ) . toHaveBeenCalled ( ) ;
3050 } ) ;
3151
32- test ( "without a child" , ( ) => {
52+ test ( "renders the localized text when no child is provided " , ( ) => {
3353 const renderer = TestRenderer . create (
34- < LocalizationProvider l10n = { new ReactLocalization ( [ ] ) } >
35- < Localized />
54+ < LocalizationProvider l10n = { new ReactLocalization ( [ createValidBundle ( ) ] ) } >
55+ < Localized id = "example-id" />
3656 </ LocalizationProvider >
3757 ) ;
3858
39- expect ( renderer . toJSON ( ) ) . toMatchInlineSnapshot ( `null ` ) ;
59+ expect ( renderer . toJSON ( ) ) . toMatchInlineSnapshot ( `"Example localized text" ` ) ;
4060 } ) ;
4161
42- test ( "with multiple children" , ( ) => {
62+ test ( "throws when multiple children are provided " , ( ) => {
4363 // React also does a console.error, ignore that here.
4464 jest . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
4565
4666 expect ( ( ) => {
4767 TestRenderer . create (
48- < LocalizationProvider l10n = { new ReactLocalization ( [ ] ) } >
49- < Localized >
68+ < LocalizationProvider
69+ l10n = { new ReactLocalization ( [ createValidBundle ( ) ] ) }
70+ >
71+ < Localized id = "example-id" >
5072 < div />
5173 < div />
5274 </ Localized >
@@ -57,17 +79,23 @@ describe("Localized - validation", () => {
5779 expect ( console . error ) . toHaveBeenCalled ( ) ;
5880 } ) ;
5981
60- test ( "with single children inside an array" , ( ) => {
82+ test ( "is valid to have a children array with a single element inside " , ( ) => {
6183 const renderer = TestRenderer . create (
62- < LocalizationProvider l10n = { new ReactLocalization ( [ ] ) } >
63- < Localized children = { [ < div /> ] } />
84+ < LocalizationProvider l10n = { new ReactLocalization ( [ createValidBundle ( ) ] ) } >
85+ < Localized id = "example-id" children = { [ < div /> ] } />
6486 </ LocalizationProvider >
6587 ) ;
6688
67- expect ( renderer . toJSON ( ) ) . toMatchInlineSnapshot ( `<div />` ) ;
89+ expect ( renderer . toJSON ( ) ) . toMatchInlineSnapshot ( `
90+ <div>
91+ Example localized text
92+ </div>
93+ ` ) ;
6894 } ) ;
6995
70- test ( "without id" , ( ) => {
96+ test ( "has a warning when no id is provided" , ( ) => {
97+ jest . spyOn ( console , "warn" ) . mockImplementation ( ( ) => { } ) ;
98+
7199 const renderer = TestRenderer . create (
72100 < LocalizationProvider l10n = { new ReactLocalization ( [ ] ) } >
73101 < Localized >
@@ -77,5 +105,13 @@ describe("Localized - validation", () => {
77105 ) ;
78106
79107 expect ( renderer . toJSON ( ) ) . toMatchInlineSnapshot ( `<div />` ) ;
108+
109+ expect ( console . warn . mock . calls ) . toMatchInlineSnapshot ( `
110+ Array [
111+ Array [
112+ "[@fluent/react] Error: No id was provided for a <Localized /> component.",
113+ ],
114+ ]
115+ ` ) ;
80116 } ) ;
81117} ) ;
0 commit comments