Skip to content

Commit

Permalink
fix the failed test
Browse files Browse the repository at this point in the history
  • Loading branch information
yujin-emma committed Feb 5, 2024
1 parent 7192546 commit ceb55e9
Showing 1 changed file with 78 additions and 5 deletions.
83 changes: 78 additions & 5 deletions src/core/server/saved_objects/import/create_saved_objects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,13 @@ describe('#createSavedObjects', () => {
});

const objs = [obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8, obj9, obj10, obj11, obj12, obj13];
const dataSourceObjs = [dataSourceObj1, dataSourceObj2];
const dataSourceObjs = [
dataSourceObj1,
dataSourceObj2,
dashboardObjWithDataSource,
visualizationObjWithDataSource,
searchObjWithDataSource,
];

const setupMockResults = (options: CreateSavedObjectsParams) => {
bulkCreate.mockResolvedValue({
Expand All @@ -232,8 +238,11 @@ describe('#createSavedObjects', () => {
const setupMockResultsWithDataSource = (options: CreateSavedObjectsParams) => {
bulkCreate.mockResolvedValue({
saved_objects: [
getResultMock.conflict(dataSourceObj2.type, dataSourceObj2.id),
getResultMock.success(dataSourceObj1, options),
getResultMock.conflict(dataSourceObj1.type, dataSourceObj1.id),
getResultMock.success(dataSourceObj2, options),
getResultMock.success(dashboardObjWithDataSource, options),
getResultMock.success(visualizationObjWithDataSource, options),
getResultMock.success(searchObjWithDataSource, options),
],
});
};
Expand Down Expand Up @@ -339,7 +348,13 @@ describe('#createSavedObjects', () => {

await createSavedObjects(options);
expect(bulkCreate).toHaveBeenCalledTimes(1);
const argObjs = [dataSourceObj1, dataSourceObj2];
const argObjs = [
dataSourceObj1,
dataSourceObj2,
dashboardObjWithDataSource,
visualizationObjWithDataSource,
searchObjWithDataSource,
];
expectBulkCreateArgs.objects(1, argObjs);
};
const testBulkCreateOptions = async (namespace?: string) => {
Expand All @@ -351,6 +366,26 @@ describe('#createSavedObjects', () => {
expect(bulkCreate).toHaveBeenCalledTimes(1);
expectBulkCreateArgs.options(1, options);
};

const testBulkCreateOptionsWithDataSource = async (
namespace?: string,
dataSourceId?: string,
dataSourceTitle?: string
) => {
const overwrite = (Symbol() as unknown) as boolean;
const options = setupParams({
objects: dataSourceObjs,
namespace,
overwrite,
dataSourceId,
dataSourceTitle,
});
setupMockResultsWithDataSource(options);

await createSavedObjects(options);
expect(bulkCreate).toHaveBeenCalledTimes(1);
expectBulkCreateArgs.options(1, options);
};
const testReturnValue = async (namespace?: string) => {
const options = setupParams({ objects: objs, namespace });
setupMockResults(options);
Expand All @@ -365,6 +400,30 @@ describe('#createSavedObjects', () => {
expect(results).toEqual(expectedResults);
};

const testReturnValueWithDataSource = async (
namespace?: string,
dataSourceId?: string,
dataSourceTitle?: string
) => {
const options = setupParams({
objects: dataSourceObjs,
namespace,
dataSourceId,
dataSourceTitle,
});
setupMockResultsWithDataSource(options);

const results = await createSavedObjects(options);
const resultSavedObjectsWithDataSource = (await bulkCreate.mock.results[0].value).saved_objects;
const [dsr1, dsr2, dsr3, dsr4, dsr5] = resultSavedObjectsWithDataSource;
const transformedResultsWithDataSource = [dsr1, dsr2, dsr3, dsr4, dsr5];
const expectedResultsWithDataSource = getExpectedResults(
transformedResultsWithDataSource,
dataSourceObjs
);
expect(results).toEqual(expectedResultsWithDataSource);
};

describe('with an undefined namespace', () => {
test('calls bulkCreate once with input objects', async () => {
await testBulkCreateObjects();
Expand All @@ -391,12 +450,26 @@ describe('#createSavedObjects', () => {
});

describe('with a data source', () => {
test('calls bulkCreate once with input objects', async () => {
test('calls bulkCreate once with input objects with data source id', async () => {
await testBulkCreateObjectsWithDataSource(
'some-namespace',
'some-datasource-id',
'some-data-source-title'
);
});
test('calls bulkCreate once with input options with data source id', async () => {
await testBulkCreateOptionsWithDataSource(
'some-namespace',
'some-datasource-id',
'some-data-source-title'
);
});
test('returns bulkCreate results that are remapped to IDs of imported objects with data source id', async () => {
await testReturnValueWithDataSource(
'some-namespace',
'some-datasource-id',
'some-data-source-title'
);
});
});
});

0 comments on commit ceb55e9

Please sign in to comment.