-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(search): Improve speed of search suggestions
By preventing every notes fetching url, loading only once a note is clicked
- Loading branch information
1 parent
0121212
commit 282c108
Showing
7 changed files
with
202 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
## ✨ Features | ||
|
||
* Upgrade [email protected] to be able to call onSelect function | ||
* Improve speed of search suggestion by preventing fetch notes url until click | ||
* Update cozy-stack-client and cozy-pouch-link to sync with cozy-client version | ||
* Update cozy-ui | ||
- Modify Viewers to handle [68.0.0 BC](https://github.com/cozy/cozy-ui/releases/tag/v68.0.0) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
src/drive/web/modules/services/components/SuggestionProvider.spec.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import React from 'react' | ||
import { render, waitFor } from '@testing-library/react' | ||
import SuggestionProvider from './SuggestionProvider' | ||
import { dummyFile, dummyNote } from 'test/dummies/dummyFile' | ||
|
||
const makeFileWithDoc = file => ({ ...file, doc: file }) | ||
const parentFolder = makeFileWithDoc(dummyFile({ _id: 'id-file' })) | ||
const folder = makeFileWithDoc(dummyFile({ dir_id: 'id-file' })) | ||
const note = makeFileWithDoc( | ||
dummyNote({ | ||
dir_id: 'id-file', | ||
name: 'name.cozy-note' | ||
}) | ||
) | ||
const mockClient = { | ||
fetchJSON: jest.fn().mockReturnValue({ rows: [parentFolder, folder, note] }) | ||
} | ||
const mockIntentAttributesClient = 'intent-attributes-client' | ||
|
||
jest.mock('cozy-client', () => ({ | ||
...jest.requireActual('cozy-client'), | ||
withClient: Component => () => { | ||
const intent = { | ||
_id: 'id_intent', | ||
attributes: { client: mockIntentAttributesClient } | ||
} | ||
return <Component client={mockClient} intent={intent}></Component> | ||
} | ||
})) | ||
jest.mock('./iconContext', () => ({ getIconUrl: () => 'iconUrl' })) | ||
|
||
describe('SuggestionProvider', () => { | ||
let events = {} | ||
let event | ||
|
||
beforeEach(() => { | ||
window.cozy.client = mockClient | ||
window.addEventListener = jest.fn((event, callback) => { | ||
events[event] = callback | ||
}) | ||
window.parent.postMessage = jest.fn() | ||
event = { | ||
origin: mockIntentAttributesClient, | ||
data: { query: 'name', id: 'id' } | ||
} | ||
}) | ||
|
||
it('should query all files to display fuzzy suggestion', () => { | ||
// Given | ||
render(<SuggestionProvider />) | ||
|
||
// When | ||
events.message(event) | ||
|
||
// Then | ||
expect(mockClient.fetchJSON).toHaveBeenCalledWith( | ||
'GET', | ||
'/data/io.cozy.files/_all_docs?include_docs=true' | ||
) | ||
}) | ||
|
||
it('should provide onSelect with open url when file is not a note + and function when it is a note', async () => { | ||
// Given | ||
render(<SuggestionProvider />) | ||
|
||
// When | ||
events.message(event) | ||
|
||
// Then | ||
await waitFor(() => { | ||
expect(window.parent.postMessage).toHaveBeenCalledWith( | ||
{ | ||
id: 'id', | ||
suggestions: [ | ||
{ | ||
icon: 'iconUrl', | ||
id: 'id-file', | ||
onSelect: 'open:http://localhost/#/folder/id-file', | ||
subtitle: '/path', | ||
term: 'name', | ||
title: 'name' | ||
}, | ||
{ | ||
icon: 'iconUrl', | ||
id: 'id-file', | ||
onSelect: 'id_note:id-file', | ||
subtitle: '/path', | ||
term: 'name.cozy-note', | ||
title: 'name.cozy-note' | ||
} | ||
], | ||
type: 'intent-id_intent:data' | ||
}, | ||
'intent-attributes-client' | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// eslint-disable-next-line no-unused-vars | ||
const { IOCozyFile } = require('cozy-client/dist/types') | ||
|
||
/** | ||
* Create a dummy file, with overridden value of given param | ||
* | ||
* @param {Partial<IOCozyFile>} [file={}] - optional file with value to keep | ||
* @returns {IOCozyFile} a dummy file | ||
*/ | ||
export const dummyFile = file => ({ | ||
_id: 'id-file', | ||
_type: 'doctype-file', | ||
name: 'name', | ||
id: 'id-file', | ||
icon: 'icon', | ||
path: '/path', | ||
type: 'directory', | ||
...file | ||
}) | ||
|
||
/** | ||
* Create a dummy note, with overridden value of given param | ||
* | ||
* @param {Partial<IOCozyFile>} [note={}] | ||
* @returns {IOCozyFile} a dummy note | ||
*/ | ||
export const dummyNote = note => ({ | ||
...dummyFile(), | ||
type: 'file', | ||
metadata: { | ||
content: '', | ||
schema: '', | ||
title: '', | ||
version: '' | ||
}, | ||
...note | ||
}) |