Skip to content

Instantly share code, notes, and snippets.

@abruzzi
Created January 29, 2018 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abruzzi/596a8e4e2f0293a793e69a0feb1b33d6 to your computer and use it in GitHub Desktop.
Save abruzzi/596a8e4e2f0293a793e69a0feb1b33d6 to your computer and use it in GitHub Desktop.
export const getCollections = () => {
return (dispatch, getState) => {
const state = getState()
const params = {
priceLow: state.sidebar.lowPrice,
priceHigh: state.sidebar.highPrice
}
dispatch({
type: COLLECTION,
payload: axios.get(`http://localhost:3000/api/collections`, {params})
})
}
}
@abruzzi
Copy link
Author

abruzzi commented Jan 30, 2018

import axios from 'axios'

import {getCollections} from './actions'
import {COLLECTION_PENDING, COLLECTION_FULFILLED, COLLECTION_REJECTED} from './types'

import store, {mockStore} from './mockStore'

describe('fetch data', () => {
  afterEach(() => {
    store.clearActions()
  })

  it('fetch data async successfully', (done) => {
    const data = []
    
    const mockResponse = (status, statusText, response) => {
      return new window.Response(JSON.stringify(response), {
        status: status,
        statusText: statusText,
        headers: {
          'Content-type': 'application/json'
        }
      });
    };

    axios.get = jest.fn().mockImplementation(() => Promise.resolve(mockResponse(200, null, {data})))

    const testState = {
      home: {
        collections: [],
        loading: false
      },

      sidebar: {
        lowPrice: 100,
        highPrice: 200
      }
    };

    const mocked = mockStore(testState)
    mocked.subscribe(() => {
      const dispatchedActions = mocked.getActions();

      if (dispatchedActions.length === 2) {
        expect(dispatchedActions[0].type).toEqual(COLLECTION_PENDING)
        expect(dispatchedActions[1].type).toEqual(COLLECTION_FULFILLED)

        done()
      }
    })

    mocked.dispatch(getCollections())
  })
})

@abruzzi
Copy link
Author

abruzzi commented Jan 31, 2018

import axios from 'axios'

import {getCollections} from './actions'
import {COLLECTION_PENDING, COLLECTION_FULFILLED, COLLECTION_REJECTED} from './types'

import store, {mockStore} from './mockStore'

describe('fetch data', () => {
  afterEach(() => {
    store.clearActions()
  })

  it('fetch data async successfully', () => {
    const data = []
    axios.get = jest.fn().mockImplementation(() => Promise.resolve(data))

    const testState = {
      home: {
        collections: [],
        loading: false
      },

      sidebar: {
        lowPrice: 100,
        highPrice: 200
      }
    };

    const mocked = mockStore(testState)

    const expectedActions = [
      {type: COLLECTION_PENDING},
      {type: COLLECTION_FULFILLED, payload: {data}}
    ]

    return mocked.dispatch(getCollections()).then(() => {
      expect(mocked.getActions()).toEqual(expectedActions)
    })
  })
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment