Skip to content

Commit

Permalink
change SET_SORT reducer to accept a order value.
Browse files Browse the repository at this point in the history
  • Loading branch information
frankPairs committed Feb 22, 2019
1 parent 5cabddc commit 2cdc94b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/ra-core/src/controller/ListController.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export class ListController extends Component {
);
}

setSort = sort => this.changeParams({ type: SET_SORT, payload: sort });
setSort = sort => this.changeParams({ type: SET_SORT, payload: { sort } });

setPage = page => this.changeParams({ type: SET_PAGE, payload: page });

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert';
import queryReducer from './queryReducer';
import queryReducer, { SORT_ASC, SORT_DESC } from './queryReducer';

describe('Query Reducer', () => {
describe('SET_PAGE action', () => {
Expand Down Expand Up @@ -66,4 +66,70 @@ describe('Query Reducer', () => {
assert.equal(updatedState.page, 1);
});
});
describe('SET_SORT action', () => {
it('should set SORT_ASC order by default when sort value is new', () => {
const updatedState = queryReducer(
{},
{
type: 'SET_SORT',
payload: { sort: 'foo' },
}
);
assert.deepEqual(updatedState, {
sort: 'foo',
order: SORT_ASC,
page: 1,
});
});
it('should set order by payload.order value when sort value is new', () => {
const updatedState = queryReducer(
{},
{
type: 'SET_SORT',
payload: { sort: 'foo', order: SORT_DESC },
}
);
assert.deepEqual(updatedState, {
sort: 'foo',
order: SORT_DESC,
page: 1,
});
});
it("should set order as the opposite of the one in previous state when sort hasn't change", () => {
const updatedState = queryReducer(
{
sort: 'foo',
order: SORT_DESC,
page: 1,
},
{
type: 'SET_SORT',
payload: { sort: 'foo' },
}
);
assert.deepEqual(updatedState, {
sort: 'foo',
order: SORT_ASC,
page: 1,
});
});
it("should set order as the opposite of the one in previous state even if order is specified in the payload when sort hasn't change", () => {
const updatedState = queryReducer(
{
sort: 'foo',
order: SORT_DESC,
page: 1,
},
{
type: 'SET_SORT',
payload: { sort: 'foo', order: SORT_DESC },
}
);
assert.deepEqual(updatedState, {
sort: 'foo',
order: SORT_ASC,
page: 1,
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface State {
const queryReducer: Reducer<State> = (previousState, { type, payload }) => {
switch (type) {
case SET_SORT:
if (payload === previousState.sort) {
if (payload.sort === previousState.sort) {
return {
...previousState,
order: oppositeOrder(previousState.order),
Expand All @@ -35,8 +35,8 @@ const queryReducer: Reducer<State> = (previousState, { type, payload }) => {

return {
...previousState,
sort: payload,
order: SORT_ASC,
sort: payload.sort,
order: payload.order || SORT_ASC,
page: 1,
};

Expand Down

0 comments on commit 2cdc94b

Please sign in to comment.