moy-fp
Version:
A functional programming library.
116 lines (103 loc) • 2.4 kB
JavaScript
import Task from '../../../src/Functor/Task/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 => new Promise(
(resolve, reject) => x.fork(
x => reject(`Task.reject(${typeof x === 'function' ? 'x => x + 1' : x})`),
x => resolve(`Task.resolve(${typeof x === 'function' ? 'x => x + 1' : x})`),
)
)
describe('test Task', () => {
test('reject', () => {
expect(
inspect(Task(
(reject, resolve) => reject(12)
))
).rejects.toBe('Task.reject(12)')
})
test('resolve', () => {
expect(
inspect(Task.of(12))
).resolves.toBe('Task.resolve(12)')
})
})
describe('test map', () => {
test('reject', () => {
expect(
inspect(Task(
(reject, resolve) => reject(12)).map(
x => x + 1
)
)
).rejects.toBe('Task.reject(12)')
})
test('resolve', () => {
expect(
inspect(Task.of(12).map(x => x + 1))
).resolves.toBe('Task.resolve(13)')
})
})
describe('test join', () => {
test('reject', () => {
expect(
inspect(Task(
(reject, resolve) => resolve(
Task(
(rej, res) => rej(12)
)
)
).join())
).rejects.toBe('Task.reject(12)')
})
test('resolve', () => {
expect(
inspect(Task.of(Task.of(12)).join())
).resolves.toBe('Task.resolve(12)')
})
})
describe('test chain', () => {
test('reject', () => {
expect(
inspect(Task(
(reject, resolve) => reject(12)
).chain(
x => Task.of(x + 1)
))
).rejects.toBe('Task.reject(12)')
})
test('resolve', () => {
expect(
inspect(Task.of(12).chain(
x => Task.of(x + 1)
))
).resolves.toBe('Task.resolve(13)')
})
})
describe('test ap', () => {
test('reject', () => {
expect(
inspect(Task(
(reject, resolve) => reject(x => x + 1)
).ap(
Task.of(12)
))
).rejects.toBe('Task.reject(x => x + 1)')
})
test('resolve', () => {
expect(
inspect(Task.of(x => x + 1).ap(
Task.of(12)
))
).resolves.toBe('Task.resolve(13)')
})
})
test('test Object.prototype.toString.call',() => {
expect(
Object.prototype.toString.call(
Task.of(12)
)
).toBe('[object Task]')
})