Skip to content

Commit 279f9dc

Browse files
fix(model): add null check in isValidId (#115)
When an entity is being saved, nulls are considered as existing entities, so it tries to update and not insert.
1 parent 53796a1 commit 279f9dc

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Model.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export default class Model extends StaticModel {
138138
}
139139

140140
isValidId(id) {
141-
return id !== undefined && id !== 0 && id !== ''
141+
return id !== undefined && id !== 0 && id !== '' && id !== null
142142
}
143143

144144
endpoint() {

tests/model.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,22 @@ describe('Model methods', () => {
320320
comment.save()
321321
})
322322

323+
test('save() method makes a POST request when ID of object is null', async () => {
324+
let post
325+
326+
axiosMock.onAny().reply((config) => {
327+
expect(config.method).toEqual('post')
328+
expect(config.data).toEqual(JSON.stringify(post))
329+
expect(config.url).toEqual('http://localhost/posts')
330+
331+
return [200, {}]
332+
})
333+
334+
post = new Post({ id: null, title: 'Cool!' })
335+
await post.save()
336+
337+
})
338+
323339
test('a request from delete() method hits the right resource', async () => {
324340

325341
axiosMock.onAny().reply((config) => {

0 commit comments

Comments
 (0)