|
| 1 | +import '@testing-library/jest-dom' |
| 2 | +import { fireEvent, render } from '@testing-library/preact' |
| 3 | +import { SurveyPopup } from '../../../extensions/surveys' |
| 4 | +import { Survey, SurveyQuestionType, SurveyType } from '../../../posthog-surveys-types' |
| 5 | + |
| 6 | +describe('SurveyPopup', () => { |
| 7 | + // Create a basic mock survey for testing |
| 8 | + const mockSurvey: Survey = { |
| 9 | + id: 'test-survey', |
| 10 | + name: 'Test Survey', |
| 11 | + description: 'A test survey', |
| 12 | + type: SurveyType.Popover, |
| 13 | + feature_flag_keys: null, |
| 14 | + linked_flag_key: null, |
| 15 | + targeting_flag_key: null, |
| 16 | + internal_targeting_flag_key: null, |
| 17 | + questions: [ |
| 18 | + { |
| 19 | + type: SurveyQuestionType.Open, |
| 20 | + question: 'Test question', |
| 21 | + description: 'Test description', |
| 22 | + id: 'q1', |
| 23 | + }, |
| 24 | + ], |
| 25 | + appearance: { |
| 26 | + displayThankYouMessage: true, |
| 27 | + thankYouMessageHeader: 'Thank you for your feedback!', |
| 28 | + thankYouMessageDescription: 'We appreciate your input.', |
| 29 | + backgroundColor: '#ffffff', |
| 30 | + borderColor: '#e5e5e5', |
| 31 | + thankYouMessageCloseButtonText: 'Close', |
| 32 | + whiteLabel: true, |
| 33 | + }, |
| 34 | + conditions: null, |
| 35 | + start_date: null, |
| 36 | + end_date: null, |
| 37 | + current_iteration: null, |
| 38 | + current_iteration_start_date: null, |
| 39 | + schedule: null, |
| 40 | + } |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + // Reset DOM |
| 44 | + document.getElementsByTagName('html')[0].innerHTML = '' |
| 45 | + localStorage.clear() |
| 46 | + jest.clearAllMocks() |
| 47 | + }) |
| 48 | + |
| 49 | + test('calls onCloseConfirmationMessage when X button is clicked', () => { |
| 50 | + // Create a mock function to test if it gets called |
| 51 | + const mockOnCloseConfirmationMessage = jest.fn() |
| 52 | + const mockRemoveSurveyFromFocus = jest.fn() |
| 53 | + |
| 54 | + // Create a custom wrapper to set isSurveySent to true |
| 55 | + // This simulates the state after survey submission when confirmation message is shown |
| 56 | + const SurveyWrapper = () => { |
| 57 | + return ( |
| 58 | + <SurveyPopup |
| 59 | + survey={mockSurvey} |
| 60 | + removeSurveyFromFocus={mockRemoveSurveyFromFocus} |
| 61 | + isPopup={true} |
| 62 | + onCloseConfirmationMessage={mockOnCloseConfirmationMessage} |
| 63 | + // Force the confirmation message to show by providing props that match showConfirmation condition |
| 64 | + // In the component: const shouldShowConfirmation = isSurveySent || previewPageIndex === survey.questions.length |
| 65 | + previewPageIndex={mockSurvey.questions.length} |
| 66 | + /> |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + // Render the component |
| 71 | + const { container } = render(<SurveyWrapper />) |
| 72 | + |
| 73 | + // Find the X/Cancel button directly in the container |
| 74 | + const cancelButton = container.querySelector('button.form-cancel[aria-label="Close survey"]') |
| 75 | + |
| 76 | + // Click the cancel button |
| 77 | + if (cancelButton) { |
| 78 | + fireEvent.click(cancelButton) |
| 79 | + // Verify that onCloseConfirmationMessage was called |
| 80 | + expect(mockOnCloseConfirmationMessage).toHaveBeenCalledTimes(1) |
| 81 | + } else { |
| 82 | + expect(cancelButton).not.toBeNull() // Use expect instead of fail |
| 83 | + } |
| 84 | + }) |
| 85 | + |
| 86 | + test('calls onCloseConfirmationMessage when Close button is clicked', () => { |
| 87 | + // Create a mock function to test if it gets called |
| 88 | + const mockOnCloseConfirmationMessage = jest.fn() |
| 89 | + const mockRemoveSurveyFromFocus = jest.fn() |
| 90 | + |
| 91 | + // Create a custom wrapper with confirmation message showing |
| 92 | + const SurveyWrapper = () => { |
| 93 | + return ( |
| 94 | + <SurveyPopup |
| 95 | + survey={mockSurvey} |
| 96 | + removeSurveyFromFocus={mockRemoveSurveyFromFocus} |
| 97 | + isPopup={true} |
| 98 | + onCloseConfirmationMessage={mockOnCloseConfirmationMessage} |
| 99 | + previewPageIndex={mockSurvey.questions.length} |
| 100 | + /> |
| 101 | + ) |
| 102 | + } |
| 103 | + |
| 104 | + // Render the component |
| 105 | + const { container } = render(<SurveyWrapper />) |
| 106 | + |
| 107 | + // Find the Close button directly in the container (rather than in Shadow DOM) |
| 108 | + const closeButton = container.querySelector('button.form-submit') |
| 109 | + |
| 110 | + // Click the Close button |
| 111 | + if (closeButton) { |
| 112 | + fireEvent.click(closeButton) |
| 113 | + // Verify that onCloseConfirmationMessage was called |
| 114 | + expect(mockOnCloseConfirmationMessage).toHaveBeenCalledTimes(1) |
| 115 | + } else { |
| 116 | + expect(closeButton).not.toBeNull() // Use expect instead of fail |
| 117 | + } |
| 118 | + }) |
| 119 | +}) |
0 commit comments