Skip to content

Commit

Permalink
fix: schema handling for EntryResource#save() CMS-2844
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-scherzinger committed Jul 18, 2017
1 parent 2caf81c commit 1f9ce99
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/resources/Resource.js
Expand Up @@ -118,12 +118,14 @@ export default class Resource {
/**
* Saves this {@link Resource}.
*
* @param {string} overwriteSchemaUrl? Other schema url to overwrite the one in
* `_link.self.profile`. Mainly for internal use.
* @returns {Promise<Resource>} Promise will resolve to the saved Resource. Will
* be the same object but with refreshed data.
*/
save() {
save(overwriteSchemaUrl) {
const out = this.toOriginal();
return validator.validate(out, this[resourceSymbol].link('self').profile)
return validator.validate(out, overwriteSchemaUrl || this[resourceSymbol].link('self').profile)
.then(() => put(this[environmentSymbol], this.newRequest().follow('self'), out))
.then(([res, traversal]) => {
this[resourceSymbol] = halfred.parse(res);
Expand Down
4 changes: 4 additions & 0 deletions src/resources/publicAPI/EntryResource.js
Expand Up @@ -324,6 +324,10 @@ export default class EntryResource extends Resource {
});
}

save() {
return super.save(`${this[resourceSymbol].link('self').profile}?template=put`);
}

/**
* Get the field type for a given property in this {@link EntryResource}
*
Expand Down
3 changes: 3 additions & 0 deletions test/mocks/schema/dm-model-put.json
Expand Up @@ -12,6 +12,9 @@
"_links": {
"$ref": "https://entrecode.de/schema/hal#_links"
},
"_embedded": {
"$ref": "https://entrecode.de/schema/hal#_embedded"
},
"_id": {
"type": "string",
"pattern": "^[0-9A-Za-z-_]{7,14}$",
Expand Down
17 changes: 17 additions & 0 deletions test/publicAPI/EntryResource.test.js
Expand Up @@ -4,8 +4,10 @@ const chai = require('chai');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const fs = require('fs');
const resolver = require('../mocks/resolver');
const mock = require('../mocks/nock');

const helper = require('../../lib/helper');
const Resource = require('../../lib/resources/Resource').default;
const ListResource = require('../../lib/resources/ListResource').default;
const EntryList = require('../../lib/resources/publicAPI/EntryList');
Expand Down Expand Up @@ -123,6 +125,21 @@ describe('Entry Resource', () => {
const throws = () => new EntryResource.default(resourceJson); // eslint-disable-line new-cap
throws.should.throw('schema must be defined');
});
it('should call put on save', () => {
mock.reset();
const stub = sinon.stub(helper, 'put');
stub.returns(resolver('public-entry.json', resource._traversal));

return resource.save()
.then(() => {
stub.should.be.called.once;
stub.restore();
})
.catch((err) => {
stub.restore();
throw err;
});
});

it('should get field type', () => {
resource.getFieldType('entry').should.be.equal('entry');
Expand Down
2 changes: 1 addition & 1 deletion typings/resources/Resource.d.ts
Expand Up @@ -9,7 +9,7 @@ export declare class Resource {

reset(): void;

save(): Promise<any>
save(overwriteSchemaUrl: string): Promise<any>

del(): Promise<void>;

Expand Down

0 comments on commit 1f9ce99

Please sign in to comment.