Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enterprise Search] Convert IndexingStatus to use logic for fetching #84710

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@
* you may not use this file except in compliance with the Elastic License.
*/

import '../../__mocks__/kea.mock';
import '../../__mocks__/shallow_useeffect.mock';

import { setMockActions, setMockValues } from '../../__mocks__';

import React from 'react';
import { shallow } from 'enzyme';

import { EuiPanel } from '@elastic/eui';

import { IndexingStatusContent } from './indexing_status_content';
import { IndexingStatusErrors } from './indexing_status_errors';
import { IndexingStatusFetcher } from './indexing_status_fetcher';
import { IndexingStatus } from './indexing_status';

describe('IndexingStatus', () => {
const getItemDetailPath = jest.fn();
const getStatusPath = jest.fn();
const onComplete = jest.fn();
const setGlobalIndexingStatus = jest.fn();
const fetchIndexingStatus = jest.fn();

const props = {
percentageComplete: 50,
Expand All @@ -32,20 +37,29 @@ describe('IndexingStatus', () => {
setGlobalIndexingStatus,
};

beforeEach(() => {
setMockActions({ fetchIndexingStatus });
});

it('renders', () => {
setMockValues({
percentageComplete: 50,
numDocumentsWithErrors: 0,
});
const wrapper = shallow(<IndexingStatus {...props} />);
const fetcher = wrapper.find(IndexingStatusFetcher).prop('children')(
props.percentageComplete,
props.numDocumentsWithErrors
);

expect(shallow(fetcher).find(EuiPanel)).toHaveLength(1);
expect(shallow(fetcher).find(IndexingStatusContent)).toHaveLength(1);
expect(wrapper.find(EuiPanel)).toHaveLength(1);
expect(wrapper.find(IndexingStatusContent)).toHaveLength(1);
expect(fetchIndexingStatus).toHaveBeenCalled();
Comment on lines +50 to +52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love how much easier this is to test w/o the render prop!! 🎉

});

it('renders errors', () => {
setMockValues({
percentageComplete: 100,
numDocumentsWithErrors: 1,
});
const wrapper = shallow(<IndexingStatus {...props} percentageComplete={100} />);
const fetcher = wrapper.find(IndexingStatusFetcher).prop('children')(100, 1);
expect(shallow(fetcher).find(IndexingStatusErrors)).toHaveLength(1);

expect(wrapper.find(IndexingStatusErrors)).toHaveLength(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import React, { useEffect } from 'react';

import { useValues, useActions } from 'kea';

import { EuiPanel, EuiSpacer } from '@elastic/eui';

import { IndexingStatusContent } from './indexing_status_content';
import { IndexingStatusErrors } from './indexing_status_errors';
import { IndexingStatusFetcher } from './indexing_status_fetcher';
import { IndexingStatusLogic } from './indexing_status_logic';

import { IIndexingStatus } from '../types';

Expand All @@ -23,22 +25,34 @@ export interface IIndexingStatusProps extends IIndexingStatus {
setGlobalIndexingStatus?(activeReindexJob: IIndexingStatus): void;
}

export const IndexingStatus: React.FC<IIndexingStatusProps> = (props) => (
<IndexingStatusFetcher {...props}>
{(percentageComplete, numDocumentsWithErrors) => (
<div>
{percentageComplete < 100 && (
<EuiPanel paddingSize="l" hasShadow>
<IndexingStatusContent percentageComplete={percentageComplete} />
</EuiPanel>
)}
{percentageComplete === 100 && numDocumentsWithErrors > 0 && (
<>
<EuiSpacer />
<IndexingStatusErrors viewLinkPath={props.viewLinkPath} />
</>
)}
</div>
)}
</IndexingStatusFetcher>
);
export const IndexingStatus: React.FC<IIndexingStatusProps> = ({
itemId,
activeReindexJobId,
viewLinkPath,
getStatusPath,
onComplete,
}) => {
const { percentageComplete, numDocumentsWithErrors } = useValues(IndexingStatusLogic);
const { fetchIndexingStatus } = useActions(IndexingStatusLogic);
const statusPath = getStatusPath(itemId, activeReindexJobId);

useEffect(() => {
fetchIndexingStatus({ statusPath, onComplete });
}, []);

return (
<div className="c-stui-indexing-status-wrapper">
scottybollinger marked this conversation as resolved.
Show resolved Hide resolved
{percentageComplete < 100 && (
<EuiPanel paddingSize="l" hasShadow={true}>
<IndexingStatusContent percentageComplete={percentageComplete} />
</EuiPanel>
)}
{percentageComplete === 100 && numDocumentsWithErrors > 0 && (
<>
<EuiSpacer />
<IndexingStatusErrors viewLinkPath={viewLinkPath} />
</>
)}
</div>
);
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { resetContext } from 'kea';

jest.mock('../http', () => ({
HttpLogic: {
values: { http: { get: jest.fn() } },
},
}));
import { HttpLogic } from '../http';

jest.mock('../flash_messages', () => ({
flashAPIErrors: jest.fn(),
}));
import { flashAPIErrors } from '../flash_messages';

import { IndexingStatusLogic } from './indexing_status_logic';

describe('IndexingStatusLogic', () => {
jest.useFakeTimers();
scottybollinger marked this conversation as resolved.
Show resolved Hide resolved

let unmount: any;

const mockStatusResponse = {
percentageComplete: 50,
numDocumentsWithErrors: 3,
activeReindexJobId: 1,
};

beforeEach(() => {
jest.clearAllMocks();
resetContext({});
unmount = IndexingStatusLogic.mount();
});

it('has expected default values', () => {
expect(IndexingStatusLogic.values).toEqual({
percentageComplete: 100,
numDocumentsWithErrors: 0,
});
});

describe('setIndexingStatus', () => {
it('sets reducers', () => {
IndexingStatusLogic.actions.setIndexingStatus(mockStatusResponse);

expect(IndexingStatusLogic.values.percentageComplete).toEqual(
mockStatusResponse.percentageComplete
);
expect(IndexingStatusLogic.values.numDocumentsWithErrors).toEqual(
mockStatusResponse.numDocumentsWithErrors
);
});
});

describe('fetchIndexingStatus', () => {
const statusPath = '/api/workplace_search/path/123';
const onComplete = jest.fn();
const TIMEOUT = 3000;

it('calls API and sets values', async () => {
const setIndexingStatusSpy = jest.spyOn(IndexingStatusLogic.actions, 'setIndexingStatus');
const promise = Promise.resolve(mockStatusResponse);
(HttpLogic.values.http.get as jest.Mock).mockReturnValue(promise);

IndexingStatusLogic.actions.fetchIndexingStatus({ statusPath, onComplete });
jest.advanceTimersByTime(TIMEOUT);

expect(HttpLogic.values.http.get).toHaveBeenCalledWith(statusPath);
await promise;

expect(setIndexingStatusSpy).toHaveBeenCalledWith(mockStatusResponse);
});

it('handles error', async () => {
const promise = Promise.reject('An error occured');
(HttpLogic.values.http.get as jest.Mock).mockReturnValue(promise);

IndexingStatusLogic.actions.fetchIndexingStatus({ statusPath, onComplete });
jest.advanceTimersByTime(TIMEOUT);

try {
await promise;
} catch {
expect(flashAPIErrors).toHaveBeenCalledWith('An error occured');
}
scottybollinger marked this conversation as resolved.
Show resolved Hide resolved
});

it('handles indexing complete state', async () => {
const promise = Promise.resolve({ ...mockStatusResponse, percentageComplete: 100 });
(HttpLogic.values.http.get as jest.Mock).mockReturnValue(promise);
IndexingStatusLogic.actions.fetchIndexingStatus({ statusPath, onComplete });
jest.advanceTimersByTime(TIMEOUT);

await promise;

expect(clearInterval).toHaveBeenCalled();
expect(onComplete).toHaveBeenCalledWith(mockStatusResponse.numDocumentsWithErrors);
});

it('handles unmounting', async () => {
unmount();
expect(clearInterval).toHaveBeenCalled();
});
});
});
scottybollinger marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { kea, MakeLogicType } from 'kea';

import { HttpLogic } from '../http';

import { IIndexingStatus } from '../types';

import { flashAPIErrors } from '../flash_messages';
scottybollinger marked this conversation as resolved.
Show resolved Hide resolved

interface IndexingStatusProps {
statusPath: string;
onComplete(numDocumentsWithErrors: number): void;
}

interface IndexingStatusActions {
fetchIndexingStatus(props: IndexingStatusProps): IndexingStatusProps;
setIndexingStatus({
percentageComplete,
numDocumentsWithErrors,
}: IIndexingStatus): IIndexingStatus;
}

interface IndexingStatusValues {
percentageComplete: number;
numDocumentsWithErrors: number;
}

let pollingInterval: number;
scottybollinger marked this conversation as resolved.
Show resolved Hide resolved

export const IndexingStatusLogic = kea<MakeLogicType<IndexingStatusValues, IndexingStatusActions>>({
actions: {
fetchIndexingStatus: ({ statusPath, onComplete }) => ({ statusPath, onComplete }),
setIndexingStatus: ({ numDocumentsWithErrors, percentageComplete }) => ({
numDocumentsWithErrors,
percentageComplete,
}),
},
reducers: {
percentageComplete: [
100,
{
setIndexingStatus: (_, { percentageComplete }) => percentageComplete,
},
],
numDocumentsWithErrors: [
0,
{
setIndexingStatus: (_, { numDocumentsWithErrors }) => numDocumentsWithErrors,
},
],
},
listeners: ({ actions }) => ({
fetchIndexingStatus: ({ statusPath, onComplete }: IndexingStatusProps) => {
pollingInterval = window.setInterval(async () => {
try {
const response = (await HttpLogic.values.http.get(statusPath)) as IIndexingStatus;
scottybollinger marked this conversation as resolved.
Show resolved Hide resolved
if (response.percentageComplete >= 100) {
clearInterval(pollingInterval);
}
actions.setIndexingStatus(response);
if (response.percentageComplete >= 100 && onComplete) {
onComplete(response.numDocumentsWithErrors);
}
} catch (e) {
flashAPIErrors(e);
}
}, 3000);
},
}),
events: () => ({
beforeUnmount() {
clearInterval(pollingInterval);
},
}),
});