Enzyme tests for higher order components - redux

I spent some time on the internet looking for docs on how to do this. There seem to be several ways. But I'm not sure if this is the best way to do it.

This is what I still have

My HOC

interface IStateProps {
  errorState: IErrorState;
  tList: IList;
}

interface IDispatchProps {
  fetch: () => any;
}

interface IState {
  isLoading: boolean;
}

interface IConnectedProps extends IStateProps, IDispatchProps {}

 export class UnconnectedBrowserLoader extends React.Component<IConnectedProps, IState> {
  public state = {
    isLoading: true,
  };

  public render() {
    const {errorState} = this.props;
    if (errorState.error) {
        return (
            <div className="non-ideal">
                <NonIdealState visual="error" title={"Could Not Load Tasks"}/>
            </div>
        );
    }
    if (this.state.isLoading) {
        return (
            <div className="non-ideal">
                <NonIdealState visual={<Spinner/>}/>
            </div>
        );
    }
    return (
        <div className="body">
            <Container/>
        </div>
    );
  }

  public componentWillMount() {
    Promise.all([
        this.props.fetch(),
    ]).then(() => {
        this.setState({isLoading: false});
    });
  }
}

function mapStateToProps(state: IBrowserState): IStateProps {
  return {errorState: state.errorState, tList: state.taskList};
}

function mapDispatchToProps(dispatch: Dispatch<IBrowserState>): 
  IDispatchProps {
    return bindActionCreators({
      fetch,
    }, dispatch);
  }

export const BrowserLoader = connect(
  mapStateToProps, mapDispatchToProps,
)(UnconnectedBrowserLoader);

      

My tests now look like this: WIP

describe("Browser tests", function() {
  test("test the browser loader for error case", () => {
    const mockStore = configureMockStore([thunk]);
    const store = mockStore({
        errorState: {
            error: "internal error"
        }
    });

    const wrapper = mount(<Provider store={store}><BrowserLoader/></Provider>);
    expect(wrapper.find('.non-ideal')).toHaveLength(1); // this works
    expect(wrapper.find('.body')).toHaveLength(0); // this works
});
test("test the browser loader", () => {
    const mockStore = configureMockStore([thunk]);
    const store = mockStore({});
    const dispatch = sinon.spy();
    const wrapper = shallow(
        <Provider store={store}><BrowserLoader/></Provider>
    );
   // Not sure how to pass mapStateToProps and mapDispatchToProps.
    const uwrapper = shallow(<UnconnectedBrowserLoader dispatch={dispatch} store={store}/>); //This does not work
    expect(shallowToJson(wrapper)).toMatchSnapshot();
  });
});

      

What am I doing wrong?! Can anyone point me to this? any help would be appreciated.

+3
javascript reactjs redux typescript2.0 enzyme


source to share


No one has answered this question yet

Check out similar questions:

2633
How do I check for an empty JavaScript object?
1126
Why use Redux over Facebook Flux?
804
How do I dispatch a Redux action with a timeout?
567
Why would we use async thread middleware in Redux?
410
Pros / cons of using redux saga with ES6 generators versus redux with ES2017 async / await
20
Nested components testing an enzyme inside React & Redux
4
Enzyme Assay Authentication Higher Order Component (HOC)
2
mapStateToProps () in Connect (EquipmentMetadata) should return a simple object. Got vague instead
1
Testing Higher Order Components Using Joke and Enzyme
0
Testing. Enzyme-Processed Components with Redux



All Articles
Loading...
X
Show
Funny
Dev
Pics