Skip to content

Commit

Permalink
feat: Sort document when using executeFromStore option
Browse files Browse the repository at this point in the history
  • Loading branch information
zatteo committed Aug 30, 2024
1 parent d636336 commit fbd44b8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/cozy-client/src/store/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,10 @@ export const executeQueryFromState = (state, queryDefinition) => {
data: res.length > 0 ? res[0] : null
}
}
const sorter = makeSorterFromDefinition(queryDefinition)
const sortedDocuments = sorter(res)
return {
data: res
data: sortedDocuments
}
}

Expand Down
22 changes: 22 additions & 0 deletions packages/cozy-client/src/store/queries.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -798,4 +798,26 @@ describe('execute query from state', () => {
)
expect(res2.data).toEqual(null)
})

it('should get all the docs from state for the doctype and sort result', () => {
const query1 = {
doctype: 'io.cozy.files',
sort: [{ created_at: 'asc' }]
}

const res1 = executeQueryFromState(state, query1)
expect(res1.data[0]._id).toEqual('123')
expect(res1.data[1]._id).toEqual('456')
expect(res1.data[2]._id).toEqual('789')

const query2 = {
doctype: 'io.cozy.files',
sort: [{ created_at: 'desc' }]
}

const res2 = executeQueryFromState(state, query2)
expect(res2.data[0]._id).toEqual('789')
expect(res2.data[1]._id).toEqual('456')
expect(res2.data[2]._id).toEqual('123')
})
})

0 comments on commit fbd44b8

Please sign in to comment.