|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +import { render, screen, fireEvent } from 'spec/helpers/testing-library'; |
| 20 | +import { IconButton } from 'src/components/IconButton'; |
| 21 | + |
| 22 | +const defaultProps = { |
| 23 | + buttonText: 'This is the IconButton text', |
| 24 | + icon: '/images/icons/sql.svg', |
| 25 | +}; |
| 26 | + |
| 27 | +describe('IconButton', () => { |
| 28 | + it('renders an IconButton with icon and text', () => { |
| 29 | + render(<IconButton {...defaultProps} />); |
| 30 | + |
| 31 | + const icon = screen.getByRole('img'); |
| 32 | + const buttonText = screen.getByText(/this is the iconbutton text/i); |
| 33 | + |
| 34 | + expect(icon).toBeVisible(); |
| 35 | + expect(buttonText).toBeVisible(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('is keyboard accessible and has correct aria attributes', () => { |
| 39 | + render(<IconButton {...defaultProps} />); |
| 40 | + |
| 41 | + const button = screen.getByRole('button'); |
| 42 | + |
| 43 | + expect(button).toHaveAttribute('tabIndex', '0'); |
| 44 | + expect(button).toHaveAttribute('aria-label', defaultProps.buttonText); |
| 45 | + }); |
| 46 | + |
| 47 | + it('handles Enter and Space key presses', () => { |
| 48 | + const mockOnClick = jest.fn(); |
| 49 | + render(<IconButton {...defaultProps} onClick={mockOnClick} />); |
| 50 | + |
| 51 | + const button = screen.getByRole('button'); |
| 52 | + |
| 53 | + fireEvent.keyDown(button, { key: 'Enter', code: 'Enter' }); |
| 54 | + expect(mockOnClick).toHaveBeenCalledTimes(1); |
| 55 | + |
| 56 | + fireEvent.keyDown(button, { key: ' ', code: 'Space' }); |
| 57 | + expect(mockOnClick).toHaveBeenCalledTimes(2); |
| 58 | + }); |
| 59 | + |
| 60 | + it('uses custom alt text when provided', () => { |
| 61 | + const customAltText = 'Custom Alt Text'; |
| 62 | + render( |
| 63 | + <IconButton |
| 64 | + buttonText="Custom Alt Text Button" |
| 65 | + icon="/images/icons/sql.svg" |
| 66 | + altText={customAltText} |
| 67 | + />, |
| 68 | + ); |
| 69 | + |
| 70 | + const icon = screen.getByAltText(customAltText); |
| 71 | + expect(icon).toBeVisible(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('displays tooltip with button text', () => { |
| 75 | + render(<IconButton {...defaultProps} />); |
| 76 | + |
| 77 | + const tooltipTrigger = screen.getByText(/this is the iconbutton text/i); |
| 78 | + expect(tooltipTrigger).toBeVisible(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('calls onClick handler when clicked', () => { |
| 82 | + const mockOnClick = jest.fn(); |
| 83 | + render(<IconButton {...defaultProps} onClick={mockOnClick} />); |
| 84 | + |
| 85 | + const button = screen.getByRole('button'); |
| 86 | + fireEvent.click(button); |
| 87 | + |
| 88 | + expect(mockOnClick).toHaveBeenCalledTimes(1); |
| 89 | + }); |
| 90 | +}); |
0 commit comments