diff --git a/src/service/QuestionGenerator.js b/src/service/QuestionGenerator.js new file mode 100644 index 0000000..9643a84 --- /dev/null +++ b/src/service/QuestionGenerator.js @@ -0,0 +1,33 @@ +import { randomNumberInRange } from './randomNumberInRange'; +import {QuestionService} from './QuestionService'; + +const questionService = new QuestionService() +export class QuestionGenerator { + + constructor(mode) { + this.mode = mode; + this.minPokeId = 1; + this.maxPokeId = 152; + this.askedQuestions = []; + this.askedQuestionsCount = 0; + }; + + async getNextQuestion() { + if (this.askedQuestionsCount === 30) { + return undefined; + }; + this.askedQuestionsCount++; + + const answerPokeId = randomNumberInRange(this.minPokeId, this.maxPokeId, this.askedQuestions); + this.askedQuestions.push(answerPokeId); + + const currentQuestionsArray = [answerPokeId]; + for (let i = 1; i <= 3; i++) { + currentQuestionsArray.push(randomNumberInRange(this.minPokeId, this.maxPokeId, currentQuestionsArray)); + }; + + let questionObj = await questionService.getQuestion(currentQuestionsArray, this.mode); + + return questionObj + }; +}; \ No newline at end of file diff --git a/src/service/QuestionGenerator.spec.js b/src/service/QuestionGenerator.spec.js new file mode 100644 index 0000000..cb0210b --- /dev/null +++ b/src/service/QuestionGenerator.spec.js @@ -0,0 +1,34 @@ +import { QuestionGenerator } from './QuestionGenerator'; +import { WHO_IS_THAT_POKEMON } from "./modes"; + +const generatedQuestion = new QuestionGenerator(WHO_IS_THAT_POKEMON); + +describe('Test QuestionGenerator class', () => { + + it('Should return undefined if number of asked questions is bigger than 30', () => { + + //given + const numberOfAskedQuestions = 31; + + //when + generatedQuestion.askedQuestionsCount = numberOfAskedQuestions + + //then + expect(generatedQuestion.getNextQuestion()).toBeUndefined; + + }); +}); + +describe('Test askedQuestionsCount property', () => { + + it('Should increment counter', async () => { + //given + const startingCount = generatedQuestion.askedQuestionsCount; + + //when + await generatedQuestion.getNextQuestion(); + + //then + expect(generatedQuestion.askedQuestionsCount).toEqual(startingCount+1); + }); +}); \ No newline at end of file diff --git a/src/service/QuestionService.js b/src/service/QuestionService.js index 2eb0213..c2dbde4 100644 --- a/src/service/QuestionService.js +++ b/src/service/QuestionService.js @@ -9,7 +9,7 @@ export class QuestionService { this.correctAnswerIndex = 0; // range 0-3 } - async getNextQuestion(pokemonIds, mode) { + async getQuestion(pokemonIds, mode) { if (!pokemonIds || pokemonIds.length != 4) { throw new Error('pokemonIds is not an array of 4 elements') diff --git a/src/service/QuestionService.spec.js b/src/service/QuestionService.spec.js index ea7d6aa..870a7b8 100644 --- a/src/service/QuestionService.spec.js +++ b/src/service/QuestionService.spec.js @@ -5,14 +5,14 @@ import { const quizQuestion = new QuestionService(); -describe('Test getNextQuestion method', () => { +describe('Test getQuestion method', () => { it("Should return question object - example output for mode 1: question: 'bulbasaur' , answers: [ 'answer1', 'answer2', 'answer3', 'answer4' ], correctAnswer: { name: 'bulbasaur', index: 1 } ", async () => { // Given const pokemonIds = [1, 2, 3, 4]; // When - const question = await quizQuestion.getNextQuestion(pokemonIds, WHO_IS_THAT_POKEMON); + const question = await quizQuestion.getQuestion(pokemonIds, WHO_IS_THAT_POKEMON); // Then expect(question).toHaveProperty('question'); @@ -40,7 +40,7 @@ describe('Test checkAnswer method', () => { const userAnswer = 'bulbasaur'; // When - const question = await quizQuestion.getNextQuestion(pokemonIds, WHO_IS_THAT_POKEMON); + const question = await quizQuestion.getQuestion(pokemonIds, WHO_IS_THAT_POKEMON); // Then expect(quizQuestion.checkAnswer(question, userAnswer)).toBeTruthy(); @@ -56,7 +56,7 @@ describe('Test checkAnswer method', () => { const userAnswer = 'venusaur'; // When - const question = await quizQuestion.getNextQuestion(pokemonIds, WHO_IS_THAT_POKEMON); + const question = await quizQuestion.getQuestion(pokemonIds, WHO_IS_THAT_POKEMON); // Then expect(quizQuestion.checkAnswer(question, userAnswer)).toBeFalsy();