Skip to content

Commit 3b75d51

Browse files
authored
Do not allow survey comments to change after rating (#51111)
1 parent 1576f84 commit 3b75d51

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/events/components/Survey.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ export const Survey = () => {
8282
}
8383
}, [email])
8484

85+
useEffect(() => {
86+
if (state === ViewState.NEXT && data?.comment !== comment.trim()) {
87+
setState(ViewState.START)
88+
setIsEmailError(false)
89+
}
90+
}, [comment])
91+
8592
const { data, error, isLoading } = useSWR(
8693
state === ViewState.NEXT && comment.trim() ? '/api/events/survey/preview/v1' : null,
8794
async (url: string) => {
@@ -107,7 +114,7 @@ export const Survey = () => {
107114
},
108115
)
109116

110-
const hasPreview = !!data && !error
117+
const hasPreview = !!data && !error && ViewState.NEXT
111118

112119
function submit(evt: React.FormEvent) {
113120
evt.preventDefault()

src/events/middleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ router.post(
9494

9595
const { rating, signals } = await analyzeComment(comment, locale)
9696

97-
res.json({ rating })
97+
res.json({ rating, comment })
9898

9999
tags.push(...signals.map((signal) => `signal:${signal}`))
100100
statsd.increment('events.survey_preview.signals', 1, tags)

src/fixtures/tests/playwright-rendering.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,53 @@ test.describe('survey', () => {
579579
await page.getByTestId('product-sidebar').getByLabel('Bar', { exact: true }).click()
580580
await expect(page.locator('[for=survey-comment]')).not.toBeVisible()
581581
})
582+
583+
test('add survey comment, then modify the survey comment', async ({ page }) => {
584+
let fulfilled = 0
585+
// Important to set this up *before* interacting with the page
586+
// in case of possible race conditions.
587+
await page.route('**/api/events', (route, request) => {
588+
route.fulfill({})
589+
expect(request.method()).toBe('POST')
590+
fulfilled++
591+
// At the time of writing you can't get the posted payload
592+
// when you use `navigator.sendBeacon(url, data)`.
593+
// So we can't make assertions about the payload.
594+
// See https://github.com/microsoft/playwright/issues/12231
595+
})
596+
597+
await page.goto('/get-started/foo/for-playwright')
598+
599+
// The label is visually an SVG. Finding it by its `for` value feels easier.
600+
await page.locator('[for=survey-yes]').click()
601+
await expect(page.getByRole('button', { name: 'Next' })).toBeVisible()
602+
await expect(page.getByRole('button', { name: 'Send' })).not.toBeVisible()
603+
604+
await page.locator('[for=survey-comment]').click()
605+
await page.locator('[for=survey-comment]').fill('This is a comment')
606+
await page.getByRole('button', { name: 'Next' }).click()
607+
await expect(page.getByRole('button', { name: 'Cancel' })).toBeVisible()
608+
await expect(page.getByRole('button', { name: 'Send' })).toBeVisible()
609+
await expect(page.getByRole('button', { name: 'Next' })).not.toBeVisible()
610+
611+
await page.locator('[for=survey-comment]').click()
612+
await page.locator('[for=survey-comment]').fill('This is a modified comment')
613+
// The "Send" button should no longer be visible
614+
// and the "Next" button should be visible
615+
await expect(page.getByRole('button', { name: 'Cancel' })).toBeVisible()
616+
await expect(page.getByRole('button', { name: 'Send' })).not.toBeVisible()
617+
await expect(page.getByRole('button', { name: 'Next' })).toBeVisible()
618+
await page.getByRole('button', { name: 'Next' }).click()
619+
await expect(page.getByRole('button', { name: 'Cancel' })).toBeVisible()
620+
await expect(page.getByRole('button', { name: 'Send' })).toBeVisible()
621+
await expect(page.getByRole('button', { name: 'Next' })).not.toBeVisible()
622+
623+
await page.getByRole('button', { name: 'Send' }).click()
624+
// One for the page view event, one for the thumbs up click, one for
625+
// the submission.
626+
expect(fulfilled).toBe(1 + 2)
627+
await expect(page.getByTestId('survey-end')).toBeVisible()
628+
})
582629
})
583630

584631
test.describe('rest API reference pages', () => {

0 commit comments

Comments
 (0)