Skip to content
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

Closed
rgbkrk opened this issue Oct 17, 2016 · 7 comments
Closed

Export ajax-helper in the released module #2045

rgbkrk opened this issue Oct 17, 2016 · 7 comments

Comments

@rgbkrk
Copy link
Contributor

rgbkrk commented Oct 17, 2016

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 a MockWebSocket from ajax-helper. Could that be exported so Rx users can rely on it for their own testing?

@kwonoj
Copy link
Member

kwonoj commented Oct 17, 2016

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 sinon, or jest, etcs).

@benlesh
Copy link
Member

benlesh commented Oct 17, 2016

cc @jayphelps

@benlesh
Copy link
Member

benlesh commented Oct 17, 2016

@rgbkrk this might even be worth just busting into it's own module, honestly. It's not really specific to RxJS.

@kwonoj
Copy link
Member

kwonoj commented Oct 18, 2016

I was actually recently thought of get rid of those mocks and using sinon instead.

@jayphelps
Copy link
Member

jayphelps commented Oct 18, 2016

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 TestScheduler would come in for those, but didn't want to distract from the meat of the example.

kwonoj added a commit to kwonoj/rxjs that referenced this issue Dec 28, 2016
kwonoj added a commit to kwonoj/rxjs that referenced this issue Jan 5, 2017
benlesh pushed a commit that referenced this issue Jan 5, 2017
* refactor(ajaxHelper): remove separate ajax helper

-relates to #2045

* chore(wallaby): update wallaby configuration
@kwonoj
Copy link
Member

kwonoj commented Jan 6, 2017

I have changed to not to expose internal mock module anymore to avoid possible confusions.

@kwonoj kwonoj closed this as completed Jan 6, 2017
@lock
Copy link

lock bot commented Jun 6, 2018

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.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants