Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(model): throw an error if operations are used with custom #173

Merged
merged 5 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@ export default class Model extends StaticModel {
*/

delete() {
if (this._customResource) {
throw Error("The delete() method cannot be used in conjunction with the custom() method. Use for() instead.")
}

if (!this.hasId()) {
throw new Error('This model has a empty ID.')
}
Expand All @@ -436,6 +440,10 @@ export default class Model extends StaticModel {
}

save() {
if (this._customResource) {
throw Error("The save() method cannot be used in conjunction with the custom() method. Use for() instead.")
}

return this.hasId() ? this._update() : this._create()
}

Expand Down Expand Up @@ -472,6 +480,10 @@ export default class Model extends StaticModel {
*/

attach(params) {
if (this._customResource) {
throw Error("The attach() method cannot be used in conjunction with the custom() method. Use for() instead.")
}

return this.request(
this._reqConfig({
method: 'POST',
Expand All @@ -482,6 +494,10 @@ export default class Model extends StaticModel {
}

sync(params) {
if (this._customResource) {
throw Error("The sync() method cannot be used in conjunction with the custom() method. Use for() instead.")
}

return this.request(
this._reqConfig({
method: 'PUT',
Expand Down
32 changes: 32 additions & 0 deletions tests/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,38 @@ describe('Model methods', () => {
expect(errorModel).toThrow('Arguments to custom() must be strings or instances of Model.')
})

test('it throws an error when CRUD and relationship operations are used in conjunction with custom()', () => {
errorModel = () => {
const post = new Post({ text: 'Hello' }).custom('foo/bar').save()
}

expect(errorModel).toThrow("The save() method cannot be used in conjunction with the custom() method. Use for() instead.")

errorModel = () => {
const post = new Post({ id: 1 }).custom('foo/bar').delete()
}

expect(errorModel).toThrow("The delete() method cannot be used in conjunction with the custom() method. Use for() instead.")

errorModel = () => {
const post = new Post({ id: 1 })
const comment = post.comments().custom('foo/bar').attach({
text: 'Awesome post!'
})
}

expect(errorModel).toThrow("The attach() method cannot be used in conjunction with the custom() method. Use for() instead.")

errorModel = () => {
const post = new Post({ id: 1 })
const comment = post.comments().custom('foo/bar').sync({
text: 'Awesome post!'
})
}

expect(errorModel).toThrow("The sync() method cannot be used in conjunction with the custom() method. Use for() instead.")
})

test('save() method makes a PUT request to the correct URL on nested object thas was fetched with find() method', async () => {
axiosMock.onGet('http://localhost/posts/1/comments/1').reply(200, commentsResponse[0])
axiosMock.onPut('http://localhost/posts/1/comments/1').reply(200, commentsResponse[0])
Expand Down