Skip to content

Commit aaad116

Browse files
committed
Added unit tests for the new wrap() method
1 parent 9ad109b commit aaad116

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

tests/model.test.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('Model methods', () => {
5555
})
5656
})
5757

58-
test('$first() returns first object in array as instance of such Model', async () => {
58+
test('$first() returns first object in array as instance of such Model with "data" wrapper', async () => {
5959
axiosMock.onGet('http://localhost/posts').reply(200, postsEmbedResponse)
6060

6161
const post = await Post.$first()
@@ -285,6 +285,55 @@ describe('Model methods', () => {
285285
expect(postsAll).toStrictEqual(postsGet)
286286
})
287287

288+
test('find() handles request with "data" wrapper when wrap() is set to "data"', async () => {
289+
// Set the wrap method to 'data'
290+
Post.prototype['wrap'] = () => {
291+
return 'data'
292+
}
293+
294+
axiosMock.onGet('http://localhost/posts/1').reply(200, postEmbedResponse)
295+
296+
const post = await Post.find(1)
297+
298+
expect(post).toEqual(postEmbedResponse.data)
299+
expect(post).toBeInstanceOf(Post)
300+
expect(post.user).toBeInstanceOf(User)
301+
post.relationships.tags.data.forEach((tag) => {
302+
expect(tag).toBeInstanceOf(Tag)
303+
})
304+
})
305+
306+
test('get() handles request with "data" wrapper when wrap() is set to "data"', async () => {
307+
// Set the wrap method to 'data'
308+
Post.prototype['wrap'] = () => {
309+
return 'data'
310+
}
311+
312+
axiosMock.onGet('http://localhost/posts').reply(200, postsEmbedResponse)
313+
314+
const posts = await Post.get()
315+
316+
expect(posts).toEqual(postsEmbedResponse.data)
317+
})
318+
319+
test('first() returns first object in array as instance of such Model with "data" wrapper when wrap() is set to "data"', async () => {
320+
// Set the wrap method to 'data'
321+
Post.prototype['wrap'] = () => {
322+
return 'data'
323+
}
324+
325+
axiosMock.onGet('http://localhost/posts').reply(200, postsEmbedResponse)
326+
327+
const post = await Post.first()
328+
329+
expect(post).toEqual(postsEmbedResponse.data[0])
330+
expect(post).toBeInstanceOf(Post)
331+
expect(post.user).toBeInstanceOf(User)
332+
post.relationships.tags.forEach((tag) => {
333+
expect(tag).toBeInstanceOf(Tag)
334+
})
335+
})
336+
288337
test('save() method makes a POST request when ID of object does not exists', async () => {
289338
let post
290339
const _postResponse = {

0 commit comments

Comments
 (0)