Skip to content

Commit 0816b85

Browse files
committed
use new context API in withLocalization and update its tests
1 parent 55db0be commit 0816b85

File tree

9 files changed

+216
-309
lines changed

9 files changed

+216
-309
lines changed

fluent-react/src/provider.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ LocalizationProvider.propTypes = {
8787
function isIterable(props, propName, componentName) {
8888
const prop = props[propName];
8989

90+
if (!prop) {
91+
return new Error(
92+
`The ${propName} prop supplied to ${componentName} is required.`
93+
)
94+
}
95+
9096
if (Symbol.iterator in Object(prop)) {
9197
return null;
9298
}

fluent-react/src/with_localization.js

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,26 @@
1-
import { createElement, Component } from "react";
1+
import { createElement, useContext } from "react";
2+
import FluentContext from "./context";
23

34
export default function withLocalization(Inner) {
4-
class WithLocalization extends Component {
5-
/*
6-
* Find a translation by `id` and format it to a string using `args`.
7-
*/
8-
getString(id, args, fallback) {
9-
const { l10n } = this.context;
10-
11-
if (!l10n) {
12-
return fallback || id;
13-
}
14-
15-
return l10n.getString(id, args, fallback);
16-
}
17-
18-
render() {
19-
return createElement(
20-
Inner,
21-
Object.assign(
22-
// getString needs to be re-bound on updates to trigger a re-render
23-
{ getString: (...args) => this.getString(...args) },
24-
this.props
25-
)
26-
);
27-
}
5+
function WithDisplay(props) {
6+
const { l10n } = useContext(FluentContext);
7+
return createElement(
8+
Inner,
9+
// getString needs to be re-bound on updates to trigger a re-render
10+
{
11+
getString: (id, args, fallback) => (
12+
l10n
13+
? l10n.getString(id, args, fallback)
14+
: fallback || id
15+
),
16+
...props
17+
},
18+
);
2819
}
2920

30-
WithLocalization.displayName = `WithLocalization(${displayName(Inner)})`;
21+
WithDisplay.displayName = `WithLocalization(${displayName(Inner)})`;
3122

32-
return WithLocalization;
23+
return WithDisplay;
3324
}
3425

3526
function displayName(component) {

fluent-react/test/exports.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as FluentReact from '../src/index';
2+
import LocalizationProvider from '../src/provider';
3+
import Localized from '../src/localized';
4+
import withLocalization from '../src/with_localization';
5+
6+
describe('Exports', () => {
7+
test('LocalizationProvider', () => {
8+
expect(FluentReact.LocalizationProvider).toBe(LocalizationProvider);
9+
});
10+
11+
test('Localized', () => {
12+
expect(FluentReact.Localized).toBe(Localized);
13+
});
14+
15+
test('withLocalization', () => {
16+
expect(FluentReact.withLocalization).toBe(withLocalization);
17+
});
18+
});

fluent-react/test/exports_test.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

fluent-react/test/localized_void_test.js

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React from "react";
2+
import TestRenderer from "react-test-renderer";
3+
import { LocalizationProvider } from "../src/index";
4+
5+
describe("LocalizationProvider - validation", () => {
6+
let consoleError = console.error;
7+
8+
beforeAll(() => {
9+
console.error = (message) => {
10+
if (/(Failed prop type)/.test(message)) {
11+
throw new Error(message);
12+
}
13+
};
14+
});
15+
16+
afterAll(() => {
17+
console.error = consoleError;
18+
});
19+
20+
test("valid use", () => {
21+
const renderer = TestRenderer.create(
22+
<LocalizationProvider bundles={[]}>
23+
<div />
24+
</LocalizationProvider>
25+
);
26+
expect(renderer.toJSON()).toMatchInlineSnapshot(`<div />`);
27+
});
28+
29+
test("without a child", () => {
30+
expect(() => {
31+
TestRenderer.create(<LocalizationProvider bundles={[]} />);
32+
}).toThrow(/required/);
33+
});
34+
35+
test("without bundles", () => {
36+
expect(() => {
37+
TestRenderer.create(<LocalizationProvider />);
38+
}).toThrow(/is required/);
39+
});
40+
41+
test("without iterable bundles", () => {
42+
expect(() => {
43+
TestRenderer.create(<LocalizationProvider bundles={0} />);
44+
}).toThrow(/must be an iterable/);
45+
});
46+
});

fluent-react/test/provider_valid_test.js

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)