-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export ajax-helper in the released module #2045
Comments
There is ongoing effort to expose public interface for creating test (#1775), it may discussable to include those helper to public or not. For me it's bit on fence if it's needed to include those mock object or not, since those are generally easily mockable via existing mocking framework (based on test framework, like |
cc @jayphelps |
@rgbkrk this might even be worth just busting into it's own module, honestly. It's not really specific to RxJS. |
I was actually recently thought of get rid of those mocks and using |
I'm not a testing pro--but I've recently started testing the websocketsubject with redux-observable by just mocking the API I provide. class FakeWebSocket extends Observable {
constructor(response$) {
this.response$ = response$;
}
multiplex(sub, unsub, messageFilter) {
this.subMsg = sub();
this.unsubMsg = unsub();
return this.response$.filter(messageFilter);
}
} So say you have an API // api.js
export const tickerSocket = Observable.webSocket('ws://whatever'); and an epic export const stockTickerEpic = (action$, store, { tickerSocket }) =>
action$.ofType('START_TICKER_STREAM')
.mergeMap(action =>
tickerSocket.multiplex(
() => JSON.stringify({ sub: action.ticker }),
() => JSON.stringify({ unsub: action.ticker }),
msg => msg.ticker === action.ticker
)
.map(tick => ({ type: 'TICKER_TICK', tick }))
); (injecting the api utilities to epics described here) Then you can test it like const action$ = ActionsObservable.of({
type: 'START_TICKER_STREAM',
ticker: 'NFLX'
});
const response$ = Observable.of(
{ ticker: 'IGNORED', price: 0 },
{ ticker: 'NFLX', price: 100 },
{ ticker: 'IGNORED', price: 0 },
);
const api = {
tickerSocket: new FakeWebSocket(response$)
};
const observer = sinon.stub();
stockTickerEpic(action$, null, api).subscribe(observer);
expect(api.tickerSocket.subMsg).to.equal(JSON.stringify({ sub: 'NFLX' }));
expect(api.tickerSocket.unsubMsg).to.equal(JSON.stringify({ unsub: 'NFLX' }));
expect(observer.args).to.deep.equal([
[{
type: 'TICKER_TICK',
tick: {
ticker: 'NFLX',
price: 100
}
}]
]); I don't really think you need to test that small of a utility, but if you want to it's as simple as: // so testing this vvvvvvv
export const tickerSocket = Observable.webSocket('ws://whatever');
// by doing
expect(tickerSocket.url).to.equal('ws://whatever'); Because any further testing is literally testing whether or not RxJS correctly implemented WebSocketSubject. Which I'm told is needless, though certainly you could find bugs in Rx that we didn't know of 😆 Again, I'm not a testing pro so I have no idea if this is good or a really bad idea. Please don't take what I say as gospel! Also, my example skimmed over things like making sure the observable completes successfully, how you would handle time-based operators like debounceTime, etc. The |
* refactor(ajaxHelper): remove separate ajax helper -relates to #2045 * chore(wallaby): update wallaby configuration
I have changed to not to expose internal mock module anymore to avoid possible confusions. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
RxJS version:
5.0.0-rc1
Additional information:
I'd love to be able to fully test a module I'm working on, where I make a call to
webSocket
(yet don't use it). I noticed that the websocket tests use aMockWebSocket
from ajax-helper. Could that be exported so Rx users can rely on it for their own testing?The text was updated successfully, but these errors were encountered: