I'm beginner with react and redux and I coded an very simple app to learn it where you can add expenses with a description an amount a date and a note.
I'm testing one of my reducers with jest and I get an error that I cannot understand. I'm probably missing something but I can't figure out so it would be nice if you could help me out.
So I have one expenseSlice with a reducer action addExpense that prepare the payload just adding an ID and set default values if necessary and add it to the previous state.
import { createSlice } from "@reduxjs/toolkit"
import { nanoid } from "nanoid"
const expensesDefaultState = []
const expensesSlice = createSlice({
name: 'expenses',
initialState: expensesDefaultState,
reducers: {
addExpense: {
reducer: (state, action) => {
return [
...state,
action.payload
]
},
prepare: (payload) => {
return {
payload: {
id: nanoid(),
description: payload.description ? payload.description : '',
note: payload.note ? payload.note : '',
amount: payload.amount ? payload.amount : 0,
createdAt: payload.createdAt ? payload.createdAt : 0
}
}
}
}
And in my test file when I test the action the test pass correctly like so :
describe('Expenses actions', () => {
it('should returns an addExpense action object with prepared payload', () => {
const newExpense = {
description: 'this is a description',
amount: 1000,
createdAt: 0,
note: 'this is anote'
}
expect(addExpense(newExpense)).toEqual({
type: 'expenses/addExpense',
payload: {
id: expect.any(String),
description: 'this is a description',
amount: 1000,
createdAt: 0,
note: 'this is a note'
}
})
})
But when I test the reducer with the same action and expect an id it failed and I get the object without the ID like so :
describe('Expense Reducer', () => {
it('should add an expense', () => {
const newExpense = {
description: 'my new expense',
amount: 1000,
createdAt: 0,
note: 'this is a note'
}
const state = expenseReducer([], {
type: 'expenses/addExpense',
payload: newExpense
})
expect(state).toEqual([
{
id: expect.any(String),
description: 'my new expense',
amount: 1000,
createdAt: 0,
note: 'this is a note'
}
])
})
})
And the error :
● Expense Reducer › should add an expense
expect(received).toEqual(expected) // deep equality
- Expected - 1
+ Received + 0
Array [
Object {
"amount": 1000,
"createdAt": 0,
"description": "my new expense",
- "id": Any<String>,
"note": "this is a note",
},
]
What I'm missing here or not understanding correctly ? Thank you in advance !