Skip to content

Commit 0a564dc

Browse files
committed
fix: fix format merging for message
fix #1500
1 parent ff2629b commit 0a564dc

File tree

6 files changed

+109
-41
lines changed

6 files changed

+109
-41
lines changed

src/formatters/message.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function deepMergeFormatsAndSetTimeZone(
6060
timeZone?: string
6161
): CustomFormats {
6262
if (!timeZone) {
63-
return {};
63+
return f1;
6464
}
6565
const mfFormats = IntlMessageFormat.formats;
6666
return {
@@ -128,7 +128,6 @@ export function formatMessage(
128128

129129
// `id` is a required field of a Message Descriptor.
130130
invariant(id, '[React Intl] An `id` must be provided to format a message.');
131-
132131
const message = messages && messages[id];
133132
formats = deepMergeFormatsAndSetTimeZone(formats, timeZone);
134133
defaultFormats = deepMergeFormatsAndSetTimeZone(defaultFormats, timeZone);

src/formatters/number.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ export function getFormatter(
3838
options: Parameters<IntlFormatters['formatNumber']>[1] = {}
3939
) {
4040
const {format} = options;
41-
let defaults =
42-
((format && getNamedFormat(formats!, 'number', format, onError)) || {}) as UnifiedNumberFormatOptions;
41+
let defaults = ((format &&
42+
getNamedFormat(formats!, 'number', format, onError)) ||
43+
{}) as UnifiedNumberFormatOptions;
4344
const filteredOptions = filterProps(options, NUMBER_FORMAT_OPTIONS, defaults);
4445

4546
return getNumberFormat(locale, filteredOptions);

test/unit/components/date.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,15 @@ describe('<FormattedDateParts>', () => {
187187

188188
mountPartsWithProvider({value: date + '', children}, intl);
189189

190-
expect(children.mock.calls[0][0]).toEqual(
191-
intl.formatDateToParts(date)
192-
);
190+
expect(children.mock.calls[0][0]).toEqual(intl.formatDateToParts(date));
193191
});
194192

195193
it('renders date 0 if value is ""', () => {
196194
const date = new Date(0);
197195

198196
mountPartsWithProvider({value: '', children}, intl);
199197

200-
expect(children.mock.calls[0][0]).toEqual(
201-
intl.formatDateToParts(date)
202-
);
198+
expect(children.mock.calls[0][0]).toEqual(intl.formatDateToParts(date));
203199
});
204200

205201
it('accepts `format` prop', () => {

test/unit/components/html-message.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22
import {mountFormattedComponentWithProvider} from '../testUtils';
33
import FormattedHTMLMessage from '../../../src/components/html-message';
44
import {createIntl} from '../../../src/components/provider';
5-
import {mount} from 'enzyme'
5+
import {mount} from 'enzyme';
66

77
const mountWithProvider = mountFormattedComponentWithProvider(
88
FormattedHTMLMessage
@@ -24,11 +24,13 @@ describe('<FormattedHTMLMessage>', () => {
2424
expect(FormattedHTMLMessage.displayName).toBeA('string');
2525
});
2626

27-
it('should throw if intl is not provided', function () {
28-
expect(() => mount(<FormattedHTMLMessage id="foo"/>)).toThrow(/Could not find required `intl` object./)
29-
})
27+
it('should throw if intl is not provided', function() {
28+
expect(() => mount(<FormattedHTMLMessage id="foo" />)).toThrow(
29+
/Could not find required `intl` object./
30+
);
31+
});
3032

31-
it('should use textComponent if tagName is ""', function () {
33+
it('should use textComponent if tagName is ""', function() {
3234
intl = createIntl({
3335
locale: 'en',
3436
defaultLocale: 'en-US',
@@ -37,17 +39,17 @@ describe('<FormattedHTMLMessage>', () => {
3739
const descriptor = {
3840
id: 'hello',
3941
defaultMessage: 'Hello, <b>World</b>!',
40-
tagName: ''
41-
}
42+
tagName: '',
43+
};
4244

4345
const rendered = mountWithProvider(descriptor, intl).find('p');
4446

4547
expect(rendered.prop('dangerouslySetInnerHTML')).toEqual({
4648
__html: intl.formatHTMLMessage(descriptor),
4749
});
48-
})
50+
});
4951

50-
it('should use span if textComponent & tagName is undefined', function () {
52+
it('should use span if textComponent & tagName is undefined', function() {
5153
intl = createIntl({
5254
locale: 'en',
5355
defaultLocale: 'en-US',
@@ -56,15 +58,15 @@ describe('<FormattedHTMLMessage>', () => {
5658
const descriptor = {
5759
id: 'hello',
5860
defaultMessage: 'Hello, <b>World</b>!',
59-
tagName: undefined
60-
}
61+
tagName: undefined,
62+
};
6163

6264
const rendered = mountWithProvider(descriptor, intl).find('span');
6365

6466
expect(rendered.prop('dangerouslySetInnerHTML')).toEqual({
6567
__html: intl.formatHTMLMessage(descriptor),
6668
});
67-
})
69+
});
6870

6971
it('renders a formatted HTML message in a <span>', () => {
7072
const descriptor = {

test/unit/components/message.tsx

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,13 @@ describe('<FormattedMessage>', () => {
160160
const descriptor = {
161161
id: 'hello',
162162
defaultMessage: 'Hello, World!',
163-
tagName: ''
163+
tagName: '',
164164
};
165165

166-
const rendered = mountWithProvider(
167-
descriptor,
168-
{...providerProps, textComponent: undefined}
169-
);
166+
const rendered = mountWithProvider(descriptor, {
167+
...providerProps,
168+
textComponent: undefined,
169+
});
170170

171171
expect(rendered.text()).toBe(intl.formatMessage(descriptor));
172172
});
@@ -314,15 +314,17 @@ describe('<FormattedMessage>', () => {
314314
time: {
315315
short: {
316316
second: 'numeric',
317-
timeZoneName: 'long'
318-
}
319-
}
317+
timeZoneName: 'long',
318+
},
319+
},
320320
},
321321
timeZone: 'Asia/Tokyo',
322322
}
323323
);
324324

325-
expect(rendered.text()).toBe('Hello, 1/1/70 - 9:00:00 AM Japan Standard Time');
325+
expect(rendered.text()).toBe(
326+
'Hello, 1/1/70 - 9:00:00 AM Japan Standard Time'
327+
);
326328
});
327329

328330
it('should merge timeZone into defaultFormats', function() {
@@ -340,15 +342,87 @@ describe('<FormattedMessage>', () => {
340342
time: {
341343
short: {
342344
second: 'numeric',
343-
timeZoneName: 'long'
344-
}
345-
}
345+
timeZoneName: 'long',
346+
},
347+
},
348+
},
349+
timeZone: 'Asia/Tokyo',
350+
}
351+
);
352+
353+
expect(rendered.text()).toBe(
354+
'Hello, 1/1/70 - 9:00:00 AM Japan Standard Time'
355+
);
356+
});
357+
358+
it('should handle defaultFormat merge correctly', function() {
359+
const rendered = mountWithProvider(
360+
{
361+
id: 'hello',
362+
defaultMessage: 'The day is {now, date, weekday-long}.',
363+
values: {
364+
now: new Date(0),
365+
},
366+
},
367+
{
368+
...providerProps,
369+
defaultLocale: undefined,
370+
formats: {
371+
date: {
372+
'weekday-long': {weekday: 'long'},
373+
},
374+
time: {
375+
hour: {hour: 'numeric'},
376+
},
377+
},
378+
defaultFormats: {
379+
date: {
380+
'weekday-long': {weekday: 'long'},
381+
},
382+
time: {
383+
hour: {hour: 'numeric'},
384+
},
385+
},
386+
timeZone: undefined,
387+
}
388+
);
389+
390+
expect(rendered.text()).toBe('The day is Wednesday.');
391+
});
392+
393+
it('should handle defaultFormat merge correctly w/ timeZone', function() {
394+
const rendered = mountWithProvider(
395+
{
396+
id: 'hello',
397+
defaultMessage: 'The day is {now, date, weekday-long}.',
398+
values: {
399+
now: new Date(0),
400+
},
401+
},
402+
{
403+
...providerProps,
404+
defaultLocale: undefined,
405+
formats: {
406+
date: {
407+
'weekday-long': {weekday: 'long'},
408+
},
409+
time: {
410+
hour: {hour: 'numeric'},
411+
},
412+
},
413+
defaultFormats: {
414+
date: {
415+
'weekday-long': {weekday: 'long'},
416+
},
417+
time: {
418+
hour: {hour: 'numeric'},
419+
},
346420
},
347421
timeZone: 'Asia/Tokyo',
348422
}
349423
);
350424

351-
expect(rendered.text()).toBe('Hello, 1/1/70 - 9:00:00 AM Japan Standard Time');
425+
expect(rendered.text()).toBe('The day is Thursday.');
352426
});
353427

354428
it('should re-render when `values` are different', () => {

test/unit/components/time.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,19 +178,15 @@ describe('<FormattedTimeParts>', () => {
178178

179179
mountPartsWithProvider({value: date.toISOString(), children}, intl);
180180

181-
expect(children.mock.calls[0][0]).toEqual(
182-
intl.formatTimeToParts(date)
183-
);
181+
expect(children.mock.calls[0][0]).toEqual(intl.formatTimeToParts(date));
184182
});
185183

186184
it('renders date 0 if value is ""', () => {
187185
const date = new Date(0);
188186

189187
mountPartsWithProvider({value: '', children}, intl);
190188

191-
expect(children.mock.calls[0][0]).toEqual(
192-
intl.formatTimeToParts(date)
193-
);
189+
expect(children.mock.calls[0][0]).toEqual(intl.formatTimeToParts(date));
194190
});
195191

196192
it('falls back and warns on invalid Intl.DateTimeFormat options', () => {

0 commit comments

Comments
 (0)