forked from hazza/dspace-angular
add tests for build decorators
This commit is contained in:
83
src/app/core/cache/builders/build-decorators.spec.ts
vendored
Normal file
83
src/app/core/cache/builders/build-decorators.spec.ts
vendored
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
import { HALLink } from '../../shared/hal-link.model';
|
||||||
|
import { HALResource } from '../../shared/hal-resource.model';
|
||||||
|
import { ResourceType } from '../../shared/resource-type';
|
||||||
|
import {
|
||||||
|
dataService,
|
||||||
|
getDataServiceFor,
|
||||||
|
getLinkDefinition,
|
||||||
|
link,
|
||||||
|
} from './build-decorators';
|
||||||
|
|
||||||
|
/* tslint:disable:max-classes-per-file */
|
||||||
|
class TestService {}
|
||||||
|
class AnotherTestService {}
|
||||||
|
class TestHALResource implements HALResource {
|
||||||
|
_links: {
|
||||||
|
self: HALLink;
|
||||||
|
foo: HALLink;
|
||||||
|
};
|
||||||
|
|
||||||
|
bar?: any
|
||||||
|
}
|
||||||
|
let testType;
|
||||||
|
|
||||||
|
describe('build decorators', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
testType = new ResourceType('testType-' + new Date().getTime());
|
||||||
|
});
|
||||||
|
describe('@dataService/getDataServiceFor', () => {
|
||||||
|
|
||||||
|
it('should register a resourcetype for a dataservice', () => {
|
||||||
|
dataService(testType)(TestService);
|
||||||
|
expect(getDataServiceFor(testType)).toBe(TestService);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe(`when the resource type isn't specified`, () => {
|
||||||
|
it(`should throw an error`, () => {
|
||||||
|
expect(() => {
|
||||||
|
dataService(undefined)(TestService);
|
||||||
|
}).toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe(`when there already is a registered dataservice for a resourcetype`, () => {
|
||||||
|
it(`should throw an error`, () => {
|
||||||
|
dataService(testType)(TestService);
|
||||||
|
expect(() => {
|
||||||
|
dataService(testType)(AnotherTestService);
|
||||||
|
}).toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe(`@link/getLinkDefinitions`, () => {
|
||||||
|
it(`should register a link`, () => {
|
||||||
|
const target = new TestHALResource();
|
||||||
|
link(testType, true, 'foo')(target, 'bar');
|
||||||
|
const result = getLinkDefinition(TestHALResource, 'foo');
|
||||||
|
expect(result.resourceType).toBe(testType);
|
||||||
|
expect(result.isList).toBe(true);
|
||||||
|
expect(result.linkName).toBe('foo');
|
||||||
|
expect(result.propertyName).toBe('bar');
|
||||||
|
});
|
||||||
|
|
||||||
|
describe(`when the linkname isn't specified`, () => {
|
||||||
|
it(`should use the propertyname`, () => {
|
||||||
|
const target = new TestHALResource();
|
||||||
|
link(testType)(target, 'foo');
|
||||||
|
const result = getLinkDefinition(TestHALResource, 'foo');
|
||||||
|
expect(result.linkName).toBe('foo');
|
||||||
|
expect(result.propertyName).toBe('foo');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe(`when there's no @link`, () => {
|
||||||
|
it(`should return undefined`, () => {
|
||||||
|
const result = getLinkDefinition(TestHALResource, 'self');
|
||||||
|
expect(result).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
/* tslint:enable:max-classes-per-file */
|
21
src/app/core/cache/builders/build-decorators.ts
vendored
21
src/app/core/cache/builders/build-decorators.ts
vendored
@@ -66,25 +66,6 @@ export function getDataServiceFor<T extends CacheableObject>(resourceType: Resou
|
|||||||
return dataServiceMap.get(resourceType.value);
|
return dataServiceMap.get(resourceType.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function resolvedLink<T extends DataService<any>, K extends keyof T>(provider: GenericConstructor<T>, methodName?: K, ...params: any[]): any {
|
|
||||||
return function r(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
|
||||||
if (!target || !propertyKey) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const metaDataList: string[] = resolvedLinkMap.get(target.constructor) || [];
|
|
||||||
if (metaDataList.indexOf(propertyKey) === -1) {
|
|
||||||
metaDataList.push(propertyKey);
|
|
||||||
}
|
|
||||||
resolvedLinkMap.set(target.constructor, metaDataList);
|
|
||||||
return Reflect.metadata(resolvedLinkKey, {
|
|
||||||
provider,
|
|
||||||
methodName,
|
|
||||||
params
|
|
||||||
}).apply(this, arguments);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class to represent the data that can be set by the @link decorator
|
* A class to represent the data that can be set by the @link decorator
|
||||||
*/
|
*/
|
||||||
@@ -124,7 +105,7 @@ export const link = <T extends HALResource>(
|
|||||||
linkName = propertyName as any;
|
linkName = propertyName as any;
|
||||||
}
|
}
|
||||||
|
|
||||||
targetMap.set(propertyName, {
|
targetMap.set(linkName, {
|
||||||
resourceType,
|
resourceType,
|
||||||
isList,
|
isList,
|
||||||
linkName,
|
linkName,
|
||||||
|
@@ -58,7 +58,4 @@ describe('ThumbnailComponent', () => {
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
;
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user