Skip to content

Commit 82336ad

Browse files
Frédéric Misereyrxaviers
authored andcommitted
Message: Ensure spread value is always an array (bugfix)
or else simple FormattedText was output as individual characters (`“t””h””i””s”`). Added some test to prevent this as well as ensuring the proper structure of Replaced Elements. Borrowed alwaysArray from Globalizejs. Ref #76 Signed-off-by: Frédéric Miserey <frederic@none.net>
1 parent d5e1eac commit 82336ad

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

src/generator.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from "react";
22
import Globalize from "globalize";
3+
import alwaysArray from "./util/always-array";
34

45
var commonPropNames = ["elements", "locale"];
56

@@ -46,8 +47,8 @@ function generator(fn, localPropNames, options) {
4647
this.globalizePropValues[0] = props.children;
4748

4849
beforeFormat.call(this, props);
49-
var formattedValue = this.globalize[fn].apply(this.globalize, this.globalizePropValues);
50-
this.value = afterFormat.call(this, formattedValue);
50+
var formattedValue = this.globalize[fn](...this.globalizePropValues);
51+
this.value = alwaysArray(afterFormat.call(this, formattedValue));
5152
}
5253

5354
render() {

src/util/always-array.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function alwaysArray(stringOrArray) {
2+
return Array.isArray(stringOrArray) ? stringOrArray : stringOrArray ? [stringOrArray] : [];
3+
}

test/formatMessage.spec.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ Globalize.loadMessages({
5151
expect(wrapper.text()).to.equal("Hi");
5252
});
5353

54+
it("outputs strings not as an array of characters", () => {
55+
const wrapper = shallow(<FormatMessage>Hi</FormatMessage>);
56+
expect(wrapper.children().getElements().length).to.equal(1);
57+
});
58+
5459
it("resolves path and prints 'Hi'", () => {
5560
const wrapper = shallow(<FormatMessage path="salutations/hi" />);
5661
expect(wrapper.text()).to.equal("Hi");
@@ -64,10 +69,12 @@ Globalize.loadMessages({
6469
it("properly replaces elements", () => {
6570
const wrapper = shallow(<FormatMessage path="elements/rglink" elements={{reactGlobalizeLink: <a href="https://github.com/jquery-support/react-globalize"></a>}} />);
6671
expect(wrapper.html()).to.equal("<span>For more information, see <a href=\"https://github.com/jquery-support/react-globalize\">React Globalize</a></span>");
72+
expect(wrapper.children().getElements().length).to.equal(2);
73+
expect(wrapper.children().get(1).type).to.equal("a");
6774
});
6875

6976
it("uses proper gender inflection", () => {
70-
const wrapper = shallow(<FormatMessage path="party" variables={{guest:"Mozart", guestGender:"male", host:"Beethoven", hostGender:"other"}} />);
77+
const wrapper = shallow(<FormatMessage path="party" variables={{guest: "Mozart", guestGender: "male", host: "Beethoven", hostGender: "other"}} />);
7178
expect(wrapper.text()).to.equal("Beethoven invites Mozart to their party");
7279
});
7380

0 commit comments

Comments
 (0)