mr_anastasov

cream.spec.js

May 24th, 2023
1,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Product } from './product.js';
  2. import { Scent } from './scent.js';
  3.  
  4. export class Cream extends Product {
  5.  
  6.     #scent;
  7.  
  8.     static #minNameLength = 3;
  9.     static #maxNameLength = 15;
  10.     static #minBrandLength = 3;
  11.     static #maxBrandLength = 15;
  12.  
  13.     /**
  14.   * @param {string} name
  15.   * @param {string} brand
  16.   * @param {number} price
  17.   * @param {Gender} gender
  18.   * @param {number} milliliters
  19.   * @param {Usage} usage
  20.   */
  21.  
  22.     constructor(name, brand, price, gender, scent) {
  23.       super(name, brand, price, gender);
  24.       this.validateScent(scent);
  25.       this.#scent = scent;
  26.     }
  27.  
  28.     validateName(value) {
  29.       if (!value) {
  30.         throw new Error('Invalid name!');
  31.       }
  32.  
  33.       if (value.length < Cream.#minNameLength || value.length > Cream.#maxNameLength) {
  34.         throw new Error(`Product name length must be between ${Cream.#minNameLength} and ${Cream.#maxNameLength}`);
  35.       }
  36.     }
  37.  
  38.     validateBrand(value) {
  39.       if (!value) {
  40.         throw new Error('Invalid brand!');
  41.       }
  42.  
  43.       if (value.length < Cream.#minBrandLength || value.length > Cream.#maxBrandLength) {
  44.         throw new Error(`Product brand length must be between ${Cream.minBrandLength} and ${Cream.maxBrandLength}`);
  45.       }
  46.     }
  47.  
  48.     get scent() {
  49.       return this.#scent;
  50.     }
  51.  
  52.     validateScent(value) {
  53.       if (!Scent.hasOwnProperty(value)) {
  54.       // if (!Object.keys(Scent).some(key => Scent[key] === value))
  55.         throw new Error('You should use rose, lavender or vanilla.');
  56.       }
  57.     }
  58.  
  59.     additionalInfo() {
  60.       return ` #Scent: ${Scent[this.#scent]}`;
  61.     }
  62. }
  63.  
  64.  
Tags: #test
Advertisement
Add Comment
Please, Sign In to add comment