Code Monkey home page Code Monkey logo

universal-rxjs-ajax's Introduction

universal-rxjs-ajax

All credit goes to jayphelps. Copied from his suggestion made here.

universal-rxjs-ajax makes Observable.ajax available in Node.js by throwing in xhr2 when necessary. Very helpful for testing Observable.ajax-based http requests with Node.js driven test frameworks.

Usage

import {request} from 'universal-rxjs-ajax'

request({url: 'http://some-api'})
  .subscribe(response => console.log(response))

Examples

redux-observable and jest

// in file reduxModule.js:
import {request} from 'universal-rxjs-ajax'

export const GET_SOMETHING = 'GET_SOMETHING'
export const GOT_SOMETHING = 'GOT_SOMETHING'
export const LOG_ERROR = 'LOG_ERROR'

export const getSomething = () => ({
  type: GET_SOMETHING
})

export const gotSomething = response => ({
  type: GOT_SOMETHING,
  payload: response
})

export const logError = error => ({
  type: LOG_ERROR,
  payload: error,
  error: true
})

// export default reducer ...

export const getSomethingEpic = action$ => 
  action$.ofType(GET_SOMETHING)
    .mergeMap(action => 
      request({url: 'http://some-api/epic-stuff'})
        .map(({response}) => gotSomething(response))
        .catch(error => logError(error))
    )


// in file reduxModule.test.js:
/**
* @jest-environment node
*/

// jsdom does not handle the XMLHttpRequest correctly!
// The above docbloc changes the jest environment to node for tests in this 
// file only. jsdom happens to be the default setting for create-react-app so
// make sure to put in the docbloc when you are using it!

import nock from 'nock'
import {ActionObservable} from 'redux-observable'
import {getSomething, getSomethingEpic} from './reduxModule'

describe('reduxModule', () => {
  // make sure nock does not interfere with other tests...
  afterEach(() => {
    nock.cleanAll()
  })

  test('getSomethingEpic returns correct action with response', async () => {
    const action$ = ActionsObservable.of(getSomething())

    // intercept request with nock
    nock('http://some-api')
      .log(console.log) // log interceptions for debugging
      .get('/epic-stuff')
      .reply(200, {epicStuff: []})

    const response = await getSomethingEpic(action$)
      .toArray() // buffers all emitted actions until Epic completes
      .toPromise() // turn Observable back to Promise so it can be awaited

    expect(response).toMatchSnapshot() // snapshots are great!
  })

  test('getSomethingEpic returns correct action with error', async () => {
    const action$ = ActionsObservable.of(getSomething())

    nock('http://some-api')
      .log(console.log)
      .get('/epic-stuff')
      .reply(200, {epicStuff: []})

    const response = await getSomethingEpic(action$)
      .toArray()
      .toPromise() 

    expect(response).toMatchSnapshot()
  })
})

Copyright and license

Copyright 2017, Matthias Munder.
Licensed under the MIT license.

js-standard-style

universal-rxjs-ajax's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.