Skip to content

Commit

Permalink
Normalize FieldDetails
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonStoltz committed Nov 16, 2020
1 parent 273a24b commit 772e801
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,16 @@ describe('DocumentDetailLogic', () => {
});

describe('getDocumentDetails', () => {
it('will call an API endpoint and then store the result', async () => {
const fields = [{ name: 'name', value: 'python', type: 'string' }];
it('will call an API endpoint and then store the normalized result', async () => {
const fields = [
{ name: 'foo', value: 'foo', type: 'string' },
{ name: 'bar', value: ['bar'], type: 'string' },
];
const normalizedFields = [
{ name: 'foo', value: ['foo'], type: 'string' },
{ name: 'bar', value: ['bar'], type: 'string' },
];

jest.spyOn(DocumentDetailLogic.actions, 'setFields');
const promise = Promise.resolve({ fields });
http.get.mockReturnValue(promise);
Expand All @@ -84,7 +92,7 @@ describe('DocumentDetailLogic', () => {

expect(http.get).toHaveBeenCalledWith(`/api/app_search/engines/engine1/documents/1`);
await promise;
expect(DocumentDetailLogic.actions.setFields).toHaveBeenCalledWith(fields);
expect(DocumentDetailLogic.actions.setFields).toHaveBeenCalledWith(normalizedFields);
});

it('handles errors', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ export interface DocumentDetailLogicValues {
fields: FieldDetails[];
}

const normalizeFields = (
fieldDetails: Array<{
name: string;
value: string | string[];
type: string;
}>
): FieldDetails[] => {
return fieldDetails.map((fieldDetail) => ({
...fieldDetail,
value: Array.isArray(fieldDetail.value) ? fieldDetail.value : [fieldDetail.value],
}));
};

export interface DocumentDetailLogicActions {
setFields(fields: FieldDetails[]): { fields: FieldDetails[] };
deleteDocument(documentId: string): { documentId: string };
Expand Down Expand Up @@ -69,7 +82,7 @@ export const DocumentDetailLogic = kea<DocumentDetailLogicType>({
const response = await http.get(
`/api/app_search/engines/${engineName}/documents/${documentId}`
);
actions.setFields(response.fields);
actions.setFields(normalizeFields(response.fields));
} catch (e) {
flashAPIErrors(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

export interface FieldDetails {
name: string;
value: string | string[];
value: string[];
type: string;
}

0 comments on commit 772e801

Please sign in to comment.