Skip to content

Commit 6b4ff69

Browse files
maxi7587pablorsk
authored andcommitted
Use is new instead of id on save (#260)
* added test to service and fix test factory * prettier * detail * fix-resource-has-one-relationships * added include_get and include_save params to IParamsResource * replaced ID for is_new and added tests
1 parent b82ac0b commit 6b4ff69

5 files changed

+28
-7
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
## [Unreleased]
7+
## [2.1.15] - 2020-01-17
88

99
### Added
1010

1111
- Added include_get and include_save to IResourceParams
12+
- Resource's save methods uses is_new instead of ID property to select between POST and PATCH
1213

1314
## [2.1.14] - 2019-10-24
1415

src/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ngx-jsonapi",
3-
"version": "2.1.14",
3+
"version": "2.1.15",
44
"description": "JSON API library for Angular",
55
"module": "ngx-jsonapi/@ngx-jsonapi/ngx-jsonapi.es5.js",
66
"es2015": "ngx-jsonapi/@ngx-jsonapi/ngx-jsonapi.js",

src/resource-integration.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { HttpClient, HttpEvent, HttpHandler, HttpRequest, HttpResponse } from '@
99
import { delay } from 'rxjs/operators';
1010
import { Core } from './core';
1111
import { TestFactory } from './tests/factories/test-factory';
12+
import { async } from '@angular/core/testing';
1213

1314
// @todo: create HttpHandlerMock class file and import it in tests to avoid duplication
1415
class HttpHandlerMock implements HttpHandler {
@@ -103,4 +104,23 @@ describe('Resource save', () => {
103104
expect(http_request_spy.calls.mostRecent().args[2].body.included.length).toBe(1);
104105
expect(http_request_spy.calls.mostRecent().args[2].body.included[0].id).toBe('author_1');
105106
});
107+
it('should use POST if is_new is truthy', async () => {
108+
let resource = TestFactory.getBook('book_1');
109+
resource.is_new = true;
110+
let http_request_spy = spyOn(HttpClient.prototype, 'request').and.callThrough();
111+
test_response_subject.next(new HttpResponse({ body: TestFactory.getResourceDocumentData(Book) }));
112+
113+
await resource.save();
114+
expect(http_request_spy.calls.mostRecent().args[0]).toBe('POST');
115+
});
116+
117+
it('should use PATCH if is_new is falsy', async () => {
118+
let resource = TestFactory.getBook('book_1');
119+
resource.is_new = false;
120+
let http_request_spy = spyOn(HttpClient.prototype, 'request').and.callThrough();
121+
test_response_subject.next(new HttpResponse({ body: TestFactory.getResourceDocumentData(Book) }));
122+
123+
await resource.save();
124+
expect(http_request_spy.calls.mostRecent().args[0]).toBe('PATCH');
125+
});
106126
});

src/resource.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('resource', () => {
4040
has_many_relationship: new DocumentCollection()
4141
};
4242
resource.links = {};
43-
resource.is_new = true;
43+
resource.is_new = false;
4444
resource.is_saving = false;
4545
resource.is_loading = false;
4646
resource.loaded = true;
@@ -339,7 +339,7 @@ describe('resource.save() method', () => {
339339
meta: { some_data: 'some_data' }
340340
}
341341
};
342-
expect(exec_spy).toHaveBeenCalledWith('1234', 'PATCH', expected_resource_in_save, true);
342+
expect(exec_spy).toHaveBeenCalledWith('1234', 'POST', expected_resource_in_save, true);
343343
});
344344

345345
it('top level meta object should be included in the request if available', async () => {
@@ -373,7 +373,7 @@ describe('resource.save() method', () => {
373373
},
374374
meta: { restore: true }
375375
};
376-
expect(exec_spy).toHaveBeenCalledWith('1234', 'PATCH', expected_resource_in_save, true);
376+
expect(exec_spy).toHaveBeenCalledWith('1234', 'POST', expected_resource_in_save, true);
377377
});
378378

379379
it('restore method should set top level meta to restore the resource (according to Reyesoft specification extension)', async () => {
@@ -407,7 +407,7 @@ describe('resource.save() method', () => {
407407
},
408408
meta: { restore: true }
409409
};
410-
expect(exec_spy).toHaveBeenCalledWith('1234', 'PATCH', expected_resource_in_save, true);
410+
expect(exec_spy).toHaveBeenCalledWith('1234', 'POST', expected_resource_in_save, true);
411411
});
412412

413413
// @todo fill from store to more new version of resource

src/resource.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ export class Resource implements ICacheable {
303303
path.appendPath(this.id);
304304
}
305305

306-
Core.exec(path.get(), this.id ? 'PATCH' : 'POST', object, true).subscribe(
306+
Core.exec(path.get(), this.is_new ? 'POST' : 'PATCH', object, true).subscribe(
307307
success => {
308308
this.is_saving = false;
309309

0 commit comments

Comments
 (0)