Skip to content

Commit

Permalink
refactor(testScheduler): use a factory function instead of the static…
Browse files Browse the repository at this point in the history
… function

use createTestScheduler() instead of TestScheduler.of()

BREAKING CHANGE: depricated TestScheduler.of()
  • Loading branch information
tusharmath committed Nov 18, 2017
1 parent 2e8e65c commit 34af3c5
Show file tree
Hide file tree
Showing 23 changed files with 66 additions and 69 deletions.
11 changes: 4 additions & 7 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ It is passed on at the time of subscription and is automatically shared with all

```ts
import * as O from 'observable-air'
import {TestScheduler} from 'observable-air/test'
import {createTestScheduler} from 'observable-air/test'

// source stream
const source = O.interval(1000)
const scheduler = new TestScheduler()
const scheduler = createTestScheduler()
const observer = {
next () { console.log('hi')}
}
Expand Down Expand Up @@ -686,16 +686,13 @@ interface TestScheduler extends Scheduler {
// Creates a TestObserver. TestObserver keeps log of when and what type of an event was fired.
Observer (): Observer
// Factory function to create a new TestScheduler
static of (): TestScheduler
}
```

**Example:**
```ts
import {compose, add} from 'ramda'
import {TestScheduler, EVENT} from 'observable-air/test'
import {createTestScheduler, EVENT} from 'observable-air/test'
import * as assert from 'assert'
import * as O from 'observable-air'
Expand All @@ -714,7 +711,7 @@ O.forEach(console.log, $) // takes 500ms to complete the test
// Testing using Assert
const tScheduler = TestScheduler
const tScheduler = createTestScheduler()
// runs async code synchronously
const {results} = tScheduler.start(() => even(100))
Expand Down
8 changes: 4 additions & 4 deletions src/testing/TestScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ export class TestScheduler implements IScheduler {
Observer<T>() {
return new TestObserver<T>(this)
}

static of(rafTimeout = DEFAULT_OPTIONS.rafTimeout) {
return new TestScheduler(rafTimeout)
}
}

export const createTestScheduler = (
rafTimeout: number = DEFAULT_OPTIONS.rafTimeout
) => new TestScheduler(rafTimeout)
2 changes: 1 addition & 1 deletion test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
*/

export {EVENT} from './src/testing/Events'
export {TestScheduler} from './src/testing/TestScheduler'
export {createTestScheduler} from './src/testing/TestScheduler'
export {fromMarble, toMarble} from './src/testing/Marble'
4 changes: 2 additions & 2 deletions test/test.Combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import * as assert from 'assert'
import {combine} from '../src/operators/Combine'
import {EVENT} from '../src/testing/Events'
import {fromMarble} from '../src/testing/Marble'
import {TestScheduler} from '../src/testing/TestScheduler'
import {createTestScheduler} from '../src/testing/TestScheduler'

describe('combine()', () => {
it('should combine events from multiple sources', () => {
const SH = TestScheduler.of()
const SH = createTestScheduler()
const {results} = SH.start(() => {
return combine((a, b, c) => a + b + c, [
SH.Hot(fromMarble('a-b-c-d|')),
Expand Down
4 changes: 2 additions & 2 deletions test/test.Concat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import * as t from 'assert'
import {concat} from '../src/operators/Concat'
import {toMarble} from '../src/testing/Marble'
import {TestScheduler} from '../src/testing/TestScheduler'
import {createTestScheduler} from '../src/testing/TestScheduler'

describe('concat()', () => {
it('should concat two stream', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const s0 = sh.Hot('--A--B--C--|')
const s1 = sh.Hot('---1---2---3---4---|')
const actual = toMarble(sh.start(() => concat(s0, s1)).results)
Expand Down
4 changes: 2 additions & 2 deletions test/test.Debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import * as t from 'assert'
import {debounce} from '../src/operators/Debounce'
import {fromMarble, toMarble} from '../src/testing/Marble'
import {TestScheduler} from '../src/testing/TestScheduler'
import {createTestScheduler} from '../src/testing/TestScheduler'

describe('debounce()', () => {
it('should not fire until the source pauses for atleast the give unit of time', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const {results} = sh.start(() =>
debounce(10, sh.Hot(fromMarble('012-345-678|')))
)
Expand Down
10 changes: 5 additions & 5 deletions test/test.Delay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@ import * as t from 'assert'
import {delay} from '../src/operators/Delay'
import {EVENT} from '../src/testing/Events'
import {fromMarble, toMarble} from '../src/testing/Marble'
import {TestScheduler} from '../src/testing/TestScheduler'
import {createTestScheduler} from '../src/testing/TestScheduler'
import {ERROR_MESSAGE, thrower} from '../src/testing/Thrower'

describe('delay()', () => {
it('should delay the source events', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const {results} = sh.start(() => delay(20, sh.Hot(fromMarble('12345|'))))
t.strictEqual(toMarble(results), '--12345|')
})

it('should forward error', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const {results} = sh.start(() => delay(20, sh.Hot(fromMarble('--#|'))))
t.strictEqual(toMarble(results), '--#--|')
})

it('should catch internal exception', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const {results} = sh.start(() =>
thrower(delay(20, sh.Hot(fromMarble('0'))))
)
t.deepEqual(results, [EVENT.error(220, Error(ERROR_MESSAGE))])
})

it('should unsubscribe', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
sh.start(() => delay(50, sh.Hot(fromMarble('0'))), 200, 230)
t.strictEqual(sh.length, 0)
})
Expand Down
10 changes: 5 additions & 5 deletions test/test.ForEach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import * as assert from 'assert'
import {forEach} from '../src/sinks/ForEach'
import {TestScheduler} from '../src/testing/TestScheduler'
import {createTestScheduler} from '../src/testing/TestScheduler'

describe('forEach()', () => {
context('when a function is passed', () => {
it('should forward values', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const $ = sh.Cold('-1234')

const expected = ['1', '2', '3', '4']
Expand All @@ -19,7 +19,7 @@ describe('forEach()', () => {
assert.deepEqual(actual, expected)
})
it('should unsubscribe from the source on error', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const expected = '^---!'
const testObservable = sh.Hot('-123#')
assert.throws(
Expand All @@ -35,7 +35,7 @@ describe('forEach()', () => {

context('when an observer is passed', () => {
it('should forward values', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const $ = sh.Hot('-1234|')

const testObserver = sh.Observer()
Expand All @@ -48,7 +48,7 @@ describe('forEach()', () => {
assert.strictEqual(actual, expected)
})
it('should forward errors', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const expected = '-123#'

const testObservable = sh.Hot(expected)
Expand Down
4 changes: 2 additions & 2 deletions test/test.Frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import * as t from 'assert'
import {scan} from '../src/operators/Scan'
import {frames} from '../src/sources/Frames'
import {toMarble} from '../src/testing/Marble'
import {TestScheduler} from '../src/testing/TestScheduler'
import {createTestScheduler} from '../src/testing/TestScheduler'

describe('frames()', () => {
it('should emit requestAnimationFrame events', () => {
const sh = TestScheduler.of(10)
const sh = createTestScheduler(10)
const {results} = sh.start(() => scan(i => i + 1, -1, frames()), 200, 250)
t.strictEqual(toMarble(results), '-0123')
})
Expand Down
4 changes: 2 additions & 2 deletions test/test.FromArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import * as t from 'assert'
import {map} from '../src/operators/Map'
import {fromArray} from '../src/sources/FromArray'
import {EVENT} from '../src/testing/Events'
import {TestScheduler} from '../src/testing/TestScheduler'
import {createTestScheduler} from '../src/testing/TestScheduler'
import {ERROR_MESSAGE, throwError} from '../src/testing/Thrower'

const {next, error} = EVENT

describe('fromArray()', () => {
it('should emit array values as events', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const testFunction = (x: any) =>
x === 2 ? throwError(ERROR_MESSAGE) : x * 100
const {results} = sh.start(() => map(testFunction, fromArray([1, 2, 3])))
Expand Down
6 changes: 3 additions & 3 deletions test/test.Interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import {scan} from '../src/operators/Scan'
import {interval} from '../src/sources/Interval'
import {EVENT, EventError} from '../src/testing/Events'
import {toMarble} from '../src/testing/Marble'
import {TestScheduler} from '../src/testing/TestScheduler'
import {createTestScheduler} from '../src/testing/TestScheduler'
import {ERROR_MESSAGE, thrower} from '../src/testing/Thrower'
const {error} = EVENT

describe('interval()', () => {
it('should emit values every t ms', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const {results} = sh.start(
() => scan(i => i + 1, -1, interval(10)),
200,
Expand All @@ -22,7 +22,7 @@ describe('interval()', () => {
t.strictEqual(toMarble(results), '-0123')
})
it('should catch exceptions', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const observer = sh.Observer<void>()
thrower(interval(100)).subscribe(observer, sh)
sh.advanceBy(100)
Expand Down
6 changes: 3 additions & 3 deletions test/test.Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import * as t from 'assert'
import {map} from '../src/operators/Map'
import {EVENT} from '../src/testing/Events'
import {TestScheduler} from '../src/testing/TestScheduler'
import {createTestScheduler} from '../src/testing/TestScheduler'

const {next, complete} = EVENT

describe('map()', () => {
it('should should be subscribe-able', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const $ = sh.Cold<number>([
next(210, 0),
next(220, 10),
Expand All @@ -27,7 +27,7 @@ describe('map()', () => {
])
})
it('should be able to subscribe to hot stream', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const $ = sh.Hot<number>([
next(100, -10),
next(210, 0),
Expand Down
4 changes: 2 additions & 2 deletions test/test.Merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import * as t from 'assert'
import {merge} from '../src/operators/Merge'
import {EVENT} from '../src/testing/Events'
import {TestScheduler} from '../src/testing/TestScheduler'
import {createTestScheduler} from '../src/testing/TestScheduler'

describe('merge', () => {
it('should merge multiple source streams', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const a$ = sh.Hot([
EVENT.next(210, 'A0'),
EVENT.next(220, 'A1'),
Expand Down
6 changes: 3 additions & 3 deletions test/test.MergeMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import * as t from 'assert'
import {mergeMap} from '../src/operators/MergeMap'
import {EVENT} from '../src/testing/Events'
import {TestScheduler} from '../src/testing/TestScheduler'
import {createTestScheduler} from '../src/testing/TestScheduler'

const {next, complete} = EVENT
describe('mergeMap()', () => {
context('when concurrency is Infinity', () => {
it('should work like flatMap()', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const sa$$ = sh.Cold([
next(10, 'A0'),
next(20, 'A1'),
Expand Down Expand Up @@ -41,7 +41,7 @@ describe('mergeMap()', () => {
})
context('when concurrency is 1', () => {
it('should work like concatMap()', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const s$$ = sh.Cold(
next(10, sh.Cold(next(1, 'A0'), complete(50))),
next(20, sh.Cold(next(1, 'B0'), complete(50))),
Expand Down
4 changes: 2 additions & 2 deletions test/test.Multicast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {map} from '../src/operators/Map'
import {multicast} from '../src/operators/Multicast'
import {EVENT} from '../src/testing/Events'
import {TestObserver} from '../src/testing/TestObserver'
import {TestScheduler} from '../src/testing/TestScheduler'
import {createTestScheduler} from '../src/testing/TestScheduler'

describe('multicast()', () => {
it('should subscribe only once', () => {
let i = 0
const sh = TestScheduler.of()
const sh = createTestScheduler()
const ob0 = new TestObserver(sh)
const ob1 = new TestObserver(sh)
const t$ = multicast(
Expand Down
8 changes: 4 additions & 4 deletions test/test.Observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import * as t from 'assert'
import {Observable} from '../src/lib/Observable'
import {slice} from '../src/main'
import {EVENT} from '../src/testing/Events'
import {TestScheduler} from '../src/testing/TestScheduler'
import {createTestScheduler} from '../src/testing/TestScheduler'

describe('new Observable()', () => {
it('should emit values via next()', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const {results} = sh.start(() => new Observable(ob => ob.next('A')))
t.deepEqual(results, [EVENT.next(201, 'A')])
})

it('should be subscribe-able', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const {results} = sh.start(
() =>
new Observable((ob, sh) => {
Expand All @@ -26,7 +26,7 @@ describe('new Observable()', () => {
})

it('should unsubscribe', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const actual = sh.start(() =>
slice(
0,
Expand Down
8 changes: 4 additions & 4 deletions test/test.Sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import * as t from 'assert'
import {sample} from '../src/operators/Sample'
import {EVENT} from '../src/testing/Events'
import {fromMarble} from '../src/testing/Marble'
import {TestScheduler} from '../src/testing/TestScheduler'
import {createTestScheduler} from '../src/testing/TestScheduler'

function toArray(...t: Array<any>) {
return t.join(',')
}

describe('sample()', () => {
it('should sample multiple sources', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const a$ = sh.Hot([
EVENT.next(210, 'A0'),
EVENT.next(230, 'A1'),
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('sample()', () => {
})

it('should sample()', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const a$ = sh.Hot([
EVENT.next(210, 0),
EVENT.next(230, 1),
Expand Down Expand Up @@ -81,7 +81,7 @@ describe('sample()', () => {
})

it('should sample()', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const t1$ = sh.Hot(fromMarble('-A-B-C-D'))
const t2$ = sh.Hot(fromMarble('--a-b-c-d'))
const {results} = sh.start(() => sample((a, b) => a + b, t2$, [t1$, t2$]))
Expand Down
4 changes: 2 additions & 2 deletions test/test.Scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import * as t from 'assert'
import {scan} from '../src/operators/Scan'
import {EVENT} from '../src/testing/Events'
import {TestScheduler} from '../src/testing/TestScheduler'
import {createTestScheduler} from '../src/testing/TestScheduler'

const {next, complete} = EVENT

describe('scan()', () => {
it('should work like R.scan', () => {
const sh = TestScheduler.of()
const sh = createTestScheduler()
const $ = sh.Cold<number>([
next(210, 0),
next(220, 1),
Expand Down
Loading

0 comments on commit 34af3c5

Please sign in to comment.