|
1 | 1 | import { fireEvent, render } from '@testing-library/preact'
|
2 |
| -import { MultipleChoiceQuestion } from '../../../extensions/surveys/components/QuestionTypes' |
3 |
| -import { MultipleSurveyQuestion, SurveyQuestionType } from '../../../posthog-surveys-types' |
| 2 | +import { MultipleChoiceQuestion, OpenTextQuestion } from '../../../extensions/surveys/components/QuestionTypes' |
| 3 | +import { BasicSurveyQuestion, MultipleSurveyQuestion, SurveyQuestionType } from '../../../posthog-surveys-types' |
4 | 4 |
|
5 | 5 | describe('MultipleChoiceQuestion', () => {
|
6 | 6 | const mockAppearance = {
|
@@ -141,5 +141,73 @@ describe('MultipleChoiceQuestion', () => {
|
141 | 141 | expect(document.activeElement).toBe(openInput)
|
142 | 142 | }, 0)
|
143 | 143 | })
|
| 144 | + |
| 145 | + it('does not propagate keydown events from open choice input', () => { |
| 146 | + const parentKeyDownHandler = jest.fn() |
| 147 | + const { container } = render( |
| 148 | + <div onKeyDown={parentKeyDownHandler}> |
| 149 | + <MultipleChoiceQuestion {...baseProps} question={multipleChoiceQuestion} /> |
| 150 | + </div> |
| 151 | + ) |
| 152 | + |
| 153 | + // Find the open-ended input using its specific ID |
| 154 | + const openInput = container.querySelector('#surveyQuestion1Choice3Open') as HTMLInputElement |
| 155 | + if (!openInput) { |
| 156 | + throw new Error('Open choice input not found') |
| 157 | + } |
| 158 | + |
| 159 | + // Simulate typing 'C' into the open-ended input |
| 160 | + fireEvent.keyDown(openInput, { key: 'C', code: 'KeyC' }) |
| 161 | + |
| 162 | + // Assert that the parent's keydown handler was NOT called |
| 163 | + expect(parentKeyDownHandler).not.toHaveBeenCalled() |
| 164 | + }) |
144 | 165 | })
|
145 | 166 | })
|
| 167 | + |
| 168 | +describe('OpenTextQuestion', () => { |
| 169 | + const mockAppearance = { |
| 170 | + backgroundColor: '#fff', |
| 171 | + submitButtonText: 'Submit', |
| 172 | + placeholder: 'Enter your response', |
| 173 | + } |
| 174 | + |
| 175 | + const baseProps = { |
| 176 | + forceDisableHtml: false, |
| 177 | + appearance: mockAppearance, |
| 178 | + onSubmit: jest.fn(), |
| 179 | + onPreviewSubmit: jest.fn(), |
| 180 | + } |
| 181 | + |
| 182 | + const openTextQuestion: BasicSurveyQuestion = { |
| 183 | + type: SurveyQuestionType.Open, |
| 184 | + question: 'What is your feedback?', |
| 185 | + description: 'Provide details below', |
| 186 | + optional: false, |
| 187 | + } |
| 188 | + |
| 189 | + it('does not propagate keydown events', () => { |
| 190 | + const parentKeyDownHandler = jest.fn() |
| 191 | + |
| 192 | + // Render the component within a div that has a keydown listener |
| 193 | + const { container } = render( |
| 194 | + <div onKeyDown={parentKeyDownHandler}> |
| 195 | + <OpenTextQuestion {...baseProps} question={openTextQuestion} /> |
| 196 | + </div> |
| 197 | + ) |
| 198 | + |
| 199 | + const textarea = container.querySelector('textarea') |
| 200 | + |
| 201 | + if (!textarea) { |
| 202 | + throw new Error('Textarea not found') |
| 203 | + } |
| 204 | + |
| 205 | + // Simulate typing 'C' into the textarea |
| 206 | + fireEvent.keyDown(textarea, { key: 'C', code: 'KeyC' }) |
| 207 | + |
| 208 | + // Assert that the parent's keydown handler was NOT called |
| 209 | + expect(parentKeyDownHandler).not.toHaveBeenCalled() |
| 210 | + }) |
| 211 | + |
| 212 | + // Add other tests for OpenTextQuestion if needed... |
| 213 | +}) |
0 commit comments