|
1 | 1 | import { render, screen } from '@testing-library/react'; |
2 | 2 | import '@testing-library/jest-dom'; |
3 | | -import ResponseActions from './ResponseActions'; |
| 3 | +import ResponseActions, { ActionProps } from './ResponseActions'; |
4 | 4 | import userEvent from '@testing-library/user-event'; |
5 | 5 | import { DownloadIcon, InfoCircleIcon, RedoIcon } from '@patternfly/react-icons'; |
6 | 6 | import Message from '../Message'; |
@@ -129,6 +129,103 @@ describe('ResponseActions', () => { |
129 | 129 | expect(goodBtn).not.toHaveClass('pf-chatbot__button--response-action-clicked'); |
130 | 130 | expect(badBtn).not.toHaveClass('pf-chatbot__button--response-action-clicked'); |
131 | 131 | }); |
| 132 | + |
| 133 | + it('should handle isClicked prop within group of buttons correctly', async () => { |
| 134 | + render( |
| 135 | + <ResponseActions |
| 136 | + actions={ |
| 137 | + { |
| 138 | + positive: { 'data-testid': 'positive-btn', onClick: jest.fn(), isClicked: true }, |
| 139 | + negative: { 'data-testid': 'negative-btn', onClick: jest.fn() } |
| 140 | + } as Record<string, ActionProps> |
| 141 | + } |
| 142 | + /> |
| 143 | + ); |
| 144 | + |
| 145 | + expect(screen.getByTestId('positive-btn')).toHaveClass('pf-chatbot__button--response-action-clicked'); |
| 146 | + expect(screen.getByTestId('negative-btn')).not.toHaveClass('pf-chatbot__button--response-action-clicked'); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should set "listen" button as active if its `isClicked` is true', async () => { |
| 150 | + render( |
| 151 | + <ResponseActions |
| 152 | + actions={ |
| 153 | + { |
| 154 | + positive: { 'data-testid': 'positive-btn', onClick: jest.fn(), isClicked: false }, |
| 155 | + negative: { 'data-testid': 'negative-btn', onClick: jest.fn(), isClicked: false }, |
| 156 | + listen: { 'data-testid': 'listen-btn', onClick: jest.fn(), isClicked: true } |
| 157 | + } as Record<string, ActionProps> |
| 158 | + } |
| 159 | + /> |
| 160 | + ); |
| 161 | + expect(screen.getByTestId('listen-btn')).toHaveClass('pf-chatbot__button--response-action-clicked'); |
| 162 | + |
| 163 | + expect(screen.getByTestId('positive-btn')).not.toHaveClass('pf-chatbot__button--response-action-clicked'); |
| 164 | + expect(screen.getByTestId('negative-btn')).not.toHaveClass('pf-chatbot__button--response-action-clicked'); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should prioritize "positive" when both "positive" and "negative" are set to clicked', async () => { |
| 168 | + render( |
| 169 | + <ResponseActions |
| 170 | + actions={ |
| 171 | + { |
| 172 | + positive: { 'data-testid': 'positive-btn', onClick: jest.fn(), isClicked: true }, |
| 173 | + negative: { 'data-testid': 'negative-btn', onClick: jest.fn(), isClicked: true } |
| 174 | + } as Record<string, ActionProps> |
| 175 | + } |
| 176 | + /> |
| 177 | + ); |
| 178 | + // Positive button should take precendence |
| 179 | + expect(screen.getByTestId('positive-btn')).toHaveClass('pf-chatbot__button--response-action-clicked'); |
| 180 | + expect(screen.getByTestId('negative-btn')).not.toHaveClass('pf-chatbot__button--response-action-clicked'); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should set an additional action button as active if it is initially clicked and no predefined are clicked', async () => { |
| 184 | + const [additionalActions] = CUSTOM_ACTIONS; |
| 185 | + const customActions = { |
| 186 | + positive: { 'data-testid': 'positive', onClick: jest.fn(), isClicked: false }, |
| 187 | + negative: { 'data-testid': 'negative', onClick: jest.fn(), isClicked: false }, |
| 188 | + ...Object.keys(additionalActions).reduce((acc, actionKey) => { |
| 189 | + acc[actionKey] = { |
| 190 | + ...additionalActions[actionKey], |
| 191 | + 'data-testid': actionKey, |
| 192 | + isClicked: actionKey === 'regenerate' |
| 193 | + }; |
| 194 | + return acc; |
| 195 | + }, {}) |
| 196 | + }; |
| 197 | + render(<ResponseActions actions={customActions} />); |
| 198 | + |
| 199 | + Object.keys(customActions).forEach((actionKey) => { |
| 200 | + if (actionKey === 'regenerate') { |
| 201 | + expect(screen.getByTestId(actionKey)).toHaveClass('pf-chatbot__button--response-action-clicked'); |
| 202 | + } else { |
| 203 | + // Other actions should not have clicked class |
| 204 | + expect(screen.getByTestId(actionKey)).not.toHaveClass('pf-chatbot__button--response-action-clicked'); |
| 205 | + } |
| 206 | + }); |
| 207 | + }); |
| 208 | + |
| 209 | + it('should activate the clicked button and deactivate any previously active button', async () => { |
| 210 | + const actions = { |
| 211 | + positive: { 'data-testid': 'positive', onClick: jest.fn(), isClicked: false }, |
| 212 | + negative: { 'data-testid': 'negative', onClick: jest.fn(), isClicked: true } |
| 213 | + }; |
| 214 | + render(<ResponseActions actions={actions} />); |
| 215 | + |
| 216 | + const negativeBtn = screen.getByTestId('negative'); |
| 217 | + const positiveBtn = screen.getByTestId('positive'); |
| 218 | + // negative button is initially clicked |
| 219 | + expect(negativeBtn).toHaveClass('pf-chatbot__button--response-action-clicked'); |
| 220 | + expect(positiveBtn).not.toHaveClass('pf-chatbot__button--response-action-clicked'); |
| 221 | + |
| 222 | + await userEvent.click(positiveBtn); |
| 223 | + |
| 224 | + // positive button should now have the clicked class |
| 225 | + expect(positiveBtn).toHaveClass('pf-chatbot__button--response-action-clicked'); |
| 226 | + expect(negativeBtn).not.toHaveClass('pf-chatbot__button--response-action-clicked'); |
| 227 | + }); |
| 228 | + |
132 | 229 | it('should render buttons correctly', () => { |
133 | 230 | ALL_ACTIONS.forEach(({ type, label }) => { |
134 | 231 | render(<ResponseActions actions={{ [type]: { onClick: jest.fn() } }} />); |
|
0 commit comments