piotrwitek / typesafe-actions

Typesafe utilities for "action-creators" in Redux / Flux Architecture
https://codesandbox.io/s/github/piotrwitek/typesafe-actions/tree/master/codesandbox
MIT License
2.41k stars 98 forks source link

[5.1] handleAction throws error in test environment using AVA #245

Open Ethella opened 3 years ago

Ethella commented 3 years ago

Description

Mandatory info

How to Reproduce

CodeSandbox Link

(PASTE HERE your codesandbox link)

No CodeSandbox Link

If no codesandbox, then please provide a full working code example below including actions, reducers and your custom types used in the example.

import test from 'ava';
import * as actions from '~/app/store/auth/auth.actions';
import { store } from '~/app/store';

test('initAuthState', async t => {

  await store.dispatch(
   actions.initAuthState()
});
// auth.actions.ts
export const initAuthState = createAction(actionTypes.INIT_AUTH_STATE)();

// reducer.ts
const AuthReducer = createReducer<AuthState, AuthActions>(initialState)
  .handleAction(initAuthState, () => ({ ...initialState }));

Error

  Uncaught exception in test/spec/app/store/auth/auth.actions.spec.ts

  /node_modules/typesafe-actions/src/utils/validation.ts:79

  Error: Argument 1 is invalid, it should be an action-creator instance from "typesafe-actions" or action type of type: string | symbol

  throwInvalidActionTypeOrActionCreator (node_modules/typesafe-actions/src/utils/validation.ts:79:9)
  map (node_modules/typesafe-actions/src/create-reducer.ts:120:15)
  Function.reducerHandler (node_modules/typesafe-actions/src/create-reducer.ts:111:8)
  Object.<anonymous> (src/app/store/auth/auth.reducer.ts:2:2138)
  Module.m._compile (node_modules/ts-node/src/index.ts:858:23)
  module.exports (node_modules/default-require-extensions/js.js:7:9)

Error trace https://github.com/piotrwitek/typesafe-actions/blob/cbed746b3e997942cc93c3b4fa5abb876194ee16/src/utils/validation.ts#L79 https://github.com/piotrwitek/typesafe-actions/blob/master/src/create-reducer.ts#L120

Expected behavior

Production seems to be working just fine, but not sure why the error is thrown when creating the reducer in a test environment.

Suggested solution(s)

An example of how to write tests would be helpful.

Project Dependencies



## Environment (optional)
<!-- Fill if you think it's relevant to your issue -->
- Browser and Version: XXX
- OS: XXX
- Node Version: XXX
- Package Manager and Version: XXX

[1]: https://github.com/piotrwitek/typesafe-actions#compatibility-notes
[2]: https://github.com/piotrwitek/typesafe-actions#migration-guides