choko-org / redux-boot

Modular Redux bootstrap with asynchronous side-effects.
MIT License
126 stars 7 forks source link

Add better async test #5

Closed recidive closed 8 years ago

recidive commented 8 years ago

Need to improve this test case:

test('Use redux-actions with redux-promise to fire async and sync side-effect actions in middlewares', (assert) => {

  const AFTER_BOOT = 'choko/core/test/AFTER_BOOT'
  const AFTER_AFTER_BOOT = 'choko/core/test/AFTER_AFTER_BOOT'

  let data = {
    1: 'bar',
    2: 'baz',
    3: 'rock'
  }

  const someApi = {
    get(id) {
      return Promise.resolve({
        name: data[id]
      })
    }
  }

  const afterBootAction = createAction(AFTER_BOOT, async (id) => {
    const result = await someApi.get(id)
    return result
  })

  const afterAfterBootAction = createAction(AFTER_AFTER_BOOT, async (id) => {
    const result = await someApi.get(id)
    return result
  })

  const initialState = {foo: 'bar'}

  const reducer = (state = initialState, action) => {
    switch (action.type) {
      case AFTER_BOOT:
        return {
          ...state,
          foo: action.payload.name
        }

      case AFTER_AFTER_BOOT:
        return {
          ...state,
          foo: action.payload.name
        }
      default:
        return state;
        break;
    }
  }

  const middleware = [
    function middle9({getState, dispatch}) {
      // Note the async keyword.
      return next => (action) => {

        console.log(action.type, 'ACTION')

        const result = next(action)

        if (action.type == AFTER_AFTER_BOOT) {
          console.log(getState(), 'STATEEEE AFTER AFTER BOOT 9');
        }

        return result
      }
    },
    function middleA({getState, dispatch}) {
      // Note the async keyword.
      return next => async (action) => {

        const result = next(action)

        if (action.type == AFTER_BOOT) {
          // console.log(getState(), 'STATEEEE before A');
          // const sideffectB = await dispatch(afterAfterBootAction(3))
          console.log(getState(), 'STATEEEE AFTER BOOT');
        }

        return result
      }
    },
    function middleB({getState, dispatch}) {
      // Note the async keyword.
      return next => async (action) => {

        const result = next(action)

        if (action.type == BOOT) {
          const sideffectA = await dispatch(afterBootAction(2))
          console.log(getState(), 'STATEEEE A');
          const sideffectB = await dispatch(afterAfterBootAction(3))
          console.log(getState(), 'STATEEEE AFTER AFTER BOOT B');
        }

        return result
      }
    },
    function middleC({getState, dispatch}) {
      // Note the async keyword.
      return next => (action) => {

        const result = next(action)

        if (action.type == AFTER_AFTER_BOOT) {
          console.log(getState(), 'STATEEEE AFTER AFTER BOOT C');
        }

        return result
      }
    }
  ]

  const testModule = {
    reducer,
    middleware
  }

  const modules = [
    testModule
  ]

  const app = Choko(initialState, modules)

  app.then(({action, store}) => {

    assert.equal(
      store.getState().foo,
      'rock',
      'Chainable async and sync side-effects were handled'
    )

    assert.end()
  })

})