[AAE-12146] Update getFeature typings (#8167)

* [AAE-11463] Update getFeature typings

* added tests
This commit is contained in:
Bartosz Sekula 2023-01-30 19:51:47 +01:00 committed by GitHub
parent 71e7fc0bf6
commit 89b79c9e45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 5 deletions

View File

@ -57,6 +57,43 @@ describe('ExtensionService', () => {
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', () => {
spyOn(console, 'warn').and.stub();

View File

@ -117,12 +117,13 @@ export class ExtensionService {
/**
* Gets features by key.
*
* @param key Key string, using dot notation
* @returns Features array found by key
* @param key Key string using dot notation or array of strings
* @param defaultValue Default value returned if feature is not found, default is empty array
* @returns Feature found by key
*/
getFeature(key: string): any[] {
const properties: string[] = Array.isArray(key) ? [key] : key.split('.');
return properties.reduce((prev, curr) => prev && prev[curr], this.features) || [];
getFeature<T = any[]>(key: string | string[], defaultValue: any = []): T {
const properties: string[] = Array.isArray(key) ? key : key.split('.');
return properties.reduce((prev, curr) => prev && prev[curr], this.features) || defaultValue;
}
getElements<T extends ExtensionElement>(key: string, fallback: Array<T> = []): Array<T> {