Skip to content
This repository has been archived by the owner on Sep 11, 2018. It is now read-only.

how to provide initialstate in test #93

Closed
agdolla opened this issue Sep 18, 2015 · 5 comments
Closed

how to provide initialstate in test #93

agdolla opened this issue Sep 18, 2015 · 5 comments

Comments

@agdolla
Copy link

agdolla commented Sep 18, 2015

Hi
I try to update the example/todo-application to the current master and it works but I can not find out, how to modify the test.
In beforeEach when component is created with shallowRenderWithProps I try to pass the initialstate but it fails. The strange thing is that HomeView component is initialized with my props but the render method called for the second time with no props so it fails.
Any idea ?

@dvdzkwsk
Copy link
Owner

Hm, are you exporting the HomeView component as a dumb component? If you're wanting to test how it renders with certain props you don't want to test the connected component, which is currently the only export: https://github.com/davezuko/react-redux-starter-kit/blob/example/todo-application/client/views/home.jsx

Edit: I guess I maybe misread your question, since you're talking about initial application state it seems, though I'm not sure how that influences the unit tests for HomeView. If what I mentioned above didn't help, could you clarify? Thanks!

@agdolla
Copy link
Author

agdolla commented Sep 21, 2015

I rebased the branch and made all the modifications so the todo example works again, you can find it in my fork at: https://github.com/agdolla/react-redux-starter-kit/tree/example/todo-application
( I left two console.log in it intentionally)
so HomeView has this line:

const todos = this.props.todos.toJS();

When running the tests this line fails because this.props.todos is undefined.

I tried to pass todos as you can see in HomeView.spec.js:

beforeEach(function () {
  const createTodoItem = (copy) => Immutable.Map({
    copy     : copy,
    complete : false
  });
  const initialState = Immutable.List([
    'Read the docs',
    'Build something cool'
  ].map(createTodoItem));
  const props = {todos: initialState};
  component = shallowRenderWithProps(props);

If you try to run the tests you'll see two lines of console output, one with this.props.todos defined and one with undefined :(
Could you please try it ?

@dvdzkwsk
Copy link
Owner

Yeah I'll definitely take a look at this, though it might not be until later today. Thanks for this though, I appreciate all the work you're doing!

@dvdzkwsk
Copy link
Owner

Hey @agdolla, I tested out your branch and it looks like you just need to pass it in to the renderWithProps function:

// current
component = shallowRenderWithProps(props);
rendered  = renderWithProps();

// fixed
component = shallowRenderWithProps(props);
rendered  = renderWithProps(props);

Does that help at all?

@agdolla
Copy link
Author

agdolla commented Sep 22, 2015

🐵 sure, I should've read the next line :)
thanks
I try to have a few tests then send you a new PR

@agdolla agdolla closed this as completed Sep 22, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants