import React from 'react';
import { render, screen } from '@testing-library/react';
import { SelectOption, SelectOptionObject } from '../SelectOption';
import { SelectProvider } from '../selectConstants';
class User implements SelectOptionObject {
private firstName: string;
private lastName: string;
private title: string;
constructor(title: string, firstName: string, lastName: string) {
this.title = title;
this.firstName = firstName;
this.lastName = lastName;
}
toString = (): string => `${this.title}: ${this.firstName} ${this.lastName}`;
}
describe('SelectOption', () => {
test('renders with value parameter successfully', () => {
const { asFragment } = render(
{},
onFavorite: () => {},
onClose: () => {},
variant: 'single',
inputIdPrefix: '',
shouldResetOnSelect: true
}}
>
);
expect(asFragment()).toMatchSnapshot();
});
test('renders with description successfully', () => {
const { asFragment } = render(
{},
onFavorite: () => {},
onClose: () => {},
variant: 'single',
inputIdPrefix: '',
shouldResetOnSelect: true
}}
>
);
expect(asFragment()).toMatchSnapshot();
});
test('renders with item count successfully', () => {
const { asFragment } = render(
{},
onFavorite: () => {},
onClose: () => {},
variant: 'single',
inputIdPrefix: '',
shouldResetOnSelect: true
}}
>
);
expect(asFragment()).toMatchSnapshot();
});
test('renders with custom display successfully', () => {
const { asFragment } = render(
{},
onFavorite: () => {},
onClose: () => {},
variant: 'single',
inputIdPrefix: '',
shouldResetOnSelect: true
}}
>
test display
);
expect(asFragment()).toMatchSnapshot();
});
test('renders with custom user object successfully', () => {
const { asFragment } = render(
{},
onFavorite: () => {},
onClose: () => {},
variant: 'single',
inputIdPrefix: '',
shouldResetOnSelect: true
}}
>
);
expect(asFragment()).toMatchSnapshot();
});
test('renders with custom display and custom user object successfully', () => {
const { asFragment } = render(
{},
onFavorite: () => {},
onClose: () => {},
variant: 'single',
inputIdPrefix: '',
shouldResetOnSelect: true
}}
>
test display
);
expect(asFragment()).toMatchSnapshot();
});
test('renders custom component', () => {
const { asFragment } = render(
{},
onFavorite: () => {},
onClose: () => {},
variant: 'single',
inputIdPrefix: '',
shouldResetOnSelect: true
}}
>
test display
);
expect(asFragment()).toMatchSnapshot();
});
describe('disabled', () => {
test('renders disabled successfully', () => {
const { asFragment } = render(
{},
onFavorite: () => {},
onClose: () => {},
variant: 'single',
inputIdPrefix: '',
shouldResetOnSelect: true
}}
>
);
expect(screen.getByText('test')).toHaveClass('pf-m-disabled');
expect(asFragment()).toMatchSnapshot();
});
});
describe('is selected', () => {
test('renders selected successfully', () => {
const { asFragment } = render(
{},
onFavorite: () => {},
onClose: () => {},
variant: 'single',
inputIdPrefix: '',
shouldResetOnSelect: true
}}
>
);
expect(asFragment()).toMatchSnapshot();
});
});
describe('checked', () => {
test('renders with checked successfully', () => {
const { asFragment } = render(
{},
onFavorite: () => {},
onClose: () => {},
variant: 'single',
inputIdPrefix: '',
shouldResetOnSelect: true
}}
>
);
expect(asFragment()).toMatchSnapshot();
});
});
describe('favorites warning', () => {
test('generates warning when id is undefined and isFavorites is set', () => {
const myMock = jest.fn() as any;
global.console = { error: myMock } as any;
render(
{},
onFavorite: () => {},
onClose: () => {},
variant: 'single',
inputIdPrefix: '',
shouldResetOnSelect: true
}}
>
);
expect(myMock).toHaveBeenCalled();
});
});
});