This commit is contained in:
Jingyi Z 2024-05-18 21:17:24 +07:00 committed by GitHub
commit 701b712b6c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 50 additions and 17 deletions

View File

@ -22,6 +22,7 @@ import userEvent from '@testing-library/user-event';
import { render, screen, waitFor } from 'spec/helpers/testing-library';
import LeftPanel from 'src/features/datasets/AddDataset/LeftPanel';
import { exampleDataset } from 'src/features/datasets/AddDataset/DatasetPanel/fixtures';
import { MemoryRouter } from 'react-router-dom';
const databasesEndpoint = 'glob:*/api/v1/database/?q*';
const schemasEndpoint = 'glob:*/api/v1/database/*/schemas*';
@ -158,16 +159,26 @@ afterEach(() => {
const mockFun = jest.fn();
test('should render', async () => {
render(<LeftPanel setDataset={mockFun} />, {
useRedux: true,
});
render(
<MemoryRouter>
<LeftPanel setDataset={mockFun} />
</MemoryRouter>,
{
useRedux: true,
},
);
expect(
await screen.findByText(/Select database or type to search databases/i),
).toBeInTheDocument();
});
test('should render schema selector, database selector container, and selects', async () => {
render(<LeftPanel setDataset={mockFun} />, { useRedux: true });
render(
<MemoryRouter>
<LeftPanel setDataset={mockFun} />
</MemoryRouter>,
{ useRedux: true },
);
expect(
await screen.findByText(/Select database or type to search databases/i),
@ -184,7 +195,12 @@ test('should render schema selector, database selector container, and selects',
});
test('does not render blank state if there is nothing selected', async () => {
render(<LeftPanel setDataset={mockFun} />, { useRedux: true });
render(
<MemoryRouter>
<LeftPanel setDataset={mockFun} />{' '}
</MemoryRouter>,
{ useRedux: true },
);
expect(
await screen.findByText(/Select database or type to search databases/i),
@ -194,9 +210,14 @@ test('does not render blank state if there is nothing selected', async () => {
});
test('renders list of options when user clicks on schema', async () => {
render(<LeftPanel setDataset={mockFun} dataset={exampleDataset[0]} />, {
useRedux: true,
});
render(
<MemoryRouter>
<LeftPanel setDataset={mockFun} dataset={exampleDataset[0]} />
</MemoryRouter>,
{
useRedux: true,
},
);
// Click 'test-postgres' database to access schemas
const databaseSelect = screen.getByRole('combobox', {
@ -216,9 +237,14 @@ test('renders list of options when user clicks on schema', async () => {
});
test('searches for a table name', async () => {
render(<LeftPanel setDataset={mockFun} dataset={exampleDataset[0]} />, {
useRedux: true,
});
render(
<MemoryRouter>
<LeftPanel setDataset={mockFun} dataset={exampleDataset[0]} />
</MemoryRouter>,
{
useRedux: true,
},
);
// Click 'test-postgres' database to access schemas
const databaseSelect = screen.getByRole('combobox', {
@ -274,11 +300,13 @@ test('searches for a table name', async () => {
test('renders a warning icon when a table name has a pre-existing dataset', async () => {
render(
<LeftPanel
setDataset={mockFun}
dataset={exampleDataset[0]}
datasetNames={['Sheet2']}
/>,
<MemoryRouter>
<LeftPanel
setDataset={mockFun}
dataset={exampleDataset[0]}
datasetNames={['Sheet2']}
/>
</MemoryRouter>,
{
useRedux: true,
},

View File

@ -28,6 +28,7 @@ import {
DatasetObject,
} from 'src/features/datasets/AddDataset/types';
import { Table } from 'src/hooks/apiResources';
import { useLocation } from 'react-router-dom';
interface LeftPanelProps {
setDataset: Dispatch<SetStateAction<object>>;
@ -122,6 +123,9 @@ export default function LeftPanel({
datasetNames,
}: LeftPanelProps) {
const { addDangerToast } = useToasts();
const location = useLocation();
const isAddingDataSet = location.pathname.includes('/dataset/add') ?? false;
const setDatabase = useCallback(
(db: Partial<DatabaseObject>) => {
@ -152,6 +156,7 @@ export default function LeftPanel({
});
};
useEffect(() => {
// TODO: storing and fetching the last selected database from local storage is potentially technical debt
const currentUserSelectedDb = getItem(
LocalStorageKeys.Database,
null,
@ -182,7 +187,7 @@ export default function LeftPanel({
return (
<LeftPanelStyle>
<TableSelector
database={dataset?.db}
database={isAddingDataSet ? null : dataset?.db}
handleError={addDangerToast}
emptyState={emptyStateComponent(false)}
onDbChange={setDatabase}