Skip to content

Commit

Permalink
Merge branch 'main' into rollups/fix-api-integration-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ElenaStoeva authored Jun 12, 2024
2 parents 9bf6a0e + 4f8cc78 commit 3db6a3f
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ describe('Data table record utils', () => {

describe('buildDataTableRecordList', () => {
test('should return a list of DataTableRecord', () => {
const result = buildDataTableRecordList(esHitsMock, dataViewMock);
const result = buildDataTableRecordList({
records: esHitsMock,
dataView: dataViewMock,
});
result.forEach((doc) => {
expect(doc).toHaveProperty('id');
expect(doc).toHaveProperty('raw');
Expand All @@ -32,7 +35,9 @@ describe('Data table record utils', () => {
});

test('should support processing each record', () => {
const result = buildDataTableRecordList(esHitsMock, dataViewMock, {
const result = buildDataTableRecordList({
records: esHitsMock,
dataView: dataViewMock,
processRecord: (record) => ({ ...record, id: 'custom-id' }),
});
result.forEach((doc) => {
Expand Down
18 changes: 11 additions & 7 deletions packages/kbn-discover-utils/src/utils/build_data_record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@ export function buildDataTableRecord(

/**
* Helper to build multiple DataTableRecords at once, saved a bit of testing code lines
* @param docs Array of documents returned from Elasticsearch
* @param records Array of documents returned from Elasticsearch
* @param dataView this current data view
*/
export function buildDataTableRecordList<T extends DataTableRecord = DataTableRecord>(
docs: EsHitRecord[],
dataView?: DataView,
{ processRecord }: { processRecord?: (record: DataTableRecord) => T } = {}
): DataTableRecord[] {
return docs.map((doc) => {
export function buildDataTableRecordList<T extends DataTableRecord = DataTableRecord>({
records,
dataView,
processRecord,
}: {
records: EsHitRecord[];
dataView?: DataView;
processRecord?: (record: DataTableRecord) => T;
}): DataTableRecord[] {
return records.map((doc) => {
const record = buildDataTableRecord(doc, dataView);
return processRecord ? processRecord(record) : record;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ describe('context predecessors', function () {
({ rows }) => {
expect(mockSearchSource.fetch$.calledOnce).toBe(true);
expect(rows).toEqual(
buildDataTableRecordList(mockSearchSource._stubHits.slice(0, 3), dataView)
buildDataTableRecordList({
records: mockSearchSource._stubHits.slice(0, 3),
dataView,
})
);
}
);
Expand Down Expand Up @@ -136,7 +139,10 @@ describe('context predecessors', function () {
expect(Object.keys(last(intervals) ?? {})).toEqual(['format', 'gte']);
expect(intervals.length).toBeGreaterThan(1);
expect(rows).toEqual(
buildDataTableRecordList(mockSearchSource._stubHits.slice(0, 3), dataView)
buildDataTableRecordList({
records: mockSearchSource._stubHits.slice(0, 3),
dataView,
})
);
}
);
Expand Down Expand Up @@ -172,7 +178,10 @@ describe('context predecessors', function () {
expect(intervals.length).toBeGreaterThan(1);

expect(rows).toEqual(
buildDataTableRecordList(mockSearchSource._stubHits.slice(-3), dataView)
buildDataTableRecordList({
records: mockSearchSource._stubHits.slice(-3),
dataView,
})
);
}
);
Expand Down Expand Up @@ -263,7 +272,10 @@ describe('context predecessors', function () {
expect(removeFieldsSpy.calledOnce).toBe(true);
expect(setFieldsSpy.calledOnce).toBe(true);
expect(rows).toEqual(
buildDataTableRecordList(mockSearchSource._stubHits.slice(0, 3), dataView)
buildDataTableRecordList({
records: mockSearchSource._stubHits.slice(0, 3),
dataView,
})
);
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ describe('context successors', function () {
({ rows }) => {
expect(mockSearchSource.fetch$.calledOnce).toBe(true);
expect(rows).toEqual(
buildDataTableRecordList(mockSearchSource._stubHits.slice(-3), dataView)
buildDataTableRecordList({
records: mockSearchSource._stubHits.slice(-3),
dataView,
})
);
}
);
Expand Down Expand Up @@ -136,7 +139,10 @@ describe('context successors', function () {
expect(Object.keys(last(intervals) ?? {})).toEqual(['format', 'lte']);
expect(intervals.length).toBeGreaterThan(1);
expect(rows).toEqual(
buildDataTableRecordList(mockSearchSource._stubHits.slice(-3), dataView)
buildDataTableRecordList({
records: mockSearchSource._stubHits.slice(-3),
dataView,
})
);
}
);
Expand Down Expand Up @@ -166,7 +172,10 @@ describe('context successors', function () {
expect(moment(last(intervals)?.gte).valueOf()).toBeGreaterThan(MS_PER_DAY * 2200);
expect(intervals.length).toBeGreaterThan(1);
expect(rows).toEqual(
buildDataTableRecordList(mockSearchSource._stubHits.slice(0, 4), dataView)
buildDataTableRecordList({
records: mockSearchSource._stubHits.slice(0, 4),
dataView,
})
);
}
);
Expand Down Expand Up @@ -252,7 +261,10 @@ describe('context successors', function () {
({ rows, interceptedWarnings }) => {
expect(mockSearchSource.fetch$.calledOnce).toBe(true);
expect(rows).toEqual(
buildDataTableRecordList(mockSearchSource._stubHits.slice(-3), dataView)
buildDataTableRecordList({
records: mockSearchSource._stubHits.slice(-3),
dataView,
})
);
const setFieldsSpy = mockSearchSource.setField.withArgs('fields');
const removeFieldsSpy = mockSearchSource.removeField.withArgs('fieldsFromSource');
Expand Down Expand Up @@ -324,7 +336,10 @@ describe('context successors', function () {
({ rows, interceptedWarnings }) => {
expect(mockSearchSource.fetch$.calledOnce).toBe(true);
expect(rows).toEqual(
buildDataTableRecordList(mockSearchSource._stubHits.slice(-3), dataView)
buildDataTableRecordList({
records: mockSearchSource._stubHits.slice(-3),
dataView,
})
);
expect(dataPluginMock.search.showWarnings).toHaveBeenCalledTimes(1);
expect(interceptedWarnings?.length).toBe(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { filter, map } from 'rxjs';
import { lastValueFrom } from 'rxjs';
import { isRunningResponse, ISearchSource } from '@kbn/data-plugin/public';
import { buildDataTableRecordList } from '@kbn/discover-utils';
import type { EsHitRecord } from '@kbn/discover-utils/types';
import type { SearchResponseWarning } from '@kbn/search-response-warnings';
import { DataViewType } from '@kbn/data-views-plugin/public';
import type { RecordsFetchResponse } from '../../types';
Expand Down Expand Up @@ -67,7 +66,9 @@ export const fetchDocuments = (
.pipe(
filter((res) => !isRunningResponse(res)),
map((res) => {
return buildDataTableRecordList(res.rawResponse.hits.hits as EsHitRecord[], dataView, {
return buildDataTableRecordList({
records: res.rawResponse.hits.hits,
dataView,
processRecord: (record) => services.profilesManager.resolveDocumentProfile({ record }),
});
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,43 @@ describe('react embeddable renderer', () => {
)
);
});

it('catches error when thrown in deserialize', async () => {
const buildEmbeddable = jest.fn();
const errorInInitializeFactory: ReactEmbeddableFactory<{ name: string; bork: string }> = {
...testEmbeddableFactory,
type: 'errorInDeserialize',
buildEmbeddable,
deserializeState: (state) => {
throw new Error('error in deserialize');
},
};
registerReactEmbeddableFactory('errorInDeserialize', () =>
Promise.resolve(errorInInitializeFactory)
);
setupPresentationPanelServices();

const onApiAvailable = jest.fn();
const embeddable = render(
<ReactEmbeddableRenderer
type={'errorInDeserialize'}
maybeId={'12345'}
onApiAvailable={onApiAvailable}
getParentApi={() => ({
getSerializedStateForChild: () => ({
rawState: {},
}),
})}
/>
);

await waitFor(() => expect(embeddable.getByTestId('errorMessageMarkdown')).toBeInTheDocument());
expect(onApiAvailable).not.toBeCalled();
expect(buildEmbeddable).not.toBeCalled();
expect(embeddable.getByTestId('errorMessageMarkdown')).toHaveTextContent(
'error in deserialize'
);
});
});

describe('reactEmbeddable phase events', () => {
Expand Down
Loading

0 comments on commit 3db6a3f

Please sign in to comment.