-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace allDay -> all_day in server API
Starts ##214
- Loading branch information
Showing
4 changed files
with
146 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import moment from 'moment'; | ||
import { decodeEvent, encodeEvent } from '../data/encoding'; | ||
|
||
describe('decodeEvent', () => { | ||
test('decodes dates', () => { | ||
const event = decodeEvent({ start: '2018-05-07T09:00:00Z', end: '2018-05-07T10:00:00Z' }); | ||
expect(event.start).toBeInstanceOf(moment); | ||
expect(event.start.format()).toBe('2018-05-07T05:00:00-04:00'); | ||
expect(event.end).toBeInstanceOf(moment); | ||
}); | ||
test('renames all_day property', () => { | ||
const event = decodeEvent({ all_day: true }); | ||
expect(event).not.toHaveProperty('all_day'); | ||
expect(event).toHaveProperty('allDay'); | ||
expect(event.allDay).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('encodeEvent', () => { | ||
test.skip('renames allDay', () => { | ||
// TODO: enable this, and remove the next two tests, once the decodeEvent no | ||
// longer has side effects. | ||
const event = encodeEvent({ allDay: true }); | ||
expect(event).toHaveProperty('all_day'); | ||
expect(event).not.toHaveProperty('allDay'); | ||
expect(event.all_day).toBe(true); | ||
}); | ||
test('renames allDay on evidence of the new API', () => { | ||
decodeEvent({ all_day: true }); // for effect | ||
const event = encodeEvent({ allDay: true }); | ||
expect(event).toHaveProperty('all_day'); | ||
expect(event).not.toHaveProperty('allDay'); | ||
}); | ||
test('does not rename allDay on evidence of the old API', () => { | ||
decodeEvent({ allDay: true }); // for effect | ||
const event = encodeEvent({ allDay: true }); | ||
expect(event).not.toHaveProperty('all_day'); | ||
expect(event).toHaveProperty('allDay'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import _ from 'lodash'; | ||
import moment from 'moment'; | ||
|
||
// Remember which API the server is speaking. This hackey (and won't work if the | ||
// user creates an event in an empty database), but only needs to get us through | ||
// an API transition and can then be removed. | ||
// | ||
// TODO: Remove this variable, and simplify its users, once olinlibrary/ABE#243 | ||
// is on production and active developer branches. | ||
let useSnakeCaseProperties = true; | ||
|
||
const toLocalDate = date => moment.utc(date).local(); | ||
|
||
const decodeDateTime = value => (moment.isMoment(value) ? value : toLocalDate(value)); | ||
|
||
/** | ||
* Convert an event from the client-server API format to this app's internal format. | ||
*/ | ||
export function decodeEvent(data) { | ||
useSnakeCaseProperties = 'all_day' in data; | ||
return _.chain(data) | ||
.mapKeys((_value, key) => (key === 'all_day' ? 'allDay' : key)) | ||
.mapValues((value, key) => (key === 'start' || key === 'end' ? decodeDateTime(data[key]) : value)) | ||
.value(); | ||
} | ||
|
||
/** | ||
* Convert an event from this app's internal format to the client-server API format. | ||
*/ | ||
export function encodeEvent(event) { | ||
return useSnakeCaseProperties ? _.mapKeys(event, (_value, key) => (key === 'allDay' ? 'all_day' : key)) : event; | ||
} |
Oops, something went wrong.