Skip to content

Commit

Permalink
Merge pull request #50 from kwonoj/feat-export-type
Browse files Browse the repository at this point in the history
feat(index): export types for functions
  • Loading branch information
kwonoj authored Sep 29, 2017
2 parents c113fdc + bcd7a3c commit 1300202
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 133 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<a name="0.0.7"></a>
## [0.0.7](https://github.com/kwonoj/rx-sandbox/compare/v0.0.6...v0.0.7) (2017-09-29)


### Features

* **index:** export types for functions ([c069f86](https://github.com/kwonoj/rx-sandbox/commit/c069f86))



<a name="0.0.6"></a>
## [0.0.6](https://github.com/kwonoj/rx-sandbox/compare/v0.0.5...v0.0.6) (2017-09-26)

Expand Down
4 changes: 3 additions & 1 deletion jest.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"coveragePathIgnorePatterns": [
"<rootDir>/node_modules/",
"<rootDir>/spec/.*\\.(ts|js)$",
"<rootDir>/build/.*\\.(ts|js)$"
"<rootDir>/build/.*\\.(ts|js)$",
"<rootDir>/src/RxSandbox.ts",
"<rootDir>/src/RxSandboxInstance.ts"
]
}
120 changes: 60 additions & 60 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rx-sandbox",
"version": "0.0.6",
"version": "0.0.7",
"description": "Marble diagram DSL based test suite for RxJS 5",
"main": "./dist/src/index.js",
"types": "./dist/src/index.d.ts",
Expand Down
36 changes: 36 additions & 0 deletions src/RxSandbox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { SubscriptionLog } from 'rxjs/testing/SubscriptionLog';
import { TestMessage } from './message/TestMessage';
import { RxSandboxInstance } from './RxSandboxInstance';

export interface RxSandbox {
/**
* Creates new instance of test scheduler for testing observables.
*
* @param {boolean} [autoFlush] Flush scheduler automatically when `getMarbles` is being called. False by default.
* @param {number} [frameTimeFactor] Custom frametime factor for virtual time frame. 1 by default.
* @param {number} [maxFrameValue] Maximum frame value of marble diagram can be read.
* If marble has value over max frame, it'll be ignored when scheduler flushes out.
* 1000 * frameTimeFactory by default.
* @return {RxSandboxInstance} instance of test scheduler interfaces.
*/
create(autoFlush?: boolean, frameTimeFactor?: number, maxFrameValue?: number): RxSandboxInstance;
/**
* Utility assertion method to assert marble based observable test messages.
* By default return values of sandbox functions are plain object works with
* any testing framework or assertion library, while this assertion provides
* better visualization against observable test messages.
*
*/
marbleAssert<T = string>(
source: Array<TestMessage<T | Array<TestMessage<T>>>> | Readonly<Array<TestMessage<T | Array<TestMessage<T>>>>>
): {
to: {
equal(
expected:
| Array<TestMessage<T | Array<TestMessage<T>>>>
| Readonly<Array<TestMessage<T | Array<TestMessage<T>>>>>
): void;
};
};
marbleAssert(source: Array<SubscriptionLog>): { to: { equal(expected: Array<SubscriptionLog>): void } };
}
59 changes: 59 additions & 0 deletions src/RxSandboxInstance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { SubscriptionLog } from 'rxjs/testing/SubscriptionLog';
import { TestMessage } from './message/TestMessage';
import { TestScheduler } from './scheduler/TestScheduler';

type hotObservable = typeof TestScheduler.prototype.createHotObservable;
type coldObservable = typeof TestScheduler.prototype.createColdObservable;
type flushScheduler = typeof TestScheduler.prototype.flush;
type advanceToScheduler = typeof TestScheduler.prototype.advanceTo;
type getObservableMessage = typeof TestScheduler.prototype.getMessages;
type expectedObservable = <T = string>(
marble: string,
value?: { [key: string]: T } | null,
error?: any
) => Readonly<Array<TestMessage<T | Array<TestMessage<T>>>>>;
type expectedSubscription = (marble: string) => SubscriptionLog;

type RxSandboxInstance = {
/**
* Creates a hot observable using marble diagram DSL, or TestMessage.
*/
hot: hotObservable;
/**
* Creates a cold obsrevable using marbld diagram DSL, or TestMessage.
*/
cold: coldObservable;
/**
* Flush out currently scheduled observables, fill values returned by `getMarbles`.
*/
flush: flushScheduler;
/**
* Flush out currently scheduled observables, only until reaches frame specfied.
*/
advanceTo: advanceToScheduler;
/**
* Get array of observable value's metadata TestMessage<T> from observable
* created via `hot` or `cold`. Returned array will be filled once scheduler flushes
* scheduled actions, either via explicit `flush` or implicit `autoFlush`.
*/
getMessages: getObservableMessage;
/**
* Utility function to generate `expected` values via marble diagram.
*/
e: expectedObservable;
/**
* Utility function to generate `expected` subscriptions via marble diagram.
*/
s: expectedSubscription;
};

export {
hotObservable,
coldObservable,
flushScheduler,
advanceToScheduler,
getObservableMessage,
expectedObservable,
expectedSubscription,
RxSandboxInstance
};
84 changes: 13 additions & 71 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,88 +1,30 @@
import { Observable } from 'rxjs/Observable';
import { ColdObservable } from 'rxjs/testing/ColdObservable';
import { HotObservable } from 'rxjs/testing/HotObservable';
import { SubscriptionLog } from 'rxjs/testing/SubscriptionLog';
import { marbleAssert } from './assert/marbleAssert';
import { parseObservableMarble } from './marbles/parseObservableMarble';
import { parseSubscriptionMarble } from './marbles/parseSubscriptionMarble';
import { TestMessage } from './message/TestMessage';
import { complete, error, next, subscribe } from './message/TestMessage';
import { RxSandbox } from './RxSandbox';
import { TestScheduler } from './scheduler/TestScheduler';
export {
hotObservable,
coldObservable,
flushScheduler,
advanceToScheduler,
getObservableMessage,
expectedObservable,
expectedSubscription,
RxSandboxInstance
} from './RxSandboxInstance';

//workaround TS4029 by explicitly import types and avoid unused import error
(() => Observable.toString())();
(() => ColdObservable.toString())();
(() => HotObservable.toString())();

export type RxSandboxInstance = {
/**
* Creates a hot observable using marble diagram DSL, or TestMessage.
*/
hot: typeof TestScheduler.prototype.createHotObservable;
/**
* Creates a cold obsrevable using marbld diagram DSL, or TestMessage.
*/
cold: typeof TestScheduler.prototype.createColdObservable;
/**
* Flush out currently scheduled observables, fill values returned by `getMarbles`.
*/
flush: typeof TestScheduler.prototype.flush;
/**
* Flush out currently scheduled observables, only until reaches frame specfied.
*/
advanceTo: typeof TestScheduler.prototype.advanceTo;
/**
* Get array of observable value's metadata TestMessage<T> from observable
* created via `hot` or `cold`. Returned array will be filled once scheduler flushes
* scheduled actions, either via explicit `flush` or implicit `autoFlush`.
*/
getMessages: typeof TestScheduler.prototype.getMessages;
/**
* Utility function to generate `expected` values via marble diagram.
*/
e: <T = string>(
marble: string,
value?: { [key: string]: T } | null,
error?: any
) => Readonly<Array<TestMessage<T | Array<TestMessage<T>>>>>;
/**
* Utility function to generate `expected` subscriptions via marble diagram.
*/
s: (marble: string) => SubscriptionLog;
};

export interface RxSandbox {
/**
* Creates new instance of test scheduler for testing observables.
*
* @param {boolean} [autoFlush] Flush scheduler automatically when `getMarbles` is being called. False by default.
* @param {number} [frameTimeFactor] Custom frametime factor for virtual time frame. 1 by default.
* @param {number} [maxFrameValue] Maximum frame value of marble diagram can be read.
* If marble has value over max frame, it'll be ignored when scheduler flushes out.
* 1000 * frameTimeFactory by default.
* @return {RxSandboxInstance} instance of test scheduler interfaces.
*/
create(autoFlush?: boolean, frameTimeFactor?: number, maxFrameValue?: number): RxSandboxInstance;
/**
* Utility assertion method to assert marble based observable test messages.
* By default return values of sandbox functions are plain object works with
* any testing framework or assertion library, while this assertion provides
* better visualization against observable test messages.
*
*/
marbleAssert<T = string>(
source: Array<TestMessage<T | Array<TestMessage<T>>>> | Readonly<Array<TestMessage<T | Array<TestMessage<T>>>>>
): {
to: {
equal(
expected:
| Array<TestMessage<T | Array<TestMessage<T>>>>
| Readonly<Array<TestMessage<T | Array<TestMessage<T>>>>>
): void;
};
};
marbleAssert(source: Array<SubscriptionLog>): { to: { equal(expected: Array<SubscriptionLog>): void } };
}
type marbleAssertion = typeof marbleAssert;

const rxSandbox: RxSandbox = {
create: (autoFlush: boolean = false, frameTimeFactor: number = 1, maxFrameValue = 1000) => {
Expand All @@ -102,4 +44,4 @@ const rxSandbox: RxSandbox = {
marbleAssert: marbleAssert
};

export { rxSandbox, TestMessage, next, error, complete, subscribe };
export { rxSandbox, TestMessage, marbleAssertion, next, error, complete, subscribe };

0 comments on commit 1300202

Please sign in to comment.