Skip to content

Commit dbac302

Browse files
committed
delete() method
1 parent 55f299f commit dbac302

File tree

3 files changed

+67
-10
lines changed

3 files changed

+67
-10
lines changed

src/Model.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ export default class Model extends StaticModel {
133133
})
134134
}
135135

136+
delete () {
137+
if (!this.hasId()) {
138+
throw new Error('This model has a empty ID.')
139+
}
140+
141+
let url = `${this.baseURL()}/${this.resource()}/${this.id}`
142+
143+
return this.request({
144+
url,
145+
method: 'DELETE'
146+
}).then(response => {
147+
return response
148+
})
149+
}
150+
136151
get () {
137152
let base = this._fromResource || `${this.baseURL()}/${this.resource()}`
138153
base = this._customResource || base

tests/model.test.js

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Post from './dummy/models/Post'
2+
import User from './dummy/models/User'
23
import { Model } from '../src'
34
import axios from 'axios'
45
import MockAdapter from 'axios-mock-adapter';
@@ -79,7 +80,6 @@ describe('Model methods', () => {
7980
})
8081

8182
test('save() method makes a POST request when ID of object does not exists', async () => {
82-
8383
let post
8484

8585
axiosMock.onAny().reply((config) => {
@@ -109,10 +109,33 @@ describe('Model methods', () => {
109109

110110
post = new Post({ id: 1, title: 'Cool!' })
111111
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+
})
112122

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.')
113136
})
114137

115-
test('an request with custom() method hit the right resource', async () => {
138+
test('a request with custom() method hits the right resource', async () => {
116139

117140
axiosMock.onAny().reply((config) => {
118141
expect(config.url).toBe('postz')
@@ -122,4 +145,31 @@ describe('Model methods', () => {
122145

123146
const post = await Post.custom('postz').first()
124147
})
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+
})
125175
})

tests/setup.test.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,4 @@ describe('Setup models', () => {
5151

5252
delete Post.prototype['resource']
5353
})
54-
55-
test('the hasMany() is set up', () => {
56-
const user = new User({ id: 1 })
57-
const posts = user.posts()
58-
59-
expect(posts).toBeInstanceOf(Post)
60-
expect(posts._fromResource).toEqual(user.baseURL() + '/users/1/posts')
61-
})
6254
})

0 commit comments

Comments
 (0)