From 00cd6d1d8afce690d7cb43f818e8a03660b7ee38 Mon Sep 17 00:00:00 2001 From: Dima Arnautov Date: Thu, 30 Apr 2020 13:36:42 +0200 Subject: [PATCH] [ML] container test --- .../explorer_swimlane_container.test.tsx | 122 ++++++++++++++++++ .../explorer_swimlane_container.tsx | 5 +- 2 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 x-pack/plugins/ml/public/embeddables/anomaly_swimlane/explorer_swimlane_container.test.tsx diff --git a/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/explorer_swimlane_container.test.tsx b/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/explorer_swimlane_container.test.tsx new file mode 100644 index 0000000000000..06a7c6ba868e3 --- /dev/null +++ b/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/explorer_swimlane_container.test.tsx @@ -0,0 +1,122 @@ +/* + * 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 React from 'react'; +import { render } from '@testing-library/react'; +import { ExplorerSwimlaneContainer } from './explorer_swimlane_container'; +import { BehaviorSubject, Observable } from 'rxjs'; +import { + AnomalySwimlaneEmbeddableInput, + AnomalySwimlaneServices, +} from './anomaly_swimlane_embeddable'; +import { CoreStart } from 'kibana/public'; +import { MlStartDependencies } from '../../plugin'; +import { useSwimlaneInputResolver } from './swimlane_input_resolver'; +// @ts-ignore +import { ExplorerSwimlane } from '../../application/explorer/explorer_swimlane'; +// @ts-ignore +import { MlTooltipComponent } from '../../application/components/chart_tooltip'; +import { SWIMLANE_TYPE } from '../../application/explorer/explorer_constants'; + +jest.mock('./swimlane_input_resolver', () => ({ + useSwimlaneInputResolver: jest.fn(() => { + return []; + }), +})); + +jest.mock('../../application/explorer/explorer_swimlane', () => ({ + ExplorerSwimlane: jest.fn(), +})); + +jest.mock('../../application/components/chart_tooltip', () => ({ + MlTooltipComponent: jest.fn(), +})); + +describe('ExplorerSwimlaneContainer', () => { + let embeddableInput: BehaviorSubject>; + let refresh: BehaviorSubject; + let services: [CoreStart, MlStartDependencies, AnomalySwimlaneServices]; + + beforeEach(() => { + embeddableInput = new BehaviorSubject({ + id: 'test-swimlane-embeddable', + } as Partial); + }); + + test('should render a swimlane with a valid embeddable input', async () => { + const mockOverallData = { + laneLabels: ['Overall'], + points: [ + { + laneLabel: 'Overall', + time: 1572825600, + value: 55.00448, + }, + ], + interval: 345600, + earliest: 1572134400, + latest: 1588377599.999, + }; + + (useSwimlaneInputResolver as jest.Mock).mockReturnValueOnce([ + mockOverallData, + SWIMLANE_TYPE.OVERALL, + undefined, + ]); + + const { findByTestId } = render( + + } + services={services} + refresh={refresh} + /> + ); + expect( + await findByTestId('mlMaxAnomalyScoreEmbeddable_test-swimlane-embeddable') + ).toBeDefined(); + }); + + test('should render an error in case it could not fetch the ML swimlane data', async () => { + (useSwimlaneInputResolver as jest.Mock).mockReturnValueOnce([ + undefined, + undefined, + undefined, + { message: 'Something went wrong' }, + ]); + + const { findByText } = render( + + } + services={services} + refresh={refresh} + /> + ); + const errorMessage = await findByText('Something went wrong'); + expect(errorMessage).toBeDefined(); + }); + + test('should render a loading indicator during the data fetching', async () => { + const { findByTestId } = render( + + } + services={services} + refresh={refresh} + /> + ); + expect( + await findByTestId('loading_mlMaxAnomalyScoreEmbeddable_test-swimlane-embeddable') + ).toBeDefined(); + }); +}); diff --git a/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/explorer_swimlane_container.tsx b/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/explorer_swimlane_container.tsx index 9acf0deeaf8b5..ec056bf3d119c 100644 --- a/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/explorer_swimlane_container.tsx +++ b/x-pack/plugins/ml/public/embeddables/anomaly_swimlane/explorer_swimlane_container.tsx @@ -104,7 +104,10 @@ export const ExplorerSwimlaneContainer: FC = ({ ) : ( - + )}