Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNG-27_question_generator #22

Merged
merged 8 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/service/QuestionGenerator.js
Original file line number Diff line number Diff line change
@@ -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 = [];
lukaszdutka marked this conversation as resolved.
Show resolved Hide resolved
this.askedQuestionsCount = 0;
};

async getNextQuestion() {
if (this.askedQuestionsCount === 30) {
return undefined;
};
this.askedQuestionsCount++;
mariusz-sm marked this conversation as resolved.
Show resolved Hide resolved

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
};
};
34 changes: 34 additions & 0 deletions src/service/QuestionGenerator.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 1 addition & 1 deletion src/service/QuestionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
8 changes: 4 additions & 4 deletions src/service/QuestionService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down