Skip to content

Commit e7173fb

Browse files
committed
Assure that dates on instances remain as dates
This makes sure that setupInstance is called after create and patch or update
1 parent 9cc1ae6 commit e7173fb

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
"body-parser": "^1.18.3",
146146
"can-fixture-socket": "^2.0.0",
147147
"chai": "^4.2.0",
148+
"date-fns": "^2.0.0-beta.2",
148149
"deep-object-diff": "^1.1.0",
149150
"eslint": "^5.16.0",
150151
"eslint-config-prettier": "^4.1.0",

test/service-module/model-methods.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Vuex from 'vuex'
1111
import { clearModels } from '../../src/service-module/global-models'
1212
import memory from 'feathers-memory'
1313
import { makeStore } from '../test-utils'
14+
import { isDate } from 'date-fns'
1415

1516
require('events').EventEmitter.prototype._maxListeners = 100
1617

@@ -86,6 +87,12 @@ function makeContext() {
8687
from: ''
8788
}
8889
}
90+
public static setupInstance(data, { models }) {
91+
if (typeof data.createdAt === 'string') {
92+
data.createdAt = new Date(data.createdAt) // just assuming the date is formatted correctly ;)
93+
}
94+
return data
95+
}
8996
public get status() {
9097
return 'pending'
9198
}
@@ -247,6 +254,19 @@ describe('Models - Methods', function () {
247254
assert.equal(typeof letter2.save, 'function', 'new instance has a save method')
248255
})
249256

257+
it('Dates remain as dates after changes', async function () {
258+
const { Letter, store, lettersService } = makeContext()
259+
let letter = new Letter({ name: 'Garmadon', age: 1025, createdAt: new Date().toString() })
260+
261+
assert(isDate(letter.createdAt), 'createdAt should be a date')
262+
263+
letter = await letter.save()
264+
assert(isDate(letter.createdAt), 'createdAt should be a date')
265+
266+
letter = await letter.save()
267+
assert(isDate(letter.createdAt), 'createdAt should be a date')
268+
})
269+
250270
it('instance.toJSON', function () {
251271
const { Task } = makeContext()
252272
const task = new Task({ id: 1, test: true })

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3781,6 +3781,11 @@ data-urls@^1.1.0:
37813781
whatwg-mimetype "^2.2.0"
37823782
whatwg-url "^7.0.0"
37833783

3784+
date-fns@^2.0.0-beta.2:
3785+
version "2.0.0-beta.2"
3786+
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-2.0.0-beta.2.tgz#ccd556df832ef761baa88c600f53d2e829245999"
3787+
integrity sha512-4cicZF707RNerr3/Q3CcdLo+3OHMCfrRXE7h5iFgn7AMvX07sqKLxSf8Yp+WJW5bvKr2cy9/PkctXLv4iFtOaA==
3788+
37843789
date-now@^0.1.4:
37853790
version "0.1.4"
37863791
resolved "https://registry.yarnpkg.com/date-now/-/date-now-0.1.4.tgz#eaf439fd4d4848ad74e5cc7dbef200672b9e345b"

0 commit comments

Comments
 (0)