-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SaveMessage component to Header #2133
Changes from 18 commits
2855caf
f9071f0
53f3b9b
66aab17
d8adad1
8f932be
063f850
69e65a8
99421c4
d91c94b
ce556c7
4356bbd
022adc8
7f0ff45
b010620
017b419
f350adc
ea63be1
f27e9d5
d52a695
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import moment from "moment"; | ||
import { useEffect, useState } from "react"; | ||
import PropTypes from 'prop-types'; | ||
|
||
// Configure moment to display '1 time-unit ago' instead of 'a time-unit ago' | ||
// https://github.com/moment/moment/issues/3764 | ||
moment.updateLocale("en", { | ||
relativeTime: { | ||
s: "seconds", | ||
m: "1 minute", | ||
mm: "%d minutes", | ||
h: "1 hour", | ||
hh: "%d hours", | ||
d: "1 day", | ||
dd: "%d days", | ||
M: "1 month", | ||
MM: "%d months", | ||
y: "1 year", | ||
yy: "%d years", | ||
}, | ||
}); | ||
|
||
const SaveMessage = ({ lastSaved }) => { | ||
const [currentMoment, setCurrentMoment] = useState(() => moment()); | ||
|
||
useEffect(() => { | ||
const timerID = setInterval(() => setCurrentMoment(moment()), 1000); | ||
return () => clearInterval(timerID); | ||
}); | ||
|
||
const lastSavedMoment = moment(lastSaved); | ||
const difference = currentMoment.diff(lastSavedMoment); | ||
const duration = moment.duration(difference); | ||
let result = "Last saved "; | ||
|
||
if (duration.asMinutes() < 1) return "Saved"; | ||
|
||
if (duration.asDays() < 1) { | ||
result += lastSavedMoment.format("h:mm a"); | ||
} else if (duration.asYears() < 1) { | ||
result += lastSavedMoment.format("MMMM D"); | ||
} else { | ||
result += lastSavedMoment.format("MMMM D, YYYY"); | ||
} | ||
|
||
result += ` (${lastSavedMoment.fromNow()})`; | ||
return result; | ||
}; | ||
|
||
SaveMessage.propTypes = { | ||
lastSaved: PropTypes.oneOfType([ | ||
PropTypes.instanceOf(Date), | ||
PropTypes.instanceOf(moment), | ||
PropTypes.string | ||
]).isRequired, | ||
}; | ||
|
||
export default SaveMessage; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import React from "react"; | ||
import { create, act } from "react-test-renderer"; | ||
import moment from "moment"; | ||
import SaveMessage from "./SaveMessage"; | ||
|
||
describe("<SaveMessage />", () => { | ||
let subject; | ||
|
||
describe('when saved less than 1 minute ago, it displays "Saved"', () => { | ||
[ | ||
["1 second ago", 1], | ||
["2 seconds ago", 2], | ||
["30 seconds ago", 30], | ||
["59 seconds", 59], | ||
].forEach(([testName, seconds]) => { | ||
test(testName, () => { | ||
const lastSaved = moment().subtract(seconds, "seconds"); | ||
// https://reactjs.org/docs/test-renderer.html#testrendereract | ||
act(() => { | ||
subject = create(<SaveMessage lastSaved={lastSaved} />); | ||
}) | ||
expect(subject.toJSON()).toEqual("Saved"); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("when observed saved time changes to 1 minute ago", () => { | ||
const now = new Date(2020, 0, 1, 12, 0); | ||
const oneMinuteFromNow = new Date(2020, 0, 1, 12, 1); | ||
let mockDateNow; | ||
|
||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
mockDateNow = jest | ||
.spyOn(Date, "now") | ||
.mockReturnValueOnce(now) | ||
.mockReturnValueOnce(now) | ||
.mockReturnValueOnce(now) | ||
.mockReturnValue(oneMinuteFromNow); | ||
}); | ||
|
||
afterEach(() => { | ||
mockDateNow.mockRestore(); | ||
jest.clearAllTimers(); | ||
}); | ||
|
||
it('auto-updates from "Saved" to (1 minute ago)', () => { | ||
act(() => { | ||
subject = create(<SaveMessage lastSaved={now} />); | ||
}) | ||
expect(subject.toJSON()).toMatch("Saved"); | ||
act(() => jest.advanceTimersByTime(60 * 1000)); | ||
expect(subject.toJSON()).toMatch(/\(1 minute ago\)$/); | ||
Comment on lines
+48
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally not a hack. I think this is how React recommends doing tests if you've got hooks, really. It's possible we should being transitioning entirely to Again, something to put a pin in and revisit later. Definitely not something we have to figure out for this PR. |
||
}); | ||
}); | ||
|
||
describe("given current time is January 1, 2020 12:00 pm", () => { | ||
const jan1AtNoon = new Date(2020, 0, 1, 12, 0); | ||
let mockDateNow; | ||
|
||
beforeEach(() => { | ||
mockDateNow = jest | ||
.spyOn(Date, "now") | ||
.mockReturnValue(jan1AtNoon.getTime()); | ||
}); | ||
|
||
afterEach(() => { | ||
mockDateNow.mockRestore(); | ||
}); | ||
|
||
[ | ||
[1, "minute", "Last saved 11:59 am (1 minute ago)"], | ||
[60 * 24 - 1, "minutes", "Last saved 12:01 pm (1 day ago)"], | ||
[3, "hours", "Last saved 9:00 am (3 hours ago)"], | ||
[1, "day", "Last saved December 31 (1 day ago)"], | ||
[30, "days", "Last saved December 2 (1 month ago)"], | ||
[364, "days", "Last saved January 2 (1 year ago)"], | ||
[3, "years", "Last saved January 1, 2017 (3 years ago)"], | ||
].forEach(([value, timeUnit, result]) => { | ||
test(`when saved ${value} ${timeUnit} ago, it displays "${result}"`, () => { | ||
const lastSaved = moment().subtract(value, timeUnit); | ||
act(() => { | ||
subject = create(<SaveMessage lastSaved={lastSaved} />); | ||
}) | ||
expect(subject.toJSON()).toEqual(result); | ||
}); | ||
}); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to open a tech debt issue to come back and think about these tests at some point. These particular ones are a decent candidate for snapshots, for example.
Certainly not something worth changing now and maybe not worth changing at all! But let's put a pin in it and figure it out later. 😄