|
| 1 | +import { act, fireEvent, screen } from '@testing-library/react'; |
| 2 | +import * as React from 'react'; |
| 3 | + |
| 4 | +import { axe } from 'jest-axe'; |
| 5 | + |
| 6 | +import { renderProvider } from '@/tests/renderProvider/renderProvider.utility'; |
| 7 | +import { windowMatchMedia } from '@/tests/windowMatchMedia'; |
| 8 | + |
| 9 | +import { BadgeUnControlled as Badge } from '../badgeUnControlled'; |
| 10 | +import { IBadgeUnControlled } from '../types'; |
| 11 | + |
| 12 | +window.matchMedia = windowMatchMedia(); |
| 13 | + |
| 14 | +// Mocks |
| 15 | +const mockProps: IBadgeUnControlled = { |
| 16 | + dataTestId: 'badge-component', |
| 17 | + variant: 'PRIMARY', |
| 18 | + size: 'DEFAULT', |
| 19 | + dot: { |
| 20 | + variant: 'WITH_BORDER', |
| 21 | + size: 'MEDIUM', |
| 22 | + number: 23, |
| 23 | + maxNumber: 99, |
| 24 | + }, |
| 25 | + popover: { |
| 26 | + variant: 'BADGE', |
| 27 | + content: ( |
| 28 | + <div> |
| 29 | + <h1>Hello</h1> |
| 30 | + </div> |
| 31 | + ), |
| 32 | + }, |
| 33 | + icon: { icon: 'CONTACTS' }, |
| 34 | + label: { content: 'Notifications' }, |
| 35 | + labelIcon: { icon: 'CHEVRON_DOWN' }, |
| 36 | + ariaLiveText: 'New notification', |
| 37 | + ['aria-label']: 'Open menu', |
| 38 | +}; |
| 39 | + |
| 40 | +describe('Badge component', () => { |
| 41 | + it('Should be displayed correctly', async () => { |
| 42 | + const ref = jest.fn(); |
| 43 | + const { container } = renderProvider(<Badge ref={ref} {...mockProps} />); |
| 44 | + |
| 45 | + const badge = screen.getByTestId(mockProps.dataTestId + 'Dot'); |
| 46 | + expect(badge).toBeInTheDocument(); |
| 47 | + |
| 48 | + const results = await axe(container); |
| 49 | + expect(container).toHTMLValidate(); |
| 50 | + expect(results).toHaveNoViolations(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('When it has a dot, the dot position is custompizable using customDotTranslate', async () => { |
| 54 | + const ref = jest.fn(); |
| 55 | + const { container } = renderProvider( |
| 56 | + <Badge ref={ref} {...mockProps} customDotTranslate="translate(2px, 2px)" /> |
| 57 | + ); |
| 58 | + |
| 59 | + const badge = screen.getByTestId(mockProps.dataTestId + 'Dot'); |
| 60 | + expect(badge).toBeInTheDocument(); |
| 61 | + |
| 62 | + const results = await axe(container); |
| 63 | + expect(container).toHTMLValidate(); |
| 64 | + expect(results).toHaveNoViolations(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('Should be displayed correctly without label', async () => { |
| 68 | + const { container } = renderProvider(<Badge {...mockProps} label={undefined} />); |
| 69 | + |
| 70 | + const badge = screen.getByTestId(mockProps.dataTestId + 'Dot'); |
| 71 | + expect(badge).toBeInTheDocument(); |
| 72 | + |
| 73 | + const results = await axe(container); |
| 74 | + expect(container).toHTMLValidate(); |
| 75 | + expect(results).toHaveNoViolations(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('Should be displayed correctly with simulate onClick', async () => { |
| 79 | + const { container } = renderProvider(<Badge {...mockProps} label={undefined} />); |
| 80 | + |
| 81 | + const triggerButton = screen.getByLabelText('Open menu'); |
| 82 | + fireEvent.click(triggerButton); |
| 83 | + |
| 84 | + const badge = screen.getByTestId(mockProps.dataTestId + 'Dot'); |
| 85 | + expect(badge).toBeInTheDocument(); |
| 86 | + |
| 87 | + const results = await axe(container); |
| 88 | + expect(container).toHTMLValidate(); |
| 89 | + expect(results).toHaveNoViolations(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('Should display contentExpand when open', async () => { |
| 93 | + const { container, getByLabelText } = renderProvider(<Badge {...mockProps} />); |
| 94 | + |
| 95 | + const triggerButton = getByLabelText('Open menu'); |
| 96 | + fireEvent.click(triggerButton); |
| 97 | + |
| 98 | + const contentExpand = screen.getByText('Hello'); |
| 99 | + expect(contentExpand).toBeInTheDocument(); |
| 100 | + |
| 101 | + const results = await axe(container); |
| 102 | + expect(container).toHTMLValidate(); |
| 103 | + expect(results).toHaveNoViolations(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('Should call onClick if defined when open', async () => { |
| 107 | + const onClick = jest.fn(); |
| 108 | + const { container, getByLabelText } = renderProvider( |
| 109 | + <Badge {...mockProps} onClick={onClick} /> |
| 110 | + ); |
| 111 | + |
| 112 | + const triggerButton = getByLabelText('Open menu'); |
| 113 | + fireEvent.click(triggerButton); |
| 114 | + |
| 115 | + expect(onClick).toHaveBeenCalled(); |
| 116 | + |
| 117 | + const results = await axe(container); |
| 118 | + expect(container).toHTMLValidate(); |
| 119 | + expect(results).toHaveNoViolations(); |
| 120 | + }); |
| 121 | + |
| 122 | + it('Popover can be closed automatically', async () => { |
| 123 | + const { getByLabelText } = renderProvider(<Badge {...mockProps} />); |
| 124 | + |
| 125 | + const triggerButton = getByLabelText('Open menu'); |
| 126 | + fireEvent.click(triggerButton); |
| 127 | + |
| 128 | + const contentExpand = screen.getByText('Hello'); |
| 129 | + expect(contentExpand).toBeInTheDocument(); |
| 130 | + |
| 131 | + await act(async () => { |
| 132 | + fireEvent.keyDown(window, { |
| 133 | + key: 'Escape', |
| 134 | + code: 'Escape', |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + expect(contentExpand).not.toBeInTheDocument(); |
| 139 | + }); |
| 140 | + |
| 141 | + it('When blur badge, it should close', () => { |
| 142 | + const { getByLabelText } = renderProvider( |
| 143 | + <> |
| 144 | + <div data-testid="button-test">test</div> |
| 145 | + <Badge {...mockProps} /> |
| 146 | + </> |
| 147 | + ); |
| 148 | + |
| 149 | + const triggerButton = getByLabelText('Open menu'); |
| 150 | + fireEvent.click(triggerButton); |
| 151 | + |
| 152 | + const contentExpand = screen.getByText('Hello'); |
| 153 | + expect(contentExpand).toBeInTheDocument(); |
| 154 | + |
| 155 | + const badgeContainer = screen.getByTestId(mockProps.dataTestId + 'BadgeContainer'); |
| 156 | + fireEvent.blur(badgeContainer); |
| 157 | + |
| 158 | + expect(contentExpand).not.toBeInTheDocument(); |
| 159 | + }); |
| 160 | +}); |
0 commit comments