diff --git a/fluent-react/src/localization.ts b/fluent-react/src/localization.ts index 3cb59292..e9e1250d 100644 --- a/fluent-react/src/localization.ts +++ b/fluent-react/src/localization.ts @@ -15,6 +15,12 @@ import voidElementTags from "../vendor/voidElementTags.js"; // &, &, &. const reMarkup = /<|&#?\w+;/; +const defaultReportError = (error: Error): void => { + /* global console */ + // eslint-disable-next-line no-console + console.warn(`[@fluent/react] ${error.name}: ${error.message}`); +}; + /** * `ReactLocalization` handles translation formatting and fallback. * @@ -29,13 +35,16 @@ const reMarkup = /<|&#?\w+;/; export class ReactLocalization { public bundles: Iterable; public parseMarkup: MarkupParser | null; + public reportError: (error: Error) => void; constructor( bundles: Iterable, - parseMarkup: MarkupParser | null = createParseMarkup() + parseMarkup: MarkupParser | null = createParseMarkup(), + reportError?: (error: Error) => void ) { this.bundles = CachedSyncIterable.from(bundles); this.parseMarkup = parseMarkup; + this.reportError = reportError || defaultReportError; } getBundle(id: string): FluentBundle | null { @@ -227,12 +236,4 @@ export class ReactLocalization { return cloneElement(sourceElement, localizedProps, ...translatedChildren); } - - // XXX Control this via a prop passed to the LocalizationProvider. - // See https://github.com/projectfluent/fluent.js/issues/411. - reportError(error: Error): void { - /* global console */ - // eslint-disable-next-line no-console - console.warn(`[@fluent/react] ${error.name}: ${error.message}`); - } } diff --git a/fluent-react/test/localized_valid.test.js b/fluent-react/test/localized_valid.test.js index baabc164..17f900fe 100644 --- a/fluent-react/test/localized_valid.test.js +++ b/fluent-react/test/localized_valid.test.js @@ -114,4 +114,20 @@ describe("Localized - validation", () => { ] `); }); + + test("Calls provided logger function, instead of default, when no id is provided", () => { + jest.spyOn(console, "warn").mockImplementation(() => {}); + const mockReportError = jest.fn(); + const renderer = TestRenderer.create( + + +
+ + + ); + + expect(renderer.toJSON()).toMatchInlineSnapshot(`
`); + expect(console.warn.mock.calls).toMatchInlineSnapshot(`[]`); + expect(mockReportError).toHaveBeenCalledWith(new Error('No string id was provided when localizing a component.')); + }); });