Skip to content

Instantly share code, notes, and snippets.

@softy-dev
Last active October 17, 2024 01:28
Show Gist options
  • Select an option

  • Save softy-dev/af5ba9ab290b5362a1b4bf36c8ec1c65 to your computer and use it in GitHub Desktop.

Select an option

Save softy-dev/af5ba9ab290b5362a1b4bf36c8ec1c65 to your computer and use it in GitHub Desktop.
Testing a few function with Jest
function capitalize(string) {
if (!string.length) {
return 'Invalid input.';
}
return string.at(0).toUpperCase() + string.slice(1);
}
function reverseString(string) {
if (!string.length) {
return 'Invalid input.';
}
return string.split('').reverse().join('');
}
const calculator = {
validateArguments: (a, b) => {
if (!Number.isInteger(a) || !Number.isInteger(b)) {
return 'Invalid input.';
}
return null;
},
add: (a, b) => {
const error = calculator.validateArguments(a, b);
return error ? error : a + b;
},
subtract: (a, b) => {
const error = calculator.validateArguments(a, b);
return error ? error : a - b;
},
divide: (a, b) => {
const error = calculator.validateArguments(a, b);
if (error) return error;
return b === 0 ? "Can't divide by zero." : a / b;
},
multiply: (a, b) => {
const error = calculator.validateArguments(a, b);
return error ? error : a * b;
},
};
function caesarCipher(string, shiftFactor) {
if (!string.length) {
return 'Invalid input.';
}
const alphabet = [...'abcdefghijklmnopqrstuvwxyz'];
const upperCaseAlphabet = [...'ABCDEFGHIJKLMNOPQRSTUVWXYZ'];
function replaceLetter(letter, alphabet) {
let index = (alphabet.indexOf(letter) + shiftFactor) % alphabet.length;
if (index < 0) {
index += alphabet.length;
}
return alphabet[index];
}
return [...string]
.map((letter) => {
if (alphabet.includes(letter)) {
return replaceLetter(letter, alphabet);
} else if (upperCaseAlphabet.includes(letter)) {
return replaceLetter(letter, upperCaseAlphabet);
} else {
return letter;
}
})
.join('');
}
function analyzeArray(array) {
if (
!Array.isArray(array) ||
!array.length ||
!array.every((element) => Number.isInteger(element))
) {
return 'Invalid input.';
}
const average = array.reduce((total, num) => total + num) / array.length;
const min = Math.min(...array);
const max = Math.max(...array);
const length = array.length;
return { average, min, max, length };
}
export { capitalize, reverseString, calculator, caesarCipher, analyzeArray };
import {
capitalize,
reverseString,
calculator,
caesarCipher,
analyzeArray,
} from './index';
describe('capitalize function', () => {
it('Return string with capitalized first letter', () => {
expect(capitalize('test')).toBe('Test');
});
it(`Return "Invalid input" for empty string argument`, () => {
expect(capitalize('')).toBe('Invalid input.');
});
it(`Return "Invalid input" for non-string argument`, () => {
expect(capitalize(0)).toBe('Invalid input.');
});
});
describe('reverseString function', () => {
it('Return string with letters in reversed order', () => {
expect(reverseString('test')).toBe('tset');
});
it(`Return "Invalid input" for empty string argument`, () => {
expect(reverseString('')).toBe('Invalid input.');
});
it(`Return "Invalid input" for non-string argument`, () => {
expect(reverseString(0)).toBe('Invalid input.');
});
});
describe('calculator', () => {
describe('add function', () => {
it('Return the sum of two integers', () => {
expect(calculator.add(1, 1)).toBe(2);
});
it(`Return "Invalid input" for non-integer arguments`, () => {
expect(calculator.add(1)).toBe('Invalid input.');
expect(calculator.add()).toBe('Invalid input.');
expect(calculator.add(1, 'test')).toBe('Invalid input.');
});
});
describe('subtract function', () => {
it('Return the difference of two integers', () => {
expect(calculator.subtract(1, 1)).toBe(0);
});
it(`Return "Invalid input" for non-integer arguments`, () => {
expect(calculator.subtract(1)).toBe('Invalid input.');
expect(calculator.subtract()).toBe('Invalid input.');
expect(calculator.subtract(1, 'test')).toBe('Invalid input.');
});
});
describe('divide function', () => {
it('Return the quotient of two integers', () => {
expect(calculator.divide(2, 2)).toBe(1);
});
it(`Return "Can't divide by zero" if the second argument is 0`, () => {
expect(calculator.divide(2, 0)).toBe("Can't divide by zero.");
});
it(`Return "Invalid input" for non-integer arguments`, () => {
expect(calculator.divide(1)).toBe('Invalid input.');
expect(calculator.divide()).toBe('Invalid input.');
expect(calculator.divide(1, 'test')).toBe('Invalid input.');
});
});
describe('multiply function', () => {
it('Return the product of two integers', () => {
expect(calculator.multiply(2, 2)).toBe(4);
});
it(`Return "Invalid input" for non-integer arguments`, () => {
expect(calculator.multiply(1)).toBe('Invalid input.');
expect(calculator.multiply()).toBe('Invalid input.');
expect(calculator.multiply(1, 'test')).toBe('Invalid input.');
});
});
});
describe('caesarCipher function', () => {
it('Return string with each letter changed by a shift factor according to the alphabet.', () => {
expect(caesarCipher('test', 2)).toBe('vguv');
});
it('Handles negative shift factor.', () => {
expect(caesarCipher('test', -2)).toBe('rcqr');
});
it('Shifted letters wrap from z to a.', () => {
expect(caesarCipher('xyz', 3)).toBe('abc');
});
it("Keep the original string's lettercase.", () => {
expect(caesarCipher('HeLLo', 3)).toBe('KhOOr');
});
it("Keep the original string's non-alphabetical characters.", () => {
expect(caesarCipher('Hello, World!', 3)).toBe('Khoor, Zruog!');
});
it(`Return "Invalid input" for empty string argument`, () => {
expect(caesarCipher('')).toBe('Invalid input.');
});
it(`Return "Invalid input" for non-string argument`, () => {
expect(caesarCipher(0)).toBe('Invalid input.');
});
});
describe('analyzeArray function', () => {
it('Return object with the following properties of the array: average, min, max, and length.', () => {
expect(analyzeArray([1, 8, 3, 4, 2, 6])).toEqual({
average: 4,
min: 1,
max: 8,
length: 6,
});
});
it("Return 'Invalid input' for array argument with a value that's not an integer.", () => {
expect(analyzeArray([1, 2, 'test', 3])).toBe('Invalid input.');
});
it("Return 'Invalid input' for empty array argument.", () => {
expect(analyzeArray([])).toBe('Invalid input.');
});
it("Return 'Invalid input' for non-array argument.", () => {
expect(analyzeArray(2)).toBe('Invalid input.');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment