Skip to content

Commit

Permalink
fix(core): fix failing events tests for sql databases
Browse files Browse the repository at this point in the history
Timestamps were not being generated correctly on mongodb, thus the tests didn't reflect they were
available on sql databases
  • Loading branch information
Frantz Kati committed Dec 18, 2020
1 parent 8db1d43 commit 0af2922
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
2 changes: 2 additions & 0 deletions packages/common/src/resources/Resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export class Resource<ResourceType = {}> implements ResourceContract {
.nullable()
.sortable()
.searchable()
.onCreate(() => new Date())
.hideOnInsertApi()
.hideOnUpdateApi()
.hideOnUpdate()
Expand All @@ -130,6 +131,7 @@ export class Resource<ResourceType = {}> implements ResourceContract {
.nullable()
.sortable()
.searchable()
.onCreate(() => new Date())
.onUpdate(() => new Date())
.hideOnInsertApi()
.hideOnUpdateApi()
Expand Down
1 change: 1 addition & 0 deletions packages/tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ tensei.sqlite
build
tensei.sqlite
.env
mikrotensei
44 changes: 33 additions & 11 deletions packages/tests/packages/rest/routes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,23 @@ test('emits inserted event after resource is inserted', async () => {

const response = await client.post('/api/users').send(fake_user)

const expectedPayload = {}

expect(response.status).toBe(201)
expect(response.body.data).toEqual({
id: expect.any(String),
id: expect.anything(),
full_name: fake_user.full_name,
password: fake_user.password,
email: fake_user.email,
posts: []
posts: [],
created_at: expect.any(String),
updated_at: expect.any(String),
})
expect(listener).toHaveBeenCalledWith({
...response.body.data,
created_at: new Date(response.body.data.created_at),
updated_at: new Date(response.body.data.updated_at),
})
expect(listener).toHaveBeenCalledWith(response.body.data)
})

test('emits updated event after resource is updated', async () => {
Expand Down Expand Up @@ -210,18 +218,28 @@ test('emits updated event after resource is updated', async () => {
full_name: updatedUserPayload.full_name
})

expect(response.status).toBe(200)
expect(response.body.data).toEqual({
id: expect.any(String),
const expectedPayload: any = {
full_name: updatedUserPayload.full_name,
password: user.password,
email: updatedUserPayload.email,
posts: [],
updated_at: expect.any(String)
}

if (orm.config.get('type') === 'mongo') {
expectedPayload.posts = []
}

expect(response.status).toBe(200)
expect(response.body.data).toEqual({
id: expect.anything(),
...expectedPayload,
updated_at: expect.any(String),
created_at: expect.any(String)
})
expect(listener).toHaveBeenCalledWith({
...response.body.data,
updated_at: new Date(response.body.data.updated_at)
id: response.body.data.id,
...expectedPayload,
updated_at: new Date(response.body.data.updated_at),
created_at: new Date(response.body.data.created_at)
})
})

Expand Down Expand Up @@ -251,5 +269,9 @@ test('emits deleted event after resource is deleted', async () => {
const response = await client.delete(`/api/users/${user.id}`)

expect(response.status).toBe(200)
expect(listener).toHaveBeenCalledWith(response.body.data)
expect(listener).toHaveBeenCalledWith({
...response.body.data,
created_at: new Date(response.body.data.created_at),
updated_at: new Date(response.body.data.updated_at),
})
})

0 comments on commit 0af2922

Please sign in to comment.