import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
import {ReleasesContext} from 'sentry/utils/releases/releasesProvider';
import ReleasesSelectControl from 'sentry/views/dashboardsV2/releasesSelectControl';
import {DashboardFilters} from 'sentry/views/dashboardsV2/types';
function renderReleasesSelect({
onSearch,
handleChangeFilter,
}: {
handleChangeFilter?: (activeFilters: DashboardFilters) => void;
onSearch?: (searchTerm: string) => void;
}) {
render(
);
}
describe('Dashboards > ReleasesSelectControl', function () {
it('updates menu title with selection', function () {
renderReleasesSelect({});
expect(screen.getByText('All Releases')).toBeInTheDocument();
userEvent.click(screen.getByText('All Releases'));
expect(screen.getByText('Latest Release(s)')).toBeInTheDocument();
userEvent.click(screen.getByText('sentry-android-shop@1.2.0'));
userEvent.click(document.body);
expect(screen.getByText('sentry-android-shop@1.2.0')).toBeInTheDocument();
expect(screen.queryByText('+1')).not.toBeInTheDocument();
});
it('updates menu title with multiple selections', function () {
renderReleasesSelect({});
expect(screen.getByText('All Releases')).toBeInTheDocument();
userEvent.click(screen.getByText('All Releases'));
userEvent.click(screen.getByText('sentry-android-shop@1.2.0'));
userEvent.click(screen.getByText('sentry-android-shop@1.4.0'));
userEvent.click(document.body);
expect(screen.getByText('sentry-android-shop@1.2.0')).toBeInTheDocument();
expect(screen.getByText('+1')).toBeInTheDocument();
});
it('calls onSearch when filtering by releases', async function () {
const mockOnSearch = jest.fn();
renderReleasesSelect({onSearch: mockOnSearch});
expect(screen.getByText('All Releases')).toBeInTheDocument();
userEvent.click(screen.getByText('All Releases'));
userEvent.type(screen.getByText('Search\u2026'), 'se');
await waitFor(() => expect(mockOnSearch).toHaveBeenCalledWith('se'));
});
it('resets search on close', async function () {
const mockOnSearch = jest.fn();
renderReleasesSelect({onSearch: mockOnSearch});
expect(screen.getByText('All Releases')).toBeInTheDocument();
userEvent.click(screen.getByText('All Releases'));
userEvent.type(screen.getByText('Search\u2026'), 'se');
await waitFor(() => expect(mockOnSearch).toHaveBeenCalledWith('se'));
userEvent.click(document.body);
await waitFor(() => expect(mockOnSearch).toHaveBeenCalledWith(''));
});
it('triggers handleChangeFilter with the release versions', function () {
const mockHandleChangeFilter = jest.fn();
renderReleasesSelect({handleChangeFilter: mockHandleChangeFilter});
expect(screen.getByText('All Releases')).toBeInTheDocument();
userEvent.click(screen.getByText('All Releases'));
userEvent.click(screen.getByText('Latest Release(s)'));
userEvent.click(screen.getByText('sentry-android-shop@1.2.0'));
userEvent.click(screen.getByText('sentry-android-shop@1.4.0'));
userEvent.click(document.body);
expect(mockHandleChangeFilter).toHaveBeenCalledWith({
release: ['latest', 'sentry-android-shop@1.2.0', 'sentry-android-shop@1.4.0'],
});
});
it('includes Latest Release(s) even if no matching releases', function () {
render(
);
expect(screen.getByText('All Releases')).toBeInTheDocument();
userEvent.click(screen.getByText('All Releases'));
userEvent.type(screen.getByText('Search\u2026'), 'latest');
screen.getByText('Latest Release(s)');
});
});