Skip to content

Commit

Permalink
adding unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
cauemarcondes committed Jun 21, 2021
1 parent f5c3464 commit 5dddd48
Showing 1 changed file with 112 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,23 @@ import { Tutorial } from './tutorial';

jest.mock('../../kibana_services', () => ({
getServices: () => ({
http: {
post: jest.fn().mockImplementation(async () => ({ count: 1 })),
},
getBasePath: jest.fn(() => 'path'),
chrome: {
setBreadcrumbs: () => {},
},
tutorialService: {
getModuleNotices: () => [],
setTutorial: jest.fn(),
getCustomComponent: jest.fn(),
getCustomStatusCheck: (name) => {
const customStatusCheckMock = {
custom_status_check_has_data: async () => true,
custom_status_check_no_data: async () => false,
};
return customStatusCheckMock[name];
},
},
}),
}));
Expand Down Expand Up @@ -56,6 +65,7 @@ const tutorial = {
elasticCloud: buildInstructionSet('elasticCloud'),
onPrem: buildInstructionSet('onPrem'),
onPremElasticCloud: buildInstructionSet('onPremElasticCloud'),
customStatusCheckName: 'custom_status_check_has_data',
};
const loadTutorialPromise = Promise.resolve(tutorial);
const getTutorial = () => {
Expand Down Expand Up @@ -145,3 +155,104 @@ test('should render ELASTIC_CLOUD instructions when isCloudEnabled is true', asy
component.update();
expect(component).toMatchSnapshot(); // eslint-disable-line
});

describe('custom status check', () => {
test('should return has_data when custom status check callback is set and returns true', async () => {
const component = mountWithIntl(
<Tutorial.WrappedComponent
addBasePath={addBasePath}
isCloudEnabled={true}
getTutorial={getTutorial}
replaceTemplateStrings={replaceTemplateStrings}
tutorialId={'my_testing_tutorial'}
bulkCreate={() => {}}
/>
);
await loadTutorialPromise;
component.update();
await component.instance().checkInstructionSetStatus(0);
expect(component.state('statusCheckStates')[0]).toEqual('has_data');
});
test('should return no_data when custom status check callback is set and returns false', async () => {
const tutorialWithCustomStatusCheckNoData = {
...tutorial,
customStatusCheckName: 'custom_status_check_no_data',
};
const component = mountWithIntl(
<Tutorial.WrappedComponent
addBasePath={addBasePath}
isCloudEnabled={true}
getTutorial={async () => tutorialWithCustomStatusCheckNoData}
replaceTemplateStrings={replaceTemplateStrings}
tutorialId={'my_testing_tutorial'}
bulkCreate={() => {}}
/>
);
await loadTutorialPromise;
component.update();
await component.instance().checkInstructionSetStatus(0);
expect(component.state('statusCheckStates')[0]).toEqual('NO_DATA');
});

test('should return no_data when custom status check callback is not defined', async () => {
const tutorialWithoutCustomStatusCheck = {
...tutorial,
customStatusCheckName: undefined,
};
const component = mountWithIntl(
<Tutorial.WrappedComponent
addBasePath={addBasePath}
isCloudEnabled={true}
getTutorial={async () => tutorialWithoutCustomStatusCheck}
replaceTemplateStrings={replaceTemplateStrings}
tutorialId={'my_testing_tutorial'}
bulkCreate={() => {}}
/>
);
await loadTutorialPromise;
component.update();
await component.instance().checkInstructionSetStatus(0);
expect(component.state('statusCheckStates')[0]).toEqual('NO_DATA');
});

test('should return has_data if esHits or customStatusCheck returns true', async () => {
const { instructionSets } = tutorial.elasticCloud;
const tutorialWithStatusCheckAndCustomStatusCheck = {
...tutorial,
customStatusCheckName: undefined,
elasticCloud: {
instructionSets: [
{
...instructionSets[0],
statusCheck: {
title: 'check status',
text: 'check status',
esHitsCheck: {
index: 'foo',
query: {
bool: {
filter: [{ term: { 'processor.event': 'onboarding' } }],
},
},
},
},
},
],
},
};
const component = mountWithIntl(
<Tutorial.WrappedComponent
addBasePath={addBasePath}
isCloudEnabled={true}
getTutorial={async () => tutorialWithStatusCheckAndCustomStatusCheck}
replaceTemplateStrings={replaceTemplateStrings}
tutorialId={'my_testing_tutorial'}
bulkCreate={() => {}}
/>
);
await loadTutorialPromise;
component.update();
await component.instance().checkInstructionSetStatus(0);
expect(component.state('statusCheckStates')[0]).toEqual('has_data');
});
});

0 comments on commit 5dddd48

Please sign in to comment.