Skip to content

Commit 772e801

Browse files
committed
Normalize FieldDetails
1 parent 273a24b commit 772e801

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

x-pack/plugins/enterprise_search/public/applications/app_search/components/documents/document_detail_logic.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,16 @@ describe('DocumentDetailLogic', () => {
7474
});
7575

7676
describe('getDocumentDetails', () => {
77-
it('will call an API endpoint and then store the result', async () => {
78-
const fields = [{ name: 'name', value: 'python', type: 'string' }];
77+
it('will call an API endpoint and then store the normalized result', async () => {
78+
const fields = [
79+
{ name: 'foo', value: 'foo', type: 'string' },
80+
{ name: 'bar', value: ['bar'], type: 'string' },
81+
];
82+
const normalizedFields = [
83+
{ name: 'foo', value: ['foo'], type: 'string' },
84+
{ name: 'bar', value: ['bar'], type: 'string' },
85+
];
86+
7987
jest.spyOn(DocumentDetailLogic.actions, 'setFields');
8088
const promise = Promise.resolve({ fields });
8189
http.get.mockReturnValue(promise);
@@ -84,7 +92,7 @@ describe('DocumentDetailLogic', () => {
8492

8593
expect(http.get).toHaveBeenCalledWith(`/api/app_search/engines/engine1/documents/1`);
8694
await promise;
87-
expect(DocumentDetailLogic.actions.setFields).toHaveBeenCalledWith(fields);
95+
expect(DocumentDetailLogic.actions.setFields).toHaveBeenCalledWith(normalizedFields);
8896
});
8997

9098
it('handles errors', async () => {

x-pack/plugins/enterprise_search/public/applications/app_search/components/documents/document_detail_logic.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ export interface DocumentDetailLogicValues {
1717
fields: FieldDetails[];
1818
}
1919

20+
const normalizeFields = (
21+
fieldDetails: Array<{
22+
name: string;
23+
value: string | string[];
24+
type: string;
25+
}>
26+
): FieldDetails[] => {
27+
return fieldDetails.map((fieldDetail) => ({
28+
...fieldDetail,
29+
value: Array.isArray(fieldDetail.value) ? fieldDetail.value : [fieldDetail.value],
30+
}));
31+
};
32+
2033
export interface DocumentDetailLogicActions {
2134
setFields(fields: FieldDetails[]): { fields: FieldDetails[] };
2235
deleteDocument(documentId: string): { documentId: string };
@@ -69,7 +82,7 @@ export const DocumentDetailLogic = kea<DocumentDetailLogicType>({
6982
const response = await http.get(
7083
`/api/app_search/engines/${engineName}/documents/${documentId}`
7184
);
72-
actions.setFields(response.fields);
85+
actions.setFields(normalizeFields(response.fields));
7386
} catch (e) {
7487
flashAPIErrors(e);
7588
}

x-pack/plugins/enterprise_search/public/applications/app_search/components/documents/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
export interface FieldDetails {
88
name: string;
9-
value: string | string[];
9+
value: string[];
1010
type: string;
1111
}

0 commit comments

Comments
 (0)