Skip to content

Commit a7f1dfa

Browse files
djmarlandlonglho
authored andcommitted
fix: FormattedRelativeTime with high seconds values (#1385)
* Fix FormattedRelativeTime with high seconds values Expected: For an item with an age of 3 days, on initial load don't add the timer but do show "3 days ago" Actual: Item shows "1 day ago" now matter how old it is The FormattedRelativeTime intends to stop using a timer once it goes over one day. However, at the point it removes this timer it also fixes the value at one day, so the display can only ever be "1 day ago", which is incorrect. This fix removes the overriding of the current state value, but continues to bypass the timer. It also adds a test for this case (which was failing before the fix)
1 parent 00cbf80 commit a7f1dfa

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/components/relative.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@ class FormattedRelativeTime extends React.PureComponent<Props, State> {
116116
const nextUnit = selectUnit(nextValueInSeconds);
117117
// We've reached the max auto incrementable unit, don't schedule another update
118118
if (nextUnit === 'day') {
119-
return this.setState({
120-
currentValueInSeconds: nextValueInSeconds < 0 ? -DAY : DAY,
121-
});
119+
return;
122120
}
123121

124122
const unitDuration = getDurationInSeconds(nextUnit);

test/unit/components/relative.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,17 @@ describe('<FormattedRelative>', () => {
194194
(rendered.find(BaseFormattedRelativeTime).instance() as any)._updateTimer
195195
).toBeNull();
196196
});
197+
it('should show high seconds values as days with no timer', function() {
198+
// span bc enzyme support for </> seems buggy
199+
const rendered = mountWithProvider(
200+
{value: -(60 * 60 * 24 * 3), unit: 'second', updateIntervalInSeconds: 1},
201+
{...intl, textComponent: 'span'}
202+
).find(BaseFormattedRelativeTime);
203+
expect(rendered.text()).toBe(intl.formatRelativeTime(-3, 'day'));
204+
expect(
205+
(rendered.find(BaseFormattedRelativeTime).instance() as any)._updateTimer
206+
).toBeNull();
207+
});
197208
it('should throw if try to increment in day', function() {
198209
// span bc enzyme support for </> seems buggy
199210
expect(() =>

0 commit comments

Comments
 (0)