@@ -254,70 +254,32 @@ describe("Quiz", () => {
254
254
} ) ;
255
255
} ) ;
256
256
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" , ( ) => {
268
259
// Instantiate a new Quiz object
269
260
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 ( ) ;
298
263
299
264
} ) ;
300
265
301
266
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" ) ;
305
271
306
- // 2. Check if the .filterQuestionsByDifficulty is a function
307
272
} ) ;
308
273
309
274
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 ) ;
315
279
316
280
} ) ;
317
281
318
282
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
-
321
283
// Array with questions to be used in the test and passed to the Quiz constructor
322
284
const questions = [
323
285
{
@@ -346,19 +308,17 @@ describe("Quiz", () => {
346
308
} ,
347
309
] ;
348
310
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 ) ;
352
313
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 ) ;
354
316
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 ] ] ) ;
356
319
} ) ;
357
320
358
321
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
-
362
322
// Array with questions to be used in the test and passed to the Quiz constructor
363
323
const questions = [
364
324
{
@@ -381,78 +341,41 @@ describe("Quiz", () => {
381
341
} ,
382
342
] ;
383
343
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 ) ;
389
346
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" ) ;
391
349
350
+ // Check if the questions array is still the same as the original (it hasn't been filtered)
351
+ expect ( quiz . questions ) . toEqual ( questions ) ;
392
352
} ) ;
393
353
} ) ;
394
354
395
355
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" , ( ) => {
407
358
// Instantiate a new Quiz object
408
359
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 ( ) ;
437
362
438
363
} ) ;
439
364
440
365
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 ) ;
446
368
369
+ // Check if the .averageDifficulty is a function
370
+ expect ( typeof quiz . averageDifficulty ) . toBe ( "function" ) ;
447
371
} ) ;
448
372
449
373
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 ) ;
455
376
377
+ // Check that the averageDifficulty() method takes no arguments
378
+ expect ( quiz . averageDifficulty . length ) . toEqual ( 0 ) ;
456
379
} ) ;
457
380
458
381
it ( "should return the average difficulty of the questions in the quiz" , ( ) => {
@@ -490,14 +413,13 @@ describe("Quiz", () => {
490
413
} ,
491
414
] ;
492
415
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 ) ;
498
418
419
+ // Check that the averageDifficulty() method returns the correct average when called
420
+ expect ( quiz . averageDifficulty ( ) ) . toEqual ( 1.8 ) ;
499
421
} ) ;
500
- } ) ;
422
+ } ) ;
501
423
} ) ;
502
424
503
425
0 commit comments