!readOnly && setShowSurvey(!showSurvey)} style={{color: getContrastingTextColor(survey.appearance.widgetColor)}}>
+
!readOnly && setShowSurvey(!showSurvey)}
+ style={{ color: getContrastingTextColor(survey.appearance.widgetColor) }}
+ >
{survey.appearance?.widgetLabel || ''}
diff --git a/src/loader-surveys.ts b/src/loader-surveys.ts
index d8c25e0de..5fbed6f32 100644
--- a/src/loader-surveys.ts
+++ b/src/loader-surveys.ts
@@ -1,7 +1,7 @@
import { generateSurveys } from './extensions/surveys'
import { window } from './utils/globals'
-export { renderSurveysPreview } from './extensions/surveys'
+export { renderSurveysPreview, renderFeedbackWidgetPreview } from './extensions/surveys'
if (window) {
;(window as any).extendPostHogWithSurveys = generateSurveys
From 738f8335db173d52aa7f66f375091510f6aa1f02 Mon Sep 17 00:00:00 2001
From: Li Yi
Date: Mon, 19 Feb 2024 14:27:21 -0500
Subject: [PATCH 3/5] fix tests and render method
---
src/__tests__/extensions/surveys.test.ts | 21 ++++++++++++++-------
src/extensions/surveys.tsx | 18 ++++++++++--------
2 files changed, 24 insertions(+), 15 deletions(-)
diff --git a/src/__tests__/extensions/surveys.test.ts b/src/__tests__/extensions/surveys.test.ts
index 0bec9f2fc..350f9c90e 100644
--- a/src/__tests__/extensions/surveys.test.ts
+++ b/src/__tests__/extensions/surveys.test.ts
@@ -58,6 +58,13 @@ describe('survey display logic', () => {
})
describe('preview renders', () => {
+ beforeEach(() => {
+ // we have to manually reset the DOM before each test
+ document.getElementsByTagName('html')[0].innerHTML = ''
+ localStorage.clear()
+ jest.clearAllMocks()
+ })
+
test('renderSurveysPreview', () => {
const mockSurvey = {
id: 'testSurvey1',
@@ -95,7 +102,7 @@ describe('preview renders', () => {
id: 'testSurvey1',
name: 'Test survey 1',
type: SurveyType.Widget,
- appearance: {},
+ appearance: { widgetLabel: 'preview test', widgetColor: 'black', widgetType: 'tab' },
start_date: '2021-01-01T00:00:00.000Z',
description: 'This is a survey description',
linked_flag_key: null,
@@ -105,14 +112,14 @@ describe('preview renders', () => {
type: SurveyQuestionType.Open,
},
],
- conditions: { widgetLabel: 'preview test', widgetColor: 'black' },
end_date: null,
targeting_flag_key: null,
}
- const surveyDiv = document.createElement('div')
- expect(surveyDiv.innerHTML).toBe('')
- renderFeedbackWidgetPreview(mockSurvey as Survey, surveyDiv)
- expect(surveyDiv.getElementsByTagName('style').length).toBe(1)
- expect(surveyDiv.getElementsByClassName('ph-survey-widget-tab').length).toBe(1)
+ const root = document.createElement('div')
+ expect(root.innerHTML).toBe('')
+ renderFeedbackWidgetPreview(mockSurvey as Survey, root)
+ expect(root.getElementsByTagName('style').length).toBe(1)
+ expect(root.getElementsByClassName('ph-survey-widget-tab').length).toBe(1)
+ expect(root.getElementsByClassName('ph-survey-widget-tab')[0].innerHTML).toContain('preview test')
})
})
diff --git a/src/extensions/surveys.tsx b/src/extensions/surveys.tsx
index 77bd47ef5..6e64713bd 100644
--- a/src/extensions/surveys.tsx
+++ b/src/extensions/surveys.tsx
@@ -139,11 +139,13 @@ export const renderSurveysPreview = (
}
export const renderFeedbackWidgetPreview = (survey: Survey, root: HTMLElement) => {
- const shadow = createWidgetShadow(survey)
- const surveyStyleSheet = createWidgetStyle(survey.appearance?.widgetColor)
- shadow.appendChild(Object.assign(document.createElement('style'), { innerText: surveyStyleSheet }))
- Preact.render(, shadow)
- root.appendChild(shadow)
+ const widgetStyleSheet = createWidgetStyle(survey.appearance?.widgetColor)
+ const styleElement = Object.assign(document.createElement('style'), { innerText: widgetStyleSheet })
+ root.appendChild(styleElement)
+ const widgetHtml = render()
+ const widgetDiv = document.createElement('div')
+ widgetDiv.innerHTML = widgetHtml
+ root.appendChild(widgetDiv)
}
// This is the main exported function
@@ -373,12 +375,12 @@ const closeSurveyPopup = (survey: Survey, posthog?: PostHog, readOnly?: boolean)
}
export function FeedbackWidget({
- posthog,
survey,
+ posthog,
readOnly,
}: {
- posthog: PostHog
survey: Survey
+ posthog?: PostHog
readOnly?: boolean
}): JSX.Element {
const [showSurvey, setShowSurvey] = useState(false)
@@ -386,7 +388,7 @@ export function FeedbackWidget({
const widgetRef = useRef(null)
useEffect(() => {
- if (readOnly) {
+ if (readOnly || !posthog) {
return
}
From 1a6a47d903d499ddd5d5e3bf3ebea5d7957e2f41 Mon Sep 17 00:00:00 2001
From: Li Yi
Date: Mon, 19 Feb 2024 21:48:52 -0500
Subject: [PATCH 4/5] load survey previews module separately otherwise it
overrides generateSurveys
---
rollup.config.js | 8 +++++++-
src/extensions/surveys.tsx | 11 +++++++----
src/loader-surveys.ts | 1 -
3 files changed, 14 insertions(+), 6 deletions(-)
diff --git a/rollup.config.js b/rollup.config.js
index b2fbae84e..ea4905a89 100644
--- a/rollup.config.js
+++ b/rollup.config.js
@@ -58,8 +58,14 @@ export default [
preact: 'preact',
},
},
+ ],
+ plugins: [...plugins],
+ },
+ {
+ input: 'src/loader-surveys-preview.ts',
+ output: [
{
- file: 'dist/surveys.esm.js',
+ file: 'dist/surveys-module-previews.js',
format: 'es',
sourcemap: true,
},
diff --git a/src/extensions/surveys.tsx b/src/extensions/surveys.tsx
index 6e64713bd..5b8578234 100644
--- a/src/extensions/surveys.tsx
+++ b/src/extensions/surveys.tsx
@@ -41,7 +41,7 @@ const handleWidget = (posthog: PostHog, survey: Survey) => {
const shadow = createWidgetShadow(survey)
const surveyStyleSheet = style(survey.appearance)
shadow.appendChild(Object.assign(document.createElement('style'), { innerText: surveyStyleSheet }))
- Preact.render(, shadow)
+ Preact.render(, shadow)
}
export const callSurveys = (posthog: PostHog, forceReload: boolean = false) => {
@@ -99,7 +99,7 @@ export const callSurveys = (posthog: PostHog, forceReload: boolean = false) => {
if (!localStorage.getItem(`seenSurvey_${survey.id}`)) {
const shadow = createShadow(style(survey?.appearance), survey.id)
- Preact.render(, shadow)
+ Preact.render(, shadow)
}
}
})
@@ -120,6 +120,7 @@ export const renderSurveysPreview = (
)
const surveyHtml = render(
)
+ const widgetHtml = render()
const widgetDiv = document.createElement('div')
widgetDiv.innerHTML = widgetHtml
root.appendChild(widgetDiv)
@@ -427,7 +428,9 @@ export function FeedbackWidget({
{survey.appearance?.widgetLabel || ''}
)}
- {showSurvey &&