This repository has been archived on 2023-05-29. You can view files and clone it, but cannot push or open issues or pull requests.
fp-ts-training/src/exo1/exo1.test.ts

80 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-07-03 19:01:45 +03:00
import * as Either from 'fp-ts/lib/Either';
import * as Option from 'fp-ts/lib/Option';
import {
divide,
DivisionByZero,
safeDivide,
safeDivideWithError,
asyncDivide,
asyncSafeDivideWithError,
} from './exo1';
describe('exo1', () => {
describe('divide', () => {
it('should return the result of dividing two numbers', () => {
expect(divide(25, 5)).toEqual(5);
});
it('should return Infinity or -Infinity if the denominator is zero', () => {
2020-07-03 19:01:45 +03:00
expect(divide(25, 0)).toBe(Infinity);
expect(divide(-25, 0)).toBe(-Infinity);
2020-07-03 19:01:45 +03:00
});
});
describe('safeDivide', () => {
it('should return the result of dividing two numbers', () => {
expect(safeDivide(25, 5)).toStrictEqual(Option.some(5));
});
it('should return Option.none if the denominator is zero', () => {
expect(safeDivide(25, 0)).toStrictEqual(Option.none);
expect(safeDivide(-25, 0)).toStrictEqual(Option.none);
2020-07-03 19:01:45 +03:00
});
});
describe('safeDivideWithError', () => {
it('should return the result of dividing two numbers', () => {
expect(safeDivideWithError(25, 5)).toStrictEqual(Either.right(5));
});
it('should return Either.left(DivisionByZero) if the denominator is zero', () => {
expect(safeDivideWithError(25, 0)).toStrictEqual(
Either.left(DivisionByZero),
);
expect(safeDivideWithError(-25, 0)).toStrictEqual(
Either.left(DivisionByZero),
);
2020-07-03 19:01:45 +03:00
});
});
describe('asyncDivide', () => {
it('should eventually return the result of dividing two numbers', async () => {
const result = await asyncDivide(25, 5);
expect(result).toEqual(5);
});
it('should eventually return Infinity if the denominator is zero', async () => {
await expect(asyncDivide(25, 0)).rejects.toThrow();
await expect(asyncDivide(-25, 0)).rejects.toThrow();
2020-07-03 19:01:45 +03:00
});
});
describe('asyncSafeDivideWithError', () => {
it('should eventually return the result of dividing two numbers', async () => {
const result = await asyncSafeDivideWithError(25, 5)();
expect(result).toStrictEqual(Either.right(5));
});
it('should eventually return Either.left(DivisionByZero) if the denominator is zero', async () => {
const resultA = await asyncSafeDivideWithError(25, 0)();
const resultB = await asyncSafeDivideWithError(-25, 0)();
2020-07-03 19:01:45 +03:00
expect(resultA).toStrictEqual(Either.left(DivisionByZero));
expect(resultB).toStrictEqual(Either.left(DivisionByZero));
2020-07-03 19:01:45 +03:00
});
});
});