-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1732 from guardian/ag/recipe-filters
Recipe filters
- Loading branch information
Showing
9 changed files
with
328 additions
and
5 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
97 changes: 97 additions & 0 deletions
97
fronts-client/src/bundles/__tests__/feastKeywordBundle.spec.ts
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,97 @@ | ||
import fetchMock from 'fetch-mock'; | ||
import configureStore from '../../util/configureStore'; | ||
import { fetchKeywords, selectors } from '../feastKeywordBundle'; | ||
|
||
const quickTimeout = () => | ||
new Promise((resolve) => window.setTimeout(resolve, 10)); | ||
|
||
describe('feastKeywordBundle', () => { | ||
beforeEach(() => fetchMock.reset()); | ||
|
||
it('should fetch celebrations and return them when asked', async () => { | ||
const store = configureStore(); | ||
fetchMock.once('https://recipes.guardianapis.com/keywords/celebrations', { | ||
celebrations: [ | ||
{ | ||
key: 'christmas', | ||
doc_count: 3, | ||
}, | ||
{ | ||
key: 'birthday', | ||
doc_count: 2, | ||
}, | ||
{ | ||
key: 'veganuary', | ||
doc_count: 2, | ||
}, | ||
{ | ||
key: 'bank-holiday', | ||
doc_count: 1, | ||
}, | ||
{ | ||
key: 'boxing-day', | ||
doc_count: 1, | ||
}, | ||
], | ||
}); | ||
|
||
await store.dispatch(fetchKeywords('celebration') as any); | ||
await quickTimeout(); //if we don't await again, the store has not been updated yet. | ||
|
||
expect(selectors.selectCelebrationKeywords(store.getState())).toEqual([ | ||
'christmas', | ||
'birthday', | ||
'veganuary', | ||
'bank-holiday', | ||
'boxing-day', | ||
]); | ||
}); | ||
|
||
it('should fetch diets and return them when asked', async () => { | ||
const store = configureStore(); | ||
fetchMock.once('https://recipes.guardianapis.com/keywords/diet-ids', { | ||
'diet-ids': [ | ||
{ | ||
key: 'vegetarian', | ||
doc_count: 66, | ||
}, | ||
{ | ||
key: 'gluten-free', | ||
doc_count: 37, | ||
}, | ||
{ | ||
key: 'meat-free', | ||
doc_count: 37, | ||
}, | ||
{ | ||
key: 'dairy-free', | ||
doc_count: 30, | ||
}, | ||
{ | ||
key: 'pescatarian', | ||
doc_count: 29, | ||
}, | ||
{ | ||
key: 'vegan', | ||
doc_count: 21, | ||
}, | ||
{ | ||
key: '', | ||
doc_count: 2, | ||
}, | ||
], | ||
}); | ||
|
||
await store.dispatch(fetchKeywords('diet') as any); | ||
await quickTimeout(); //if we don't await again, the store has not been updated yet. | ||
|
||
expect(selectors.selectDietKeywords(store.getState())).toEqual([ | ||
'vegetarian', | ||
'gluten-free', | ||
'meat-free', | ||
'dairy-free', | ||
'pescatarian', | ||
'vegan', | ||
]); | ||
}); | ||
}); |
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,63 @@ | ||
import createAsyncResourceBundle, { | ||
IPagination, | ||
} from '../lib/createAsyncResourceBundle'; | ||
import { ThunkResult } from '../types/Store'; | ||
import { FeastKeywordType } from '../types/FeastKeyword'; | ||
import { liveRecipes } from '../services/recipeQuery'; | ||
import { createSelector } from 'reselect'; | ||
import { State } from '../types/State'; | ||
|
||
const bundle = createAsyncResourceBundle('feastKeywords', { | ||
indexById: true, | ||
selectLocalState: (state) => state.feastKeywords, | ||
}); | ||
|
||
export const fetchKeywords = | ||
(forType: FeastKeywordType): ThunkResult<void> => | ||
async (dispatch) => { | ||
dispatch(actions.fetchStart(forType)); | ||
|
||
try { | ||
const kwdata = await liveRecipes.keywords(forType); | ||
|
||
const payload: { | ||
ignoreOrder?: undefined; | ||
pagination?: IPagination; | ||
order?: string[]; | ||
} = { | ||
pagination: { | ||
pageSize: kwdata.length, | ||
totalPages: 1, | ||
currentPage: 1, | ||
}, | ||
order: undefined, | ||
}; | ||
|
||
dispatch( | ||
actions.fetchSuccess( | ||
kwdata.filter((kw) => !!kw.id), | ||
payload, | ||
), | ||
); | ||
} catch (err) { | ||
console.error(`Unable to fetch keywords: `, err); | ||
dispatch(actions.fetchError(err)); | ||
} | ||
}; | ||
|
||
const selectAllKeywords = (state: State) => state.feastKeywords.data; | ||
const makeKeywordSelector = (kwType: FeastKeywordType) => | ||
createSelector([selectAllKeywords], (kws) => { | ||
return Object.keys(kws).filter((_) => kws[_].keywordType === kwType); | ||
}); | ||
const selectCelebrationKeywords = makeKeywordSelector('celebration'); | ||
const selectDietKeywords = makeKeywordSelector('diet'); | ||
|
||
export const actionNames = bundle.actionNames; | ||
export const actions = bundle.actions; | ||
export const reducer = bundle.reducer; | ||
export const selectors = { | ||
...bundle.selectors, | ||
selectCelebrationKeywords, | ||
selectDietKeywords, | ||
}; |
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
Oops, something went wrong.