Skip to content

Commit

Permalink
feat: add fromMaybe, toMaybe tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VKTRenokh committed Feb 19, 2024
1 parent 011a57d commit cbc401f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/either/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './from-throwable'
export * from './from-maybe'
export * from './to-maybe'
2 changes: 1 addition & 1 deletion src/either/utils/to-maybe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Maybe, none, of } from 'src/maybe'
import { Maybe, none, of } from '../../maybe'
import { Either } from '../either'

/**
Expand Down
31 changes: 29 additions & 2 deletions tests/either.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { E } from '../src'
import { E, M } from '../src'

const eitherExampleString = 'cannot work with 0'
const eitherExampleError = new Error(eitherExampleString)
Expand Down Expand Up @@ -135,7 +135,7 @@ describe('either.ts', () => {
)
.fold(
() => {
throw new Error('shouldnt be called')
throw new Error('should not be called')
},
(user) =>
expect(user).toMatchObject({
Expand All @@ -144,4 +144,31 @@ describe('either.ts', () => {
}),
)
})

it('fromMaybe', () => {
E.fromMaybe(M.of(42), '!').fold(
() => {
throw new Error('should not be called')
},
(v) => expect(v).toBe(42),
)

E.fromMaybe(M.none<number>(), 'maybe is none').fold(
(e) => expect(e).toBe('maybe is none'),
() => {
throw new Error('should not be called')
},
)
})

it('toMaybe', () => {
const eitherRight: E.Either<string, number> = E.right(5)
const eitherLeft: E.Either<string, number> =
E.left('string')

E.toMaybe(eitherRight).map((value) =>
expect(value).toBe(5),
)
expect(E.toMaybe(eitherLeft).value).toBeNull()
})
})

0 comments on commit cbc401f

Please sign in to comment.