project: testing (#15)

This commit is contained in:
Smigz 2024-02-16 16:07:49 -05:00 committed by GitHub
parent 6f8a4ec879
commit d899a773bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 9544 additions and 0 deletions

3
testing/src/index.js Normal file
View file

@ -0,0 +1,3 @@
import { calculator } from './odinTests';
console.log(calculator(1, 2, '+'));

42
testing/src/odinTests.js Normal file
View file

@ -0,0 +1,42 @@
const capitalize = (thing) => thing.charAt(0).toUpperCase() + thing.slice(1);
const reverseString = (thing) => thing.split('').reverse().join('');
const calculator = (a, b, operation) => {
switch (operation) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b === 0) {
return 'error';
}
return a / b;
}
};
const ceaserCipher = (thing) => {
let cipher = thing.split('').map((item) => {
let charCode = item.charCodeAt(0);
return String.fromCharCode(charCode + 3);
});
return cipher.join('');
};
const analyzeArray = (arr) => {
let min = Math.min(...arr);
let max = Math.max(...arr);
let length = arr.length;
let avg = arr.reduce((sum, num) => sum + num, 0) / length;
return {
average: avg,
min: min,
max: max,
length: length,
};
};
export { analyzeArray, ceaserCipher, calculator, capitalize, reverseString };

View file

@ -0,0 +1,30 @@
import {
analyzeArray,
ceaserCipher,
calculator,
reverseString,
capitalize,
} from './odinTests';
test('make sure mike = Mike', () => {
expect(capitalize('mike')).toBe('Mike');
});
test('make sure mike = ekim', () => {
expect(reverseString('mike')).toBe('ekim');
});
test('Make sure calculator works', () => {
expect(calculator(1, 2, '+')).toBe(3);
expect(calculator(1, 2, '-')).toBe(-1);
expect(calculator(1, 2, '*')).toBe(2);
expect(calculator(1, 0, '/')).toBe('error');
expect(calculator(1, 2, '/')).toBe(0.5);
expect(ceaserCipher('bob')).toBe('ere');
expect(analyzeArray([1, 2, 3, 4])).toEqual({
average: 2.5,
min: 1,
max: 4,
length: 4,
});
});

17
testing/src/style.css Normal file
View file

@ -0,0 +1,17 @@
input {
outline: none;
}
input:user-invalid {
outline: none;
border: 3px solid red;
}
input:valid {
outline: none;
border: 3px solid green;
}
span {
margin-left: 10px;
color: red;
}

5
testing/src/sum.js Normal file
View file

@ -0,0 +1,5 @@
function sum(a, b) {
return a + b;
}
module.exports = sum;

5
testing/src/sum.test.js Normal file
View file

@ -0,0 +1,5 @@
const sum = require('./sum');
test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});