Advertisement
teofarov13

Untitled

Oct 21st, 2023
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { assert } from 'chai';
  2. import { isProductAvailable, canAffordProduct, getRecommendedProducts } from './onlineStore';
  3.  
  4. describe('Online Store test suit', () => {
  5.  
  6.     describe('isProductAvailable', () => {
  7.         it('throws error if product is not a string or stockQuantity is not a number', () => {
  8.             assert.throw(() => isProductAvailable(123, 'stock'), Error, 'Invalid input.');
  9.             assert.throw(() => isProductAvailable('product', 'stock'), Error, 'Invalid input.');
  10.         });
  11.  
  12.         it('returns out of stock message if stockQuantity is less than or equal to 0', () => {
  13.             assert.equal(isProductAvailable('product', 0), 'Sorry, product is currently out of stock.');
  14.             assert.equal(isProductAvailable('product', -1), 'Sorry, product is currently out of stock.');
  15.         });
  16.  
  17.         it('returns available for purchase message if stockQuantity is greater than 0', () => {
  18.             assert.equal(isProductAvailable('product', 1), 'Great! product is available for purchase.');
  19.             assert.equal(isProductAvailable('product', 10), 'Great! product is available for purchase.');
  20.         });
  21.     });
  22.  
  23.     describe('canAffordProduct', () => {
  24.         it('throws error if productPrice is not a number or accountBalance is not a number', () => {
  25.             assert.throw(() => canAffordProduct('price', 100), Error, 'Invalid input.');
  26.             assert.throw(() => canAffordProduct(100, 'balance'), Error, 'Invalid input.');
  27.         });
  28.  
  29.         it('returns insufficient funds message if accountBalance is less than productPrice', () => {
  30.             assert.equal(canAffordProduct(100, 50), "You don't have sufficient funds to buy this product.");
  31.             assert.equal(canAffordProduct(1000, 500), "You don't have sufficient funds to buy this product.");
  32.         });
  33.  
  34.         it('returns remaining balance message if accountBalance is greater than or equal to productPrice', () => {
  35.             assert.equal(canAffordProduct(100, 150), 'Product purchased. Your remaining balance is $50.');
  36.             assert.equal(canAffordProduct(1000, 1500), 'Product purchased. Your remaining balance is $500.');
  37.         });
  38.     });
  39.  
  40.     describe('getRecommendedProducts', () => {
  41.         it('throws error if productList is not an array or category is not a string', () => {
  42.             assert.throw(() => getRecommendedProducts('productList', 'category'), Error, 'Invalid input.');
  43.             assert.throw(() => getRecommendedProducts(['product'], 123), Error, 'Invalid input.');
  44.         });
  45.  
  46.         it('returns no recommended products message if there are no recommended products in the category', () => {
  47.             const productList = [
  48.                 {name: 'product1', category: 'category1'},
  49.                 {name: 'product2', category: 'category2'},
  50.                 {name: 'product3', category: 'category3'}
  51.             ];
  52.             assert.equal(getRecommendedProducts(productList, 'category4'), 'Sorry, we currently have no recommended products in the category4 category.');
  53.         });
  54.  
  55.         it('returns recommended products message if there are recommended products in the category', () => {
  56.             const productList = [
  57.                 {name: 'product1', category: 'category1'},
  58.                 {name: 'product2', category: 'category2'},
  59.                 {name: 'product3', category: 'category1'}
  60.             ];
  61.             assert.equal(getRecommendedProducts(productList, 'category1'), 'Recommended products in the category1 category: product1, product3');
  62.             assert.equal(getRecommendedProducts(productList, 'category2'), 'Recommended products in the category2 category: product2');
  63.         });
  64.     });
  65. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement