Skip to content

Commit 9c817b1

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 globalizejs#76 Signed-off-by: Frédéric Miserey <[email protected]>
1 parent 6af11a3 commit 9c817b1

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/generator.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import reactCreateClass from "./react-create-class";
22
import React from "react";
33
import Globalize from "globalize";
4+
import alwaysArray from "./util/always-array";
45

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

@@ -45,7 +46,7 @@ function generator(fn, localPropNames, options) {
4546

4647
beforeFormat.call(this, props);
4748
var formattedValue = this.globalize[fn].apply(this.globalize, this.globalizePropValues);
48-
this.value = afterFormat.call(this, formattedValue);
49+
this.value = alwaysArray(afterFormat.call(this, formattedValue));
4950
},
5051
render: function() {
5152
return React.DOM.span(this.domProps, this.value);

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)