Skip to content

Commit

Permalink
fix the test by adding the getUpdates$ method to the mock queryString…
Browse files Browse the repository at this point in the history
… object

Signed-off-by: Anan Zhuang <[email protected]>
  • Loading branch information
ananzh committed Oct 16, 2024
1 parent f569bd3 commit d97ce5b
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/plugins/data/public/ui/dataset_selector/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import React from 'react';
import { mount } from 'enzyme';
import { act } from 'react-dom/test-utils';
import { DatasetSelector as ConnectedDatasetSelector } from './index';
import { DatasetSelector } from './dataset_selector';
import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public';
Expand All @@ -19,6 +20,8 @@ jest.mock('./dataset_selector', () => ({
}));

describe('ConnectedDatasetSelector', () => {
const mockSubscribe = jest.fn();
const mockUnsubscribe = jest.fn();
const mockQueryString = {
getQuery: jest.fn().mockReturnValue({}),
getDefaultQuery: jest.fn().mockReturnValue({}),
Expand All @@ -27,19 +30,22 @@ describe('ConnectedDatasetSelector', () => {
getDatasetService: jest.fn().mockReturnValue({
addRecentDataset: jest.fn(),
}),
getUpdates$: jest.fn().mockReturnValue({
subscribe: mockSubscribe.mockReturnValue({ unsubscribe: mockUnsubscribe }),
}),
};
const mockOnSubmit = jest.fn();
const mockServices = {
data: {
query: {
// @ts-ignore
queryString: mockQueryString,
},
},
};

beforeEach(() => {
(useOpenSearchDashboards as jest.Mock).mockReturnValue({ services: mockServices });
jest.clearAllMocks();
});

it('should render DatasetSelector with correct props', () => {
Expand All @@ -66,10 +72,23 @@ describe('ConnectedDatasetSelector', () => {
) => void;

const newDataset: Dataset = { id: 'test', title: 'Test Dataset', type: 'test' };
setSelectedDataset(newDataset);
act(() => {
setSelectedDataset(newDataset);
});

expect(mockQueryString.getInitialQueryByDataset).toHaveBeenCalledTimes(1);
expect(mockQueryString.setQuery).toHaveBeenCalledTimes(1);
expect(mockOnSubmit).toHaveBeenCalledTimes(1);
});

it('should subscribe to queryString.getUpdates$ and unsubscribe on unmount', () => {
const wrapper = mount(<ConnectedDatasetSelector onSubmit={mockOnSubmit} />);

expect(mockQueryString.getUpdates$).toHaveBeenCalledTimes(1);
expect(mockSubscribe).toHaveBeenCalledTimes(1);

wrapper.unmount();

expect(mockUnsubscribe).toHaveBeenCalledTimes(1);
});
});

0 comments on commit d97ce5b

Please sign in to comment.