1
1
import Post from './dummy/models/Post'
2
+ import User from './dummy/models/User'
2
3
import { Model } from '../src'
3
4
import axios from 'axios'
4
5
import MockAdapter from 'axios-mock-adapter' ;
@@ -79,7 +80,6 @@ describe('Model methods', () => {
79
80
} )
80
81
81
82
test ( 'save() method makes a POST request when ID of object does not exists' , async ( ) => {
82
-
83
83
let post
84
84
85
85
axiosMock . onAny ( ) . reply ( ( config ) => {
@@ -109,10 +109,33 @@ describe('Model methods', () => {
109
109
110
110
post = new Post ( { id : 1 , title : 'Cool!' } )
111
111
await post . save ( )
112
+ } )
113
+
114
+ test ( 'a request from delete() method hits the right resource' , async ( ) => {
115
+
116
+ axiosMock . onAny ( ) . reply ( ( config ) => {
117
+ expect ( config . method ) . toEqual ( 'delete' )
118
+ expect ( config . url ) . toBe ( 'http://localhost/posts/1' )
119
+
120
+ return [ 200 , { } ]
121
+ } )
112
122
123
+ const post = new Post ( { id : 1 } )
124
+
125
+ post . delete ( )
126
+ } )
127
+
128
+ test ( 'a request from delete() method when model has not ID throws a exception' , async ( ) => {
129
+
130
+ errorModel = ( ) => {
131
+ let post = new Post ( )
132
+ post . delete ( )
133
+ }
134
+
135
+ expect ( errorModel ) . toThrow ( 'This model has a empty ID.' )
113
136
} )
114
137
115
- test ( 'an request with custom() method hit the right resource' , async ( ) => {
138
+ test ( 'a request with custom() method hits the right resource' , async ( ) => {
116
139
117
140
axiosMock . onAny ( ) . reply ( ( config ) => {
118
141
expect ( config . url ) . toBe ( 'postz' )
@@ -122,4 +145,31 @@ describe('Model methods', () => {
122
145
123
146
const post = await Post . custom ( 'postz' ) . first ( )
124
147
} )
148
+
149
+ test ( 'a request from hasMany() method hits right resource' , async ( ) => {
150
+ let user
151
+ let posts
152
+
153
+ axiosMock . onAny ( ) . reply ( ( config ) => {
154
+ expect ( config . method ) . toEqual ( 'get' )
155
+ expect ( config . url ) . toEqual ( 'http://localhost/users/1/posts' )
156
+
157
+ return [ 200 , { } ]
158
+ } )
159
+
160
+ user = new User ( { id : 1 } )
161
+ posts = await user . posts ( ) . get ( )
162
+ } )
163
+
164
+ test ( 'a request hasMany() method returns a array of Models' , async ( ) => {
165
+
166
+ axiosMock . onGet ( 'http://localhost/users/1/posts' ) . reply ( 200 , postsArrayResponse )
167
+
168
+ const user = new User ( { id : 1 } )
169
+ const posts = await user . posts ( ) . get ( )
170
+
171
+ posts . forEach ( post => {
172
+ expect ( post ) . toBeInstanceOf ( Post )
173
+ } ) ;
174
+ } )
125
175
} )
0 commit comments