-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fetch the copy for the default epic from an API endpoint once when we boot the app. At this point we're still missing the following, which we'll follow up on in future PRs: * Elegant handling/retrying if we fail to fetch the content * We don't have full support for all possible placeholders yet (I think the full list is %%CURRENCY_SYMBOL%%, %%COUNTRY_NAME%% and %%ARTICLE_COUNT%%) * We do support interpolating the currency symbol, but we just use a hardcoded value. In future we'll want to bring across the logic from frontend where we resolve this based on the geolocation.
- Loading branch information
Showing
6 changed files
with
245 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { fetchDefaultEpicContent } from './contributionsApi'; | ||
|
||
jest.mock('node-fetch', () => require('fetch-mock').sandbox()); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const fetchMock = require('node-fetch'); | ||
|
||
describe('fetchDefaultEpic', () => { | ||
it('fetches and returns the data in the expected format', async () => { | ||
const url = | ||
'https://interactive.guim.co.uk/docsdata/1fy0JolB1bf1IEFLHGHfUYWx-niad7vR9K954OpTOvjE.json'; | ||
|
||
const response = { | ||
sheets: { | ||
control: [ | ||
{ | ||
heading: 'Since you’re here...', | ||
paragraphs: 'First paragraph', | ||
highlightedText: 'Highlighted Text', | ||
}, | ||
{ | ||
heading: '', | ||
paragraphs: 'Second paragraph', | ||
highlightedText: '', | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
fetchMock.get(url, response); | ||
|
||
const epicData = await fetchDefaultEpicContent(); | ||
|
||
expect(epicData).toEqual({ | ||
heading: 'Since you’re here...', | ||
paragraphs: ['First paragraph', 'Second paragraph'], | ||
highlighted: ['Highlighted Text'], | ||
}); | ||
}); | ||
}); |
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,34 @@ | ||
import fetch from 'node-fetch'; | ||
|
||
const defaultEpicUrl = | ||
'https://interactive.guim.co.uk/docsdata/1fy0JolB1bf1IEFLHGHfUYWx-niad7vR9K954OpTOvjE.json'; | ||
|
||
export type DefaultEpicContent = { | ||
heading?: string; | ||
paragraphs: string[]; | ||
highlighted: string[]; | ||
}; | ||
|
||
export const fetchDefaultEpicContent = async (): Promise<DefaultEpicContent> => { | ||
const response = await fetch(defaultEpicUrl); | ||
if (!response.ok) { | ||
throw new Error( | ||
`Encountered a non-ok response when fetching default epic: ${response.status}`, | ||
); | ||
} | ||
const data = await response.json(); | ||
const control = data?.sheets?.control; | ||
|
||
return control.reduce( | ||
( | ||
acc: DefaultEpicContent, | ||
item: { heading: string; paragraphs: string; highlightedText: string }, | ||
) => { | ||
if (!acc.heading && item.heading) acc.heading = item.heading; | ||
if (item.paragraphs) acc.paragraphs.push(item.paragraphs); | ||
if (item.highlightedText) acc.highlighted.push(item.highlightedText); | ||
return acc; | ||
}, | ||
{ paragraphs: [], highlighted: [] }, | ||
); | ||
}; |
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
Oops, something went wrong.