Skip to content

Commit

Permalink
tests for keyword bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
fredex42 committed Dec 12, 2024
1 parent b0a0465 commit ed75186
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions fronts-client/src/bundles/__tests__/feastKeywordBundle.spec.ts
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',
]);
});
});

0 comments on commit ed75186

Please sign in to comment.