Skip to content

Commit 581c29d

Browse files
committed
test(model): test instance in nested objects
1 parent 34fccf3 commit 581c29d

File tree

9 files changed

+130
-35
lines changed

9 files changed

+130
-35
lines changed

tests/dummy/data/comments.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,34 @@ export const Comments = [
33
id: 1,
44
post_id: 1,
55
someId: 'ma018-9ha12',
6-
text: 'Hello'
6+
text: 'Hello',
7+
replies: [
8+
{
9+
id: 3,
10+
comment_id: 1,
11+
someId: 'ma020-9ha15',
12+
text: 'Hello',
13+
}
14+
]
715
},
816
{
917
id: 2,
1018
post_id: 1,
1119
someId: 'mw012-7ha19',
12-
text: 'How are you?'
20+
text: 'How are you?',
21+
replies: [
22+
{
23+
id: 4,
24+
comment_id: 2,
25+
someId: 'mw023-9ha18',
26+
text: 'Hello',
27+
},
28+
{
29+
id: 5,
30+
comment_id: 2,
31+
someId: 'mw035-0ha22',
32+
text: 'Hello',
33+
}
34+
]
1335
}
14-
]
36+
]

tests/dummy/data/post.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ export const Post =
22
{
33
id: 1,
44
someId: 'af621-4aa41',
5-
firstname: 'John',
6-
lastname: 'Doe',
7-
age: 25
8-
}
5+
text: 'Lorem Ipsum Dolor',
6+
user: {
7+
firstname: 'John',
8+
lastname: 'Doe',
9+
age: 25
10+
}
11+
}

tests/dummy/data/postEmbed.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ export const Post = {
22
data: {
33
id: 1,
44
someId: 'af621-4aa41',
5-
firstname: 'John',
6-
lastname: 'Doe',
7-
age: 25
5+
text: 'Lorem Ipsum Dolor',
6+
user: {
7+
firstname: 'John',
8+
lastname: 'Doe',
9+
age: 25
10+
}
811
}
912
}

tests/dummy/data/posts.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ export const Posts = [
22
{
33
id: 1,
44
someId: 'du761-8bc98',
5-
firstname: 'John',
6-
lastname: 'Doe',
7-
age: 25
5+
text: 'Lorem Ipsum Dolor',
6+
user: {
7+
firstname: 'John',
8+
lastname: 'Doe',
9+
age: 25
10+
}
811
},
912
{
1013
id: 1,
1114
someId: 'pa813-7jx02',
12-
firstname: 'Mary',
13-
lastname: 'Doe',
14-
age: 25
15+
text: 'Lorem Ipsum Dolor',
16+
user: {
17+
firstname: 'Mary',
18+
lastname: 'Doe',
19+
age: 25
20+
}
1521
}
16-
]
22+
]

tests/dummy/data/postsEmbed.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,22 @@ export const Posts = {
33
{
44
id: 1,
55
someId: 'ap319-0nh56',
6-
firstname: 'John',
7-
lastname: 'Doe',
8-
age: 25
6+
text: 'Lorem Ipsum Dolor',
7+
user: {
8+
firstname: 'John',
9+
lastname: 'Doe',
10+
age: 25
11+
}
912
},
1013
{
1114
id: 1,
1215
someId: 'by887-0nv66',
13-
firstname: 'Mary',
14-
lastname: 'Doe',
15-
age: 25
16+
text: 'Lorem Ipsum Dolor',
17+
user: {
18+
firstname: 'Mary',
19+
lastname: 'Doe',
20+
age: 25
21+
}
1622
}
1723
]
1824
}

tests/dummy/models/Comment.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import BaseModel from './BaseModel'
22

33
export default class Comment extends BaseModel {
4-
5-
}
4+
relations () {
5+
return {
6+
replies: Comment
7+
}
8+
}
9+
}

tests/dummy/models/Post.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import BaseModel from './BaseModel'
22
import Comment from './Comment'
3+
import User from './User'
34

45
export default class Post extends BaseModel {
56
comments () {
67
return this.hasMany(Comment)
78
}
8-
}
9+
10+
relations () {
11+
return {
12+
user: User
13+
}
14+
}
15+
}

tests/dummy/models/User.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import Post from './Post'
33

44
export default class User extends BaseModel {
55

6+
get fullname () {
7+
return `${this.firstname} ${this.lastname}`
8+
}
9+
610
posts () {
711
return this.hasMany(Post)
812
}
9-
}
13+
}

tests/model.test.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ describe('Model methods', () => {
4848
const post = await Post.first()
4949
expect(post).toEqual(postsResponse[0])
5050
expect(post).toBeInstanceOf(Post)
51+
expect(post.user).toBeInstanceOf(User)
5152
})
5253

5354
test('$first() returns first object in array as instance of such Model', async () => {
@@ -58,6 +59,7 @@ describe('Model methods', () => {
5859

5960
expect(post).toEqual(postsEmbedResponse.data[0])
6061
expect(post).toBeInstanceOf(Post)
62+
expect(post.user).toBeInstanceOf(User)
6163
})
6264

6365
test('first() method returns a empty object when no items have found', async () => {
@@ -73,6 +75,7 @@ describe('Model methods', () => {
7375
const post = await Post.find(1)
7476
expect(post).toEqual(postResponse)
7577
expect(post).toBeInstanceOf(Post)
78+
expect(post.user).toBeInstanceOf(User)
7679
})
7780

7881
test('$find() handles request with "data" wrapper', async () => {
@@ -82,6 +85,7 @@ describe('Model methods', () => {
8285

8386
expect(post).toEqual(postEmbedResponse.data)
8487
expect(post).toBeInstanceOf(Post)
88+
expect(post.user).toBeInstanceOf(User)
8589
})
8690

8791
test('$find() handles request without "data" wrapper', async () => {
@@ -91,6 +95,7 @@ describe('Model methods', () => {
9195

9296
expect(post).toEqual(postResponse)
9397
expect(post).toBeInstanceOf(Post)
98+
expect(post.user).toBeInstanceOf(User)
9499

95100
})
96101

@@ -101,6 +106,7 @@ describe('Model methods', () => {
101106

102107
posts.forEach(post => {
103108
expect(post).toBeInstanceOf(Post)
109+
expect(post.user).toBeInstanceOf(User)
104110
});
105111
})
106112

@@ -109,11 +115,18 @@ describe('Model methods', () => {
109115
expect(config.method).toEqual('get')
110116
expect(config.url).toEqual('http://localhost/posts/1/comments')
111117

112-
return [200, {}]
118+
return [200, commentsResponse]
113119
})
114120

115121
const post = new Post({ id: 1 })
116-
await post.comments().get()
122+
const comments = await post.comments().get()
123+
124+
comments.forEach(comment => {
125+
expect(comment).toBeInstanceOf(Comment)
126+
comment.replies.forEach(reply => {
127+
expect(reply).toBeInstanceOf(Comment)
128+
})
129+
})
117130
})
118131

119132
test('get() hits right resource (nested object, custom PK)', async () => {
@@ -127,10 +140,17 @@ describe('Model methods', () => {
127140
expect(config.method).toEqual('get')
128141
expect(config.url).toEqual(`http://localhost/posts/${post.someId}/comments`)
129142

130-
return [200, {}]
143+
return [200, commentsResponse]
131144
})
132145

133-
post.comments().get()
146+
const comments = await post.comments().get()
147+
148+
comments.forEach(comment => {
149+
expect(comment).toBeInstanceOf(Comment)
150+
comment.replies.forEach(reply => {
151+
expect(reply).toBeInstanceOf(Comment)
152+
})
153+
})
134154
})
135155

136156
test('$get() fetch style request with "data" wrapper', async () => {
@@ -162,26 +182,46 @@ describe('Model methods', () => {
162182
expect(config.method).toEqual('get')
163183
expect(config.url).toEqual(`http://localhost/posts/${post.someId}/comments`)
164184

165-
return [200, {}]
185+
return [200, commentsResponse]
166186
})
167187

168-
post.comments().$get()
188+
const comments = await post.comments().$get()
189+
190+
comments.forEach(comment => {
191+
expect(comment).toBeInstanceOf(Comment)
192+
comment.replies.forEach(reply => {
193+
expect(reply).toBeInstanceOf(Comment)
194+
})
195+
})
169196
})
170197

171198
test('save() method makes a POST request when ID of object does not exists', async () => {
172199
let post
200+
const _postResponse = {
201+
id: 1,
202+
title: 'Cool!',
203+
text: 'Lorem Ipsum Dolor',
204+
user: {
205+
firstname: 'John',
206+
lastname: 'Doe',
207+
age: 25
208+
}
209+
}
173210

174211
axiosMock.onAny().reply((config) => {
175212
expect(config.method).toEqual('post')
176213
expect(config.data).toEqual(JSON.stringify(post))
177214
expect(config.url).toEqual('http://localhost/posts')
178215

179-
return [200, {}]
216+
return [200, _postResponse]
180217
})
181218

182219
post = new Post({ title: 'Cool!' })
183-
await post.save()
220+
post = await post.save()
184221

222+
expect(post).toEqual(_postResponse)
223+
expect(post).toBeInstanceOf(Post)
224+
expect(post.user).toBeInstanceOf(User)
185225
})
186226

187227
test('save() method makes a PUT request when ID of object exists', async () => {

0 commit comments

Comments
 (0)