|
| 1 | +import '@testing-library/jest-dom' |
| 2 | +import { cleanup, render, screen, waitFor } from '@testing-library/react' |
| 3 | +import userEvent from '@testing-library/user-event' |
| 4 | + |
| 5 | +import { useMocks } from '~/mocks/jest' |
| 6 | +import { initKeaTests } from '~/test/init' |
| 7 | +import { AccessControlLevel, FeatureFlagEvaluationRuntime, type FeatureFlagType } from '~/types' |
| 8 | + |
| 9 | +import { QuickSurveyForm } from './QuickSurveyModal' |
| 10 | + |
| 11 | +jest.mock('scenes/surveys/SurveyAppearancePreview', () => ({ |
| 12 | + SurveyAppearancePreview: () => <div>Preview</div>, |
| 13 | +})) |
| 14 | +jest.mock('scenes/surveys/SurveySettings', () => ({ |
| 15 | + SurveyPopupToggle: () => <div>Survey Toggle</div>, |
| 16 | +})) |
| 17 | +jest.mock('scenes/surveys/AddEventButton', () => ({ |
| 18 | + AddEventButton: ({ onEventSelect }: any) => <button onClick={() => onEventSelect('test-event')}>Add event</button>, |
| 19 | +})) |
| 20 | + |
| 21 | +const mockFlag: FeatureFlagType = { |
| 22 | + id: 1, |
| 23 | + key: 'test-flag', |
| 24 | + name: 'Test Flag', |
| 25 | + filters: { groups: [], multivariate: null, payloads: {} }, |
| 26 | + deleted: false, |
| 27 | + active: true, |
| 28 | + created_at: '2023-01-01', |
| 29 | + created_by: null, |
| 30 | + is_simple_flag: false, |
| 31 | + rollout_percentage: null, |
| 32 | + ensure_experience_continuity: false, |
| 33 | + experiment_set: [], |
| 34 | + rollback_conditions: [], |
| 35 | + performed_rollback: false, |
| 36 | + can_edit: true, |
| 37 | + tags: [], |
| 38 | + features: [], |
| 39 | + usage_dashboard: undefined, |
| 40 | + analytics_dashboards: [], |
| 41 | + has_enriched_analytics: false, |
| 42 | + surveys: [], |
| 43 | + updated_at: '2023-01-01', |
| 44 | + version: 1, |
| 45 | + last_modified_by: null, |
| 46 | + evaluation_tags: [], |
| 47 | + is_remote_configuration: false, |
| 48 | + has_encrypted_payloads: false, |
| 49 | + status: 'ACTIVE', |
| 50 | + evaluation_runtime: FeatureFlagEvaluationRuntime.ALL, |
| 51 | + user_access_level: AccessControlLevel.Admin, |
| 52 | +} |
| 53 | + |
| 54 | +describe('QuickSurveyForm', () => { |
| 55 | + beforeEach(() => { |
| 56 | + initKeaTests() |
| 57 | + useMocks({ |
| 58 | + get: { |
| 59 | + '/api/projects/:team_id/property_definitions': { results: [], count: 0 }, |
| 60 | + '/api/projects/:team_id/surveys': { results: [] }, |
| 61 | + }, |
| 62 | + post: { |
| 63 | + '/api/projects/:team_id/surveys': () => [200, { id: 'new-survey' }], |
| 64 | + }, |
| 65 | + patch: { |
| 66 | + '/api/environments/@current/add_product_intent/': () => [200, {}], |
| 67 | + }, |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + afterEach(() => { |
| 72 | + cleanup() |
| 73 | + }) |
| 74 | + |
| 75 | + it('disables create button when question is empty', async () => { |
| 76 | + render(<QuickSurveyForm flag={mockFlag} />) |
| 77 | + |
| 78 | + const questionInput = screen.getByPlaceholderText('What do you think?') |
| 79 | + userEvent.clear(questionInput) |
| 80 | + |
| 81 | + expect(screen.getByRole('button', { name: /create & launch/i })).toHaveAttribute('aria-disabled', 'true') |
| 82 | + }) |
| 83 | + |
| 84 | + it('creates survey with selected events in conditions', async () => { |
| 85 | + let capturedRequest: any |
| 86 | + useMocks({ |
| 87 | + post: { |
| 88 | + '/api/projects/:team_id/surveys': async (req) => { |
| 89 | + capturedRequest = await req.json() |
| 90 | + return [200, { id: 'new-survey' }] |
| 91 | + }, |
| 92 | + }, |
| 93 | + }) |
| 94 | + |
| 95 | + render(<QuickSurveyForm flag={mockFlag} />) |
| 96 | + |
| 97 | + // Add an event |
| 98 | + await userEvent.click(screen.getByRole('button', { name: /add event/i })) |
| 99 | + expect(screen.getByText('test-event')).toBeInTheDocument() |
| 100 | + |
| 101 | + // Create survey |
| 102 | + await userEvent.click(screen.getByRole('button', { name: /create & launch/i })) |
| 103 | + |
| 104 | + await waitFor(() => { |
| 105 | + expect(capturedRequest).not.toBeUndefined() |
| 106 | + expect(capturedRequest.conditions.events.values).toEqual([{ name: 'test-event' }]) |
| 107 | + expect(capturedRequest.linked_flag_id).toBe(1) |
| 108 | + }) |
| 109 | + }) |
| 110 | + |
| 111 | + it('creates survey without events when none selected', async () => { |
| 112 | + let capturedRequest: any |
| 113 | + useMocks({ |
| 114 | + post: { |
| 115 | + '/api/projects/:team_id/surveys': async (req) => { |
| 116 | + capturedRequest = await req.json() |
| 117 | + return [200, { id: 'new-survey' }] |
| 118 | + }, |
| 119 | + }, |
| 120 | + }) |
| 121 | + |
| 122 | + render(<QuickSurveyForm flag={mockFlag} />) |
| 123 | + |
| 124 | + await userEvent.click(screen.getByRole('button', { name: /create & launch/i })) |
| 125 | + |
| 126 | + await waitFor(() => { |
| 127 | + expect(capturedRequest).not.toBeUndefined() |
| 128 | + expect(capturedRequest.conditions.events.values).toEqual([]) |
| 129 | + }) |
| 130 | + }) |
| 131 | + |
| 132 | + it('includes selected variant in survey conditions', async () => { |
| 133 | + const multivariateFlag: FeatureFlagType = { |
| 134 | + ...mockFlag, |
| 135 | + filters: { |
| 136 | + groups: [], |
| 137 | + multivariate: { |
| 138 | + variants: [ |
| 139 | + { key: 'control', rollout_percentage: 50 }, |
| 140 | + { key: 'test', rollout_percentage: 50 }, |
| 141 | + ], |
| 142 | + }, |
| 143 | + payloads: {}, |
| 144 | + }, |
| 145 | + } |
| 146 | + |
| 147 | + let capturedRequest: any |
| 148 | + useMocks({ |
| 149 | + post: { |
| 150 | + '/api/projects/:team_id/surveys': async (req) => { |
| 151 | + capturedRequest = await req.json() |
| 152 | + return [200, { id: 'new-survey' }] |
| 153 | + }, |
| 154 | + }, |
| 155 | + }) |
| 156 | + |
| 157 | + render(<QuickSurveyForm flag={multivariateFlag} />) |
| 158 | + |
| 159 | + // Select the 'test' variant |
| 160 | + const testVariantRadio = screen.getByLabelText(/test/i, { selector: 'input[type="radio"]' }) |
| 161 | + await userEvent.click(testVariantRadio) |
| 162 | + |
| 163 | + await userEvent.click(screen.getByRole('button', { name: /create & launch/i })) |
| 164 | + |
| 165 | + await waitFor(() => { |
| 166 | + expect(capturedRequest).not.toBeUndefined() |
| 167 | + expect(capturedRequest.conditions.linkedFlagVariant).toBe('test') |
| 168 | + }) |
| 169 | + }) |
| 170 | +}) |
0 commit comments