Skip to content

Commit

Permalink
fix: FormattedRelativeTime with high seconds values (#1385)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
djmarland authored and longlho committed Jul 29, 2019
1 parent 00cbf80 commit a7f1dfa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/components/relative.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ class FormattedRelativeTime extends React.PureComponent<Props, State> {
const nextUnit = selectUnit(nextValueInSeconds);
// We've reached the max auto incrementable unit, don't schedule another update
if (nextUnit === 'day') {
return this.setState({
currentValueInSeconds: nextValueInSeconds < 0 ? -DAY : DAY,
});
return;
}

const unitDuration = getDurationInSeconds(nextUnit);
Expand Down
11 changes: 11 additions & 0 deletions test/unit/components/relative.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ describe('<FormattedRelative>', () => {
(rendered.find(BaseFormattedRelativeTime).instance() as any)._updateTimer
).toBeNull();
});
it('should show high seconds values as days with no timer', function() {
// span bc enzyme support for </> seems buggy
const rendered = mountWithProvider(
{value: -(60 * 60 * 24 * 3), unit: 'second', updateIntervalInSeconds: 1},
{...intl, textComponent: 'span'}
).find(BaseFormattedRelativeTime);
expect(rendered.text()).toBe(intl.formatRelativeTime(-3, 'day'));
expect(
(rendered.find(BaseFormattedRelativeTime).instance() as any)._updateTimer
).toBeNull();
});
it('should throw if try to increment in day', function() {
// span bc enzyme support for </> seems buggy
expect(() =>
Expand Down

0 comments on commit a7f1dfa

Please sign in to comment.