Skip to content

Commit

Permalink
feat(client): handle elapsed waiting time with N/A label
Browse files Browse the repository at this point in the history
  • Loading branch information
djey47 authored and Jérôme Canler committed Jan 3, 2018
1 parent fbf1c7a commit cb23c7f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
15 changes: 8 additions & 7 deletions src/client/support/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ export const toHoursMinutes = (date?: string): string => {
};

/**
* @return waiting time in minutes from transport time ISO
* @return waiting time in minutes from startMoment to transportTime ISO
*/
export const toWaitingTime = (transportTime?: string, now: Moment, messages: Object): string => {
const endDate = moment(transportTime);
if (!endDate.isValid()) return transportTime || '';
const startDate = moment(now);
export const toWaitingTime = (transportTime?: string, startMoment: Moment, messages: Object): string => {
const endMoment = moment(transportTime);
if (!endMoment.isValid()) return transportTime || '';

let waitingTime = Math.floor(endDate.diff(startDate) / 1000 / 60);
return `${waitingTime > 0 ? waitingTime : 0} ${translate(MessageKeys.UNITS_MINUTES, messages)}`;
let waitingTime = Math.floor(endMoment.diff(startMoment) / 1000 / 60);

if (waitingTime >= 0) return `${waitingTime} ${translate(MessageKeys.UNITS_MINUTES, messages)}`;
return translate(MessageKeys.UNAVAILABLE, messages);
};
4 changes: 2 additions & 2 deletions src/client/support/format.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ describe('toHoursMinutes function', () => {
describe('toWaitingTime function', () => {
const now = moment('2017-07-16T23:10:00.000Z');

it('should return 0 when arrival time has expired', () => {
it('should return N/A when arrival time has expired', () => {
// given-when
const actual = toWaitingTime('2017-07-16T23:08:00.000Z', now, {});
// then
expect(actual).toEqual('0 {units.minutes}');
expect(actual).toEqual('{unavailable}');
});

it('should return duration in minutes when proper time given', () => {
Expand Down
32 changes: 32 additions & 0 deletions tools/dev-server/mocks/sncf/records.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,38 @@
},
"record_timestamp": "2016-04-23T20:00:41+00:00"
},
"LA DEFENSE GRANDE ARCHE": {
"datasetid": "sncf-gares-et-arrets-transilien-ile-de-france",
"recordid": "6afb7a7be3de654e8c1223f205f5955c207e57a5",
"fields": {
"commune": "Courbevoie",
"code_insee_commune": "92062",
"libelle": "LA DEFENSE GRANDE ARCHE",
"libelle_sms_gare": "La Défense",
"adresse": "Rue Carpeaux - 92800 Puteaux",
"x_lambert_ii_etendu": 592793,
"y_lambert_ii_etendu": 2432805,
"uic7": "8738221",
"code_uic": "87382218",
"zone_navigo": 3,
"coord_gps_wgs84": [
48.8934895769,
2.23827508094
],
"libelle_stif_info_voyageurs": "GARE DE LA DEFENSE",
"nom_gare": "La Défense",
"libelle_point_d_arret": "LA DEFENSE GRANDE ARCHE",
"gare_non_sncf": 0
},
"geometry": {
"type": "Point",
"coordinates": [
2.23827508094,
48.8934895769
]
},
"record_timestamp": "2016-04-23T20:00:41+00:00"
},
"LE VAL D'OR": {
"datasetid": "sncf-gares-et-arrets-transilien-ile-de-france",
"recordid": "d48e618af1984cf7ed572a5eb575c2e832970599",
Expand Down
7 changes: 0 additions & 7 deletions tools/dev-server/mocks/transilien/transilien.json

This file was deleted.

0 comments on commit cb23c7f

Please sign in to comment.