Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: tests for Searches component (ET-198) #9576

Merged
merged 8 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 4 additions & 96 deletions webui/react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion webui/react/src/components/ColumnPickerMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ const ColumnPickerTab: React.FC<ColumnTabProps> = ({
);

return (
<div data-test-component="columnPickerTab">
<div data-test-component="columnPickerTab" data-testid="column-picker-tab">
<Input
allowClear
autoFocus
Expand Down
138 changes: 138 additions & 0 deletions webui/react/src/components/Searches/Searches.test.tsx
johnkim-det marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import UIProvider, { DefaultTheme } from 'hew/Theme';
import { BrowserRouter } from 'react-router-dom';

import { ThemeProvider } from 'components/ThemeProvider';
import { Project } from 'types';

import Searches from './Searches';
import { defaultProjectSettings } from './Searches.settings';

const projectMock: Project = {
archived: false,
description: '',
id: 1849,
immutable: false,
lastExperimentStartedAt: '2024-06-03T19:33:38.731220Z',
name: 'test',
notes: [],
numActiveExperiments: 1,
numExperiments: 16,
state: 'UNSPECIFIED',
userId: 1354,
workspaceId: 1684,
workspaceName: '',
};

const expectedFilterString = JSON.stringify({
filterGroup: {
children: [
{ children: [], conjunction: 'and', kind: 'group' },
{
columnName: 'searcherType',
kind: 'field',
location: 'LOCATION_TYPE_EXPERIMENT',
operator: '!=',
type: 'COLUMN_TYPE_TEXT',
value: 'single',
},
],
conjunction: 'and',
kind: 'group',
},
showArchived: false,
});

const searchExperiments = vi.hoisted(() => vi.fn());

vi.mock('services/api', () => ({
getProjectColumns: vi.fn().mockReturnValue([]),
getWorkspaces: vi.fn().mockResolvedValue({ workspaces: [] }),
resetUserSetting: () => Promise.resolve(),
searchExperiments,
}));

vi.mock('stores/userSettings', async (importOriginal) => {
const userSettings = await import('stores/userSettings');
const store = new userSettings.UserSettingsStore();

store.clear();

return {
...(await importOriginal<typeof import('stores/userSettings')>()),
default: store,
};
});

vi.mock('hooks/useMobile', async (importOriginal) => {
return {
...(await importOriginal<typeof import('hooks/useMobile')>()),
default: () => false,
};
});

const user = userEvent.setup();

const setup = (numExperiments?: number) => {
searchExperiments.mockImplementation(() => {
return Promise.resolve({
experiments: [],
pagination: { total: numExperiments ?? 0 },
});
});

render(
<UIProvider theme={DefaultTheme.Light}>
<ThemeProvider>
<BrowserRouter>
<Searches project={projectMock} />
</BrowserRouter>
</ThemeProvider>
</UIProvider>,
);
};

describe('Searches', () => {
it('should display count', async () => {
setup(2);
expect(screen.getByText('Loading searches...')).toBeInTheDocument();

await waitFor(() => {
expect(screen.getByText('2 searches')).toBeInTheDocument();
});
Comment on lines +101 to +103
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await waitFor(() => {
expect(screen.getByText('2 searches')).toBeInTheDocument();
});
expect(await screen.findByText('2 searches')).toBeInTheDocument();

});

it('should display empty state', async () => {
setup();
expect(screen.getByText('Loading searches...')).toBeInTheDocument();
johnkim-det marked this conversation as resolved.
Show resolved Hide resolved

await waitFor(() => {
expect(screen.getByText('0 searches')).toBeInTheDocument();
expect(screen.getByText('No Searches')).toBeInTheDocument();
});
Comment on lines +110 to +113
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await waitFor(() => {
expect(screen.getByText('0 searches')).toBeInTheDocument();
expect(screen.getByText('No Searches')).toBeInTheDocument();
});
expect(await screen.findByText('0 searches')).toBeInTheDocument();
expect(await screen.findByText('No Searches')).toBeInTheDocument();

});

it('should display column picker menu without tab selection', async () => {
setup();

await user.click(screen.getByTestId('columns-menu-button'));
expect(screen.queryByRole('tab')).not.toBeInTheDocument();
expect(screen.getByTestId('column-picker-tab')).toBeInTheDocument();
});

it('should have hidden filter to exclude single-trial experiments', () => {
setup();

expect(vi.mocked(searchExperiments)).toHaveBeenCalledWith(
expect.objectContaining({
filter: expectedFilterString,
limit: defaultProjectSettings.pageLimit,
offset: 0,
projectId: projectMock.id,
sort: defaultProjectSettings.sortString,
}),
{ signal: expect.any(AbortSignal) },
);
});
});
15 changes: 13 additions & 2 deletions webui/react/src/components/Searches/Searches.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import DataGrid, {
} from 'hew/DataGrid/DataGrid';
import { MenuItem } from 'hew/Dropdown';
import Icon from 'hew/Icon';
import Link from 'hew/Link';
import Message from 'hew/Message';
import Pagination from 'hew/Pagination';
import Row from 'hew/Row';
Expand All @@ -31,7 +32,7 @@ import { useObservable } from 'micro-observables';
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { v4 as uuidv4 } from 'uuid';

import { Error, NoExperiments } from 'components/exceptions';
import { Error } from 'components/exceptions';
import ExperimentActionDropdown from 'components/ExperimentActionDropdown';
import { FilterFormStore, ROOT_ID } from 'components/FilterForm/components/FilterFormStore';
import {
Expand All @@ -55,6 +56,7 @@ import useMobile from 'hooks/useMobile';
import usePolling from 'hooks/usePolling';
import { useSettings } from 'hooks/useSettings';
import { useTypedParams } from 'hooks/useTypedParams';
import { paths } from 'routes/utils';
import { getProjectColumns, searchExperiments } from 'services/api';
import {
V1BulkExperimentFilters,
Expand Down Expand Up @@ -873,7 +875,16 @@ const Searches: React.FC<Props> = ({ project }) => {
<div className={css.content} ref={contentRef}>
{!isLoading && experiments.length === 0 ? (
numFilters === 0 ? (
<NoExperiments />
<Message
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qq: what is the reason to replace <NoExperiments />?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want it to say "Searches" instead of "Experiments"

action={
<Link external href={paths.docs('/get-started/webui-qs.html')}>
Quick Start Guide
</Link>
}
description="Keep track of searches in a project by connecting up your code."
icon="experiment"
title="No Searches"
/>
) : (
<Message description="No results matching your filters" icon="search" />
)
Expand Down
Loading