Skip to content

Commit

Permalink
Explicitly test reporting errors in fluent-react (#520)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
gregtatum authored Jan 7, 2021
1 parent 72f5970 commit a1ac29d
Show file tree
Hide file tree
Showing 11 changed files with 576 additions and 375 deletions.
6 changes: 6 additions & 0 deletions fluent-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
"engines": {
"node": ">=10.0.0"
},
"jest": {
"setupFilesAfterEnv": [
"./test/setup.js"
],
"restoreMocks": true
},
"dependencies": {
"@fluent/sequence": "0.5.0",
"cached-iterable": "^0.2.1",
Expand Down
23 changes: 16 additions & 7 deletions fluent-react/test/localized_change.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React from "react";
import TestRenderer from "react-test-renderer";
import { FluentBundle, FluentResource } from "@fluent/bundle";
import { ReactLocalization, LocalizationProvider, Localized }
from "../esm/index";
import {
ReactLocalization,
LocalizationProvider,
Localized
} from "../esm/index";

test("relocalizes", () => {
const Root = ({ l10n }) => (
Expand All @@ -14,20 +17,26 @@ test("relocalizes", () => {
);

const bundle1 = new FluentBundle();
bundle1.addResource(new FluentResource(`
bundle1.addResource(
new FluentResource(`
foo = FOO
`));
const renderer = TestRenderer.create(<Root l10n={new ReactLocalization([bundle1])} />);
`)
);
const renderer = TestRenderer.create(
<Root l10n={new ReactLocalization([bundle1])} />
);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
<div>
FOO
</div>
`);

const bundle2 = new FluentBundle();
bundle2.addResource(new FluentResource(`
bundle2.addResource(
new FluentResource(`
foo = BAR
`));
`)
);
renderer.update(<Root l10n={new ReactLocalization([bundle2])} />);
expect(renderer.toJSON()).toMatchInlineSnapshot(`
<div>
Expand Down
31 changes: 21 additions & 10 deletions fluent-react/test/localized_fallback.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import React from "react";
import TestRenderer from "react-test-renderer";
import { FluentBundle, FluentResource } from "@fluent/bundle";
import { ReactLocalization, LocalizationProvider, Localized }
from "../esm/index";
import {
ReactLocalization,
LocalizationProvider,
Localized
} from "../esm/index";

test("uses message from 1st bundle", () => {
const bundle1 = new FluentBundle();
bundle1.addResource(new FluentResource(`
bundle1.addResource(
new FluentResource(`
foo = FOO
`));
`)
);

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

bundle1.addResource(new FluentResource(`
bundle1.addResource(
new FluentResource(`
not-foo = NOT FOO
`));
bundle2.addResource(new FluentResource(`
`)
);
bundle2.addResource(
new FluentResource(`
foo = FOO
`));
`)
);

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

test("falls back back for missing message", function() {
const bundle1 = new FluentBundle();
bundle1.addResource(new FluentResource(`
bundle1.addResource(
new FluentResource(`
not-foo = NOT FOO
`));
`)
);

const renderer = TestRenderer.create(
<LocalizationProvider l10n={new ReactLocalization([bundle1])}>
Expand Down
Loading

0 comments on commit a1ac29d

Please sign in to comment.