Advertisement
Grossos

motorcycle-rider.test.js

Feb 10th, 2024
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // JS Advanced Exam
  2. // Problem 3. Unit Testing
  3. // Your Task
  4. // Using Mocha and Chai write JS Unit Tests to test a variable named MotorcycleRider, which represents an object. You may use the following code as a template:
  5. // describe("Tests …", function() {
  6. //     describe("TODO …", function() {
  7. //         it("TODO …", function() {
  8. //             // TODO: …
  9. //         });
  10. //      });
  11. //      // TODO: …
  12. // });
  13. // The object that should have the following functionality:            
  14. // licenseRestriction (category) - A function that accepts one parameter: string.
  15. // If the category is "AM" return the string:
  16. // "Mopeds with a maximum design speed of 45 km. per hour, engine volume is no more than 50 cubic centimeters, and the minimum age is 16."
  17. // If the category is "A1" return the string:
  18. // "Motorcycles with engine volume is no more than 125 cubic centimeters, maximum power of 11KW. and the minimum age is 16."
  19. // If the category is "A2" return the string:
  20. // "Motorcycles with maximum power of 35KW. and the minimum age is 18."
  21. // If the category is "A" return the string:
  22. // "No motorcycle restrictions, and the minimum age is 24."
  23. // If the value of the string type is different from "AM,A1,A2,A", throw an error:
  24. //  " Invalid Information!"
  25. // motorcycleShowroom (engineVolume, maximumEngineVolume) - A function that accepts an array and number. The engineVolume array will store the engine volume of a motorcycles in cubic centimeters (["125", "250", "600"…]), you need to check every element in the array and if its less or equal to maximumEngineVolume.
  26. // You must add an element (engineVolume) if maximumEngineVolume is less or equal to engineVolume from the array in to new availableBikes array.
  27. // Finally, return the array length in following string:
  28. // "There are ${availableBikes.length} available motorcycles matching your criteria!"
  29. // There is a need for validation for the input, an array and number may not always be valid. In case of submitted invalid parameters, throw an error "Invalid Information!":
  30. // If passed engineVolume or maximumEngineVolume parameter are not an array and number.
  31. // If the engineVolume is an empty array, and if maximumEngineVolume is less than 50.
  32. // otherSpendings (equipment, consumables, discount) - A function that accepts three parameters: array, array and boolean.
  33. // Calculate the total price you are going to pay depending on the purchased equipment and consumables:
  34. // The result must be formatted to the second digit after the decimal point.
  35. // The bike shop offers two options for equipment and consumables:
  36. // The two options for equipment are:
  37. // helmet, which costs $200
  38. // jacked, which costs $300
  39. // The two options for consumables are:
  40. // engine oil, which costs $70
  41. // oil filter, which costs $30
  42. // If the discount is true, 10% discount should be applied. Then return the following message:
  43. // "You spend $${totalPrice} for equipment and consumables with 10% discount!"
  44. // Else, return the following message:  
  45. // "You spend $${totalPrice} for equipment and consumables!"
  46. // You need to validate the input, if the equipment, consumables and discount are not a array, array and boolean an error: "Invalid information!"
  47. // JS Code
  48. // To ease you in the process, you are provided with an implementation that meets all of the specification requirements for the MotorcycleRider object:
  49. // chooseYourCar.js
  50. // const motorcycleRider = {
  51. //   licenseRestriction(category) {
  52. //     if (category === "AM") {
  53. //       return 'Mopeds with a maximum design speed of 45 km. per hour, engine volume is no more than 50 cubic centimeters, and the minimum age is 16.'
  54. //     } else if (category === "A1") {
  55. //       return 'Motorcycles with engine volume is no more than 125 cubic centimeters, maximum power of 11KW. and the minimum age is 16.'
  56. //     } else if (category === "A2") {
  57. //       return 'Motorcycles with maximum power of 35KW. and the minimum age is 18.'
  58. //     } else if (category === "A") {
  59. //       return 'No motorcycle restrictions, and the minimum age is 24.'
  60. //     } else {
  61. //       throw new Error("Invalid Information!");
  62. //     }
  63. //   },
  64. //   motorcycleShowroom(engineVolume, maximumEngineVolume) {
  65. //     if (!Array.isArray(engineVolume) || typeof maximumEngineVolume !== "number" || engineVolume.length < 1 || maximumEngineVolume < 50) {
  66. //       throw new Error("Invalid Information!");
  67. //     }
  68. //     let availableBikes = [];
  69. //     engineVolume.forEach((engine) => {
  70. //       if (engine <= maximumEngineVolume && engine >= 50) {
  71. //         availableBikes.push(engine);
  72. //       }
  73. //     });
  74. //     return `There are ${availableBikes.length} available motorcycles matching your criteria!`;
  75. //   },
  76. //   otherSpendings(equipment, consumables, discount) {
  77. //     if (
  78. //       !Array.isArray(equipment) ||
  79. //       !Array.isArray(consumables) ||
  80. //       typeof discount !== "boolean"
  81. //     ) {
  82. //       throw new Error("Invalid Information!");
  83. //     }
  84. //     let totalPrice = 0;
  85. //     equipment.forEach((element) => {
  86. //       if (element === "helmet") {
  87. //         totalPrice += 200
  88. //       } else if (element === "jacked") {
  89. //         totalPrice += 300
  90. //       }
  91. //     });
  92. //     consumables.forEach((element) => {
  93. //       if (element === "engine oil") {
  94. //         totalPrice += 70
  95. //       } else if (element === "oil filter") {
  96. //         totalPrice += 30
  97. //       }
  98. //     });
  99. //     if (discount) {
  100. //       totalPrice = totalPrice * 0.9;
  101. //       return `You spend $${totalPrice.toFixed(2)} for equipment and consumables with 10% discount!`
  102. //     } else {
  103. //       return `You spend $${totalPrice.toFixed(2)} for equipment and consumables!`
  104. //     }
  105. //   }
  106. // };
  107. //     }
  108. // }
  109. // Submission
  110. // Submit your tests inside a describe() statement, as shown above.
  111.  
  112. import { motorcycleRider } from './motorcycle-rider.js'
  113. import { expect } from 'chai';
  114.  
  115.  
  116. describe('Testing functionality', () => {
  117.  
  118.     describe('licenseRestriction(category)', () => {
  119.         it('should test licenseRestriction(AM)', () => {
  120.             expect(motorcycleRider.licenseRestriction('AM')).to.equal('Mopeds with a maximum design speed of 45 km. per hour, engine volume is no more than 50 cubic centimeters, and the minimum age is 16.');
  121.         });
  122.         it('should test licenseRestriction(A1)', () => {
  123.             expect(motorcycleRider.licenseRestriction('A1')).to.equal('Motorcycles with engine volume is no more than 125 cubic centimeters, maximum power of 11KW. and the minimum age is 16.');
  124.         });
  125.         it('should test licenseRestriction(A2)', () => {
  126.             expect(motorcycleRider.licenseRestriction('A2')).to.equal('Motorcycles with maximum power of 35KW. and the minimum age is 18.');
  127.         });
  128.         it('should test licenseRestriction(A)', () => {
  129.             expect(motorcycleRider.licenseRestriction('A')).to.equal('No motorcycle restrictions, and the minimum age is 24.');
  130.         });
  131.  
  132.         it('should throw error', () => {
  133.             expect(() => { motorcycleRider.licenseRestriction('dasd') }).to.throw();
  134.         });
  135.     });
  136.  
  137.     describe('motorcycleShowroom(engineVolume, maximumEngineVolume)', () => {
  138.  
  139.         it('should return There are ... available motorcycles matching your criteria!', () => {
  140.             expect(motorcycleRider.motorcycleShowroom(['125', '250', '600'], 600)).to.equal(`There are 3 available motorcycles matching your criteria!`)
  141.         })
  142.  
  143.         it('should return There are ... available motorcycles matching your criteria!', () => {
  144.             expect(motorcycleRider.motorcycleShowroom(['125', '250', '600'], 250)).to.equal(`There are 2 available motorcycles matching your criteria!`)
  145.         })
  146.  
  147.         it('should return There are ... available motorcycles matching your criteria!', () => {
  148.             expect(motorcycleRider.motorcycleShowroom(['125', '250', '600'], 125)).to.equal(`There are 1 available motorcycles matching your criteria!`)
  149.         })
  150.  
  151.         it('should expect engineVolume to equal maximumEngineVolume', () => {
  152.             expect(() => { motorcycleRider.motorcycleShowroom(['125'], 1) }).to.throw();
  153.         });
  154.  
  155.         it('should test arr and num params', () => {
  156.             expect(() => { motorcycleRider.motorcycleShowroom(['1'], 1) }).to.throw();
  157.         });
  158.  
  159.         it('should test if first param is empty array ', () => {
  160.             expect(() => { motorcycleRider.motorcycleShowroom([], 51) }).to.throw();
  161.         });
  162.  
  163.         it('should test if second param is under 50 ', () => {
  164.             expect(() => { motorcycleRider.motorcycleShowroom(['423'], 49) }).to.throw();
  165.         });
  166.  
  167.         it('should iff passed engineVolume or maximumEngineVolume parameter are not an array and number. ', () => {
  168.             expect(() => { motorcycleRider.motorcycleShowroom(['423'], 'string') }).to.throw();
  169.         });
  170.  
  171.         it('should iff passed engineVolume or maximumEngineVolume parameter are not an array and number. ', () => {
  172.             expect(() => { motorcycleRider.motorcycleShowroom(1, 'string') }).to.throw();
  173.         });
  174.     });
  175.  
  176.     describe('otherSpendings(equipment, consumables, discount)', () => {
  177.         it('should test without discount', () => {
  178.             expect(motorcycleRider.otherSpendings(['helmet'], ['oil filter'], false)).to.equal('You spend $230.00 for equipment and consumables!');
  179.         });
  180.  
  181.         it('should test with discount', () => {
  182.             expect(motorcycleRider.otherSpendings(['helmet'], ['oil filter'], true)).to.equal('You spend $207.00 for equipment and consumables with 10% discount!');
  183.         });
  184.  
  185.         it('should test without discount', () => {
  186.             expect(motorcycleRider.otherSpendings(['jacked'], ['engine oil'], false)).to.equal('You spend $370.00 for equipment and consumables!');
  187.         });
  188.  
  189.         it('should test with discount', () => {
  190.             expect(motorcycleRider.otherSpendings(['jacked'], ['engine oil'], true)).to.equal('You spend $333.00 for equipment and consumables with 10% discount!');
  191.         });
  192.  
  193.         it('should test if equipment is not array', () => {
  194.             expect(() => motorcycleRider.otherSpendings(123, ['oil filter'], false)).to.throw();
  195.         });
  196.         it('should test if consumables is not array', () => {
  197.             expect(() => motorcycleRider.otherSpendings(['helmet'], 123, false)).to.throw();
  198.         });
  199.         it('should test if discount is not boolean', () => {
  200.             expect(() => motorcycleRider.otherSpendings(['helmet'], ['oil filter'], 123)).to.throw();
  201.         });
  202.     })
  203. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement