Skip to content

Commit b3d6694

Browse files
author
Antonio
authored
feat(DIST-700): Add transitiveSearchParams (#179)
feat(DIST-700): Comments review test(DIST-700): Fix functional tests
1 parent 9c83413 commit b3d6694

12 files changed

+146
-31
lines changed

packages/demo-html/public/widget-html.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212
</style>
1313
</head>
1414
<body>
15-
<div id="wrapper" data-tf-widget="moe6aa" data-tf-medium="unit-test"></div>
15+
<div
16+
id="wrapper"
17+
data-tf-widget="moe6aa"
18+
data-tf-medium="unit-test"
19+
data-tf-transitive-search-params="foo,bar"
20+
data-tf-source="localhost"
21+
></div>
1622
<script src="./lib/embed-next.js"></script>
1723
</body>
1824
</html>

packages/demo-html/public/widget-js.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
<div id="wrapper"></div>
1717
<script src="./lib/embed-next.js"></script>
1818
<script>
19-
window.tf.createWidget("moe6aa", { container: document.getElementById("wrapper"), medium: "unit-test" });
19+
window.tf.createWidget("moe6aa", {
20+
container: document.getElementById("wrapper"),
21+
source: "localhost",
22+
transitiveSearchParams: ["foo", "bar"],
23+
medium: "unit-test",
24+
});
2025
</script>
2126
</body>
2227
</html>

packages/embed/.prettierrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"trailingComma": "es5",
33
"tabWidth": 2,
44
"semi": false,
5-
"singleQuote": true
5+
"singleQuote": true,
6+
"printWidth": 120
67
}
Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,42 @@
11
describe('Widget', () => {
2-
before(() => {
3-
cy.visit('/widget-js.html')
4-
})
2+
testWidget('js')
3+
testWidget('html')
4+
})
55

6-
it('should display widget', () => {
7-
cy.get('.typeform-widget iframe').should('be.visible')
8-
cy.get('.typeform-widget iframe').invoke('attr', 'src').should('contain', 'form.typeform.com/to/')
9-
})
6+
function testWidget(type = 'html') {
7+
describe(`Widget ${type}`, () => {
8+
before(() => {
9+
cy.visit(`/widget-${type}.html`)
10+
})
11+
12+
it('should display widget', () => {
13+
cy.get('.typeform-widget iframe').should('be.visible')
14+
cy.get('.typeform-widget iframe').invoke('attr', 'src').should('contain', 'form.typeform.com/to/')
15+
})
16+
17+
it('should pass options as query param', () => {
18+
cy.get('.typeform-widget iframe')
19+
.invoke('attr', 'src')
20+
.should('contain', 'typeform-embed=embed-widget&typeform-source=localhost&typeform-medium=unit-test')
21+
})
1022

11-
it('should pass options as query param', () => {
12-
cy.get('.typeform-widget iframe')
13-
.invoke('attr', 'src')
14-
.should('contain', 'typeform-embed=embed-widget&typeform-source=localhost&typeform-medium=unit-test')
23+
describe(`Widget ${type} With Parameters`, () => {
24+
before(() => {
25+
cy.visit(`/widget-${type}.html?foo=foo&bar=bar&baz=baz`)
26+
})
27+
28+
it('should display widget', () => {
29+
cy.get('.typeform-widget iframe').should('be.visible')
30+
cy.get('.typeform-widget iframe').invoke('attr', 'src').should('contain', 'form.typeform.com/to/')
31+
})
32+
33+
it('should pass params from options to the iframe', () => {
34+
cy.get('.typeform-widget iframe').invoke('attr', 'src').should('contain', 'foo=foo&bar=bar')
35+
})
36+
37+
it('should not pass params not in the list to the iframe', () => {
38+
cy.get('.typeform-widget iframe').invoke('attr', 'src').should('not.contain', 'baz=baz')
39+
})
40+
})
1541
})
16-
})
42+
}

packages/embed/src/base/url-options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export type UrlOptions = {
2222
*
2323
* @type {string[]}
2424
*/
25-
transferableUrlParameters?: string[]
25+
transitiveSearchParams?: string[]
2626
/**
2727
* Hide typeform footer, that appears showing the progress bar and the navigation buttons.
2828
*

packages/embed/src/initializers/build-options-from-attributes.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe('build-options-from-attributes', () => {
3737
onReady: 'function',
3838
onSubmit: 'function',
3939
onQuestionChanged: 'function',
40+
transitiveSearchParams: 'array',
4041
})
4142
expect(options).toEqual({
4243
source: 'unit-test-source',

packages/embed/src/initializers/build-options-from-attributes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const buildOptionsFromAttributes = (
1515
onReady: 'function',
1616
onSubmit: 'function',
1717
onQuestionChanged: 'function',
18+
transitiveSearchParams: 'array',
1819
...additionalAttributes,
1920
})
2021
}

packages/embed/src/utils/build-iframe-src.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EmbedType, UrlOptions } from '../base'
22

33
import { removeUndefinedKeys } from './remove-undefined-keys'
44
import { isDefined } from './is-defined'
5+
import { getTransitiveSearchParams } from './get-transitive-search-params'
56

67
const defaultUrlOptions: UrlOptions = {
78
source: window?.location?.hostname.replace(/^www\./, ''),
@@ -20,30 +21,33 @@ const typesToEmbed: Record<EmbedType, string> = {
2021
'side-tab': 'popup-side-panel',
2122
}
2223

23-
const mapOptionsToQueryParams = (type: EmbedType, embedId: string, options: UrlOptions): Record<string, any> => ({
24-
'typeform-embed-id': embedId,
25-
'typeform-embed': typesToEmbed[type],
26-
'typeform-source': options.source,
27-
'typeform-medium': options.medium,
28-
'typeform-medium-version': options.mediumVersion,
29-
'embed-hide-footer': options.hideFooter ? 'true' : undefined,
30-
'embed-hide-headers': options.hideHeaders ? 'true' : undefined,
31-
'embed-opacity': options.opacity,
32-
'disable-tracking': options.disableTracking ? 'true' : undefined,
33-
})
24+
const mapOptionsToQueryParams = (type: EmbedType, embedId: string, options: UrlOptions): Record<string, any> => {
25+
const transitiveParams = getTransitiveSearchParams(options.transitiveSearchParams)
26+
const params = {
27+
'typeform-embed-id': embedId,
28+
'typeform-embed': typesToEmbed[type],
29+
'typeform-source': options.source,
30+
'typeform-medium': options.medium,
31+
'typeform-medium-version': options.mediumVersion,
32+
'embed-hide-footer': options.hideFooter ? 'true' : undefined,
33+
'embed-hide-headers': options.hideHeaders ? 'true' : undefined,
34+
'embed-opacity': options.opacity,
35+
'disable-tracking': options.disableTracking ? 'true' : undefined,
36+
}
37+
return { ...params, ...transitiveParams }
38+
}
3439

3540
export const buildIframeSrc = (params: BuildIframeSrcOptions): string => {
3641
const { formId, type, embedId, options } = params
37-
3842
const queryParams = mapOptionsToQueryParams(type, embedId, addDefaultUrlOptions(options))
3943

4044
const url = new URL(`https://form.typeform.com/to/${formId}`)
45+
4146
Object.entries(queryParams)
4247
.filter(([, paramValue]) => isDefined(paramValue))
4348
.forEach(([paramName, paramValue]) => {
4449
url.searchParams.set(paramName, paramValue)
4550
})
46-
4751
return url.href
4852
}
4953

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { getTransitiveSearchParams } from './get-transitive-search-params'
2+
3+
describe('transferUrlParametersToQueryStrings', () => {
4+
const location: Location = window.location
5+
6+
beforeAll(() => {
7+
delete (window as any).location
8+
const search =
9+
'?foo=jason&bar=rachel&utm_medium=cpc&utm_campaign=camp2008&utm_source=instagram&embed-hide-footer=false'
10+
11+
window.location = {
12+
search,
13+
href: `http://localhost/${search}`,
14+
} as any
15+
})
16+
17+
afterAll(() => {
18+
window.location = location
19+
})
20+
21+
it('transfer the parameters of the URL in the query strings', () => {
22+
const urlParameters = ['foo', 'bar']
23+
const queryStringWithTransferedUrlParameters = getTransitiveSearchParams(urlParameters)
24+
expect(queryStringWithTransferedUrlParameters).toEqual({
25+
foo: 'jason',
26+
bar: 'rachel',
27+
})
28+
})
29+
})
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const getTransitiveSearchParams = (transitiveSearchParams?: string[]) => {
2+
const url = new URL(window.location.href)
3+
const queryParamsWithTransitiveParams = {}
4+
5+
if (transitiveSearchParams && transitiveSearchParams.length > 0) {
6+
transitiveSearchParams.forEach((key: string) => {
7+
if (url.searchParams.has(key)) {
8+
queryParamsWithTransitiveParams[key] = url.searchParams.get(key)
9+
}
10+
})
11+
}
12+
13+
return queryParamsWithTransitiveParams
14+
}

0 commit comments

Comments
 (0)