-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(TU-3717): Expose method to fetch form details in callback (#4)
- Loading branch information
Showing
11 changed files
with
191 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import fetchMock from 'jest-fetch-mock' | ||
|
||
import { fetchFormDetails } from './fetch-form-details' | ||
|
||
fetchMock.enableMocks() | ||
|
||
describe('#fetchFormDetails', () => { | ||
beforeEach(() => { | ||
fetchMock.resetMocks() | ||
}) | ||
|
||
it('fetches data from oembed URL', async () => { | ||
fetchMock.mockReturnValueOnce(new Promise((res) => res(new Response('{}')))) | ||
await fetchFormDetails('12345') | ||
expect(fetchMock).toHaveBeenCalledWith( | ||
`https://form.typeform.com/oembed?url=${encodeURIComponent('https://form.typeform.com/to/12345')}`, | ||
) | ||
}) | ||
|
||
it('returns empty object when it fails to fetch form details', async () => { | ||
fetchMock.mockReject(() => Promise.reject('error')) | ||
const formDetails = await fetchFormDetails('12345') | ||
expect(formDetails).toEqual({}) | ||
}) | ||
|
||
it('returns form details when it fetches form details', async () => { | ||
const title = 'foobar' | ||
const url = 'https://form.typeform.com/to/12345' | ||
const imageUrl = 'https://images.typeform.com/images/abcde' | ||
const oembedBodyMock = JSON.stringify({ | ||
title, | ||
author_url: url, | ||
thumbnail_url: imageUrl, | ||
}) | ||
fetchMock.mockReturnValueOnce(new Promise((res) => res(new Response(oembedBodyMock)))) | ||
const formDetails = await fetchFormDetails('12345') | ||
expect(formDetails).toEqual({ | ||
title, | ||
url, | ||
imageUrl, | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export interface FormDetails { | ||
title?: string | ||
url?: string | ||
imageUrl?: string | ||
} | ||
export const fetchFormDetails = async (formId: string): Promise<FormDetails> => { | ||
const host = 'https://form.typeform.com' | ||
const formUrl = `${host}/to/${formId}` | ||
|
||
try { | ||
const result = await fetch(`${host}/oembed?url=${encodeURIComponent(formUrl)}`) | ||
if (!result.ok) { | ||
return {} | ||
} | ||
const data = await result.json() | ||
const { title, author_url: url, thumbnail_url: image } = data || {} | ||
return { title, url, imageUrl: image?.href ?? image } | ||
} catch (e) { | ||
return {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.