Skip to content

Commit

Permalink
added Uts for index/canvas
Browse files Browse the repository at this point in the history
Signed-off-by: Riya Saxena <[email protected]>
  • Loading branch information
riysaxen-amzn committed Nov 13, 2024
1 parent bb9d6c0 commit 909c4ac
Showing 1 changed file with 7 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import DiscoverCanvas from './index';
import { useDiscoverContext } from '../context';
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
import { setColumns, useDispatch, useSelector } from '../../utils/state_management';
import { ResultStatus } from '../utils/use_search';
import { RefetchSubject, ResultStatus, SearchData, SearchRefetch } from '../utils/use_search';
import { EuiPanel } from '@elastic/eui';
import { injectI18n, FormattedMessage } from '@osd/i18n/react';
import { DataPublicPluginStart, UI_SETTINGS } from 'src/plugins/data/public';
Expand All @@ -20,6 +20,7 @@ import { setUISettings } from '../../../../../../plugins/vis_augmenter/public';
import { createDataExplorerServicesMock } from '../../../../../../plugins/data_explorer/public/utils/mocks';
import { DiscoverViewServices } from '../../../build_services';
import { discoverPluginMock } from '../../../mocks';
import { BehaviorSubject, Subject } from 'rxjs';

// Mocking required modules
jest.mock('../context', () => ({
Expand Down Expand Up @@ -128,14 +129,16 @@ const mockUseOpenSearchDashboards = () => {

// Tests
describe('DiscoverCanvas', () => {
const mockRefetch = { next: jest.fn() };
const mockRefetchSubject: RefetchSubject = new Subject<SearchRefetch>();
mockRefetchSubject.next('refetch');
const data$ = new BehaviorSubject<SearchData>({ status: ResultStatus.LOADING });
let dataMock: jest.Mocked<DataPublicPluginStart>;

beforeEach(() => {
dataMock = dataPluginMock.createStartContract();
useDiscoverContext.mockReturnValue({
data$: { getValue: jest.fn().mockReturnValue({ status: ResultStatus.LOADING }) },
refetch$: mockRefetch,
data$,
refetch$: mockRefetchSubject,
indexPattern: {
timeFieldName: 'timestamp',
isTimeBased: jest.fn().mockReturnValue(true),
Expand All @@ -150,11 +153,6 @@ describe('DiscoverCanvas', () => {
});

it('renders without index pattern', () => {
useDiscoverContext.mockReturnValueOnce({
data$: { getValue: jest.fn().mockReturnValue({ status: ResultStatus.LOADING }) },
refetch$: mockRefetch,
indexPattern: null,
});

Check failure on line 156 in src/plugins/discover/public/application/view_components/canvas/index.test.tsx

View workflow job for this annotation

GitHub Actions / Lint and validate

Delete `⏎`
render(<DiscoverCanvas setHeaderActionMenu={jest.fn()} history={[]} optionalRef={null} />);

Expand All @@ -168,35 +166,20 @@ describe('DiscoverCanvas', () => {
});

it('shows no results when status is NO_RESULTS', () => {

Check failure on line 168 in src/plugins/discover/public/application/view_components/canvas/index.test.tsx

View workflow job for this annotation

GitHub Actions / Lint and validate

Delete `⏎`
useDiscoverContext.mockReturnValueOnce({
data$: { getValue: jest.fn().mockReturnValue({ status: ResultStatus.NO_RESULTS }) },
refetch$: mockRefetch,
indexPattern: { timeFieldName: 'timestamp' },
});

render(<DiscoverCanvas setHeaderActionMenu={jest.fn()} history={[]} optionalRef={null} />);

expect(screen.getByTestId('discover-no-results')).toBeInTheDocument();
});

it('shows uninitialized state when status is UNINITIALIZED', () => {
useDiscoverContext.mockReturnValueOnce({
data$: { getValue: jest.fn().mockReturnValue({ status: ResultStatus.UNINITIALIZED }) },
refetch$: mockRefetch,
indexPattern: { timeFieldName: 'timestamp' },
});

Check failure on line 176 in src/plugins/discover/public/application/view_components/canvas/index.test.tsx

View workflow job for this annotation

GitHub Actions / Lint and validate

Delete `⏎`
render(<DiscoverCanvas setHeaderActionMenu={jest.fn()} history={[]} optionalRef={null} />);

expect(screen.getByTestId('discover-uninitialized')).toBeInTheDocument();
});

it('renders chart and table when status is READY', async () => {

Check failure on line 182 in src/plugins/discover/public/application/view_components/canvas/index.test.tsx

View workflow job for this annotation

GitHub Actions / Lint and validate

Delete `⏎`
useDiscoverContext.mockReturnValueOnce({
data$: { getValue: jest.fn().mockReturnValue({ status: ResultStatus.READY, rows: [] }) },
refetch$: mockRefetch,
indexPattern: { timeFieldName: 'timestamp' },
});

render(<DiscoverCanvas setHeaderActionMenu={jest.fn()} history={[]} optionalRef={null} />);

Expand All @@ -209,12 +192,6 @@ describe('DiscoverCanvas', () => {
useDispatch.mockReturnValue(dispatchMock);

const newIndexPattern = { timeFieldName: 'timestamp' };
useDiscoverContext.mockReturnValueOnce({
data$: { getValue: jest.fn().mockReturnValue({ status: ResultStatus.READY }) },
refetch$: mockRefetch,
indexPattern: newIndexPattern,
});

render(<DiscoverCanvas setHeaderActionMenu={jest.fn()} history={[]} optionalRef={null} />);

expect(dispatchMock).toHaveBeenCalledWith({ columns: [] });
Expand Down

0 comments on commit 909c4ac

Please sign in to comment.