Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import type { GlobalSearchResult } from '@kbn/global-search-plugin/common/types';
import { Tag } from '@kbn/saved-objects-tagging-plugin/public';
import { resultToOption } from './result_to_option';

const createSearchResult = (parts: Partial<GlobalSearchResult> = {}): GlobalSearchResult => ({
Expand Down Expand Up @@ -89,4 +90,44 @@ describe('resultToOption', () => {
})
);
});

it("doesn't crash on unknown tag", () => {
const input = createSearchResult({
type: 'dashboard',
meta: { categoryLabel: 'category', displayName: 'foo', tagIds: ['known', 'unknown'] },
});

const getTag = (tagId: string): Tag | undefined => {
if (tagId === 'known') {
return {
id: 'known',
name: 'Known',
description: 'Known',
color: '#000000',
};
}
};

const logSpy = jest.spyOn(console, 'warn').mockImplementation();

const option = resultToOption(input, [], getTag);
expect(logSpy).toBeCalledWith(
'SearchBar: Tag with id "unknown" not found. Tag "unknown" is referenced by the search result "dashboard:id". Skipping displaying the missing tag.'
);
expect(option.append).toMatchInlineSnapshot(`
<ResultTagList
searchTagIds={Array []}
tags={
Array [
Object {
"color": "#000000",
"description": "Known",
"id": "known",
"name": "Known",
},
]
}
/>
`);
});
});
21 changes: 16 additions & 5 deletions x-pack/plugins/global_search_bar/public/lib/result_to_option.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,22 @@ export const resultToOption = (
: [{ text: cleanMeta((meta.displayName as string) ?? type) }];

if (getTag && tagIds.length) {
// TODO #85189 - refactor to use TagList instead of getTag
// Casting to Tag[] because we know all our IDs will be valid here, no need to check for undefined
option.append = (
<ResultTagList tags={tagIds.map(getTag) as Tag[]} searchTagIds={searchTagIds} />
);
const tags = tagIds.map(getTag).filter((tag, index) => {
if (!tag) {
// eslint-disable-next-line no-console
console.warn(
`SearchBar: Tag with id "${tagIds[index]}" not found. Tag "${tagIds[index]}" is referenced by the search result "${result.type}:${result.id}". Skipping displaying the missing tag.`
);
return false;
}

return true;
}) as Tag[];

if (tags.length) {
// TODO #85189 - refactor to use TagList instead of getTag
option.append = <ResultTagList tags={tags} searchTagIds={searchTagIds} />;
}
}

return option;
Expand Down