Skip to content

Commit e567cae

Browse files
authored
Merge pull request #64 from ironhack-labs/uros/feat/update-starter-code-add-tests
Add missing unit tests for the`Quiz` class methods
2 parents 9e7eb20 + b760063 commit e567cae

File tree

1 file changed

+41
-119
lines changed

1 file changed

+41
-119
lines changed

tests/quiz.spec.js

+41-119
Original file line numberDiff line numberDiff line change
@@ -254,70 +254,32 @@ describe("Quiz", () => {
254254
});
255255
});
256256

257-
// ****************************************************************************************************
258-
// DAY 2
259-
//
260-
// The 1st test is already written for you - checking if the 'filter()' array method is used.
261-
//
262-
// The test block below is currently skipped ('xdescribe').
263-
// Once you start working on the tests, change the 'xdescribe' to 'describe' to enable the tests.
264-
// ****************************************************************************************************
265-
266-
xdescribe("filterQuestionsByDifficulty() method", () => {
267-
it("should use the 'filter()' array method on the 'questions' array", () => {
257+
describe("filterQuestionsByDifficulty() method", () => {
258+
it("should be defined", () => {
268259
// Instantiate a new Quiz object
269260
const quiz = new Quiz([], "test", 60);
270-
// Set up a spy on the 'filter()' array method to track calls to it
271-
const filterSpy = spyOn(quiz.questions, "filter");
272-
273-
// Call the 'filterQuestionsByDifficulty()' method
274-
quiz.filterQuestionsByDifficulty(1);
275-
276-
// Check if the 'filter()' array method was called on the 'questions' array
277-
expect(filterSpy).toHaveBeenCalled();
278-
// Check that the 'filter()' array method was called only once
279-
expect(filterSpy).toHaveBeenCalledTimes(1);
280-
// Check that the 'filter()' array method was called correctly, with a function as its argument
281-
expect(filterSpy).toHaveBeenCalledWith(jasmine.any(Function));
282-
});
283-
284-
285-
// ****************************************************************************************************
286-
// DAY 2: 'filterQuestionsByDifficulty()' method
287-
//
288-
// Below are 4 tests that you need to write for the 'filterQuestionsByDifficulty()' method.
289-
// ****************************************************************************************************
290-
291-
292-
it("should be defined", () => {
293-
// YOUR CODE HERE:
294-
//
295-
// 1. Instantiate a new Quiz object
296-
297-
// 2. Check if the filterQuestionsByDifficulty() method is defined
261+
// Check if the filterQuestionsByDifficulty() method is defined
262+
expect(quiz.filterQuestionsByDifficulty).toBeDefined();
298263

299264
});
300265

301266
it("should be a function", () => {
302-
// YOUR CODE HERE:
303-
//
304-
// 1. Instantiate a new Quiz object
267+
// Instantiate a new Quiz object
268+
const quiz = new Quiz([], "test", 60);
269+
// Check if the filterQuestionsByDifficulty() is a function
270+
expect(typeof quiz.filterQuestionsByDifficulty).toBe("function");
305271

306-
// 2. Check if the .filterQuestionsByDifficulty is a function
307272
});
308273

309274
it("should receive 1 argument (difficulty)", () => {
310-
// YOUR CODE HERE:
311-
//
312-
// 1. Instantiate a new Quiz object
313-
314-
// 2. Check if the filterQuestionsByDifficulty() method takes 1 argument
275+
// Instantiate a new Quiz object
276+
const quiz = new Quiz([], "test", 60);
277+
// Check if the filterQuestionsByDifficulty() method takes 1 argument
278+
expect(quiz.filterQuestionsByDifficulty.length).toEqual(1);
315279

316280
});
317281

318282
it("should update the 'questions' array with the questions filtered by difficulty", () => {
319-
// 1. Read the above test description to understand what this test should do
320-
321283
// Array with questions to be used in the test and passed to the Quiz constructor
322284
const questions = [
323285
{
@@ -346,19 +308,17 @@ describe("Quiz", () => {
346308
},
347309
];
348310

349-
// YOUR CODE HERE:
350-
//
351-
// 2. Instantiate a new Quiz object with the test questions
311+
// Instantiate a new Quiz object with the test questions
312+
const quiz = new Quiz(questions, "test", 60);
352313

353-
// 3. Call the `filterQuestionsByDifficulty()` method with a number between 1 and 3 as a 1st argument.
314+
// Call the `filterQuestionsByDifficulty()` method with a number between 1 and 3 as a 1st argument.
315+
quiz.filterQuestionsByDifficulty(2);
354316

355-
// 4. Check if the questions array has been filtered correctly
317+
// Check if the questions array has been filtered correctly
318+
expect(quiz.questions).toEqual([questions[1], questions[2]]);
356319
});
357320

358321
it("should not change the 'questions' array if the 1st argument is not a number between 1 and 3", () => {
359-
// 1. Read the above test description to understand what this test should do
360-
361-
362322
// Array with questions to be used in the test and passed to the Quiz constructor
363323
const questions = [
364324
{
@@ -381,78 +341,41 @@ describe("Quiz", () => {
381341
},
382342
];
383343

384-
// YOUR CODE HERE:
385-
//
386-
// 2. Instantiate a new Quiz object with the test questions
387-
388-
// 3. Call the `filterQuestionsByDifficulty()` method with a string as a 1st argument (wrong data type).
344+
// Instantiate a new Quiz object with the test questions
345+
const quiz = new Quiz(questions, "test", 60);
389346

390-
// 4. Check if the questions array is still the same as the original (it hasn't been filtered)
347+
// Call the `filterQuestionsByDifficulty()` method with a string as a 1st argument (wrong data type).
348+
quiz.filterQuestionsByDifficulty("foo");
391349

350+
// Check if the questions array is still the same as the original (it hasn't been filtered)
351+
expect(quiz.questions).toEqual(questions);
392352
});
393353
});
394354

395355

396-
// ****************************************************************************************************
397-
// DAY 2
398-
//
399-
// The 1st test is already written for you - checking if the 'reduce()' array method is used.
400-
//
401-
// The test block below is currently skipped ('xdescribe').
402-
// Once you start working on the tests, change the 'xdescribe' to 'describe' to enable the tests.
403-
// ****************************************************************************************************
404-
405-
xdescribe("averageDifficulty() method", () => {
406-
it("should use the 'reduce()' array method on the 'questions' array", () => {
356+
describe("averageDifficulty() method", () => {
357+
it("should be defined", () => {
407358
// Instantiate a new Quiz object
408359
const quiz = new Quiz([], "test", 60);
409-
// Set up a spy on the 'reduce()' array method to track calls to it
410-
const reduceSpy = spyOn(quiz.questions, "reduce");
411-
412-
// Call the 'averageDifficulty()' method
413-
quiz.averageDifficulty();
414-
415-
// Check if the 'reduce()' array method was called on the 'questions' array
416-
expect(reduceSpy).toHaveBeenCalled();
417-
// Check that the 'reduce()' array method was called only once
418-
expect(reduceSpy).toHaveBeenCalledTimes(1);
419-
// Check that the 'reduce()' array method was called correctly, with a function as its 1st argument and optional 2nd argument
420-
expect(reduceSpy.calls.allArgs()[0][0]).toEqual(jasmine.any(Function));
421-
});
422-
423-
424-
// ****************************************************************************************************
425-
// DAY 2: 'averageDifficulty()' method
426-
//
427-
// Below are 4 tests that you need to write for the 'averageDifficulty()' method.
428-
// ****************************************************************************************************
429-
430-
431-
it("should be defined", () => {
432-
// YOUR CODE HERE:
433-
//
434-
// 1. Instantiate a new Quiz object
435-
436-
// 2. Check if the averageDifficulty() method is defined
360+
// Check if the averageDifficulty() method is defined
361+
expect(quiz.averageDifficulty).toBeDefined();
437362

438363
});
439364

440365
it("should be a function", () => {
441-
// YOUR CODE HERE:
442-
//
443-
// 1. Instantiate a new Quiz object
444-
445-
// 2. Check if the .averageDifficulty is a function
366+
// Instantiate a new Quiz object
367+
const quiz = new Quiz([], "test", 60);
446368

369+
// Check if the .averageDifficulty is a function
370+
expect(typeof quiz.averageDifficulty).toBe("function");
447371
});
448372

449373
it("should receive no arguments", () => {
450-
// YOUR CODE HERE:
451-
//
452-
// 1. Instantiate a new Quiz object
453-
454-
// 2. Check that the averageDifficulty() method takes no arguments
374+
// Instantiate a new Quiz object
375+
const quiz = new Quiz([], "test", 60);
455376

377+
// Check that the averageDifficulty() method takes no arguments
378+
expect(quiz.averageDifficulty.length).toEqual(0);
456379
});
457380

458381
it("should return the average difficulty of the questions in the quiz", () => {
@@ -490,14 +413,13 @@ describe("Quiz", () => {
490413
},
491414
];
492415

493-
// YOUR CODE HERE:
494-
//
495-
// 1. Instantiate a new Quiz object with the test questions
496-
497-
// 2. Check that the averageDifficulty() method returns the correct average when called
416+
// Instantiate a new Quiz object with the test questions
417+
const quiz = new Quiz(questions, "test", 60);
498418

419+
// Check that the averageDifficulty() method returns the correct average when called
420+
expect(quiz.averageDifficulty()).toEqual(1.8);
499421
});
500-
});
422+
});
501423
});
502424

503425

0 commit comments

Comments
 (0)