Skip to content

Commit 25c62ae

Browse files
committed
add tests
1 parent 504ccec commit 25c62ae

File tree

4 files changed

+144
-63
lines changed

4 files changed

+144
-63
lines changed

playwright/surveys/feedback-widget.spec.ts

+106
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SurveySchedule } from '../../src/posthog-surveys-types'
12
import { pollUntilEventCaptured } from '../utils/event-capture-utils'
23
import { expect, test } from '../utils/posthog-playwright-test-base'
34
import { start } from '../utils/setup'
@@ -210,4 +211,109 @@ test.describe('surveys - feedback widget', () => {
210211
white
211212
)
212213
})
214+
215+
// ... existing code ...
216+
test('auto contrasts text color for feedback tab', async ({ page, context }) => {
217+
const surveysAPICall = page.route('**/surveys/**', async (route) => {
218+
await route.fulfill({
219+
json: {
220+
surveys: [
221+
{
222+
id: '123',
223+
name: 'Test survey',
224+
type: 'widget',
225+
start_date: '2021-01-01T00:00:00Z',
226+
questions: [openTextQuestion],
227+
appearance: {
228+
widgetLabel: 'white widget',
229+
widgetType: 'tab',
230+
widgetColor: 'white',
231+
},
232+
},
233+
],
234+
},
235+
})
236+
})
237+
238+
await start(startOptions, page, context)
239+
await surveysAPICall
240+
241+
await expect(page.locator('.PostHogWidget123').locator('.ph-survey-widget-tab')).toBeVisible()
242+
243+
await expect(page.locator('.PostHogWidget123').locator('.ph-survey-widget-tab')).toHaveCSS('color', black)
244+
await expect(page.locator('.PostHogWidget123').locator('.ph-survey-widget-tab')).toHaveCSS(
245+
'background-color',
246+
white
247+
)
248+
})
249+
250+
test('renders survey with schedule always and allows multiple submissions', async ({ page, context }) => {
251+
const surveysAPICall = page.route('**/surveys/**', async (route) => {
252+
await route.fulfill({
253+
json: {
254+
surveys: [
255+
{
256+
id: '123',
257+
name: 'Test survey',
258+
type: 'widget',
259+
start_date: '2021-01-01T00:00:00Z',
260+
questions: [openTextQuestion],
261+
appearance: {
262+
widgetLabel: 'Feedback',
263+
widgetType: 'tab',
264+
displayThankYouMessage: true,
265+
},
266+
conditions: {
267+
url: null,
268+
selector: null,
269+
scrolled: null,
270+
},
271+
schedule: SurveySchedule.Always,
272+
},
273+
],
274+
},
275+
})
276+
})
277+
278+
await start(startOptions, page, context)
279+
await surveysAPICall
280+
281+
// 1. Check that the survey widget is rendered
282+
await expect(page.locator('.PostHogWidget123').locator('.ph-survey-widget-tab')).toBeVisible()
283+
284+
// 2. Open the survey
285+
await page.locator('.PostHogWidget123').locator('.ph-survey-widget-tab').click()
286+
await expect(page.locator('.PostHogWidget123').locator('.survey-form')).toBeVisible()
287+
288+
// 3. Answer the survey
289+
await page.locator('.PostHogWidget123').locator('.survey-form').locator('textarea').fill('first submission')
290+
await page.locator('.PostHogWidget123').locator('.survey-form').locator('.form-submit').click()
291+
292+
// 4. Check for thank you message
293+
await expect(page.locator('.PostHogWidget123').locator('.thank-you-message-header')).toBeVisible()
294+
await expect(page.locator('.PostHogWidget123').locator('.thank-you-message-header')).toHaveText('Thank you!')
295+
296+
// Verify the event was sent
297+
await pollUntilEventCaptured(page, 'survey sent')
298+
299+
// 5. Close the thank you message and click the survey tab again
300+
await page.locator('.PostHogWidget123').locator('.form-submit').click()
301+
await expect(page.locator('.PostHogWidget123').locator('.survey-form')).not.toBeVisible()
302+
303+
// Open the survey again
304+
await page.locator('.PostHogWidget123').locator('.ph-survey-widget-tab').click()
305+
306+
// 6. Verify survey is rendered again
307+
await expect(page.locator('.PostHogWidget123').locator('.survey-form')).toBeVisible()
308+
309+
// Submit again with different text
310+
await page.locator('.PostHogWidget123').locator('.survey-form').locator('textarea').fill('second submission')
311+
await page.locator('.PostHogWidget123').locator('.survey-form').locator('.form-submit').click()
312+
313+
// Verify thank you message appears again
314+
await expect(page.locator('.PostHogWidget123').locator('.thank-you-message-header')).toBeVisible()
315+
316+
// Verify second event was sent
317+
await pollUntilEventCaptured(page, 'survey sent')
318+
})
213319
})
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import '@testing-library/jest-dom'
2-
import { fireEvent, render } from '@testing-library/preact'
2+
import { cleanup, fireEvent, render, screen } from '@testing-library/preact'
33
import { SurveyPopup } from '../../../extensions/surveys'
44
import { Survey, SurveyQuestionType, SurveyType } from '../../../posthog-surveys-types'
55

@@ -41,79 +41,47 @@ describe('SurveyPopup', () => {
4141

4242
beforeEach(() => {
4343
// Reset DOM
44-
document.getElementsByTagName('html')[0].innerHTML = ''
44+
cleanup()
4545
localStorage.clear()
4646
jest.clearAllMocks()
4747
})
4848

49-
test('calls onCloseConfirmationMessage when X button is clicked', () => {
49+
test('calls onCloseConfirmationMessage when X button is clicked in the confirmation message', () => {
5050
// Create a mock function to test if it gets called
5151
const mockOnCloseConfirmationMessage = jest.fn()
5252
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-
53+
render(
54+
<SurveyPopup
55+
survey={mockSurvey}
56+
removeSurveyFromFocus={mockRemoveSurveyFromFocus}
57+
isPopup={true}
58+
onCloseConfirmationMessage={mockOnCloseConfirmationMessage}
59+
// Force the confirmation message to show
60+
previewPageIndex={mockSurvey.questions.length}
61+
/>
62+
)
63+
const cancelButton2 = screen.getByRole('button', { name: 'Close survey', hidden: true })
7664
// 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-
}
65+
fireEvent.click(cancelButton2)
66+
// Verify that onCloseConfirmationMessage was called
67+
expect(mockOnCloseConfirmationMessage).toHaveBeenCalledTimes(1)
8468
})
8569

86-
test('calls onCloseConfirmationMessage when Close button is clicked', () => {
87-
// Create a mock function to test if it gets called
70+
test('calls onCloseConfirmationMessage when survey is closed in the confirmation message', () => {
8871
const mockOnCloseConfirmationMessage = jest.fn()
8972
const mockRemoveSurveyFromFocus = jest.fn()
73+
render(
74+
<SurveyPopup
75+
survey={mockSurvey}
76+
removeSurveyFromFocus={mockRemoveSurveyFromFocus}
77+
isPopup={true}
78+
onCloseConfirmationMessage={mockOnCloseConfirmationMessage}
79+
previewPageIndex={mockSurvey.questions.length}
80+
/>
81+
)
9082

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-
}
83+
const closeButton = screen.getByRole('button', { name: /close/i })
84+
fireEvent.click(closeButton)
85+
expect(mockOnCloseConfirmationMessage).toHaveBeenCalledTimes(1)
11886
})
11987
})

src/extensions/surveys/components/BottomSection.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export function BottomSection({
3131
<button
3232
className="form-submit"
3333
disabled={submitDisabled}
34+
aria-label="Submit survey"
3435
type="button"
3536
style={isPopup ? { color: textColor } : {}}
3637
onClick={() => {

src/extensions/surveys/components/QuestionHeader.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ export function Cancel({ onClick }: { onClick: () => void }) {
3636

3737
return (
3838
<div className="cancel-btn-wrapper">
39-
<button className="form-cancel" onClick={onClick} disabled={isPreviewMode} aria-label="Close survey">
39+
<button
40+
className="form-cancel"
41+
onClick={onClick}
42+
disabled={isPreviewMode}
43+
aria-label="Close survey"
44+
role="button"
45+
>
4046
{cancelSVG}
4147
</button>
4248
</div>

0 commit comments

Comments
 (0)