|
| 1 | +import userEvent from '@testing-library/user-event'; |
| 2 | +import { fireEvent } from '@testing-library/dom'; |
| 3 | +import { testViewSync } from '../../../testHelpers/testView'; |
| 4 | + |
| 5 | +import { Events } from '../../events'; |
| 6 | +import { AppEvent } from '../../AppEvents'; |
| 7 | + |
| 8 | +import { CategoryTab } from '../CategoryTab'; |
| 9 | +import { Category } from '../../types'; |
| 10 | + |
| 11 | +describe('CategoryTab', () => { |
| 12 | + const category: Category = { |
| 13 | + key: 'smileys-emotion', |
| 14 | + message: 'Smileys & Emotion', |
| 15 | + order: 0 |
| 16 | + }; |
| 17 | + |
| 18 | + const events = new Events<AppEvent>(); |
| 19 | + const emitSpy = jest.spyOn(events, 'emit'); |
| 20 | + |
| 21 | + const icon = 'fa-face-smile'; |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + emitSpy.mockReset(); |
| 25 | + }); |
| 26 | + |
| 27 | + function renderTab() { |
| 28 | + return testViewSync(CategoryTab, [{ category, icon }], { events }); |
| 29 | + } |
| 30 | + |
| 31 | + test('renders a category tab', () => { |
| 32 | + const tab = renderTab(); |
| 33 | + |
| 34 | + const button = tab.ui.button; |
| 35 | + expect(button).toHaveAttribute('data-category', 'smileys-emotion'); |
| 36 | + expect(button).toHaveAttribute('title', 'Smileys & Emotion'); |
| 37 | + expect(button).toHaveAttribute('tabindex', '-1'); |
| 38 | + }); |
| 39 | + |
| 40 | + test('toggles the active status', () => { |
| 41 | + const tab = renderTab(); |
| 42 | + const button = tab.ui.button; |
| 43 | + expect(button.ariaSelected).toBe('false'); |
| 44 | + |
| 45 | + // By default it should make the tab focusable and set aria-selected to true |
| 46 | + tab.setActive(true); |
| 47 | + expect(button.ariaSelected).toBe('true'); |
| 48 | + expect(button.tabIndex).toBe(0); |
| 49 | + |
| 50 | + // Deactivation should reset tabindex and aria-selected |
| 51 | + tab.setActive(false); |
| 52 | + expect(button.ariaSelected).toBe('false'); |
| 53 | + expect(button.tabIndex).toBe(-1); |
| 54 | + |
| 55 | + // Setting active without focus (like when scrolling) should not change tabindex or aria-selected |
| 56 | + tab.setActive(true, false); |
| 57 | + expect(button.ariaSelected).toBe('false'); |
| 58 | + }); |
| 59 | + |
| 60 | + test('emits a category:select event when clicked', () => { |
| 61 | + const tab = renderTab(); |
| 62 | + const button = tab.ui.button; |
| 63 | + |
| 64 | + userEvent.click(button); |
| 65 | + expect(events.emit).toHaveBeenCalledWith('category:select', 'smileys-emotion', expect.anything()); |
| 66 | + }); |
| 67 | + |
| 68 | + test('emits a category:select event when focused', () => { |
| 69 | + const tab = renderTab(); |
| 70 | + const button = tab.ui.button; |
| 71 | + |
| 72 | + fireEvent.focus(button); |
| 73 | + expect(events.emit).toHaveBeenCalledWith('category:select', 'smileys-emotion', expect.anything()); |
| 74 | + }); |
| 75 | +}); |
0 commit comments