Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change SET_SORT reducer to accept a order value. #2921

Merged
merged 1 commit into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/ra-core/src/controller/ListController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ export class UnconnectedListController 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 @@ -21,7 +21,7 @@ const queryReducer: Reducer<ListParams> = (
) => {
switch (type) {
case SET_SORT:
if (payload === previousState.sort) {
if (payload.sort === previousState.sort) {
return {
...previousState,
order: oppositeOrder(previousState.order),
Expand All @@ -31,8 +31,8 @@ const queryReducer: Reducer<ListParams> = (

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

Expand Down