Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Kawika Avilla <[email protected]>
  • Loading branch information
kavilla committed Oct 19, 2024
1 parent 9ca559a commit 4515d42
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 72 deletions.

This file was deleted.

64 changes: 7 additions & 57 deletions src/plugins/data/public/ui/dataset_selector/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,94 +38,44 @@ describe('ConnectedDatasetSelector', () => {
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', () => {
const wrapper = mount(
<ConnectedDatasetSelector
onSubmit={mockOnSubmit}
selectedDataset={undefined}
setSelectedDataset={jest.fn()}
setIndexPattern={jest.fn()}
services={mockServices}
/>
);
const wrapper = mount(<ConnectedDatasetSelector onSubmit={mockOnSubmit} />);
expect(wrapper.find(DatasetSelector).props()).toEqual({
selectedDataset: undefined,
setSelectedDataset: expect.any(Function),
setIndexPattern: expect.any(Function),
handleDatasetChange: expect.any(Function),
services: mockServices,
});
});

it('should initialize selectedDataset correctly', () => {
const mockDataset: Dataset = { id: 'initial', title: 'Initial Dataset', type: 'test' };
mockQueryString.getQuery.mockReturnValueOnce({ dataset: mockDataset });

const wrapper = mount(
<ConnectedDatasetSelector
onSubmit={mockOnSubmit}
selectedDataset={mockDataset}
setSelectedDataset={jest.fn()}
setIndexPattern={jest.fn()}
services={mockServices}
/>
);
const wrapper = mount(<ConnectedDatasetSelector onSubmit={mockOnSubmit} />);
expect(wrapper.find(DatasetSelector).prop('selectedDataset')).toEqual(mockDataset);
});

it('should call handleDatasetChange only once when dataset changes', () => {
const setSelectedDataset = jest.fn();
const setIndexPattern = jest.fn();
const wrapper = mount(
<ConnectedDatasetSelector
onSubmit={mockOnSubmit}
selectedDataset={undefined}
setSelectedDataset={setSelectedDataset}
setIndexPattern={setIndexPattern}
services={mockServices}
/>
);
const handleDatasetChange = wrapper.find(DatasetSelector).prop('handleDatasetChange') as (
const wrapper = mount(<ConnectedDatasetSelector onSubmit={mockOnSubmit} />);
const setSelectedDataset = wrapper.find(DatasetSelector).prop('setSelectedDataset') as (
dataset?: Dataset
) => void;

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

expect(mockQueryString.getInitialQueryByDataset).toHaveBeenCalledTimes(1);
expect(mockQueryString.setQuery).toHaveBeenCalledTimes(1);
expect(mockOnSubmit).toHaveBeenCalledTimes(1);
expect(setSelectedDataset).toHaveBeenCalledWith(newDataset);
expect(setIndexPattern).toHaveBeenCalledWith(newDataset.id);
});

it('should subscribe to queryString.getUpdates$ and unsubscribe on unmount', () => {
const wrapper = mount(
<ConnectedDatasetSelector
onSubmit={mockOnSubmit}
selectedDataset={undefined}
setSelectedDataset={jest.fn()}
setIndexPattern={jest.fn()}
services={mockServices}
/>
);

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

wrapper.unmount();

expect(mockUnsubscribe).toHaveBeenCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ describe('test redux state persistence', () => {
"metadata": Object {
"indexPattern": "id",
"originatingApp": undefined,
"selectedDataset": undefined,
},
}
`);
Expand Down

0 comments on commit 4515d42

Please sign in to comment.