From 59f04d9d188e769d2398358bac0a64b5b179175d Mon Sep 17 00:00:00 2001 From: Martin Salles Date: Sun, 25 Feb 2024 12:58:37 +0100 Subject: [PATCH] Fix quizz screen when no question matches criteria --- src/screens/QuizzScreen.js | 6 +++++ src/screens/__tests__/QuizzScreen.test.js | 15 +++++++++++++ .../__snapshots__/QuizzScreen.test.js.snap | 22 +++++++++++++++++++ src/utils/locales/en.js | 1 + src/utils/locales/fr.js | 1 + 5 files changed, 45 insertions(+) diff --git a/src/screens/QuizzScreen.js b/src/screens/QuizzScreen.js index 7676b01..9202c88 100644 --- a/src/screens/QuizzScreen.js +++ b/src/screens/QuizzScreen.js @@ -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 ( + + {I18n.t('quizzScreen.noQuestions')} + + ); const [selectedQuestions, _setSelectedQuestions] = useState(getRandomElementsFromArray(filteredQuestions, number)); const [current, setCurrent] = useState(1); diff --git a/src/screens/__tests__/QuizzScreen.test.js b/src/screens/__tests__/QuizzScreen.test.js index 4183eba..34a6e87 100644 --- a/src/screens/__tests__/QuizzScreen.test.js +++ b/src/screens/__tests__/QuizzScreen.test.js @@ -28,4 +28,19 @@ describe('', () => { 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(); + + expect(toJSON()).toMatchSnapshot(); + }); }); diff --git a/src/screens/__tests__/__snapshots__/QuizzScreen.test.js.snap b/src/screens/__tests__/__snapshots__/QuizzScreen.test.js.snap index 3938376..c843645 100644 --- a/src/screens/__tests__/__snapshots__/QuizzScreen.test.js.snap +++ b/src/screens/__tests__/__snapshots__/QuizzScreen.test.js.snap @@ -1435,3 +1435,25 @@ exports[` renders correctly 3`] = ` `; + +exports[` renders correctly with no questions 1`] = ` + + + + We have no question matching your criteria. + + + +`; diff --git a/src/utils/locales/en.js b/src/utils/locales/en.js index 520dc25..1c66f97 100644 --- a/src/utils/locales/en.js +++ b/src/utils/locales/en.js @@ -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: { diff --git a/src/utils/locales/fr.js b/src/utils/locales/fr.js index 4ff6f67..bc4e3d8 100644 --- a/src/utils/locales/fr.js +++ b/src/utils/locales/fr.js @@ -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: {