Skip to content

Commit a1ac29d

Browse files
authored
Explicitly test reporting errors in fluent-react (#520)
* Run prettier on test files to minimize inline snapshot code diff Inline snapshots in Jest are powered by Prettier, however prettier does not appear to be hooked up to the eslint. https://jestjs.io/docs/en/snapshot-testing#inline-snapshots In the following commits I add new inline snapshot tests, which invalidates a lot of the existing formatting. This commit runs prettier on the entire test directory to ensure that the following commits have clean code diffs. * Spy on console.warn in tests, and throw an error if called The current tests aren't explicit on testing the behavior of what errors get reported. This makes it difficult to understand the current behavior. This commit adds a mechanism to fail a test if the console.warn is called unexpectedly. Following commits will fix up tests that fail from this new mechanism. * Add explicit checks for what happens with console.warn in tests These tests use the toMatchInlineSnapshot method to assert that an error message was called. The snapshot allows for easy updating if the error messages are re-phrased in the future.
1 parent 72f5970 commit a1ac29d

11 files changed

+576
-375
lines changed

fluent-react/package.json

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
"engines": {
4545
"node": ">=10.0.0"
4646
},
47+
"jest": {
48+
"setupFilesAfterEnv": [
49+
"./test/setup.js"
50+
],
51+
"restoreMocks": true
52+
},
4753
"dependencies": {
4854
"@fluent/sequence": "0.5.0",
4955
"cached-iterable": "^0.2.1",

fluent-react/test/localized_change.test.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import React from "react";
22
import TestRenderer from "react-test-renderer";
33
import { FluentBundle, FluentResource } from "@fluent/bundle";
4-
import { ReactLocalization, LocalizationProvider, Localized }
5-
from "../esm/index";
4+
import {
5+
ReactLocalization,
6+
LocalizationProvider,
7+
Localized
8+
} from "../esm/index";
69

710
test("relocalizes", () => {
811
const Root = ({ l10n }) => (
@@ -14,20 +17,26 @@ test("relocalizes", () => {
1417
);
1518

1619
const bundle1 = new FluentBundle();
17-
bundle1.addResource(new FluentResource(`
20+
bundle1.addResource(
21+
new FluentResource(`
1822
foo = FOO
19-
`));
20-
const renderer = TestRenderer.create(<Root l10n={new ReactLocalization([bundle1])} />);
23+
`)
24+
);
25+
const renderer = TestRenderer.create(
26+
<Root l10n={new ReactLocalization([bundle1])} />
27+
);
2128
expect(renderer.toJSON()).toMatchInlineSnapshot(`
2229
<div>
2330
FOO
2431
</div>
2532
`);
2633

2734
const bundle2 = new FluentBundle();
28-
bundle2.addResource(new FluentResource(`
35+
bundle2.addResource(
36+
new FluentResource(`
2937
foo = BAR
30-
`));
38+
`)
39+
);
3140
renderer.update(<Root l10n={new ReactLocalization([bundle2])} />);
3241
expect(renderer.toJSON()).toMatchInlineSnapshot(`
3342
<div>

fluent-react/test/localized_fallback.test.js

+21-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import React from "react";
22
import TestRenderer from "react-test-renderer";
33
import { FluentBundle, FluentResource } from "@fluent/bundle";
4-
import { ReactLocalization, LocalizationProvider, Localized }
5-
from "../esm/index";
4+
import {
5+
ReactLocalization,
6+
LocalizationProvider,
7+
Localized
8+
} from "../esm/index";
69

710
test("uses message from 1st bundle", () => {
811
const bundle1 = new FluentBundle();
9-
bundle1.addResource(new FluentResource(`
12+
bundle1.addResource(
13+
new FluentResource(`
1014
foo = FOO
11-
`));
15+
`)
16+
);
1217

1318
const renderer = TestRenderer.create(
1419
<LocalizationProvider l10n={new ReactLocalization([bundle1])}>
@@ -29,12 +34,16 @@ test("uses message from the 2nd bundle", function() {
2934
const bundle1 = new FluentBundle();
3035
const bundle2 = new FluentBundle();
3136

32-
bundle1.addResource(new FluentResource(`
37+
bundle1.addResource(
38+
new FluentResource(`
3339
not-foo = NOT FOO
34-
`));
35-
bundle2.addResource(new FluentResource(`
40+
`)
41+
);
42+
bundle2.addResource(
43+
new FluentResource(`
3644
foo = FOO
37-
`));
45+
`)
46+
);
3847

3948
const renderer = TestRenderer.create(
4049
<LocalizationProvider l10n={new ReactLocalization([bundle1, bundle2])}>
@@ -53,9 +62,11 @@ foo = FOO
5362

5463
test("falls back back for missing message", function() {
5564
const bundle1 = new FluentBundle();
56-
bundle1.addResource(new FluentResource(`
65+
bundle1.addResource(
66+
new FluentResource(`
5767
not-foo = NOT FOO
58-
`));
68+
`)
69+
);
5970

6071
const renderer = TestRenderer.create(
6172
<LocalizationProvider l10n={new ReactLocalization([bundle1])}>

0 commit comments

Comments
 (0)