Skip to content

Commit

Permalink
Merge pull request #29 from VKTRenokh/develop
Browse files Browse the repository at this point in the history
Tests, Unobserve
  • Loading branch information
VKTRenokh committed Mar 7, 2024
2 parents 90bd7be + a56c88c commit a1b3dd6
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-dolls-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@victorenokh/maybe.ts": patch
---

Fix observer#unobserve
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
tests
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
coverage
4 changes: 2 additions & 2 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"*.{json,js,ts,html}": ["npm run prettier"],
"*.{js,ts}": ["npm run lint-fix"]
"src/**/*.{json,js,ts,html}": ["npm run prettier"],
"src/**/*.{js,ts}": ["npm run lint-fix"]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"build": "tsc --build",
"release": "npm run build && changeset publish",
"version-packages": "changeset version",
"test-coverage": "jest --coverage",
"test": "jest",
"prettier": "prettier --config .prettierrc 'src/**/*.ts' --write",
"prepare": "husky install",
Expand Down
6 changes: 5 additions & 1 deletion src/observable/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export const of = <T>(v: T): Observable<T> => {

return {
value,
unobserve: () => observers.splice(index, 1),
unobserve: () => (
console.log(observers),
observers.splice(index - 1, 1),
console.log(observers)
),
}
},
next: (v: T) => ((value = v), notify(), of(v)),
Expand Down
29 changes: 29 additions & 0 deletions tests/either.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,17 @@ describe('either.ts', () => {
)

eitherExample(4, 2).flatMap(flatMapFn).fold(left, right)
expect(flatMapFn).toHaveBeenCalled()
expect(left).not.toHaveBeenCalled()
expect(right).toHaveBeenCalled()

eitherExample(2, 2).flatMap(flatMapFn).fold(left, right)
expect(flatMapFn).toHaveBeenCalledTimes(2)
expect(left).toHaveBeenCalledTimes(1)
expect(right).toHaveBeenCalled()

eitherExample(0, 2).flatMap(flatMapFn)
expect(flatMapFn).toHaveBeenCalledTimes(2)
})

it('filterOrElse', () => {
Expand Down Expand Up @@ -175,6 +180,7 @@ describe('either.ts', () => {
it('merge', () => {
const a: E.Either<string, number> = E.right(5)
const b: E.Either<string, string> = E.right('Hello')
const c: E.Either<string, number> = E.left('Some error')

a.merge(b).fold(
() => {
Expand All @@ -185,6 +191,13 @@ describe('either.ts', () => {
expect(m.right).toBe('Hello')
},
)

b.merge(c).fold(
(e) => expect(e).toBe('Some error'),
() => {
throw new Error("should'nt be called")
},
)
})

it('merge util', () => {
Expand All @@ -203,4 +216,20 @@ describe('either.ts', () => {
},
)
})

it('isLeft', () => {
const right: E.Either<string, string> = E.right('hello')
const left: E.Either<string, string> = E.left('bye')

expect(right.isLeft()).not.toBeTruthy()
expect(left.isLeft()).toBeTruthy()
})

it('isRight', () => {
const right: E.Either<string, string> = E.right('hello')
const left: E.Either<string, string> = E.left('bye')

expect(right.isRight()).toBeTruthy()
expect(left.isRight()).not.toBeTruthy()
})
})
50 changes: 49 additions & 1 deletion tests/maybe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ describe('maybe.ts', () => {
})

it('mapNullable', () => {
const shouldntBeCalled = jest.fn(() => {
throw new Error('shouldnt be called')
})

const a = M.of(32).mapNullable(() => undefined)
const b = M.of(5).mapNullable(() => 5)
const c = M.none<number>().mapNullable(shouldntBeCalled)

expect(a.value).toBeNull()
expect(b.value).toBe(5)
expect(c.value).toBe(null)
expect(shouldntBeCalled).not.toHaveBeenCalled()
})

it('flatMap', () => {
Expand Down Expand Up @@ -68,15 +75,37 @@ describe('maybe.ts', () => {
expect(a.merge(nullable).value).toBeNull()
})

it('asyncMap', () => {
it('asyncMap', async () => {
const sleep = (ms: number): Promise<number> =>
new Promise((res) => setTimeout(() => res(ms), ms))

const sleepWithError = (ms: number): Promise<number> =>
new Promise((_, rej) => setTimeout(() => rej(ms), ms))

M.of(500)
.asyncMap((ms) => sleep(ms / 2))
.then((maybeTime) => {
expect(maybeTime.value).toBe(250)
})

const catchError = jest.fn((err) =>
expect(err).toBe(250),
)

const withError = await M.of(500).asyncMap(
(ms) => sleepWithError(ms / 2),
catchError,
)

const withNothing = await M.none<number>().asyncMap(
() => {
throw new Error('shouldntBeCalled')
},
)

expect(withNothing.value).toBeNull()
expect(catchError).toHaveBeenCalled()
expect(withError.value).toBeNull()
})

it('equals', () => {
Expand All @@ -90,6 +119,25 @@ describe('maybe.ts', () => {

it('apply', () => {
const double = M.of((num: number) => num * 2)
const doubleNoCall = M.of((num: number) => {
throw new Error('shouldntBeCalled')
})

expect(M.of(42).apply(double).value).toBe(84)
expect(
M.none<number>().apply(doubleNoCall).value,
).toBeNull()
})

it('isNothing', () => {
expect(M.none().isNothing()).toBeTruthy()
expect(M.of(2).isNothing()).toBeFalsy()
})

it('call', () => {
const fn = jest.fn(() => 0)

expect(M.call(fn)).toBe(0)
expect(fn).toHaveBeenCalled()
})
})
30 changes: 30 additions & 0 deletions tests/observable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,34 @@ describe('observable.ts', () => {
a.dependingNext(double)
expect(merged.value).toStrictEqual([8, 18, 3])
})

it('observe.unobserve', () => {
const a = O.of(4)
const observer = jest.fn((_: number) => {
throw new Error('should not be called')
})

a.observe(observer, true).unobserve()

a.next(10)
expect(a.value).toBe(10)
})

it('equals', () => {
const x = 5
const f = (v: number) => O.of(v * 2)
const g = (v: number) => O.of(v + 3)

const law1_lhs = O.of(x).flatMap(f)
const law1_rhs = f(x)
expect(law1_lhs.equals(law1_rhs)).toBeTruthy()

const law2_lhs = O.of(x).flatMap(O.of)
const law2_rhs = O.of(x)
expect(law2_lhs.equals(law2_rhs)).toBeTruthy()

const law3_lhs = O.of(x).flatMap(f).flatMap(g)
const law3_rhs = O.of(x).flatMap((x) => f(x).flatMap(g))
expect(law3_lhs.equals(law3_rhs)).toBeTruthy()
})
})

0 comments on commit a1b3dd6

Please sign in to comment.