Skip to content

Commit

Permalink
refactor(search): move findAll into cozy-client
Browse files Browse the repository at this point in the history
  • Loading branch information
trollepierre committed Aug 9, 2022
1 parent 9a99f83 commit 742b360
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
6 changes: 2 additions & 4 deletions src/drive/web/modules/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,9 @@ export const buildEncryptionByIdQuery = id => ({
/**
* Provide selector and options to fetch files
*
* @param {string} bookmark - The query bookmark
* @returns {{options: {MangoQueryOptions}, selector: object}} A minimalist file list
*/
export const prepareSuggestionQuery = (bookmark = '') => {
export const prepareSuggestionQuery = () => {
const selector = {
_id: {
$gt: null
Expand All @@ -351,8 +350,7 @@ export const prepareSuggestionQuery = (bookmark = '') => {
},
fields: ['_id', 'trashed', 'dir_id', 'name', 'path'],
indexedFields: ['_id'],
limit: 1000,
bookmark
limit: 1000
}
return { selector, options }
}
Expand Down
18 changes: 4 additions & 14 deletions src/drive/web/modules/services/components/SuggestionProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,10 @@ class SuggestionProvider extends React.Component {
const { client } = this.props
this.hasIndexFilesBeenLaunched = true

let next = true
let files = []
let bookmark = ''

while (next) {
const { selector, options } = prepareSuggestionQuery(bookmark)
// We directly use the collection to avoid using the store for nothing.
const resp = await client
.collection(DOCTYPE_FILES)
.find(selector, options)
files = [...files, ...resp.data]
bookmark = resp.bookmark
next = resp.next
}
const { selector, options } = prepareSuggestionQuery()
const files = await client
.collection(DOCTYPE_FILES)
.findAll(selector, options)

const folders = files.filter(file => file.type === TYPE_DIRECTORY)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import React from 'react'
import { render } from '@testing-library/react'
import SuggestionProvider from './SuggestionProvider'

let files = []
const mockFind = jest.fn().mockReturnValue({ next: false, data: files })
const mockClient = { collection: jest.fn().mockReturnValue({ find: mockFind }) }
const mockFindAll = jest.fn().mockReturnValue([])
const mockClient = {
collection: jest.fn().mockReturnValue({ findAll: mockFindAll })
}
const mockIntentAttributesClient = 'intent-attributes-client'

jest.mock('cozy-client', () => {
Expand Down Expand Up @@ -38,14 +39,13 @@ describe('SuggestionProvider', () => {

// Then
expect(mockClient.collection).toHaveBeenCalledWith('io.cozy.files')
expect(mockFind).toHaveBeenCalledWith(
expect(mockFindAll).toHaveBeenCalledWith(
{
_id: {
$gt: null
}
},
{
bookmark: '',
fields: ['_id', 'trashed', 'dir_id', 'name', 'path'],
indexedFields: ['_id'],
limit: 1000,
Expand Down

0 comments on commit 742b360

Please sign in to comment.