|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | + |
| 5 | +import Selector from './index'; |
| 6 | + |
| 7 | +const options = [ |
| 8 | + { id: 1, title: 'Opción 1', disabled: false }, |
| 9 | + { id: 2, title: 'Opción 2', disabled: true }, |
| 10 | + { id: 3, title: 'Opción 3', disabled: false } |
| 11 | +]; |
| 12 | + |
| 13 | +describe('When tab is internally controlled', () => { |
| 14 | + const handleChange = jest.fn(); |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + render(<Selector options={options} onChange={handleChange} />); |
| 18 | + }); |
| 19 | + |
| 20 | + test('it selects a tab when user clicks it and the rest is not selected', () => { |
| 21 | + const tabToSelect = screen.getByText('Opción 1'); |
| 22 | + userEvent.click(tabToSelect); |
| 23 | + expect(handleChange).toHaveBeenCalledTimes(1); |
| 24 | + expect(handleChange).toHaveBeenCalledWith(1); |
| 25 | + expect(tabToSelect).toHaveClass('active'); |
| 26 | + }); |
| 27 | + |
| 28 | + test('can not select a disabled option', () => { |
| 29 | + userEvent.click(screen.getByText('Opción 2')); |
| 30 | + expect(handleChange).not.toHaveBeenCalled(); |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +describe('When tab is externally controlled', () => { |
| 35 | + const handleChange = jest.fn(); |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + render(<Selector options={options} onChange={handleChange} active={1} />); |
| 39 | + }); |
| 40 | + |
| 41 | + test('clicking does not select an element', () => { |
| 42 | + const currentSelectedTab = screen.getByText('Opción 1'); |
| 43 | + const tabToSelect = screen.getByText('Opción 3'); |
| 44 | + userEvent.click(tabToSelect); |
| 45 | + expect(currentSelectedTab).toHaveClass('active'); |
| 46 | + expect(tabToSelect).not.toHaveClass('active'); |
| 47 | + }); |
| 48 | + |
| 49 | + test('can not select a disabled option', () => { |
| 50 | + userEvent.click(screen.getByText('Opción 2')); |
| 51 | + expect(handleChange).not.toHaveBeenCalled(); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +describe('When the tab has initial value and it is internally controlled', () => { |
| 56 | + test('starts with the initial value selected', () => { |
| 57 | + render(<Selector options={options} onChange={jest.fn()} initialValue={3} />); |
| 58 | + const tab = screen.getByText('Opción 3'); |
| 59 | + expect(tab).toHaveClass('active'); |
| 60 | + }); |
| 61 | +}); |
0 commit comments