Skip to content

Commit

Permalink
test: Enhanced test coverage for ImportFlyout component unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: kishor82 <[email protected]>
  • Loading branch information
kishor82 committed Aug 7, 2023
1 parent 52f7815 commit b160b06
Showing 1 changed file with 83 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { ReactWrapper, mount } from 'enzyme';
const mockFile = new File(['{"text":"Sample JSON data"}'], 'sample.json', {
type: 'application/json',
});
const mockFile1 = new File(['{"text":"Sample JSON data1"}'], 'sample.json', {
type: 'application/json',
});

const mockInvalidFile = new File(['Some random data'], 'sample.json', {
type: 'application/json',
Expand All @@ -31,21 +34,36 @@ const callOutIdentifier = 'EuiCallOut';

const invalidFileError = 'The selected file is not valid. Please select a valid JSON file.';

const defaultQuery = {
id: '43461752-1fd5-472e-8487-b47ff7fccbc8',
createdAt: 1690958998493,
updatedAt: 1690958998493,
text: 'GET _search\n{\n "query": {\n "match_all": {}\n }\n}',
};

describe('ImportFlyout Component', () => {
let mockedAppContextValue: ContextValue;
const mockClose = jest.fn();
const mockRefresh = jest.fn();
const mockFindAll = jest.fn();
const mockCreate = jest.fn();
const mockUpdate = jest.fn();

let component: ReactWrapper<any, Readonly<{}>, React.Component<{}, {}, any>>;

beforeEach(async () => {
jest.clearAllMocks();

mockedAppContextValue = serviceContextMock.create();

mockedAppContextValue.services.objectStorageClient.text = {
create: jest.fn(),
update: jest.fn(),
findAll: jest.fn().mockResolvedValue([]),
create: mockCreate,
update: mockUpdate,
findAll: mockFindAll,
};

mockFindAll.mockResolvedValue([defaultQuery]);

await act(async () => {
component = mount(wrapWithIntl(<ImportFlyout close={mockClose} refresh={mockRefresh} />), {
wrappingComponent: ServicesContextProvider,
Expand All @@ -67,10 +85,11 @@ describe('ImportFlyout Component', () => {
expect(component.find(confirmBtnIdentifier).first().props().disabled).toBe(true);
component.update();

act(() => {
await act(async () => {
const nodes = component.find(filePickerIdentifier);
// @ts-ignore
nodes.first().props().onChange([mockFile]);
nodes.first().props().onChange([mockFile1]);
await new Promise((r) => setTimeout(r, 1));
});
await nextTick();
component.update();
Expand All @@ -80,19 +99,20 @@ describe('ImportFlyout Component', () => {
});

it('should handle import process with default import mode', async () => {
// Test import process for different scenarios
await act(async () => {
const nodes = component.find(filePickerIdentifier);
// @ts-ignore
nodes.first().props().onChange([mockFile]);
await nextTick();
// Applied a timeout after FileReader.onload event to resolve side effect issue.
await new Promise((r) => setTimeout(r, 10));
});

await nextTick();
component.update();

await act(async () => {
component.find(confirmBtnIdentifier).first().simulate('click');
await nextTick();
component.find(confirmBtnIdentifier).first().simulate('click');
});

component.update();
Expand All @@ -101,6 +121,8 @@ describe('ImportFlyout Component', () => {
expect(component.find(mergeOptionIdentifier).first().props().checked).toBe(true);
expect(component.find(overwriteOptionIdentifier).first().props().checked).toBe(false);

// should update existing query
expect(mockUpdate).toBeCalledTimes(1);
expect(mockClose).toBeCalledTimes(1);
expect(mockRefresh).toBeCalledTimes(1);
});
Expand All @@ -115,8 +137,8 @@ describe('ImportFlyout Component', () => {
component.update();

await act(async () => {
component.find(confirmBtnIdentifier).first().simulate('click');
await nextTick();
component.find(confirmBtnIdentifier).first().simulate('click');
});

component.update();
Expand All @@ -126,6 +148,32 @@ describe('ImportFlyout Component', () => {
expect(component.find(importErrorTextIdentifier).text()).toEqual(invalidFileError);
});

it('should handle internal errors', async () => {
const errorMessage = 'some internal error';
mockFindAll.mockRejectedValue(new Error(errorMessage));
await act(async () => {
const nodes = component.find(filePickerIdentifier);
// @ts-ignore
nodes.first().props().onChange([mockFile]);
await new Promise((r) => setTimeout(r, 1));
});

component.update();

await act(async () => {
await nextTick();
component.find(confirmBtnIdentifier).first().simulate('click');
});

component.update();

expect(component.find(callOutIdentifier).exists()).toBe(true);

expect(component.find(importErrorTextIdentifier).text()).toEqual(
`The file could not be processed due to error: "${errorMessage}"`
);
});

it('should cancel button work normally', async () => {
act(() => {
component.find(cancelBtnIdentifier).first().simulate('click');
Expand All @@ -136,28 +184,28 @@ describe('ImportFlyout Component', () => {

describe('OverwriteModal', () => {
beforeEach(async () => {
jest.clearAllMocks();
// Select a file
await act(async () => {
const nodes = component.find(filePickerIdentifier);
// @ts-ignore
nodes.first().props().onChange([mockFile]);
await nextTick();
await new Promise((r) => setTimeout(r, 1));
});

component.update();

// change import mode to overwrite
await act(async () => {
component.find(overwriteOptionIdentifier).last().simulate('change');
await nextTick();
component.find(overwriteOptionIdentifier).last().simulate('change');
});

component.update();

// import selected file
await act(async () => {
component.find(confirmBtnIdentifier).first().simulate('click');
await nextTick();
component.find(confirmBtnIdentifier).first().simulate('click');
});

component.update();
Expand All @@ -168,12 +216,14 @@ describe('ImportFlyout Component', () => {

// confirm overwrite
await act(async () => {
component.find(confirmModalConfirmButton).first().simulate('click');
await nextTick();
component.find(confirmModalConfirmButton).first().simulate('click');
});

component.update();

// should update existing query
expect(mockUpdate).toBeCalledTimes(1);
expect(mockClose).toBeCalledTimes(1);
expect(mockRefresh).toBeCalledTimes(1);

Expand All @@ -195,5 +245,24 @@ describe('ImportFlyout Component', () => {
// confirm overwrite modal should close after cancel.
expect(component.find('OverwriteModal').exists()).toBe(false);
});

it('should create storage text when storage client returns empty result with overwrite import mode', async () => {
mockFindAll.mockResolvedValue([]);

// confirm overwrite
await act(async () => {
await nextTick();
component.find(confirmModalConfirmButton).first().simulate('click');
});

component.update();

expect(component.find(overwriteOptionIdentifier).first().props().checked).toBe(true);

// should create new query
expect(mockCreate).toBeCalledTimes(1);
expect(mockClose).toBeCalledTimes(1);
expect(mockRefresh).toBeCalledTimes(1);
});
});
});

0 comments on commit b160b06

Please sign in to comment.