moy-fp
Version:
A functional programming library.
113 lines (98 loc) • 2.08 kB
JavaScript
import Maybe from '../../../src/Functor/Maybe/index'
/**
* you should not use this similar funciton to get value anywhere in your application
* if you must need the inner value, you may need Comonad
* here is just simple for test
*/
const inspect = x => x.value === null || x.value === undefined ?
'Nothing' :
`Just(${x.value})`
describe('test Maybe', () => {
test('Just', () => {
expect(
inspect(Maybe(12))
).toBe('Just(12)')
})
test('Nothing', () => {
expect(
inspect(Maybe(null))
).toBe('Nothing')
})
})
describe('test of', () => {
test('Just', () => {
expect(
inspect(Maybe.of(12))
).toBe('Just(12)')
})
test('Nothing', () => {
expect(
inspect(Maybe.of(null))
).toBe('Nothing')
})
})
describe('test map', () => {
test('Just', () => {
expect(
inspect(Maybe.of(12).map(x => x + 1))
).toBe('Just(13)')
})
test('Nothing', () => {
expect(
inspect(Maybe.of(null).map(x => x + 1))
).toBe('Nothing')
})
})
describe('test join', () => {
test('Just', () => {
expect(
inspect(Maybe.of(
Maybe.of(12)
).join())
).toBe('Just(12)')
})
test('Nothing', () => {
expect(
inspect(Maybe.of(null).join())
).toBe('Nothing')
})
})
describe('test chain', () => {
test('Just', () => {
expect(
inspect(Maybe.of(12).chain(
x => Maybe.of(x + 1)
))
).toBe('Just(13)')
})
test('Nothing', () => {
expect(
inspect(Maybe.of(null).chain(
x => Maybe.of(x + 1)
))
).toBe('Nothing')
})
})
describe('test ap', () => {
test('Just', () => {
expect(
inspect(Maybe.of(x => x + 1).ap(
Maybe.of(12)
))
).toBe('Just(13)')
})
test('Nothing', () => {
expect(
inspect(Maybe.of(null).ap(
Maybe.of(12)
))
).toBe('Nothing')
})
})
test('test Object.prototype.toString.call',() => {
expect(
Object.prototype.toString.call(
Maybe.of(12)
)
).toBe('[object Maybe]')
})