Sorted by rating on client side

The feature we are going to add is sorting the speakers by rating. In previous chapters, rating was not discussed or even enforced, so modifications will need to happen to include rating in the model that has been built so far. That is, of course, if you have not already completed the full model as defined by the C# code.

As with earlier examples in this chapter, try to add this behavior yourself and then look at our following solution.

In the speakerReducer.spec.js file, we have added a single test for default sorting of speakers by rank. The test can be added to the describe block for the speaker reducer:

it('sorts speakers by rank', () => {
// Arrange
const initialState = [];
const speaker1 = { id: 'test-speaker-1', firstName: 'Test 1', lastName: 'Speaker', rank: 1};
const speaker2 = { id: 'test-speaker-2', firstName: 'Test 2', lastName: 'Speaker', rank: 2};
const action = actions.getSpeakersSuccess([speaker1, speaker2]);

// Act
const newState = speakersReducer(initialState, action);

// Assert
expect(newState).to.have.lengthOf(2);
expect(newState[0]).to.deep.equal(speaker2);
});

And the code to make this test pass is in the speakerReducer.js file:

export function speakersReducer(state = [], action) {
switch(action.type) {
case types.GET_SPEAKERS_SUCCESS:
return action.speakers.sort((a, b) => {
return b.rank > a.rank;
});
default:
return state;
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.177.115