Skip to content

Commit 2ab364d

Browse files
committed
Fix checkLuxonDateTime
While adding a test, it seemed that the format tokens were not correct. Fixed those as well.
1 parent 32e3bd4 commit 2ab364d

3 files changed

Lines changed: 44 additions & 6 deletions

File tree

src/Casts.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ function checkMomentInstance(attr, value) {
2424

2525
function checkLuxonDateTime(attr, value) {
2626
invariant(
27-
moment.isMoment(value),
28-
`Attribute \`${attr}\` is not a luxon DateTime.`
27+
DateTime.isDateTime(value),
28+
`Attribute \`${attr}\` is not a luxon instance.`
2929
);
3030
}
3131

3232
const LUXON_DATE_FORMAT = 'yyyy-LL-dd';
33-
const LUXON_DATETIME_FORMAT = 'yyy-LL-ddTHH:mm:ssZZZ';
33+
const LUXON_DATETIME_FORMAT = "yyyy'-'LL'-'dd'T'HH':'mm':'ssZZ";
3434

3535
const CASTS = {
3636
momentDate: {
@@ -70,7 +70,7 @@ const CASTS = {
7070
if (value === null || value === undefined) {
7171
return null;
7272
}
73-
return DateTime.fromFormat(value, LUXON_DATE_FORMAT);
73+
return DateTime.fromISO(value);
7474
},
7575
toJS(attr, value) {
7676
if (value === null || value === undefined) {
@@ -86,7 +86,8 @@ const CASTS = {
8686
if (value === null) {
8787
return null;
8888
}
89-
return DateTime.fromFormat(value, LUXON_DATETIME_FORMAT);
89+
90+
return DateTime.fromISO(value);
9091
},
9192
toJS(attr, value) {
9293
if (value === null) {

src/__tests__/Casts/datetime.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Model, Casts } from '../../';
22
import { observable } from 'mobx';
3+
import { Settings } from 'luxon';
34
import moment from 'moment';
45
import momentLocale from 'moment/min/moment-with-locales';
56

@@ -79,3 +80,39 @@ test('moment instance with locale should be recognized', () => {
7980
animal.bornAt = momentLocale('2017-03-22T22:08:23+00:00');
8081
expect(animal.toJS().bornAt).toEqual(expect.stringContaining('2017-03-22'));
8182
});
83+
84+
describe('luxon compatibility', () => {
85+
Settings.defaultZoneName = 'utc';
86+
87+
class LuxonAnimal extends Animal {
88+
@observable createdAt = '';
89+
90+
casts() {
91+
return {
92+
bornAt: Casts.luxonDatetime,
93+
};
94+
}
95+
};
96+
97+
test('toJS() should throw error when luxon instance is gone', () => {
98+
const animal = new LuxonAnimal({ bornAt: '2017-03-22T22:08:23+00:00' });
99+
100+
animal.bornAt = 'asdf';
101+
102+
expect(() => {
103+
return animal.toJS();
104+
}).toThrow('Attribute `bornAt` is not a luxon instance.');
105+
});
106+
107+
test('should be serialized in toBackend()', () => {
108+
const animal = new LuxonAnimal({ bornAt: '2017-03-22T22:08:23+00:00' });
109+
110+
expect(animal.toBackend().born_at).toEqual('2017-03-22T22:08:23+00:00');
111+
});
112+
113+
test('should be serialized in toBackend() when given a binder specific format', () => {
114+
const animal = new LuxonAnimal({ bornAt: '2017-03-22T22:08:23.575242+0000' });
115+
116+
expect(animal.toBackend().born_at).toEqual('2017-03-22T22:08:23+00:00');
117+
});
118+
});

src/__tests__/Model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import axios from 'axios';
22
import { toJS, observable } from 'mobx';
33
import MockAdapter from 'axios-mock-adapter';
44
import _ from 'lodash';
5-
import { Model, BinderApi } from '../';
5+
import { Model, BinderApi, Casts } from '../';
66
import {
77
Animal,
88
AnimalStore,

0 commit comments

Comments
 (0)