ReactJS/Redux: Testing reducer state with array of objects inside receives undefined array
up vote
0
down vote
favorite
I am working for reducer test, when i try to test 1 of my actions types the expected and received values does not match.
eventReducer.test.js
import reducer from './eventReducer';
import { GET_EVENTS } from '../actions/types';
describe('event reducer', () => {
it('should return initial state', () => {
expect(reducer(undefined, {})).toEqual({
eventDetails: ,
loading: false
});
})
it('should store data from db upon mounting component', () =>{
expect(reducer({}, {
type: GET_EVENTS,
eventDetails: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
]
})
).toEqual({
eventDetails: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
],
loading: false
})
});
})
eventReducer.js
import { GET_EVENTS, ADD_EVENT, DELETE_EVENT, EVENTS_LOADING } from '../actions/types';
const initialState = {
eventDetails: ,
loading: false
}
export default function (state = initialState, action) {
switch(action.type) {
case GET_EVENTS:
return {
...state,
eventDetails: action.payload,
loading: false
};
case DELETE_EVENT:
return {
...state,
eventDetails: state.eventDetails.filter(event => event._id !==
action.payload)
};
case ADD_EVENT:
return {
...state,
eventDetails: [action.payload, ...state.eventDetails]
};
case EVENTS_LOADING:
return {
...state,
loading: true
}
default:
return state;
}
}
My first test is running properly, but when i try to execute second one, it doesn't match received and expected values, problem is with eventDetails array, it keeps outputting that it's value is undefined.
Test output
FAIL src/reducers/eventReducer.test.js
● event reducer › should store data from db upon mounting component
expect(received).toEqual(expected)
Expected value to equal:
{"eventDetails": [{"firstName": "Damian", "lastName": "Wasilewski"}], "loading": false}
Received:
{"eventDetails": undefined, "loading": false}
Difference:
- Expected
+ Received
Object {
- "eventDetails": Array [
- Object {
- "firstName": "Damian",
- "lastName": "Wasilewski",
- },
- ],
+ "eventDetails": undefined,
"loading": false,
}
21 | ]
22 | })
> 23 | ).toEqual({
| ^
24 | eventDetails: [
25 | {
26 | firstName: 'Damian',
at Object.toEqual (src/reducers/eventReducer.test.js:23:5)
What could cause problem of undefined array?
reactjs redux jestjs
add a comment |
up vote
0
down vote
favorite
I am working for reducer test, when i try to test 1 of my actions types the expected and received values does not match.
eventReducer.test.js
import reducer from './eventReducer';
import { GET_EVENTS } from '../actions/types';
describe('event reducer', () => {
it('should return initial state', () => {
expect(reducer(undefined, {})).toEqual({
eventDetails: ,
loading: false
});
})
it('should store data from db upon mounting component', () =>{
expect(reducer({}, {
type: GET_EVENTS,
eventDetails: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
]
})
).toEqual({
eventDetails: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
],
loading: false
})
});
})
eventReducer.js
import { GET_EVENTS, ADD_EVENT, DELETE_EVENT, EVENTS_LOADING } from '../actions/types';
const initialState = {
eventDetails: ,
loading: false
}
export default function (state = initialState, action) {
switch(action.type) {
case GET_EVENTS:
return {
...state,
eventDetails: action.payload,
loading: false
};
case DELETE_EVENT:
return {
...state,
eventDetails: state.eventDetails.filter(event => event._id !==
action.payload)
};
case ADD_EVENT:
return {
...state,
eventDetails: [action.payload, ...state.eventDetails]
};
case EVENTS_LOADING:
return {
...state,
loading: true
}
default:
return state;
}
}
My first test is running properly, but when i try to execute second one, it doesn't match received and expected values, problem is with eventDetails array, it keeps outputting that it's value is undefined.
Test output
FAIL src/reducers/eventReducer.test.js
● event reducer › should store data from db upon mounting component
expect(received).toEqual(expected)
Expected value to equal:
{"eventDetails": [{"firstName": "Damian", "lastName": "Wasilewski"}], "loading": false}
Received:
{"eventDetails": undefined, "loading": false}
Difference:
- Expected
+ Received
Object {
- "eventDetails": Array [
- Object {
- "firstName": "Damian",
- "lastName": "Wasilewski",
- },
- ],
+ "eventDetails": undefined,
"loading": false,
}
21 | ]
22 | })
> 23 | ).toEqual({
| ^
24 | eventDetails: [
25 | {
26 | firstName: 'Damian',
at Object.toEqual (src/reducers/eventReducer.test.js:23:5)
What could cause problem of undefined array?
reactjs redux jestjs
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am working for reducer test, when i try to test 1 of my actions types the expected and received values does not match.
eventReducer.test.js
import reducer from './eventReducer';
import { GET_EVENTS } from '../actions/types';
describe('event reducer', () => {
it('should return initial state', () => {
expect(reducer(undefined, {})).toEqual({
eventDetails: ,
loading: false
});
})
it('should store data from db upon mounting component', () =>{
expect(reducer({}, {
type: GET_EVENTS,
eventDetails: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
]
})
).toEqual({
eventDetails: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
],
loading: false
})
});
})
eventReducer.js
import { GET_EVENTS, ADD_EVENT, DELETE_EVENT, EVENTS_LOADING } from '../actions/types';
const initialState = {
eventDetails: ,
loading: false
}
export default function (state = initialState, action) {
switch(action.type) {
case GET_EVENTS:
return {
...state,
eventDetails: action.payload,
loading: false
};
case DELETE_EVENT:
return {
...state,
eventDetails: state.eventDetails.filter(event => event._id !==
action.payload)
};
case ADD_EVENT:
return {
...state,
eventDetails: [action.payload, ...state.eventDetails]
};
case EVENTS_LOADING:
return {
...state,
loading: true
}
default:
return state;
}
}
My first test is running properly, but when i try to execute second one, it doesn't match received and expected values, problem is with eventDetails array, it keeps outputting that it's value is undefined.
Test output
FAIL src/reducers/eventReducer.test.js
● event reducer › should store data from db upon mounting component
expect(received).toEqual(expected)
Expected value to equal:
{"eventDetails": [{"firstName": "Damian", "lastName": "Wasilewski"}], "loading": false}
Received:
{"eventDetails": undefined, "loading": false}
Difference:
- Expected
+ Received
Object {
- "eventDetails": Array [
- Object {
- "firstName": "Damian",
- "lastName": "Wasilewski",
- },
- ],
+ "eventDetails": undefined,
"loading": false,
}
21 | ]
22 | })
> 23 | ).toEqual({
| ^
24 | eventDetails: [
25 | {
26 | firstName: 'Damian',
at Object.toEqual (src/reducers/eventReducer.test.js:23:5)
What could cause problem of undefined array?
reactjs redux jestjs
I am working for reducer test, when i try to test 1 of my actions types the expected and received values does not match.
eventReducer.test.js
import reducer from './eventReducer';
import { GET_EVENTS } from '../actions/types';
describe('event reducer', () => {
it('should return initial state', () => {
expect(reducer(undefined, {})).toEqual({
eventDetails: ,
loading: false
});
})
it('should store data from db upon mounting component', () =>{
expect(reducer({}, {
type: GET_EVENTS,
eventDetails: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
]
})
).toEqual({
eventDetails: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
],
loading: false
})
});
})
eventReducer.js
import { GET_EVENTS, ADD_EVENT, DELETE_EVENT, EVENTS_LOADING } from '../actions/types';
const initialState = {
eventDetails: ,
loading: false
}
export default function (state = initialState, action) {
switch(action.type) {
case GET_EVENTS:
return {
...state,
eventDetails: action.payload,
loading: false
};
case DELETE_EVENT:
return {
...state,
eventDetails: state.eventDetails.filter(event => event._id !==
action.payload)
};
case ADD_EVENT:
return {
...state,
eventDetails: [action.payload, ...state.eventDetails]
};
case EVENTS_LOADING:
return {
...state,
loading: true
}
default:
return state;
}
}
My first test is running properly, but when i try to execute second one, it doesn't match received and expected values, problem is with eventDetails array, it keeps outputting that it's value is undefined.
Test output
FAIL src/reducers/eventReducer.test.js
● event reducer › should store data from db upon mounting component
expect(received).toEqual(expected)
Expected value to equal:
{"eventDetails": [{"firstName": "Damian", "lastName": "Wasilewski"}], "loading": false}
Received:
{"eventDetails": undefined, "loading": false}
Difference:
- Expected
+ Received
Object {
- "eventDetails": Array [
- Object {
- "firstName": "Damian",
- "lastName": "Wasilewski",
- },
- ],
+ "eventDetails": undefined,
"loading": false,
}
21 | ]
22 | })
> 23 | ).toEqual({
| ^
24 | eventDetails: [
25 | {
26 | firstName: 'Damian',
at Object.toEqual (src/reducers/eventReducer.test.js:23:5)
What could cause problem of undefined array?
reactjs redux jestjs
reactjs redux jestjs
edited Nov 22 at 4:13
iagowp
969724
969724
asked Nov 21 at 20:51
D.Wasilewski
206
206
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
You are setting event.details to action.payload, but on your test you are not sending a payload property.
it('should store data from db upon mounting component', () =>{
expect(reducer({}, {
type: GET_EVENTS,
payload: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
]
})
).toEqual({
eventDetails: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
],
loading: false
})
});
})
This should solve your problem
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You are setting event.details to action.payload, but on your test you are not sending a payload property.
it('should store data from db upon mounting component', () =>{
expect(reducer({}, {
type: GET_EVENTS,
payload: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
]
})
).toEqual({
eventDetails: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
],
loading: false
})
});
})
This should solve your problem
add a comment |
up vote
0
down vote
accepted
You are setting event.details to action.payload, but on your test you are not sending a payload property.
it('should store data from db upon mounting component', () =>{
expect(reducer({}, {
type: GET_EVENTS,
payload: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
]
})
).toEqual({
eventDetails: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
],
loading: false
})
});
})
This should solve your problem
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You are setting event.details to action.payload, but on your test you are not sending a payload property.
it('should store data from db upon mounting component', () =>{
expect(reducer({}, {
type: GET_EVENTS,
payload: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
]
})
).toEqual({
eventDetails: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
],
loading: false
})
});
})
This should solve your problem
You are setting event.details to action.payload, but on your test you are not sending a payload property.
it('should store data from db upon mounting component', () =>{
expect(reducer({}, {
type: GET_EVENTS,
payload: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
]
})
).toEqual({
eventDetails: [
{
firstName: 'Damian',
lastName: 'Wasilewski'
}
],
loading: false
})
});
})
This should solve your problem
answered Nov 22 at 3:15
iagowp
969724
969724
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420294%2freactjs-redux-testing-reducer-state-with-array-of-objects-inside-receives-undef%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown