Skip to content

Commit d56333e

Browse files
committed
UserResource: Attributes are optional
1 parent dd5dd30 commit d56333e

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

collections/users/controller.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ describe('UserController', () => {
4242
const data = (ctx.response.body as Response)?.data as UserResource
4343

4444
expect(ctx.response.status).toBe(200)
45-
expect(data.attributes.name).toBe(object.name)
46-
expect(data.attributes.username).toBe(object.username)
45+
expect(data.attributes?.name).toBe(object.name)
46+
expect(data.attributes?.username).toBe(object.username)
4747
}
4848
})
4949
})
@@ -69,8 +69,8 @@ describe('UserController', () => {
6969
const data = (ctx.response.body as Response)?.data as UserResource
7070

7171
expect(ctx.response.status).toBe(200)
72-
expect(data.attributes.name).toBe(name)
73-
expect(data.attributes.username).toBe(user.username)
72+
expect(data.attributes?.name).toBe(name)
73+
expect(data.attributes?.username).toBe(user.username)
7474
expect(data.id).toBe(user.id)
7575
})
7676
})

types/user-resource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type BaseResource from './base-resource.ts'
22
import UserAttributes, { createUserAttributes } from './user-attributes.ts'
33

44
export default interface UserResource extends BaseResource {
5-
attributes: UserAttributes
5+
attributes?: UserAttributes
66
}
77

88
const createUserResource = (overrides?: Partial<UserResource>): UserResource => {

utils/transformers/user-to-user-resource.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ describe('userToUserResource', () => {
2828
const fields = Object.keys(object) as UserAttributesKeys[]
2929
const excluded = allUserAttributes.filter(attr => !fields.includes(attr))
3030
const actual = userToUserResource(user, fields)
31+
const attributes = actual.attributes!
3132

32-
expect(Object.keys(actual.attributes)).toHaveLength(fields.length)
33-
for (const field of fields) expect(actual.attributes[field]).toBe(user[field])
34-
for (const ex of excluded) expect(actual.attributes[ex]).not.toBeDefined()
33+
expect(Object.keys(attributes)).toHaveLength(fields.length)
34+
for (const field of fields) expect(attributes[field]).toBe(user[field])
35+
for (const ex of excluded) expect(attributes[ex]).not.toBeDefined()
3536
}
3637
})
3738
})

utils/transformers/user-to-user-response.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ describe('userToUserResponse', () => {
4444
const fields = Object.keys(object) as UserAttributesKeys[]
4545
const excluded = allUserAttributes.filter(attr => !fields.includes(attr))
4646
const res = userToUserResponse(user, fields)
47-
const data = res.data as UserResource
47+
const attributes = (res.data as UserResource).attributes!
4848

49-
expect(Object.keys(data.attributes)).toHaveLength(fields.length)
50-
for (const field of fields) expect(data.attributes[field]).toBe(user[field])
51-
for (const ex of excluded) expect(data.attributes[ex]).not.toBeDefined()
49+
expect(Object.keys(attributes)).toHaveLength(fields.length)
50+
for (const field of fields) expect(attributes[field]).toBe(user[field])
51+
for (const ex of excluded) expect(attributes[ex]).not.toBeDefined()
5252
}
5353
})
5454
})

0 commit comments

Comments
 (0)