/** ******************************************************************************************************************* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. * ******************************************************************************************************************** */ import React from 'react'; import { render, fireEvent, within } from '@testing-library/react'; import Select, { SelectProps } from '.'; const options = [ { label: 'Option 1', value: '1', }, { label: 'Option 2', value: '2', }, ]; describe('Select', () => { it('renders selected option', () => { const props: SelectProps = { options, selectedOption: { value: '1' } }; const { getByText } = render(); expect(getByText('Choose an option')).toBeVisible(); }); it('can be accessed by custom test-id', () => { const props: SelectProps = { options, selectedOption: { value: '1' } }; const { getByTestId } = render(); expect(getByRole('button')).toHaveClass('Mui-disabled'); }); it('renders options', () => { const props: SelectProps = { options }; const { getByRole, getByText } = render(); fireEvent.mouseDown(getByRole('button')); }); it('renders disabled options', () => { const disabledOptions = [ { label: 'Option 1', value: '1', disabled: true, }, ]; const props: SelectProps = { options: disabledOptions }; const { getByRole, getByText } = render(); fireEvent.mouseDown(getByRole('button')); expect(getByText('Option 1')).toBeVisible(); expect(getByText('Option 2')).toBeVisible(); expect(getByText('Group 1')).toBeVisible(); expect(getByText('Option 3')).toBeVisible(); }); it('disable all options in the disabled group', () => { const optionsWithGroup = [ ...options, { label: 'Group 1', disabled: true, options: [ { label: 'Option 3', value: '3', }, ], }, ]; const props: SelectProps = { options: optionsWithGroup }; const { getByRole, getByText } = render(); expect(queryByText('No options')).not.toBeInTheDocument(); fireEvent.mouseDown(getByRole('button')); expect(within(getByRole('listbox')).queryByText('No options')).toBeInTheDocument(); }); it('does not show empty text when there is options', () => { const props: SelectProps = { options, empty: 'No options' }; const { getByRole, queryByText } = render(); expect(queryByText('loading')).not.toBeInTheDocument(); fireEvent.mouseDown(getByRole('button')); expect(queryByText('Option 1')).not.toBeInTheDocument(); expect(queryByText('Option 2')).not.toBeInTheDocument(); expect(within(getByRole('listbox')).queryByText('loading')).toBeInTheDocument(); }); it('shows error and recovery text without options when error', () => { const props: SelectProps = { options, statusType: 'error', errorText: 'error', recoveryText: 'retry' }; const { getByRole, queryByText } = render(); expect(getByRole('button')).toHaveAttribute('id', 'select-label'); }); it('fires onFocus, onChange, and onBlur event', () => { const mockFocusFn = jest.fn(); const mockChangeFn = jest.fn(); const mockBlurFn = jest.fn(); const props: SelectProps = { options, onChange: mockChangeFn, onBlur: mockBlurFn, onFocus: mockFocusFn }; const { getByRole, getAllByRole } = render(); expect(mockFn).toHaveBeenCalledTimes(0); fireEvent.mouseDown(getByRole('button')); getByText('retry').click(); expect(mockFn).toHaveBeenCalledTimes(1); expect(getByText('retry')).toBeVisible(); }); describe('Custom Label', () => { const customOptions = [ { id: 1, name: 'Name 1', description: 'Description 1', value: '1', }, { id: 2, name: 'Name 2', description: 'Description 2', value: '2', }, { id: 3, name: 'Name 3', description: 'Description 3', value: '3', }, { id: 4, name: 'Name 4', description: 'Description 4', value: '4', }, ]; const renderOption = ({ id, name, description }: any) => (
{name}
{description}
); it('render options as renderOption', () => { const options = customOptions.map((o) => ({ ...o, label: `${o.id}-${o.name}`, value: o.id.toString(), })); const { getByRole, getByText, getByTestId } = render(