|
1 | 1 | import { Model, Casts } from '../../'; |
2 | 2 | import { observable } from 'mobx'; |
| 3 | +import { Settings } from 'luxon'; |
3 | 4 | import moment from 'moment'; |
4 | 5 | import momentLocale from 'moment/min/moment-with-locales'; |
5 | 6 |
|
@@ -79,3 +80,39 @@ test('moment instance with locale should be recognized', () => { |
79 | 80 | animal.bornAt = momentLocale('2017-03-22T22:08:23+00:00'); |
80 | 81 | expect(animal.toJS().bornAt).toEqual(expect.stringContaining('2017-03-22')); |
81 | 82 | }); |
| 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 | +}); |
0 commit comments