Skip to content

Commit

Permalink
Fix quizz screen when no question matches criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
sallesma committed Feb 25, 2024
1 parent 50484f1 commit 59f04d9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/screens/QuizzScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export default (props) => {
(level === Levels.ANY || level === question.level) &&
(checkedCategories.length === 0 || checkedCategories.includes(question.category)),
);
if (filteredQuestions.length === 0)
return (
<ScrollView style={styles.container} contentContainerStyle={styles.contentContainer}>
<Text>{I18n.t('quizzScreen.noQuestions')}</Text>
</ScrollView>
);

const [selectedQuestions, _setSelectedQuestions] = useState(getRandomElementsFromArray(filteredQuestions, number));
const [current, setCurrent] = useState(1);
Expand Down
15 changes: 15 additions & 0 deletions src/screens/__tests__/QuizzScreen.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,19 @@ describe('<QuizzScreen />', () => {

expect(toJSON()).toMatchSnapshot();
});

it('renders correctly with no questions', async () => {
const navigation = { setOptions: jest.fn() };
const route = {
params: {
number: 5,
time: 30,
level: Levels.DIFFICULT,
checkedCategories: [Categories.INDOOR],
},
};
const { toJSON, getByText } = render(<QuizzScreen navigation={navigation} route={route} />);

expect(toJSON()).toMatchSnapshot();
});
});
22 changes: 22 additions & 0 deletions src/screens/__tests__/__snapshots__/QuizzScreen.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1435,3 +1435,25 @@ exports[`<QuizzScreen /> renders correctly 3`] = `
</View>
</RCTScrollView>
`;

exports[`<QuizzScreen /> renders correctly with no questions 1`] = `
<RCTScrollView
contentContainerStyle={
{
"paddingBottom": 64,
}
}
style={
{
"flex": 1,
"padding": 16,
}
}
>
<View>
<Text>
We have no question matching your criteria.
</Text>
</View>
</RCTScrollView>
`;
1 change: 1 addition & 0 deletions src/utils/locales/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export default {
one: '{{count}} correct answer',
other: '{{count}} correct answers',
},
noQuestions: 'We have no question matching your criteria.',
nextQuestion: 'Next question',
},
rulesScreen: {
Expand Down
1 change: 1 addition & 0 deletions src/utils/locales/fr.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export default {
one: '{{count}} bonne réponse',
other: '{{count}} bonnes réponses',
},
noQuestions: 'Aucune question ne correspond aux critères choisis.',
nextQuestion: 'Question suivante',
},
rulesScreen: {
Expand Down

0 comments on commit 59f04d9

Please sign in to comment.