All files / spec loading_bar_middleware.js

100% Statements 47/47
100% Branches 0/0
93.75% Functions 15/16
100% Lines 46/46
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 921x   1x 1x 1x   1x 4x           1x 1x 1x 1x     1x 1x 1x 1x         1x 2x 2x 2x     1x       1x 1x 1x 1x         1x 2x 2x 2x     1x       1x 1x 1x 1x         1x 2x 2x 2x     1x       1x   1x 1x 1x       1x 1x 1x 1x     1x        
import expect from 'expect'
 
import createMockStore from './helpers/create_mock_store'
import loadingBarMiddleware from '../src/loading_bar_middleware'
import { showLoading, hideLoading } from '../src/loading_bar_ducks'
 
describe('loadingBarMiddleware', () => {
  const mockStore = (mockDispatch) =>
    createMockStore(
      [loadingBarMiddleware],
      mockDispatch
    )
 
  it('returns a function to handle next', () => {
    const mockDispatch = () => {}
    const nextHandler = loadingBarMiddleware(mockDispatch)
    expect(nextHandler).toBeA('function')
  })
 
  describe('with an action containing "_PENDING" in type', () => {
    it('dispatches SHOW action', () => {
      const originalAction = { type: 'something/FETCH_PENDING' }
      const expectedActions = [
        originalAction,
        showLoading(),
      ]
 
      const mockDispatch = (action) => {
        const expectedAction = expectedActions.shift()
        expect(action).toEqual(expectedAction)
        return action
      }
 
      mockStore(mockDispatch).dispatch(originalAction)
    })
  })
 
  describe('with an action containing "_FULFILLED" in type', () => {
    it('dispatches HIDE action', () => {
      const originalAction = { type: 'something/FETCH_FULFILLED' }
      const expectedActions = [
        originalAction,
        hideLoading(),
      ]
 
      const mockDispatch = (action) => {
        const expectedAction = expectedActions.shift()
        expect(action).toEqual(expectedAction)
        return action
      }
 
      mockStore(mockDispatch).dispatch(originalAction)
    })
  })
 
  describe('with an action containing "_REJECTED" in type', () => {
    it('dispatches HIDE action', () => {
      const originalAction = { type: 'something/FETCH_REJECTED' }
      const expectedActions = [
        originalAction,
        hideLoading(),
      ]
 
      const mockDispatch = (action) => {
        const expectedAction = expectedActions.shift()
        expect(action).toEqual(expectedAction)
        return action
      }
 
      mockStore(mockDispatch).dispatch(originalAction)
    })
  })
 
  describe('with an action does not containing "_PENDING", "_FULFILLED", ' +
           '"_REJECTED" in type', () => {
    it('does not dispatch SHOW and HIDE actions', () => {
      const originalAction = { type: 'something/RANDOM' }
      const expectedActions = [
        originalAction,
      ]
 
      const mockDispatch = (action) => {
        const expectedAction = expectedActions.shift()
        expect(action).toEqual(expectedAction)
        return action
      }
 
      mockStore(mockDispatch).dispatch(originalAction)
    })
  })
})