Skip to content

Commit b83ef18

Browse files
committed
testing synchronize code with Id Monad
1 parent 8be8cde commit b83ef18

File tree

3 files changed

+42
-49
lines changed

3 files changed

+42
-49
lines changed

src/__tests__/fantasyx-test.ts

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
import { Observable, Subject } from '@reactivex/rxjs'
2-
import '../xs/rx'
1+
import { streamOps } from '../xs'
2+
import '../xs/array'
33
import { FantasyX } from '../fantasy/fantasyx'
4+
import { Id } from '../fantasy/typeclasses/id'
45
import { Update } from '../interfaces'
56
import { Xstream } from '../fantasy/xstream'
67
import { flatMap, FlatMap } from '../fantasy/typeclasses/flatmap'
78
import { concat, Semigroup } from '../fantasy/typeclasses/semigroup'
89
describe('FantasyX', () => {
910
let intent;
1011
beforeEach(() => {
11-
intent = new Subject()
12+
intent = streamOps.subject()
1213
})
13-
it('map and fold', (done) => {
14-
Xstream
14+
it('map and fold', () => {
15+
intent.next(1)
16+
intent.next(2)
17+
let res = Xstream
1518
.fromIntent()
1619
.toFantasyX()
1720
.map(a => {
@@ -23,59 +26,44 @@ describe('FantasyX', () => {
2326
})
2427
.foldS((s, a) => ({ count: a.count + s.count }))
2528
.toStream(intent)
26-
.reduce((acc, f: any) => {
27-
return f(acc)
28-
}, { count: 10 })
29-
.subscribe(a => {
30-
expect(a.count).toBe(22)// 5 + 10 + 7
31-
done()
32-
});
29+
.reduce((acc, f) => f(acc), { count: 10 })
30+
expect(res).toEqual({ count: 10 + 5 + 7 })
31+
})
32+
33+
it('flatMap Xstream', () => {
3334
intent.next(1)
3435
intent.next(2)
35-
intent.complete()
36-
})
3736

38-
it('flatMap Xstream', (done) => {
39-
FlatMap.Xstream.flatMap(
40-
(x: number) => Xstream.fromPromise<"RxStream", number, number>(Promise.resolve({ count: x + 1 }))
41-
, Xstream.fromIntent<"RxStream", number>())
37+
let res = FlatMap.Xstream.flatMap(
38+
(x: number) => Xstream.fromPromise<"ArrayStream", number, number>(new Id({ count: x + 1 }))
39+
, Xstream.fromIntent<"ArrayStream", number>())
4240
.toFantasyX()
4341
.toStream(intent)
4442
.reduce((acc, f: any) => f(acc), { count: 10 })
45-
.toPromise().then(a => expect(a).toEqual({ count: 3 })).then(done)
46-
intent.next(1)
47-
intent.next(2)
48-
intent.complete()
43+
expect(res).toEqual({ count: 3 })
4944
})
5045

51-
it('concat object', (done) => {
52-
Semigroup.Xstream.concat(
46+
it('concat object', () => {
47+
intent.next({ count1: 1 })
48+
intent.next({ count2: 2 })
49+
50+
let res = Semigroup.Xstream.concat(
5351
Xstream.fromIntent()
5452
, Xstream.fromIntent())
5553
.toFantasyX()
5654
.toStream(intent)
5755
.reduce((acc, f: any) => f(acc), { count: 0 })
58-
.toPromise()
59-
.then(a => expect(a).toEqual(
60-
{ "count": 0, "count1": 1, "count2": 2 }
61-
))
62-
.then(done)
63-
intent.next({ count1: 1 })
64-
intent.next({ count2: 2 })
65-
intent.complete()
56+
expect(res).toEqual({ "count": 0, "count1": 1, "count2": 2 })
6657
})
6758

68-
it('concat promise', (done) => {
69-
Semigroup.Xstream.concat(
70-
Xstream.fromPromise(Promise.resolve({ count1: 1 }))
71-
, Xstream.fromPromise(Promise.resolve({ count2: 2 })))
59+
it('concat promise', () => {
60+
61+
let res = Semigroup.Xstream.concat(
62+
Xstream.fromPromise(new Id({ count1: 1 }))
63+
, Xstream.fromPromise(new Id({ count2: 2 })))
7264
.toFantasyX()
7365
.toStream(intent)
7466
.reduce((acc, f: any) => f(acc), { count: 0 })
75-
.toPromise()
76-
.then(a => expect(a).toEqual(
77-
{ "count": 0, "count1": 1, "count2": 2 }
78-
))
79-
.then(done)
67+
expect(res).toEqual({ "count": 0, "count1": 1, "count2": 2 })
8068
})
8169
})

src/fantasy/typeclasses/semigroup.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export class StringSemigroup implements Semigroup<string> {
3838
export class ArraySemigroup<A> implements Semigroup<Array<A>> {
3939
_T: Array<A>
4040
concat(a: Array<A>, b: Array<A>): Array<A> {
41+
console.log(a, b, '----------------------')
42+
4143
return a.concat(b)
4244
}
4345
}

src/xs/array.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ StreamOps.prototype.combine = function <A, C>(
2828
f: (...a: any[]) => C,
2929
...v: any[]
3030
): Array<C> {
31-
return f.call(null, v)
31+
return f.apply(null, v)
3232
}
3333

3434
StreamOps.prototype.filter = function <A>(f: (a: A) => boolean, fa: Array<A>): Array<A> {
@@ -41,16 +41,19 @@ StreamOps.prototype.flatMap = function <A, B>(f: (a: A) => Array<B>, fa: Array<A
4141
return fa.reduce((acc, a) => acc.concat(f(a)), [] as B[])
4242
}
4343

44-
class Subject<T> extends Array<T> {
45-
next(a: T) {
46-
this.push(a)
47-
}
48-
complete() {
49-
}
44+
function Subject() {
45+
}
46+
Subject.prototype = Array.prototype
47+
48+
Subject.prototype.next = function(a: any) {
49+
this.push(a)
50+
}
51+
52+
Subject.prototype.complete = function() {
5053
}
5154

5255
StreamOps.prototype.subject = function <A>() {
53-
return new Subject()
56+
return new (<any>Subject)()
5457
}
5558

5659
StreamOps.prototype.subscribe = function <A>(fa: Array<A>, next: (v: A) => void, complete?: () => void) {

0 commit comments

Comments
 (0)