mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-12 17:04:57 +00:00
[AAE-12146] Update getFeature typings (#8167)
* [AAE-11463] Update getFeature typings * added tests
This commit is contained in:
parent
71e7fc0bf6
commit
89b79c9e45
@ -57,6 +57,43 @@ describe('ExtensionService', () => {
|
|||||||
expect(service.setup).toHaveBeenCalledWith(blankConfig);
|
expect(service.setup).toHaveBeenCalledWith(blankConfig);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('getFeature', () => {
|
||||||
|
it('should return array if seached feature is an array', async () => {
|
||||||
|
const searchedArrayFeature = [{ test: 'test' }];
|
||||||
|
|
||||||
|
service.setup({
|
||||||
|
...blankConfig,
|
||||||
|
features: {
|
||||||
|
searchedArrayFeature
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const requestedFeatue = service.getFeature('searchedArrayFeature');
|
||||||
|
expect(requestedFeatue).toEqual(searchedArrayFeature);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return object if seached feature is an object', async () => {
|
||||||
|
const searchedObjectFeature: { test: string } = { test: 'test' };
|
||||||
|
service.setup({
|
||||||
|
...blankConfig,
|
||||||
|
features: {
|
||||||
|
searchedObjectFeature
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const requestedFeatue = service.getFeature<{ test: string }>('searchedObjectFeature');
|
||||||
|
expect(requestedFeatue).toEqual(searchedObjectFeature);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return default value if feature is not found', async () => {
|
||||||
|
const defaultValue = {};
|
||||||
|
service.setup(blankConfig);
|
||||||
|
|
||||||
|
const requestedFeatue = service.getFeature<{ test: string }>('searchedFeature', defaultValue);
|
||||||
|
expect(requestedFeatue).toEqual(defaultValue);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should raise warning if setting up with missing config', () => {
|
it('should raise warning if setting up with missing config', () => {
|
||||||
spyOn(console, 'warn').and.stub();
|
spyOn(console, 'warn').and.stub();
|
||||||
|
|
||||||
|
@ -117,12 +117,13 @@ export class ExtensionService {
|
|||||||
/**
|
/**
|
||||||
* Gets features by key.
|
* Gets features by key.
|
||||||
*
|
*
|
||||||
* @param key Key string, using dot notation
|
* @param key Key string using dot notation or array of strings
|
||||||
* @returns Features array found by key
|
* @param defaultValue Default value returned if feature is not found, default is empty array
|
||||||
|
* @returns Feature found by key
|
||||||
*/
|
*/
|
||||||
getFeature(key: string): any[] {
|
getFeature<T = any[]>(key: string | string[], defaultValue: any = []): T {
|
||||||
const properties: string[] = Array.isArray(key) ? [key] : key.split('.');
|
const properties: string[] = Array.isArray(key) ? key : key.split('.');
|
||||||
return properties.reduce((prev, curr) => prev && prev[curr], this.features) || [];
|
return properties.reduce((prev, curr) => prev && prev[curr], this.features) || defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
getElements<T extends ExtensionElement>(key: string, fallback: Array<T> = []): Array<T> {
|
getElements<T extends ExtensionElement>(key: string, fallback: Array<T> = []): Array<T> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user