Refactor HalResource tests (#4595)

pull/4514/head
Alex Dik 8 years ago committed by Oliver Günther
parent 29cde57927
commit cd4a220a97
  1. 170
      frontend/app/components/api/api-v3/hal-resources/hal-resource.service.test.ts
  2. 33
      frontend/app/components/api/api-v3/hal-resources/hal-resource.service.ts

@ -306,147 +306,129 @@ describe('HalResource service', () => {
});
});
describe('when transforming an object with _links', () => {
describe('when creating a resource form a source with linked resources', () => {
beforeEach(() => {
source = {
_type: 'Hello',
_links: {
post: {
href: '/api/v3/hello',
method: 'post'
},
put: {
href: '/api/v3/hello',
method: 'put'
},
patch: {
href: '/api/v3/hello',
method: 'patch'
},
'get': {
href: '/api/v3/hello',
},
'delete': {
href: '/api/v3/hello',
method: 'delete'
},
self: {
href: '/api/v3/hello',
title: 'some title'
href: 'unicorn/69'
},
beaver: {
href: 'justin/420'
}
}
};
resource = new HalResource(source);
});
it('should return an empty $embedded object', () => {
expect(resource.$embedded).to.eql({});
it('should have no "self" property', () => {
expect(resource.self).to.not.exist;
});
describe('when after the $links property is generated', () => {
it('should exist', () => {
expect(resource.$links).to.exist;
it('should have a beaver', () => {
expect(resource.beaver).to.exist;
});
it('should not have the original `_links` property', () => {
it('should have no "_links" property', () => {
expect(resource._links).to.not.exist;
});
it('should have callable links', () => {
expect(resource.$links).to.respondTo('self');
expect(resource.$links).to.respondTo('put');
expect(resource.$links).to.respondTo('post');
it('should leave the source accessible', () => {
expect(resource.$source).to.eql(source);
});
it('should have a callable self link', () => {
expect(() => resource.$links.self()).to.not.throw(Error);
});
it('should not be restangularized', () => {
expect(resource.$links.restangularized).to.not.be.ok;
it('should have a callable beaver', () => {
expect(() => resource.$links.beaver()).to.not.throw(Error);
});
it('should have a links property with the same keys as the original _links', () => {
it('should have a $links property with the keys of its source _links', () => {
const transformedLinks = Object.keys(resource.$links);
const plainLinks = Object.keys(source._links);
expect(transformedLinks).to.have.members(plainLinks);
});
});
});
describe('when transforming an object with _embedded', () => {
describe('when creating a resource from a source with embedded resources', () => {
beforeEach(() => {
source = {
_type: 'Hello',
_embedded: {
resource: {
_links: {},
_embedded: {
first: {
_embedded: {
second: {
_links: {},
property: 'yet another value'
}
},
property: 'another value'
}
},
propertyResource: {
_links: {}
}
},
property: 'value'
resource: {_links: {}},
}
};
resource = new HalResource(source);
});
it('should not be restangularized', () => {
expect(resource.restangularized).to.not.be.ok;
it('should not have the original _embedded property', () => {
expect(resource._embedded).to.not.be.ok;
});
it('should be transformed', () => {
expect(resource.$isHal).to.be.true;
it('should have a property, that is a loaded resource', () => {
expect(resource.resource.$loaded).to.be.true;
});
it('should have a new "embedded" property', () => {
expect(resource.$embedded);
it('should have an embedded resource, that is loaded', () => {
expect(resource.$embedded.resource.$loaded).to.be.true;
});
it('should not have the original _embedded property', () => {
expect(resource._embedded).to.not.be.ok;
it('should have a property that is the resource', () => {
expect(resource.resource).to.equal(resource.$embedded.resource);
});
it.skip('should have a callable link to that resource', () => {
expect(() => resource.$links.resource()).to.not.throw(Error);
});
it('should transform its resources', () => {
expect(resource.$embedded.resource.$isHal).to.be.true;
describe('when overriding the property with a resource', () => {
var link;
beforeEach(() => {
link = {href: 'pony'};
resource.resource = HalResource.fromLink(link);
});
it('should not transform its properties', () => {
expect(resource.$embedded.property.$isHal).to.not.be.ok;
it('should set the property to that resource', () => {
expect(resource.resource.href).to.equal(link.href);
});
describe('when transforming nested embedded resources', () => {
it.skip('should set the corresponding link', () => {
expect(resource.$links.resource.$link.href).to.equal(link.href);
});
});
describe('when the embedded resources are nested', () => {
var first;
var second;
var deep;
beforeEach(() => {
first = resource.$embedded.resource.$embedded.first;
second = resource.$embedded.resource.$embedded.first.$embedded.second;
});
source._embedded.resource._embedded = {
first: {
_embedded: {
second: {
_links: {},
property: 'yet another value'
}
},
property: 'another value'
}
};
it('should transform properties that are resources', () => {
expect(resource.$embedded.resource.propertyResource.$isHal).to.be.true;
first = resource.$embedded.resource.$embedded.first;
deep = resource.$embedded.resource.$embedded.first.$embedded.second;
});
it('should transform all nested resources recursively', () => {
expect(first.$isHal).to.be.true;
expect(second.$isHal).to.be.true;
it('should crate all nested resources recursively', () => {
expect(deep.$isHal).to.be.true;
});
it('should transfer the properties of the nested resources correctly', () => {
expect(first.property).to.eq('another value');
expect(second.property).to.eq('yet another value');
expect(deep.property).to.eq('yet another value');
});
});
});
@ -538,36 +520,14 @@ describe('HalResource service', () => {
describe('when transforming an object with an _embedded list with the list element having _links', () => {
beforeEach(() => {
source = {
_type: 'Hello',
_embedded: {
elements: [
{
_type: 'ListElement',
_links: {}
},
{
_type: 'ListElement',
_links: {}
}
]
elements: [{_links: {}}, {_links: {}}]
}
};
resource = new HalResource(source);
});
it('should not be restangularized', () => {
expect(resource.restangularized).to.not.be.ok;
});
it('should be transformed', () => {
expect(resource.$isHal).to.be.true;
});
it('should have a new "embedded" property', () => {
expect(resource.$embedded);
});
it('should not have the original _embedded property', () => {
expect(resource._embedded).to.not.be.ok;
});

@ -49,8 +49,13 @@ export class HalResource {
return new resourceClass(element);
}
public static getEmptyResource():any {
return {_links: {self: {href: null}}};
public static fromLink(link:HalLinkInterface) {
const resource = HalResource.getEmptyResource(HalLink.fromObject(link));
return new HalResource(resource, false);
}
public static getEmptyResource(self = {href: null}):any {
return {_links: {self: self}};
}
public $links:any = {};
@ -132,16 +137,6 @@ function initializeResource(halResource:HalResource) {
}
}
function createLinkedResource(linkName, link) {
var resource = HalResource.getEmptyResource();
resource._links.self = link;
const resourceClass = halResourceTypesStorage
.getResourceClassOfAttribute(halResource.constructor._type, linkName);
return new resourceClass(resource, false);
}
function proxyProperties() {
var source = halResource.$source.restangularized ? halResource.$source.plain() : halResource.$source;
@ -169,7 +164,7 @@ function initializeResource(halResource:HalResource) {
if (Array.isArray(link)) {
var items = link.map(item => createLinkedResource(linkName, item.$link));
var property:Array<HalResource> = new ObservableArray(...items).on('change', () => {
var property:HalResource[] = new ObservableArray(...items).on('change', () => {
property.forEach(item => {
if (!item.$link) {
property.splice(property.indexOf(item), 1);
@ -237,7 +232,17 @@ function initializeResource(halResource:HalResource) {
});
}
function setter(val, linkName) {
function createLinkedResource(linkName, link) {
var resource = HalResource.getEmptyResource();
resource._links.self = link;
const resourceClass = halResourceTypesStorage
.getResourceClassOfAttribute(halResource.constructor._type, linkName);
return new resourceClass(resource, false);
}
function setter(val:HalResource, linkName:string) {
if (!val) {
halResource.$source._links[linkName] = {href: null};
}

Loading…
Cancel
Save