Skip to content

Commit

Permalink
address ome review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Jul 2, 2020
1 parent 2a76b1c commit 4b86f07
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ const createType = (props: Partial<SavedObjectsType>): SavedObjectsType => {
namespaceType: 'single',
mappings: { properties: {} },
...props,
management: {
defaultSearchField: 'field',
getInAppUrl: (obj) => ({ path: `/object/${obj.id}`, uiCapabilitiesPath: '' }),
...props.management,
},
};
};

Expand Down Expand Up @@ -65,6 +60,53 @@ describe('mapToResult', () => {
score: 42,
});
});

it('throws if the type do not have management information', () => {
const object = createObject(
{ id: 'dash1', type: 'dashboard', score: 42 },
{ title: 'My dashboard' }
);

expect(() => {
mapToResult(
object,
createType({
name: 'dashboard',
management: {
getInAppUrl: (obj) => ({ path: `/dashboard/${obj.id}`, uiCapabilitiesPath: '' }),
},
})
);
}).toThrowErrorMatchingInlineSnapshot(
`"Trying to map an object from a type without management metadata"`
);

expect(() => {
mapToResult(
object,
createType({
name: 'dashboard',
management: {
defaultSearchField: 'title',
},
})
);
}).toThrowErrorMatchingInlineSnapshot(
`"Trying to map an object from a type without management metadata"`
);

expect(() => {
mapToResult(
object,
createType({
name: 'dashboard',
management: undefined,
})
);
}).toThrowErrorMatchingInlineSnapshot(
`"Trying to map an object from a type without management metadata"`
);
});
});

describe('mapToResults', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ export const mapToResult = (
object: SavedObjectsFindResult<unknown>,
type: SavedObjectsType
): GlobalSearchProviderResult => {
const { defaultSearchField, getInAppUrl } = type.management!;

const { defaultSearchField, getInAppUrl } = type.management ?? {};
if (defaultSearchField === undefined || getInAppUrl === undefined) {
throw new Error('Trying to map an object from a type without management metadata');
}
return {
id: object.id,
title: (object.attributes as any)[defaultSearchField!],
// defaultSearchField is dynamic and not 'directly' bound to the generic type of the SavedObject
// so we are forced to cast the attributes to any to access the properties associated with it.
title: (object.attributes as any)[defaultSearchField],
type: object.type,
url: getInAppUrl!(object).path,
url: getInAppUrl(object).path,
score: object.score,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export const createSavedObjectsResultProvider = (): GlobalSearchResultProvider =
.getAllTypes()
.filter((type) => !typeRegistry.isHidden(type.name))
.filter((type) => type.management?.defaultSearchField && type.management?.getInAppUrl);
const searchFields = [
...new Set(searchableTypes.map((type) => type.management!.defaultSearchField!)),
];
const searchFields = uniq(
searchableTypes.map((type) => type.management!.defaultSearchField!)
);

const responsePromise = client.find({
page: 1,
Expand All @@ -39,3 +39,5 @@ export const createSavedObjectsResultProvider = (): GlobalSearchResultProvider =
},
};
};

const uniq = <T>(values: T[]): T[] => [...new Set(values)];

0 comments on commit 4b86f07

Please sign in to comment.