Skip to content
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

Merged
merged 20 commits into from Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Expand Up @@ -4,6 +4,8 @@ Anticipated release: April 6, 2020

#### 🚀 New features

- The header displays the time ago since the APD was last saved ([#2104])

#### 🐛 Bugs fixed

#### ⚙️ Behind the scenes
Expand All @@ -12,4 +14,6 @@ Anticipated release: April 6, 2020

# Previous releases

See our [release history](https://github.com/18F/cms-hitech-apd/releases)
See our [release history](https://github.com/18F/cms-hitech-apd/releases)

[#2104]: https://github.com/18F/cms-hitech-apd/issues/2104
3 changes: 2 additions & 1 deletion web/src/components/Header.js
Expand Up @@ -8,6 +8,7 @@ import { getIsAdmin } from '../reducers/user.selector';
import { t } from '../i18n';

import DashboardButton from './DashboardButton';
import SaveMessage from './SaveMessage';

import Icon, {
Check,
Expand Down Expand Up @@ -86,7 +87,7 @@ class Header extends Component {
</span>
) : (
<span>
<Check /> Saved {lastSaved}
<Check /> <SaveMessage lastSaved={lastSaved} />
</span>
)}
</span>
Expand Down
4 changes: 2 additions & 2 deletions web/src/components/Header.test.js
Expand Up @@ -139,7 +139,7 @@ describe('Header component', () => {
}}
isAdmin={false}
isSaving={false}
lastSaved="last save date"
lastSaved="2020-01-01T12:00:00.000Z"
pushRoute={() => {}}
showSiteTitle={false}
/>
Expand All @@ -159,7 +159,7 @@ describe('Header component', () => {
}}
isAdmin={false}
isSaving
lastSaved="last save date"
lastSaved="2020-01-01T17:00:00.000Z"
pushRoute={() => {}}
showSiteTitle={false}
/>
Expand Down
58 changes: 58 additions & 0 deletions web/src/components/SaveMessage.js
@@ -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;
89 changes: 89 additions & 0 deletions web/src/components/SaveMessage.test.js
@@ -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");
Comment on lines +17 to +22
Copy link
Contributor

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. 😄

});
});
});

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
Copy link
Contributor

Choose a reason for hiding this comment

The 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 react-test-renderer. Otherwise, there are ways to make it work with Enzyme.

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);
});
});
});
});
6 changes: 4 additions & 2 deletions web/src/components/__snapshots__/Header.test.js.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.