After updating node to newest version, my tests breaks saying my array doesn't reverse. I have an array with 4 numbers and jest saying it expects the numbers in my array reversed, but after running the .reverse() it doesnt do as its supposed to. Is this a new kind of problem with node?
it(`should handle ${SORT_MEMBERS}`, () => {
const members = [{ ssn: 7 }, { ssn: 6 }, { ssn: 5 }, { ssn: 4 }];
const attr = 'ssn';
const order = SORT_ORDER.ASC;
let newState = reducer(state, {
type: INIT_MEMBERS,
payload: { members },
});
newState = reducer(newState, {
type: SORT_MEMBERS,
payload: { attr, order }
});
expect(newState).toEqual({
...state,
members: members.reverse(),
sort: { attr, order },
});
});
Initial state:
const initialState = {
batchUpdates: {
salaryChangePercentage: '',
applyDate: null,
},
form: {
ableToWork: null,
memberErrors: null,
},
updates: {}, // hash with ssn as key
members: [],
errors: {},
sort: {
attr: null,
order: null,
},
saving: false,
saved: false,
saveResult: null,
saveError: null,
};
case SORT_MEMBER:
case SORT_MEMBERS:
return {
...state,
members: sortMembers(state.members, state.updates, action.payload.attr, action.payload.order),
sort: {
attr: action.payload.attr,
order: action.payload.order,
},
};
And finally my sort method:
export const sortMembers = (members, updates, attr, order) => {
const sorted = members.slice();
const path = attr.split('.');
const byUpdates = path.length === 2 && path[0] === 'updates';
if (!byUpdates) {
return sorted.sort((a, b) => (
order === SORT_ORDER.DESC ? a[attr] < b[attr] : a[attr] > b[attr]
));
}
// Sort by updates on given ssn
return sorted.sort((a, b) => (
order === SORT_ORDER.DESC
? updates[a.ssn][path[1]] < updates[b.ssn][path[1]] : updates[a.ssn][path[1]] > updates[b.ssn][path[1]]
));
};
It used to work before I updated my Node.js
I found the solution to the problem!
There is a misstype in following code:
order === SORT_ORDER.DESC ? a[attr] < b[attr] : a[attr] > b[attr]
There should be - and +, not > and <. The correct is following:
order === SORT_ORDER.DESC ? a[attr] + b[attr] : a[attr] - b[attr]