Skip to content

Commit

Permalink
Fix @Date getter
Browse files Browse the repository at this point in the history
  • Loading branch information
radex committed Feb 12, 2019
1 parent 06170dd commit 60e1f18
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

## Unreleased

### Changes

- 鈿狅笍 Potentially BREAKING fix: a `@date` field now returns a Jan 1, 1970 date instead of `null` if the field's raw value is `0`.
This is considered a bug fix, since it's unexpected to receive a `null` from a getter of a field whose column schema doesn't say `isOptional: true`.
However, if you relied on this behavior, this might be a breaking change.

## 0.10.1 - 2019-02-12

### Changes
Expand Down
2 changes: 1 addition & 1 deletion src/decorators/date/index.js
Expand Up @@ -24,7 +24,7 @@ const dateDecorator = makeDecorator(
enumerable: true,
get(): ?Date {
const rawValue = this._getRaw(columnName)
return rawValue ? new Date(rawValue) : null
return typeof rawValue === 'number' ? new Date(rawValue) : null
},
set(date: ?Date): void {
const rawValue = date ? +new Date(date) : null
Expand Down
12 changes: 12 additions & 0 deletions src/decorators/date/test.js
Expand Up @@ -37,6 +37,18 @@ describe('decorators/timestamp', () => {
model.date = null
expect(model._getRaw('date')).toBe(null)
})
it('returns 1970 date, not null if timestamp=0', () => {
const model = new MockModel({ schema }, { date: 0 })
expect(model.date).toBeInstanceOf(Date)
expect(+model.date).toBe(0)
})
it('sets 1970 date, not null if timestamp', () => {
const model = new MockModel({ schema }, {})
model._isEditing = true
model.date = new Date(0)
expect(model._getRaw('date')).toBe(0)
expect(+model.date).toBe(0)
})
it('fails if applied to incorrect fields', () => {
expect(
() =>
Expand Down

0 comments on commit 60e1f18

Please sign in to comment.