|
| 1 | +import React from 'react'; |
| 2 | +import { test, expect } from '@playwright/experimental-ct-react'; |
| 3 | +import { QualtricsStory } from './QualtricsStory'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Component Tests for Qualtrics URL Parameter Resolution |
| 7 | + * |
| 8 | + * Related: Issue #1211 — Qualtrics should accept urlParams the same way |
| 9 | + * TrackedLink does, including dynamic `reference` fields that resolve |
| 10 | + * values from player/game state at render time. |
| 11 | + * |
| 12 | + * These tests verify: |
| 13 | + * QURL-001 Static params (`value`) are appended to the iframe src |
| 14 | + * QURL-002 deliberationId and sampleId are always appended |
| 15 | + * QURL-003 Reference to urlParams resolves and is appended |
| 16 | + * QURL-004 Reference to participantInfo field resolves and is appended |
| 17 | + * QURL-005 Mixed static + reference params both appear in the URL |
| 18 | + * QURL-006 QualtricsEOS postMessage calls onSubmit and records data |
| 19 | + * |
| 20 | + * Mock setup: |
| 21 | + * hooksConfig.empirica — provides MockEmpiricaProvider with player attrs |
| 22 | + * (participantData, sampleId, urlParams, name, etc.) |
| 23 | + * IdleProvider — wraps Qualtrics to avoid console errors from the |
| 24 | + * default IdleContext no-op when setAllowIdle fires |
| 25 | + */ |
| 26 | + |
| 27 | +// --------------------------------------------------------------------------- |
| 28 | +// Helpers |
| 29 | +// --------------------------------------------------------------------------- |
| 30 | + |
| 31 | +const SURVEY_URL = 'https://test.qualtrics.com/jfe/form/SV_testSurvey123'; |
| 32 | + |
| 33 | +/** Base player config with deliberationId and sampleId populated. */ |
| 34 | +const basePlayer = { |
| 35 | + id: 'p0', |
| 36 | + attrs: { |
| 37 | + participantData: { deliberationId: 'delib-abc-123' }, |
| 38 | + sampleId: 'sample-xyz-456', |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
| 42 | +/** Minimal hooksConfig for tests that only need basic player state. */ |
| 43 | +const baseEmpirica = { |
| 44 | + currentPlayerId: 'p0', |
| 45 | + players: [basePlayer], |
| 46 | + game: { attrs: {} }, |
| 47 | + stage: { attrs: {} }, |
| 48 | +}; |
| 49 | + |
| 50 | +// --------------------------------------------------------------------------- |
| 51 | +// Tests |
| 52 | +// --------------------------------------------------------------------------- |
| 53 | + |
| 54 | +/** |
| 55 | + * Test ID: QURL-001 |
| 56 | + * Issue: #1211 |
| 57 | + * Validates: Static `value` params are appended to the iframe src URL |
| 58 | + */ |
| 59 | +test('QURL-001: static params appended to iframe URL', async ({ mount }) => { |
| 60 | + const component = await mount( |
| 61 | + <QualtricsStory |
| 62 | + url={SURVEY_URL} |
| 63 | + params={[ |
| 64 | + { key: 'condition', value: 'treatment' }, |
| 65 | + { key: 'sessionNum', value: 2 }, |
| 66 | + ]} |
| 67 | + />, |
| 68 | + { hooksConfig: { empirica: baseEmpirica } }, |
| 69 | + ); |
| 70 | + |
| 71 | + const src = await component.locator('[data-test="qualtricsIframe"]').getAttribute('src'); |
| 72 | + const url = new URL(src); |
| 73 | + expect(url.searchParams.get('condition')).toBe('treatment'); |
| 74 | + expect(url.searchParams.get('sessionNum')).toBe('2'); |
| 75 | +}); |
| 76 | + |
| 77 | +/** |
| 78 | + * Test ID: QURL-002 |
| 79 | + * Issue: #1211 |
| 80 | + * Validates: deliberationId and sampleId are always appended, even with no |
| 81 | + * extra params |
| 82 | + */ |
| 83 | +test('QURL-002: deliberationId and sampleId always appended', async ({ mount }) => { |
| 84 | + const component = await mount( |
| 85 | + <QualtricsStory url={SURVEY_URL} params={[]} />, |
| 86 | + { hooksConfig: { empirica: baseEmpirica } }, |
| 87 | + ); |
| 88 | + |
| 89 | + const src = await component.locator('[data-test="qualtricsIframe"]').getAttribute('src'); |
| 90 | + const url = new URL(src); |
| 91 | + expect(url.searchParams.get('deliberationId')).toBe('delib-abc-123'); |
| 92 | + expect(url.searchParams.get('sampleId')).toBe('sample-xyz-456'); |
| 93 | +}); |
| 94 | + |
| 95 | +/** |
| 96 | + * Test ID: QURL-003 |
| 97 | + * Issue: #1211 |
| 98 | + * Validates: `reference: 'urlParams.PROLIFIC_PID'` resolves from player |
| 99 | + * urlParams and is appended to the iframe src |
| 100 | + */ |
| 101 | +test('QURL-003: reference to urlParams resolves in iframe URL', async ({ mount }) => { |
| 102 | + const component = await mount( |
| 103 | + <QualtricsStory |
| 104 | + url={SURVEY_URL} |
| 105 | + params={[{ key: 'prolificId', reference: 'urlParams.PROLIFIC_PID' }]} |
| 106 | + />, |
| 107 | + { |
| 108 | + hooksConfig: { |
| 109 | + empirica: { |
| 110 | + ...baseEmpirica, |
| 111 | + players: [{ |
| 112 | + id: 'p0', |
| 113 | + attrs: { |
| 114 | + ...basePlayer.attrs, |
| 115 | + urlParams: { PROLIFIC_PID: 'PROLIFIC-TEST-123' }, |
| 116 | + }, |
| 117 | + }], |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + ); |
| 122 | + |
| 123 | + const src = await component.locator('[data-test="qualtricsIframe"]').getAttribute('src'); |
| 124 | + const url = new URL(src); |
| 125 | + expect(url.searchParams.get('prolificId')).toBe('PROLIFIC-TEST-123'); |
| 126 | +}); |
| 127 | + |
| 128 | +/** |
| 129 | + * Test ID: QURL-004 |
| 130 | + * Issue: #1211 |
| 131 | + * Validates: `reference: 'participantInfo.name'` resolves from player state |
| 132 | + * and is appended to the iframe src |
| 133 | + */ |
| 134 | +test('QURL-004: reference to participantInfo resolves in iframe URL', async ({ mount }) => { |
| 135 | + const component = await mount( |
| 136 | + <QualtricsStory |
| 137 | + url={SURVEY_URL} |
| 138 | + params={[{ key: 'pName', reference: 'participantInfo.name' }]} |
| 139 | + />, |
| 140 | + { |
| 141 | + hooksConfig: { |
| 142 | + empirica: { |
| 143 | + ...baseEmpirica, |
| 144 | + players: [{ |
| 145 | + id: 'p0', |
| 146 | + attrs: { ...basePlayer.attrs, name: 'Alice' }, |
| 147 | + }], |
| 148 | + }, |
| 149 | + }, |
| 150 | + }, |
| 151 | + ); |
| 152 | + |
| 153 | + const src = await component.locator('[data-test="qualtricsIframe"]').getAttribute('src'); |
| 154 | + const url = new URL(src); |
| 155 | + expect(url.searchParams.get('pName')).toBe('Alice'); |
| 156 | +}); |
| 157 | + |
| 158 | +/** |
| 159 | + * Test ID: QURL-005 |
| 160 | + * Issue: #1211 |
| 161 | + * Validates: Static and reference params can be mixed; all appear in the URL |
| 162 | + * alongside the always-present deliberationId and sampleId |
| 163 | + */ |
| 164 | +test('QURL-005: mixed static and reference params both appear in URL', async ({ mount }) => { |
| 165 | + const component = await mount( |
| 166 | + <QualtricsStory |
| 167 | + url={SURVEY_URL} |
| 168 | + params={[ |
| 169 | + { key: 'staticFlag', value: 'on' }, |
| 170 | + { key: 'dynamicId', reference: 'urlParams.PROLIFIC_PID' }, |
| 171 | + ]} |
| 172 | + />, |
| 173 | + { |
| 174 | + hooksConfig: { |
| 175 | + empirica: { |
| 176 | + ...baseEmpirica, |
| 177 | + players: [{ |
| 178 | + id: 'p0', |
| 179 | + attrs: { |
| 180 | + ...basePlayer.attrs, |
| 181 | + urlParams: { PROLIFIC_PID: 'PROLIFIC-MIX-456' }, |
| 182 | + }, |
| 183 | + }], |
| 184 | + }, |
| 185 | + }, |
| 186 | + }, |
| 187 | + ); |
| 188 | + |
| 189 | + const src = await component.locator('[data-test="qualtricsIframe"]').getAttribute('src'); |
| 190 | + const url = new URL(src); |
| 191 | + expect(url.searchParams.get('staticFlag')).toBe('on'); |
| 192 | + expect(url.searchParams.get('dynamicId')).toBe('PROLIFIC-MIX-456'); |
| 193 | + // Always-present params unaffected |
| 194 | + expect(url.searchParams.get('deliberationId')).toBe('delib-abc-123'); |
| 195 | + expect(url.searchParams.get('sampleId')).toBe('sample-xyz-456'); |
| 196 | +}); |
| 197 | + |
| 198 | +/** |
| 199 | + * Test ID: QURL-006 |
| 200 | + * Issue: #1211 |
| 201 | + * Validates: A QualtricsEOS postMessage triggers onSubmit and records |
| 202 | + * qualtricsDataReady on the player |
| 203 | + */ |
| 204 | +test('QURL-006: QualtricsEOS postMessage calls onSubmit', async ({ mount, page }) => { |
| 205 | + await page.evaluate(() => { window.__qualtricsSubmitted = false; }); |
| 206 | + |
| 207 | + await mount( |
| 208 | + <QualtricsStory url={SURVEY_URL} params={[]} />, |
| 209 | + { hooksConfig: { empirica: baseEmpirica } }, |
| 210 | + ); |
| 211 | + |
| 212 | + await page.evaluate(() => { |
| 213 | + window.postMessage('QualtricsEOS|SV_testSurveyId|FS_testSessionId', '*'); |
| 214 | + }); |
| 215 | + |
| 216 | + await page.waitForFunction(() => window.__qualtricsSubmitted === true, { timeout: 2000 }); |
| 217 | +}); |
0 commit comments