Skip to content

Instantly share code, notes, and snippets.

@ikotse-code
Last active December 10, 2025 15:11
Show Gist options
  • Select an option

  • Save ikotse-code/34d6eb3f60008904d1b47cb1433c09f3 to your computer and use it in GitHub Desktop.

Select an option

Save ikotse-code/34d6eb3f60008904d1b47cb1433c09f3 to your computer and use it in GitHub Desktop.
5
import { HomeWork } from '../src/homework'
import { multiplication, calculatePercentage, isEven, stringComparison, isPositive } from '../src/homework'
describe('HomeWork Class testing', () => {
let operation: HomeWork;
test('should multiply two numbers correctly', () => {
operation = new HomeWork();
expect(operation.multiplication(2, 3)).toBe(6);
})
test('should multiply two numbers correctly', () => {
operation = new HomeWork();
expect(operation.multiplication(-3, 3)).not.toBe(9);
})
test('should calculate what percentage one number is of another correctly', () => {
operation = new HomeWork();
expect(operation.calculatePercentage(5, 10)).toBe(50);
})
test('should calculate what percentage one number is of another correctly', () => {
operation = new HomeWork();
expect(operation.calculatePercentage(5, 10)).not.toBe(10);
})
test('should return true if number is even correctly', () => {
operation = new HomeWork();
expect(operation.isEven(5)).toBeFalsy();
})
test('should return true if number is even correctly', () => {
operation = new HomeWork();
expect(operation.isEven(10)).toBeTruthy();
})
test('should return true if two strings are exactly equal correctly', () => {
operation = new HomeWork();
expect(operation.stringComparison("Test%", "Test%")).not.toBeFalsy();
})
test('should return true if two strings are exactly equal correctly', () => {
operation = new HomeWork();
expect(operation.stringComparison("ABC_%1", "abc_%1")).toBeFalsy();
})
test('should return true if number is greater than zero correctly', () => {
operation = new HomeWork();
expect(operation.isPositive(5)).not.toBeFalsy();
})
test('should return true if number is greater than zero correctly', () => {
operation = new HomeWork();
expect(operation.isPositive(0)).toBeFalsy();
})
})
test('multiplies two integer numbers: basic', () => {
const expectedResult: number = -4;
const actualResult: number = multiplication(2, -2);
expect(expectedResult).toBe(actualResult);
})
test('multiplies two integer numbers', () => {
// toBeCloseTo to handle floating-point precision issues
expect(multiplication(0.1, 0.2)).toBeCloseTo(0.02);
})
test('calculates what percentage one number is of another', () => {
expect(calculatePercentage(100, 100)).toBe(100);
})
test('calculates what percentage one number is of another', () => {
expect(() => calculatePercentage(100, 0)).toThrow("Division by zero is not allowed");
})
test('returns true if number is even', () => {
expect(isEven(-2)).toBeTruthy();
})
test('returns true if two strings are exactly equal', () => {
expect(stringComparison("abc z", "abc z")).toBeFalsy();
})
test('returns true if number is greater than zero', () => {
expect(isPositive(-2)).not.toBeTruthy();
})
export class HomeWork {
multiplication(a: number, b: number): number {
return a * b;
}
calculatePercentage(a: number, b: number) {
if (b === 0) {
throw new Error("Division by zero is not allowed");
}
return a * 100 / b;
}
isEven(a: number): boolean {
return a % 2 === 0;
}
stringComparison(str1: string, str2: string): boolean {
return str1 === str2;
}
isPositive(a: number): boolean {
return a > 0;
}
}
export function multiplication(a: number, b: number): number {
return a * b;
}
export function calculatePercentage(a: number, b: number) {
if (b === 0) {
throw new Error("Division by zero is not allowed");
}
return a * 100 / b;
}
export function isEven(a: number): boolean {
return a % 2 === 0;
}
export function stringComparison(str1: string, str2: string): boolean {
return str1 === str2;
}
export function isPositive(a: number): boolean {
return a > 0;
}
@pogodins
Copy link

Great to see that you handled "Division by zero is not allowed" situation and used various Jest matchers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment