Skip to content

upaulo/js-unit-tests

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JS Unit Tests Project

License: ISC

About

This is my 8th project during my journey at Trybe!

Repository Structure

  • The src folder contains all functions

  • The tests folder contains all tests

Implemented Functionalities

Function Average

Implemented the function average.js and its tests average.spec.js
  • The function average.js receives a array and returns the average of the values received. If the function receives any non-numeric value or an empty array, the value undefined is returned, and all results are rounded to integer values.

  • The tests average.spec.js ensure that:

    • The function returns the average of its values when receiving an array of numbers
    • The function returns undefined when receiving an array containing non-numeric values
    • The function returns undefined when receiving an empty array

Function Numbers

Implemented the function numbers.js and its tests numbers.spec.js
  • The function numbers receives a array and returns true if all parameters are of type number and false otherwise. If the function receives an empty array, the value undefined is returned

  • The tests numbers.spec.js ensure that:

    • The function returns true when receiving an array containing only numeric values
    • The function returns false when receiving an array containing non-numeric values
    • The function returns undefined when receiving an empty array

Function Circle

Implemented the function circle.js and its tests circle.spec.js
  • The circle function takes the radius of a circle and returns an object containing its information: Radius, Area, and Circumference. If a radius is not specified, the function returns undefined.

  • The tests circle.spec.js ensure that:

    • The function returns an object
    • Within the return object, it contains the radius, area, and circumference
    • The function returns undefined if the parameter is not a number or is not passed

Function Create Student

Implemented the function createStudent.js and its tests createStudent.spec.js
  • The function createStudent receives a name as a parameter and returns an object containing two keys:
  {
    name: 'containing the name passed as a parameter',
    feedback: 'containing a function that returns the phrase "Eita pessoa boa!" when called'
  }
  • The tests createStudent.spec.js ensure that:

    • The function returns an object
    • Within the return object, it contains the name and feedback
    • The function returns undefined if the parameter is not passed

Function Product Details

Implemented the function productDetails.js and its tests productDetails.spec.js
  • The productDetails function receives a string representing the name of a product and returns an array containing all the objects found in data where the name matches the searched name:
productDetails('camisa');

Returns:

[
  {
    name: 'Camisa Gola V',
    productId: 456,
  },
  {
    name: 'Camisa Regata',
    productId: 112,
  }
]
  • The tests productDetails.spec.js ensure that:

    • The function returns an array
    • That the items returned within the array are objects
    • That the search is case insensitive
    • The function returns null if the parameter is not passed

Function Object Playground

Implemented the function objPlayground.js and its tests objPlayground.spec.js
  • The function calculator receives two integers as parameters and returns the following object:

    • The results of the divisions are always rounded down
{
  sum: // returns the result of the sum of the two numbers,
  mult: // returns the result of the multiplication of the two numbers,
  div: // returns the result of the division of the two numbers,
  sub: // returns the result of the subtraction of the two numbers
}

Behavior:

calculator(36, 12); // { sum: 48, mult: 432, div: 3, sub: 24 }
  • The function arrayGenerator converts objects into arrays of keys, values, or both. It should receive two parameters:

    • The first parameter should be a string indicating the type of conversion
    • The second parameter should be an object similar to what is returned by the calculator function

Behavior:

arrayGenerator('keys', { sum: 3, mult: 2, div: 1, sub: 0 })
// [ 'sum', 'mult', 'div', 'sub' ]
arrayGenerator('values', { sum: 3, mult: 2, div: 1, sub: 0 })
// [ 3, 2, 1, 0 ]
arrayGenerator('entries', { sum: 3, mult: 2, div: 1, sub: 0 })
// [ [ 'sum', 3 ], [ 'mult', 2 ], [ 'div', 1 ], [ 'sub', 0 ] ]
  • The tests in objPlayground.spec.js ensure that:

    • The function calculator returns the correct calculations
    • arrayGenerator(keys, object) returns an array with the keys of the object passed as a parameter
    • arrayGenerator(values, object) returns an array with the values of the object passed as a parameter
    • arrayGenerator(entries, object) returns an array with the entries of the object passed as a parameter

Function My Counter

Implemented the function myCounter.js and its tests myCounter.spec.js
  • The function myCounter has two nested loops that insert values into an array until its stopping condition. Useful when you need to iterate over a list or matrix in two dimensions or in algorithms that involve multiple iteration, such as depth-first or breadth-first search in a tree, this function can be adapted to generate sequences of iteration steps for analysis and debugging

  • The tests in myCounter.spec.js ensure that:

    • myCounter return the expected data according to what is implemented

Function Get Character

Implemented the function getCharacter.js and its tests getCharacter.spec.js
  • The getCharacter function receives a string parameter representing the name of a fictional character and returns an object containing the character's name, class, and phrases

Example:

getCharacter('Arya');

Returns:

{
  name: 'Arya Stark',
  class: 'Rogue',
  phrases: ['Not today', 'A girl has no name.']
}
  • The tests in getCharacter.spec.js ensure that:

    • The function returns undefined if the parameter is not passed
    • The search is case insensitive
    • When receiving a valid string, meaning a character that exists in the database, it returns the correct data
    • If passed an invalid string, meaning a character that does not exist in the database, it returns undefined

Function Create Menu

Implemented the function restaurant.js and its tests restaurant.spec.js
  • The logic consists of being able to register a menu, and the system should provide an object that allows:

    • Reading the registered menu
    • Placing orders
    • Checking what was ordered
    • Summing up the bill

The menu should be registered by separating the foods (food) from the drinks (drink) and should be passed as an object, following the example below:

  {
    food: {productName: value, productName: value},
    drinks: {productName: value, productName: value},
  }

Obs: the value must be of type number, and decimal places should be separated by a dot, not a comma

Example of usage:

const menuData = {
  food: { coxinha: 3.9, sandwich: 9.9 },
  drink: { water: 3.9, beer: 6.9 },
};

const menu = createMenu(menuData);

menu.order('coxinha');
menu.order('cerveja');

console.log(menu.consumption); // Should return the array ['coxinha', 'beer']
console.log(menu.pay()); // Should return the value 11.88 (10.80 + 10% tax)
  • The tests in restaurant.spec.js ensure that:

    • createMenu() returns an object that has the key fetchMenu, which has a function as its value
    • The menu passed to the createMenu() function is identical to the menu retrieved by the function 'returnedObject.fetchMenu()'
    • returnedObject.consumption, after the menu is created, returns an empty array
    • returnedObject.order() if the value passed as a parameter exists in the menu, it is added to the key consumption, if it does not exist, display the message "Item unavailable" and do not change the consumption key
    • returnedObject.consumption correctly stores the items ordered through the order function
    • When calling returnedObject.pay(), it returns the sum of the prices of everything that was ordered (which is stored in consumption) plus 10% tax

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published